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
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
"strconv"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
)
type ExpandProvider struct {
}
type CollapseProvider struct {
}
const (
CMD_EXPAND = "expand"
CMD_COLLAPSE = "collapse"
)
func init() {
RegisterCommandProvider(&ExpandProvider{})
RegisterCommandProvider(&CollapseProvider{})
}
func (me *ExpandProvider) GetTrigger() string {
return CMD_EXPAND
}
func (me *CollapseProvider) GetTrigger() string {
return CMD_COLLAPSE
}
func (me *ExpandProvider) GetCommand(c *Context) *model.Command {
return &model.Command{
Trigger: CMD_EXPAND,
AutoComplete: true,
AutoCompleteDesc: c.T("api.command_expand.desc"),
DisplayName: c.T("api.command_expand.name"),
}
}
func (me *CollapseProvider) GetCommand(c *Context) *model.Command {
return &model.Command{
Trigger: CMD_COLLAPSE,
AutoComplete: true,
AutoCompleteDesc: c.T("api.command_collapse.desc"),
DisplayName: c.T("api.command_collapse.name"),
}
}
func (me *ExpandProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
return setCollapsePreference(c, false)
}
func (me *CollapseProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
return setCollapsePreference(c, true)
}
func setCollapsePreference(c *Context, isCollapse bool) *model.CommandResponse {
pref := model.Preference{
UserId: c.Session.UserId,
Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
Name: model.PREFERENCE_NAME_COLLAPSE_SETTING,
Value: strconv.FormatBool(isCollapse),
}
if result := <-app.Srv.Store.Preference().Save(&model.Preferences{pref}); result.Err != nil {
return &model.CommandResponse{Text: c.T("api.command_expand_collapse.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
socketMessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", c.Session.UserId, nil)
socketMessage.Add("preference", pref.ToJson())
go app.Publish(socketMessage)
var rmsg string
if isCollapse {
rmsg = c.T("api.command_collapse.success")
} else {
rmsg = c.T("api.command_expand.success")
}
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: rmsg}
}
|