-
Notifications
You must be signed in to change notification settings - Fork 6
/
msgnotify.go
70 lines (60 loc) · 1.98 KB
/
msgnotify.go
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
package msgnotify
import (
"context"
"strings"
"github.com/diamondburned/gotkit/app"
"github.com/diamondburned/gotkit/app/notify"
"github.com/diamondburned/gotkit/gtkutil"
"github.com/diamondburned/gotktrix/internal/app/messageview/message/mauthor"
"github.com/diamondburned/gotktrix/internal/gotktrix"
"github.com/diamondburned/gotrix/event"
"github.com/diamondburned/gotrix/matrix"
)
// OpenRoomCommand is the command structure for the open room action.
type OpenRoomCommand struct {
UserID matrix.UserID `json:"user_id"`
RoomID matrix.RoomID `json:"room_id"`
}
// StartNotify starts notifying the user for any new messages that mentions the
// user. A stop callback is returned. actionID must be application-scoped and
// therefore have the "app." prefix.
func StartNotify(ctx context.Context, actionID string) (stop func()) {
if !strings.HasPrefix(actionID, "app.") {
panic("actionID does not have the app prefix")
}
client := gotktrix.FromContext(ctx)
return client.SubscribeAllTimeline(func(ev event.RoomEvent) {
message, ok := ev.(*event.RoomMessageEvent)
if !ok {
return
}
// TODO: NotifySoundMessage?
action := client.NotifyMessage(message, gotktrix.NotifyMessage)
if action == 0 {
return
}
unreadIcon := notify.IconName("unread-mail")
icon := notify.Icon(unreadIcon)
avatar, _ := client.MemberAvatar(message.RoomID, message.Sender)
if avatar != nil {
avatarURL, _ := client.SquareThumbnail(*avatar, notify.MaxIconSize, 1)
icon = notify.IconURL(ctx, avatarURL, unreadIcon)
}
notification := notify.Notification{
ID: notify.HashID("new_message", client.UserID, message.RoomID),
Title: mauthor.Name(client, message.RoomID, message.Sender),
Body: message.Body,
Icon: icon,
Sound: notify.MessageSound,
Action: notify.Action{
ActionID: actionID,
Argument: gtkutil.NewJSONVariant(OpenRoomCommand{
UserID: client.UserID,
RoomID: message.RoomID,
}),
},
}
a := app.FromContext(ctx)
notification.Send(a)
})
}