-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmessage.go
57 lines (48 loc) · 1.29 KB
/
message.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
package main
import (
"bytes"
"text/template"
"github.com/eternal-flame-AD/gotify-broadcast/model"
"github.com/eternal-flame-AD/gotify-broadcast/rules"
plugin "github.com/gotify/plugin-api"
)
var msgTemplate = template.Must(template.New("message").Parse(`
{{.Msg.Message}}
==============
Sent with gotify-broadcast plugin.
Sender: {{.Sender.Name}}{{if .Sender.Admin}} (Admin){{end}}
Channel: {{.ChannelName}}
Priority: {{.Msg.Priority}}
`))
func (c *Plugin) recvMessage(msg model.Message) {
if !c.enabled {
return
}
if msg.Receiver.ID != c.UserCtx.ID {
return
}
if action := c.config.SenderFilter.Match(msg, rules.Accept); action == rules.Accept {
wrappedMsg := bytes.NewBuffer([]byte{})
if err := msgTemplate.Execute(wrappedMsg, msg); err == nil {
msg.Msg.Message = wrappedMsg.String()
}
c.msgHandler.SendMessage(msg.Msg)
}
}
func (c *Plugin) sendMessage(msg plugin.Message, chanName string) int {
sent := 0
for _, recipient := range usersList.GetUsersList() {
msgWrapped := model.Message{
Sender: c.UserCtx,
Receiver: recipient,
Msg: msg,
ChannelName: chanName,
IsSend: true,
}
if action := c.config.ReceiverFilter.Match(msgWrapped, rules.Accept); action == rules.Accept {
msgExchanger.MsgChan <- msgWrapped
sent++
}
}
return sent
}