-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
160 lines (118 loc) · 3.27 KB
/
plugin.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
package main
import (
"strings"
"sync"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/robfig/cron/v3"
)
// Plugin implements the interface expected by the Mattermost server to communicate between the server and plugin processes.
type Plugin struct {
plugin.MattermostPlugin
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
cron *cron.Cron
cronEntryID cron.EntryID
users []string
paused []string
usersMeetings [][]string
botUserID string
}
func (p *Plugin) refreshCron(configuration *configuration) {
p.cron.Remove(p.cronEntryID)
p.addCronFunc()
}
func (p *Plugin) addCronFunc() {
config := p.getConfiguration()
configCron := config.Cron
if configCron == "" {
configCron = "@weekly"
}
if config.Cron == "custom" && len(config.CustomCron) > 0 {
configCron = config.CustomCron
}
// every minute "* * * * *"
p.cronEntryID, _ = p.cron.AddFunc(configCron, func() {
p.runMeetings()
})
}
func (p *Plugin) runMeetings() {
users := p.getAvailableUsers()
config := p.getConfiguration()
meetings := getMeetingsShuffleUsers(users, p.configuration.NumUsersPerMeeting, p.usersMeetings)
rooms := strings.Split(config.MeetingRooms, ",")
for index, meeting := range meetings {
meetingUrl := ""
if len(rooms) > index {
meetingUrl = rooms[index]
}
p.startMeeting(meeting, meetingUrl)
}
}
func (p *Plugin) startMeeting(meeting []string, meetingUrl string) {
maxMeetings := 100
p.usersMeetings = PrependSlice(p.usersMeetings, meeting)
if len(p.usersMeetings) > maxMeetings {
p.usersMeetings = p.usersMeetings[:len(p.usersMeetings)-1]
}
users := append(meeting, p.botUserID)
channel, _ := p.API.GetGroupChannel(users)
config := p.getConfiguration()
post := &model.Post{
UserId: p.botUserID,
ChannelId: channel.Id,
Message: config.InitText,
}
p.persistMeetings()
p.API.CreatePost(post)
if len(meetingUrl) > 0 {
post := &model.Post{
UserId: p.botUserID,
ChannelId: channel.Id,
Message: meetingUrl,
}
p.API.CreatePost(post)
}
}
func (p *Plugin) UserHasLeftTeam(c *plugin.Context, teamMember *model.TeamMember) {
p.users = Remove(p.users, teamMember.UserId)
p.persistMeetings()
}
func (p *Plugin) getAvailableUsers() []string {
var users []string
for _, userId := range p.users {
if !Contains(p.paused, userId) {
users = append(users, userId)
}
}
return users
}
func (p *Plugin) addUser(userID string) {
if !Contains(p.users, userID) {
p.users = append(p.users, userID)
}
}
func (p *Plugin) removeUser(userID string) {
p.users = Remove(p.users, userID)
p.persistMeetings()
}
func (p *Plugin) OnDeactivate() error {
p.cron.Remove(p.cronEntryID)
p.cron.Stop()
return p.persistUsers()
}
func (p *Plugin) usersMeetingsByUsername() [][]string {
mettings := [][]string{}
for _, meeting := range p.usersMeetings {
newMeeting := []string{}
for _, userId := range meeting {
userData, _ := p.API.GetUser(userId)
newMeeting = append(newMeeting, userData.Username)
}
mettings = append(mettings, newMeeting)
}
return mettings
}