-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotify_linux.go
More file actions
196 lines (175 loc) · 4.81 KB
/
Copy pathnotify_linux.go
File metadata and controls
196 lines (175 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//go:build linux
package main
import (
"encoding/json"
"os"
"sync"
"github.com/gen2brain/beeep"
"github.com/godbus/dbus/v5"
"github.com/google/uuid"
)
const (
portalNotificationInterface = "org.freedesktop.portal.Notification"
portalNotificationAdd = portalNotificationInterface + ".AddNotification"
portalNotificationAction = portalNotificationInterface + ".ActionInvoked"
portalNotificationOpenAction = "open-thread"
)
var (
notificationConn *dbus.Conn
notificationSignals chan *dbus.Signal
notificationDone chan struct{}
notificationMu sync.Mutex
)
type notificationTarget struct {
Account string `json:"account"`
ThreadID string `json:"threadId"`
}
// setupNotificationListener opens a session-bus connection and watches for the
// notification portal's ActionInvoked signal so a clicked notification can open
// its thread.
func (a *App) setupNotificationListener() {
conn, err := dbus.ConnectSessionBus()
if err != nil {
a.logf("failed to connect to notification portal: %v", err)
return
}
matchOptions := []dbus.MatchOption{
dbus.WithMatchInterface(portalNotificationInterface),
dbus.WithMatchMember("ActionInvoked"),
dbus.WithMatchObjectPath(portalDesktopPath),
}
if err := conn.AddMatchSignal(matchOptions...); err != nil {
_ = conn.Close()
a.logf("failed to listen for notification portal actions: %v", err)
return
}
signals := make(chan *dbus.Signal, 10)
done := make(chan struct{})
conn.Signal(signals)
notificationMu.Lock()
notificationConn = conn
notificationSignals = signals
notificationDone = done
notificationMu.Unlock()
// Portal notification actions include the account/thread payload as their
// target, so no process-local notification-id map is needed.
go func() {
for {
select {
case <-done:
return
case signal, ok := <-signals:
if !ok {
return
}
if signal.Name != portalNotificationAction {
continue
}
if target, ok := portalNotificationTarget(signal.Body); ok {
a.openThreadFromNotification(target.Account, target.ThreadID)
}
}
}
}()
}
func (a *App) closeNotificationListener() {
notificationMu.Lock()
conn := notificationConn
signals := notificationSignals
done := notificationDone
notificationConn = nil
notificationSignals = nil
notificationDone = nil
notificationMu.Unlock()
if conn == nil {
return
}
close(done)
conn.RemoveSignal(signals)
_ = conn.RemoveMatchSignal(
dbus.WithMatchInterface(portalNotificationInterface),
dbus.WithMatchMember("ActionInvoked"),
dbus.WithMatchObjectPath(portalDesktopPath),
)
_ = conn.Close()
}
// deliverNotification raises the notification through the desktop portal. The
// legacy notification service is used only as a fallback outside Flatpak, where
// no sandbox permission is needed.
func (a *App) deliverNotification(n notification) {
notificationMu.Lock()
conn := notificationConn
notificationMu.Unlock()
if conn != nil {
id := "meron-" + uuid.NewString()
call := conn.Object(portalDesktopName, portalDesktopPath).Call(
portalNotificationAdd,
0,
id,
portalNotificationOptions(n),
)
if call.Err == nil {
return
}
a.logf("notification portal failed: %v", call.Err)
}
if os.Getenv("FLATPAK_ID") != "" {
return
}
if err := beeep.Notify(n.title, n.body, notifyIcon()); err != nil {
a.logf("notify new mail failed: %v", err)
}
}
type portalSerializedIcon struct {
Type string
Value dbus.Variant
}
func portalNotificationOptions(n notification) map[string]dbus.Variant {
options := map[string]dbus.Variant{
"title": dbus.MakeVariant(n.title),
"body": dbus.MakeVariant(n.body),
"priority": dbus.MakeVariant("normal"),
"icon": dbus.MakeVariant(portalSerializedIcon{
Type: "themed",
Value: dbus.MakeVariant([]string{"jp.nonbili.meron"}),
}),
}
if n.account == "" || n.threadID == "" {
return options
}
target, err := json.Marshal(notificationTarget{
Account: n.account,
ThreadID: n.threadID,
})
if err != nil {
return options
}
options["default-action"] = dbus.MakeVariant(portalNotificationOpenAction)
options["default-action-target"] = dbus.MakeVariant(string(target))
return options
}
func portalNotificationTarget(body []any) (notificationTarget, bool) {
if len(body) < 3 {
return notificationTarget{}, false
}
action, ok := body[1].(string)
if !ok || action != portalNotificationOpenAction {
return notificationTarget{}, false
}
parameters, ok := body[2].([]dbus.Variant)
if !ok || len(parameters) == 0 {
return notificationTarget{}, false
}
raw, ok := parameters[0].Value().(string)
if !ok {
return notificationTarget{}, false
}
var target notificationTarget
if err := json.Unmarshal([]byte(raw), &target); err != nil {
return notificationTarget{}, false
}
if target.Account == "" || target.ThreadID == "" {
return notificationTarget{}, false
}
return target, true
}