generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 59
/
plugin.go
671 lines (558 loc) · 20.8 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"sync"
"time"
"github.com/mattermost/mattermost-plugin-api/experimental/telemetry"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/pkg/errors"
)
const (
// WSEventRefresh is the WebSocket event for refreshing the Todo list
WSEventRefresh = "refresh"
// WSEventConfigUpdate is the WebSocket event to update the Todo list's configurations on webapp
WSEventConfigUpdate = "config_update"
)
// ListManager represents the logic on the lists
type ListManager interface {
// AddIssue adds a todo to userID's myList with the message
AddIssue(userID, message, description, postID string) (*Issue, error)
// SendIssue sends the todo with the message from senderID to receiverID and returns the receiver's issueID
SendIssue(senderID, receiverID, message, description, postID string) (string, error)
// GetIssueList gets the todos on listID for userID
GetIssueList(userID, listID string) ([]*ExtendedIssue, error)
// CompleteIssue completes the todo issueID for userID, and returns the issue and the foreign ID if any
CompleteIssue(userID, issueID string) (issue *Issue, foreignID string, listToUpdate string, err error)
// AcceptIssue moves one the todo issueID of userID from inbox to myList, and returns the message and the foreignUserID if any
AcceptIssue(userID, issueID string) (todoMessage string, foreignUserID string, err error)
// RemoveIssue removes the todo issueID for userID and returns the issue, the foreign ID if any and whether the user sent the todo to someone else
RemoveIssue(userID, issueID string) (issue *Issue, foreignID string, isSender bool, listToUpdate string, err error)
// PopIssue the first element of myList for userID and returns the issue and the foreign ID if any
PopIssue(userID string) (issue *Issue, foreignID string, err error)
// BumpIssue moves a issueID sent by userID to the top of its receiver inbox list
BumpIssue(userID string, issueID string) (todoMessage string, receiver string, foreignIssueID string, err error)
// EditIssue updates the message on an issue
EditIssue(userID string, issueID string, newMessage string, newDescription string) (foreignUserID string, list string, oldMessage string, err error)
// ChangeAssignment updates an issue to assign a different person
ChangeAssignment(issueID string, userID string, sendTo string) (issueMessage, oldOwner string, err error)
// GetUserName returns the readable username from userID
GetUserName(userID string) string
}
// Plugin implements the interface expected by the Mattermost server to communicate between the server and plugin processes.
type Plugin struct {
plugin.MattermostPlugin
BotUserID string
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
listManager ListManager
telemetryClient telemetry.Client
tracker telemetry.Tracker
}
func (p *Plugin) OnActivate() error {
config := p.getConfiguration()
if err := config.IsValid(); err != nil {
return err
}
botID, err := p.Helpers.EnsureBot(&model.Bot{
Username: "todo",
DisplayName: "Todo Bot",
Description: "Created by the Todo plugin.",
})
if err != nil {
return errors.Wrap(err, "failed to ensure todo bot")
}
p.BotUserID = botID
p.listManager = NewListManager(p.API)
p.telemetryClient, err = telemetry.NewRudderClient()
if err != nil {
p.API.LogWarn("telemetry client not started", "error", err.Error())
}
return p.API.RegisterCommand(getCommand())
}
func (p *Plugin) OnDeactivate() error {
if p.telemetryClient != nil {
err := p.telemetryClient.Close()
if err != nil {
p.API.LogWarn("OnDeactivate: failed to close telemetryClient", "error", err.Error())
}
}
return nil
}
// ServeHTTP demonstrates a plugin that handles HTTP requests by greeting the world.
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/add":
p.handleAdd(w, r)
case "/list":
p.handleList(w, r)
case "/remove":
p.handleRemove(w, r)
case "/complete":
p.handleComplete(w, r)
case "/accept":
p.handleAccept(w, r)
case "/bump":
p.handleBump(w, r)
case "/telemetry":
p.handleTelemetry(w, r)
case "/config":
p.handleConfig(w, r)
case "/edit":
p.handleEdit(w, r)
case "/change_assignment":
p.handleChangeAssignment(w, r)
default:
http.NotFound(w, r)
}
}
type telemetryAPIRequest struct {
Event string
Properties map[string]interface{}
}
func (p *Plugin) handleTelemetry(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var telemetryRequest *telemetryAPIRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&telemetryRequest)
if err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
if telemetryRequest.Event != "" {
p.trackFrontend(userID, telemetryRequest.Event, telemetryRequest.Properties)
}
}
type addAPIRequest struct {
Message string `json:"message"`
Description string `json:"description"`
SendTo string `json:"send_to"`
PostID string `json:"post_id"`
}
func (p *Plugin) handleAdd(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var addRequest *addAPIRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&addRequest)
if err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
senderName := p.listManager.GetUserName(userID)
if addRequest.SendTo == "" {
_, err = p.listManager.AddIssue(userID, addRequest.Message, addRequest.Description, addRequest.PostID)
if err != nil {
p.API.LogError("Unable to add issue err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to add issue", err)
return
}
p.trackAddIssue(userID, sourceWebapp, addRequest.PostID != "")
p.sendRefreshEvent(userID, []string{MyListKey})
replyMessage := fmt.Sprintf("@%s attached a todo to this thread", senderName)
p.postReplyIfNeeded(addRequest.PostID, replyMessage, addRequest.Message)
return
}
receiver, appErr := p.API.GetUserByUsername(addRequest.SendTo)
if appErr != nil {
p.API.LogError("username not valid, err=" + appErr.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to find user", appErr)
return
}
if receiver.Id == userID {
_, err = p.listManager.AddIssue(userID, addRequest.Message, addRequest.Description, addRequest.PostID)
if err != nil {
p.API.LogError("Unable to add issue err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to add issue", err)
return
}
p.trackAddIssue(userID, sourceWebapp, addRequest.PostID != "")
p.sendRefreshEvent(userID, []string{MyListKey})
replyMessage := fmt.Sprintf("@%s attached a todo to this thread", senderName)
p.postReplyIfNeeded(addRequest.PostID, replyMessage, addRequest.Message)
return
}
receiverAllowIncomingTaskRequestsPreference, err := p.getAllowIncomingTaskRequestsPreference(receiver.Id)
if err != nil {
p.API.LogError("Error when getting allow incoming task request preference, err=", err)
receiverAllowIncomingTaskRequestsPreference = true
}
if !receiverAllowIncomingTaskRequestsPreference {
replyMessage := fmt.Sprintf("@%s has blocked Todo requests", receiver.Username)
p.PostBotDM(userID, replyMessage)
return
}
issueID, err := p.listManager.SendIssue(userID, receiver.Id, addRequest.Message, addRequest.Description, addRequest.PostID)
if err != nil {
p.API.LogError("Unable to send issue err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to send issue", err)
return
}
p.trackSendIssue(userID, sourceWebapp, addRequest.PostID != "")
p.sendRefreshEvent(userID, []string{OutListKey})
p.sendRefreshEvent(receiver.Id, []string{InListKey})
receiverMessage := fmt.Sprintf("You have received a new Todo from @%s", senderName)
p.PostBotCustomDM(receiver.Id, receiverMessage, addRequest.Message, issueID)
replyMessage := fmt.Sprintf("@%s sent @%s a todo attached to this thread", senderName, addRequest.SendTo)
p.postReplyIfNeeded(addRequest.PostID, replyMessage, addRequest.Message)
}
func (p *Plugin) postReplyIfNeeded(postID, message, todo string) {
if postID != "" {
err := p.ReplyPostBot(postID, message, todo)
if err != nil {
p.API.LogError(err.Error())
}
}
}
func (p *Plugin) handleList(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
listInput := r.URL.Query().Get("list")
listID := MyListKey
switch listInput {
case OutFlag:
listID = OutListKey
case InFlag:
listID = InListKey
}
issues, err := p.listManager.GetIssueList(userID, listID)
if err != nil {
p.API.LogError("Unable to get issues for user err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to get issues for user", err)
return
}
if len(issues) > 0 && r.URL.Query().Get("reminder") == "true" && p.getReminderPreference(userID) {
var lastReminderAt int64
lastReminderAt, err = p.getLastReminderTimeForUser(userID)
if err != nil {
p.API.LogError("Unable to send reminder err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to send reminder", err)
return
}
var timezone *time.Location
offset, _ := strconv.Atoi(r.Header.Get("X-Timezone-Offset"))
timezone = time.FixedZone("local", -60*offset)
// Post reminder message if it's the next day and been more than an hour since the last post
now := model.GetMillis()
nt := time.Unix(now/1000, 0).In(timezone)
lt := time.Unix(lastReminderAt/1000, 0).In(timezone)
if nt.Sub(lt).Hours() >= 1 && (nt.Day() != lt.Day() || nt.Month() != lt.Month() || nt.Year() != lt.Year()) {
p.PostBotDM(userID, "Daily Reminder:\n\n"+issuesListToString(issues))
p.trackDailySummary(userID)
err = p.saveLastReminderTimeForUser(userID)
if err != nil {
p.API.LogError("Unable to save last reminder for user err=" + err.Error())
}
}
}
issuesJSON, err := json.Marshal(issues)
if err != nil {
p.API.LogError("Unable marhsal issues list to json err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable marhsal issues list to json", err)
return
}
_, err = w.Write(issuesJSON)
if err != nil {
p.API.LogError("Unable to write json response err=" + err.Error())
}
}
type editAPIRequest struct {
ID string `json:"id"`
Message string `json:"message"`
Description string `json:"description"`
}
func (p *Plugin) handleEdit(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var editRequest *editAPIRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&editRequest); err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()
foreignUserID, list, oldMessage, err := p.listManager.EditIssue(userID, editRequest.ID, editRequest.Message, editRequest.Description)
if err != nil {
p.API.LogError("Unable to edit message: err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to edit issue", err)
return
}
p.trackEditIssue(userID)
p.sendRefreshEvent(userID, []string{list})
if foreignUserID != "" {
var lists []string
if list == OutListKey {
lists = []string{MyListKey, InListKey}
} else {
lists = []string{OutListKey}
}
p.sendRefreshEvent(foreignUserID, lists)
userName := p.listManager.GetUserName(userID)
message := fmt.Sprintf("@%s modified a Todo from:\n%s\nTo:\n%s", userName, oldMessage, editRequest.Message)
p.PostBotDM(foreignUserID, message)
}
}
type changeAssignmentAPIRequest struct {
ID string `json:"id"`
SendTo string `json:"send_to"`
}
func (p *Plugin) handleChangeAssignment(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var changeRequest *changeAssignmentAPIRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&changeRequest); err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
r.Body.Close()
if changeRequest.SendTo == "" {
http.Error(w, "No user specified", http.StatusBadRequest)
return
}
receiver, appErr := p.API.GetUserByUsername(changeRequest.SendTo)
if appErr != nil {
p.API.LogError("username not valid, err=" + appErr.Error())
p.handleErrorWithCode(w, http.StatusNotFound, "Unable to find user", appErr)
return
}
issueMessage, oldOwner, err := p.listManager.ChangeAssignment(changeRequest.ID, userID, receiver.Id)
if err != nil {
p.API.LogError("Unable to change the assignment of an issue: err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to change the assignment", err)
return
}
p.trackChangeAssignment(userID)
p.sendRefreshEvent(userID, []string{MyListKey, OutListKey})
userName := p.listManager.GetUserName(userID)
if receiver.Id != userID {
p.sendRefreshEvent(receiver.Id, []string{InListKey})
receiverMessage := fmt.Sprintf("You have received a new Todo from @%s", userName)
p.PostBotCustomDM(receiver.Id, receiverMessage, issueMessage, changeRequest.ID)
}
if oldOwner != "" {
p.sendRefreshEvent(oldOwner, []string{InListKey, MyListKey})
oldOwnerMessage := fmt.Sprintf("@%s removed you from Todo:\n%s", userName, issueMessage)
p.PostBotDM(oldOwner, oldOwnerMessage)
}
}
type acceptAPIRequest struct {
ID string `json:"id"`
}
func (p *Plugin) handleAccept(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var acceptRequest *acceptAPIRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&acceptRequest); err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
todoMessage, sender, err := p.listManager.AcceptIssue(userID, acceptRequest.ID)
if err != nil {
p.API.LogError("Unable to accept issue err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to accept issue", err)
return
}
p.trackAcceptIssue(userID)
p.sendRefreshEvent(userID, []string{MyListKey, InListKey})
p.sendRefreshEvent(sender, []string{OutListKey})
userName := p.listManager.GetUserName(userID)
message := fmt.Sprintf("@%s accepted a Todo you sent: %s", userName, todoMessage)
p.PostBotDM(sender, message)
}
type completeAPIRequest struct {
ID string `json:"id"`
}
func (p *Plugin) handleComplete(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var completeRequest *completeAPIRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&completeRequest); err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
issue, foreignID, listToUpdate, err := p.listManager.CompleteIssue(userID, completeRequest.ID)
if err != nil {
p.API.LogError("Unable to complete issue err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to complete issue", err)
return
}
p.sendRefreshEvent(userID, []string{listToUpdate})
p.trackCompleteIssue(userID)
userName := p.listManager.GetUserName(userID)
replyMessage := fmt.Sprintf("@%s completed a todo attached to this thread", userName)
p.postReplyIfNeeded(issue.PostID, replyMessage, issue.Message)
if foreignID == "" {
return
}
p.sendRefreshEvent(foreignID, []string{OutListKey})
message := fmt.Sprintf("@%s completed a Todo you sent: %s", userName, issue.Message)
p.PostBotDM(foreignID, message)
}
type removeAPIRequest struct {
ID string `json:"id"`
}
func (p *Plugin) handleRemove(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var removeRequest *removeAPIRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&removeRequest)
if err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
issue, foreignID, isSender, listToUpdate, err := p.listManager.RemoveIssue(userID, removeRequest.ID)
if err != nil {
p.API.LogError("Unable to remove issue, err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to remove issue", err)
return
}
p.sendRefreshEvent(userID, []string{listToUpdate})
p.trackRemoveIssue(userID)
userName := p.listManager.GetUserName(userID)
replyMessage := fmt.Sprintf("@%s removed a todo attached to this thread", userName)
p.postReplyIfNeeded(issue.PostID, replyMessage, issue.Message)
if foreignID == "" {
return
}
list := InListKey
message := fmt.Sprintf("@%s removed a Todo you received: %s", userName, issue.Message)
if isSender {
message = fmt.Sprintf("@%s declined a Todo you sent: %s", userName, issue.Message)
list = OutListKey
}
p.sendRefreshEvent(foreignID, []string{list})
p.PostBotDM(foreignID, message)
}
type bumpAPIRequest struct {
ID string `json:"id"`
}
func (p *Plugin) handleBump(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var bumpRequest *bumpAPIRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&bumpRequest)
if err != nil {
p.API.LogError("Unable to decode JSON err=" + err.Error())
p.handleErrorWithCode(w, http.StatusBadRequest, "Unable to decode JSON", err)
return
}
todoMessage, foreignUser, foreignIssueID, err := p.listManager.BumpIssue(userID, bumpRequest.ID)
if err != nil {
p.API.LogError("Unable to bump issue, err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to bump issue", err)
return
}
p.trackBumpIssue(userID)
if foreignUser == "" {
return
}
p.sendRefreshEvent(foreignUser, []string{InListKey})
userName := p.listManager.GetUserName(userID)
message := fmt.Sprintf("@%s bumped a Todo you received.", userName)
p.PostBotCustomDM(foreignUser, message, todoMessage, foreignIssueID)
}
// API endpoint to retrieve plugin configurations
func (p *Plugin) handleConfig(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
if p.configuration != nil {
// retrieve client only configurations
clientConfig := struct {
HideTeamSidebar bool `json:"hide_team_sidebar"`
}{
HideTeamSidebar: p.configuration.HideTeamSidebar,
}
configJSON, err := json.Marshal(clientConfig)
if err != nil {
p.API.LogError("Unable to marshal plugin configuration to json err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to marshal plugin configuration to json", err)
return
}
_, err = w.Write(configJSON)
if err != nil {
p.API.LogError("Unable to write json response err=" + err.Error())
}
}
}
func (p *Plugin) sendRefreshEvent(userID string, lists []string) {
p.API.PublishWebSocketEvent(
WSEventRefresh,
map[string]interface{}{"lists": lists},
&model.WebsocketBroadcast{UserId: userID},
)
}
// Publish a WebSocket event to update the client config of the plugin on the webapp end.
func (p *Plugin) sendConfigUpdateEvent() {
clientConfigMap := map[string]interface{}{
"hide_team_sidebar": p.configuration.HideTeamSidebar,
}
p.API.PublishWebSocketEvent(
WSEventConfigUpdate,
clientConfigMap,
&model.WebsocketBroadcast{},
)
}
func (p *Plugin) handleErrorWithCode(w http.ResponseWriter, code int, errTitle string, err error) {
w.WriteHeader(code)
b, _ := json.Marshal(struct {
Error string `json:"error"`
Details string `json:"details"`
}{
Error: errTitle,
Details: err.Error(),
})
_, _ = w.Write(b)
}