-
Notifications
You must be signed in to change notification settings - Fork 17
/
notification.go
276 lines (219 loc) 路 7.99 KB
/
notification.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package dispatcher
import (
"fmt"
"html"
"log"
"strings"
"github.com/gomarkdown/markdown"
"github.com/matrix-org/gomatrix"
"github.com/pushbits/server/internal/model"
"github.com/pushbits/server/internal/pberrors"
)
// MessageFormat is a matrix message format
type MessageFormat string
// MsgType is a matrix msgtype
type MsgType string
// Define matrix constants
const (
MessageFormatHTML = MessageFormat("org.matrix.custom.html")
MsgTypeText = MsgType("m.text")
)
// MessageEvent is the content of a matrix message event
type MessageEvent struct {
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
MsgType MsgType `json:"msgtype"`
RelatesTo RelatesTo `json:"m.relates_to,omitempty"`
Format MessageFormat `json:"format"`
NewContent NewContent `json:"m.new_content,omitempty"`
}
// RelatesTo holds information about relations to other message events
type RelatesTo struct {
InReplyTo map[string]string `json:"m.in_reply_to,omitempty"`
RelType string `json:"rel_type,omitempty"`
EventID string `json:"event_id,omitempty"`
}
// NewContent holds information about an updated message event
type NewContent struct {
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
MsgType MsgType `json:"msgtype"`
Format MessageFormat `json:"format"`
}
// SendNotification sends a notification to the specified user.
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) (id string, err error) {
log.Printf("Sending notification to room %s.", a.MatrixID)
plainMessage := strings.TrimSpace(n.Message)
plainTitle := strings.TrimSpace(n.Title)
message := d.getFormattedMessage(n)
title := d.getFormattedTitle(n)
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
formattedText := fmt.Sprintf("%s %s", title, message)
respSendEvent, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)
return respSendEvent.EventID, err
}
// DeleteNotification sends a notification to the specified user that another notificaion is deleted
func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNotification) error {
log.Printf("Sending delete notification to room %s", a.MatrixID)
var oldFormattedBody string
var oldBody string
// get the message we want to delete
deleteMessage, err := d.getMessage(a, n.ID)
if err != nil {
log.Println(err)
return pberrors.ErrorMessageNotFound
}
oldBody, oldFormattedBody, err = bodiesFromMessage(deleteMessage)
if err != nil {
return err
}
// update the message with strikethrough
newBody := fmt.Sprintf("<del>%s</del>\n- deleted", oldBody)
newFormattedBody := fmt.Sprintf("<del>%s</del><br>- deleted", oldFormattedBody)
_, err = d.replaceMessage(a, newBody, newFormattedBody, deleteMessage.ID, oldBody, oldFormattedBody)
if err != nil {
return err
}
_, err = d.respondToMessage(a, "This message got deleted", "<i>This message got deleted.</i>", deleteMessage)
return err
}
// HTML-formats the title
func (d *Dispatcher) getFormattedTitle(n *model.Notification) string {
trimmedTitle := strings.TrimSpace(n.Title)
title := html.EscapeString(trimmedTitle)
if d.formatting.ColoredTitle {
title = d.coloredText(d.priorityToColor(n.Priority), title)
}
return "<b>" + title + "</b><br /><br />"
}
// Converts different syntaxes to a HTML-formatted message
func (d *Dispatcher) getFormattedMessage(n *model.Notification) string {
trimmedMessage := strings.TrimSpace(n.Message)
message := strings.Replace(html.EscapeString(trimmedMessage), "\n", "<br />", -1) // default to text/plain
if optionsDisplayRaw, ok := n.Extras["client::display"]; ok {
optionsDisplay, ok := optionsDisplayRaw.(map[string]interface{})
if ok {
if contentTypeRaw, ok := optionsDisplay["contentType"]; ok {
contentType := fmt.Sprintf("%v", contentTypeRaw)
log.Printf("Message content type: %s", contentType)
switch contentType {
case "html", "text/html":
message = strings.Replace(trimmedMessage, "\n", "<br />", -1)
case "markdown", "md", "text/md", "text/markdown":
// allow HTML in Markdown
message = string(markdown.ToHTML([]byte(trimmedMessage), nil, nil))
}
}
}
}
return message
}
// Maps priorities to hex colors
func (d *Dispatcher) priorityToColor(prio int) string {
switch {
case prio < 0:
return "#828282"
case prio <= 3: // info - default color
return ""
case prio <= 10: // low - yellow
return "#edd711"
case prio <= 20: // mid - orange
return "#ed6d11"
case prio > 20: // high - red
return "#ed1f11"
}
return ""
}
// Maps a priority to a color tag
func (d *Dispatcher) coloredText(color string, text string) string {
if color == "" {
return text
}
return "<font data-mx-color='" + color + "'>" + text + "</font>"
}
// Searches in the messages list for the given id
func (d *Dispatcher) getMessage(a *model.Application, id string) (gomatrix.Event, error) {
start := ""
end := ""
maxPages := 10 // maximum pages to request (10 messages per page)
for i := 0; i < maxPages; i++ {
messages, _ := d.client.Messages(a.MatrixID, start, end, 'b', 10)
for _, event := range messages.Chunk {
if event.ID == id {
return event, nil
}
}
start = messages.End
}
return gomatrix.Event{}, pberrors.ErrorMessageNotFound
}
// Replaces the content of a matrix message
func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedBody string, messageID string, oldBody, oldFormattedBody string) (*gomatrix.RespSendEvent, error) {
newMessage := NewContent{
Body: newBody,
FormattedBody: newFormattedBody,
MsgType: MsgTypeText,
Format: MessageFormatHTML,
}
replaceRelation := RelatesTo{
RelType: "m.replace",
EventID: messageID,
}
replaceEvent := MessageEvent{
Body: oldBody,
FormattedBody: oldFormattedBody,
MsgType: MsgTypeText,
NewContent: newMessage,
RelatesTo: replaceRelation,
Format: MessageFormatHTML,
}
sendEvent, err := d.client.SendMessageEvent(a.MatrixID, "m.room.message", replaceEvent)
if err != nil {
log.Println(err)
return nil, err
}
return sendEvent, nil
}
// Sends a notification in response to another matrix message event
func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody string, respondMessage gomatrix.Event) (*gomatrix.RespSendEvent, error) {
oldBody, oldFormattedBody, err := bodiesFromMessage(respondMessage)
if err != nil {
return nil, err
}
// formatting according to https://matrix.org/docs/spec/client_server/latest#fallbacks-and-event-representation
newFormattedBody := fmt.Sprintf("<mx-reply><blockquote><a href='https://matrix.to/#/%s/%s'>In reply to</a> <a href='https://matrix.to/#/%s'>%s</a><br />%s</blockquote>\n</mx-reply>%s", respondMessage.RoomID, respondMessage.ID, respondMessage.Sender, respondMessage.Sender, oldFormattedBody, formattedBody)
newBody := fmt.Sprintf("> <%s>%s\n\n%s", respondMessage.Sender, oldBody, body)
notificationEvent := MessageEvent{
FormattedBody: newFormattedBody,
Body: newBody,
MsgType: MsgTypeText,
Format: MessageFormatHTML,
}
notificationReply := make(map[string]string)
notificationReply["event_id"] = respondMessage.ID
notificationRelation := RelatesTo{
InReplyTo: notificationReply,
}
notificationEvent.RelatesTo = notificationRelation
return d.client.SendMessageEvent(a.MatrixID, "m.room.message", notificationEvent)
}
// Extracts body and formatted body from a matrix message event
func bodiesFromMessage(message gomatrix.Event) (body, formattedBody string, err error) {
if val, ok := message.Content["body"]; ok {
body, ok := val.(string)
if !ok {
return "", "", pberrors.ErrorMessageNotFound
}
formattedBody = body
} else {
return "", "", pberrors.ErrorMessageNotFound
}
if val, ok := message.Content["formatted_body"]; ok {
body, ok := val.(string)
if !ok {
return "", "", pberrors.ErrorMessageNotFound
}
formattedBody = body
}
return body, formattedBody, nil
}