summaryrefslogtreecommitdiffstats
path: root/api/command_expand_collapse.go
blob: c56845a9e74b8ec3024b71b4dc3a781f1d8a021e (plain)
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
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"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, channelId string, message string) *model.CommandResponse {
	return setCollapsePreference(c, "false")
}

func (me *CollapseProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
	return setCollapsePreference(c, "true")
}

func setCollapsePreference(c *Context, value string) *model.CommandResponse {
	pref := model.Preference{
		UserId:   c.Session.UserId,
		Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS,
		Name:     model.PREFERENCE_NAME_COLLAPSE_SETTING,
		Value:    value,
	}

	if result := <-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("", "", c.Session.UserId, model.WEBSOCKET_EVENT_PREFERENCE_CHANGED)
	socketMessage.Add("preference", pref.ToJson())
	go Publish(socketMessage)

	return &model.CommandResponse{}
}