-
Notifications
You must be signed in to change notification settings - Fork 18
/
application.go
125 lines (97 loc) · 3.18 KB
/
application.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
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
package dispatcher
import (
"fmt"
"log"
"github.com/pushbits/server/internal/model"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
mId "maunium.net/go/mautrix/id"
)
func buildRoomTopic(id uint) string {
return fmt.Sprintf("Application %d", id)
}
// RegisterApplication creates a channel for an application.
func (d *Dispatcher) RegisterApplication(id uint, name, token, user string) (string, error) {
log.Printf("Registering application %s, notifications will be relayed to user %s.\n", name, user)
resp, err := d.mautrixClient.CreateRoom(&mautrix.ReqCreateRoom{
Visibility: "private",
Invite: []mId.UserID{mId.UserID(user)},
IsDirect: true,
Name: name,
Preset: "private_chat",
Topic: buildRoomTopic(id),
})
if err != nil {
log.Print(err)
return "", err
}
log.Printf("Application %s is now relayed to room with ID %s.\n", name, resp.RoomID.String())
return resp.RoomID.String(), err
}
// DeregisterApplication deletes a channel for an application.
func (d *Dispatcher) DeregisterApplication(a *model.Application, u *model.User) error {
log.Printf("Deregistering application %s (ID %d) with Matrix ID %s.\n", a.Name, a.ID, a.MatrixID)
// The user might have left the channel, but we can still try to remove them.
if _, err := d.mautrixClient.KickUser(mId.RoomID(a.MatrixID), &mautrix.ReqKickUser{
Reason: "This application was deleted",
UserID: mId.UserID(u.MatrixID),
}); err != nil {
log.Print(err)
return err
}
if _, err := d.mautrixClient.LeaveRoom(mId.RoomID(a.MatrixID)); err != nil {
log.Print(err)
return err
}
if _, err := d.mautrixClient.ForgetRoom(mId.RoomID(a.MatrixID)); err != nil {
log.Print(err)
return err
}
return nil
}
func (d *Dispatcher) sendRoomEvent(roomID, eventType string, content interface{}) error {
if _, err := d.mautrixClient.SendStateEvent(mId.RoomID(roomID), event.NewEventType(eventType), "", content); err != nil {
log.Print(err)
return err
}
return nil
}
// UpdateApplication updates a channel for an application.
func (d *Dispatcher) UpdateApplication(a *model.Application) error {
log.Printf("Updating application %s (ID %d) with Matrix ID %s.\n", a.Name, a.ID, a.MatrixID)
content := map[string]interface{}{
"name": a.Name,
}
if err := d.sendRoomEvent(a.MatrixID, "m.room.name", content); err != nil {
return err
}
content = map[string]interface{}{
"topic": buildRoomTopic(a.ID),
}
if err := d.sendRoomEvent(a.MatrixID, "m.room.topic", content); err != nil {
return err
}
return nil
}
// IsOrphan checks if the user is still connected to the channel.
func (d *Dispatcher) IsOrphan(a *model.Application, u *model.User) (bool, error) {
resp, err := d.mautrixClient.JoinedMembers(mId.RoomID(a.MatrixID))
if err != nil {
return false, err
}
found := false
for userID := range resp.Joined {
found = found || (userID.String() == u.MatrixID)
}
return !found, nil
}
// RepairApplication re-invites the user to the channel.
func (d *Dispatcher) RepairApplication(a *model.Application, u *model.User) error {
_, err := d.mautrixClient.InviteUser(mId.RoomID(a.MatrixID), &mautrix.ReqInviteUser{
UserID: mId.UserID(u.MatrixID),
})
if err != nil {
return err
}
return nil
}