/
notification.go
59 lines (51 loc) · 2.03 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
package service
import (
"github.com/thoas/go-funk"
"github.com/mattermost/mattermost-plugin-confluence/server/config"
"github.com/mattermost/mattermost-plugin-confluence/server/serializer"
"github.com/mattermost/mattermost-plugin-confluence/server/util"
)
func SendConfluenceNotifications(event serializer.ConfluenceEvent, eventType string) {
url := event.GetURL()
spaceKey := event.GetSpaceKey()
pageID := event.GetPageID()
post := event.GetNotificationPost(eventType)
if post == nil || pageID == "" || url == "" || spaceKey == "" {
return
}
subscriptionChannelIDs := getNotificationChannelIDs(url, spaceKey, pageID, eventType)
for _, channelID := range subscriptionChannelIDs {
post.ChannelId = channelID
if _, err := config.Mattermost.CreatePost(post); err != nil {
config.Mattermost.LogError("Unable to create Post in Mattermost", "Error", err.Error())
}
}
}
func getNotificationChannelIDs(url, spaceKey, pageID, eventType string) []string {
urlSpaceKeySubscriptions, err := GetSubscriptionsByURLSpaceKey(url, spaceKey)
if err != nil {
config.Mattermost.LogError("Unable to get subscribed channels.", "Error", err.Error())
return nil
}
urlPageIDSubscriptions, err := GetSubscriptionsByURLPageID(url, pageID)
if err != nil {
config.Mattermost.LogError("Unable to get subscribed channels.", "Error", err.Error())
return nil
}
urlSpaceKeySubscriptionChannelIDs := make([]string, 0)
urlPageIDSubscriptionChannelIDs := make([]string, 0)
for channelID, events := range urlPageIDSubscriptions {
if funk.Contains(events, eventType) {
urlPageIDSubscriptionChannelIDs = append(urlPageIDSubscriptionChannelIDs, channelID)
}
}
for channelID, events := range urlSpaceKeySubscriptions {
if funk.Contains(events, eventType) {
urlSpaceKeySubscriptionChannelIDs = append(urlSpaceKeySubscriptionChannelIDs, channelID)
}
}
var channelIDs []string
channelIDs = append(channelIDs, urlSpaceKeySubscriptionChannelIDs...)
channelIDs = append(channelIDs, urlPageIDSubscriptionChannelIDs...)
return util.Deduplicate(channelIDs)
}