github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/command_expand_collapse.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "strconv" 8 9 "github.com/mattermost/mattermost-server/model" 10 goi18n "github.com/nicksnyder/go-i18n/i18n" 11 ) 12 13 type ExpandProvider struct { 14 } 15 16 type CollapseProvider struct { 17 } 18 19 const ( 20 CMD_EXPAND = "expand" 21 CMD_COLLAPSE = "collapse" 22 ) 23 24 func init() { 25 RegisterCommandProvider(&ExpandProvider{}) 26 RegisterCommandProvider(&CollapseProvider{}) 27 } 28 29 func (me *ExpandProvider) GetTrigger() string { 30 return CMD_EXPAND 31 } 32 33 func (me *CollapseProvider) GetTrigger() string { 34 return CMD_COLLAPSE 35 } 36 37 func (me *ExpandProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 38 return &model.Command{ 39 Trigger: CMD_EXPAND, 40 AutoComplete: true, 41 AutoCompleteDesc: T("api.command_expand.desc"), 42 DisplayName: T("api.command_expand.name"), 43 } 44 } 45 46 func (me *CollapseProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 47 return &model.Command{ 48 Trigger: CMD_COLLAPSE, 49 AutoComplete: true, 50 AutoCompleteDesc: T("api.command_collapse.desc"), 51 DisplayName: T("api.command_collapse.name"), 52 } 53 } 54 55 func (me *ExpandProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 56 return a.setCollapsePreference(args, false) 57 } 58 59 func (me *CollapseProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 60 return a.setCollapsePreference(args, true) 61 } 62 63 func (a *App) setCollapsePreference(args *model.CommandArgs, isCollapse bool) *model.CommandResponse { 64 pref := model.Preference{ 65 UserId: args.UserId, 66 Category: model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, 67 Name: model.PREFERENCE_NAME_COLLAPSE_SETTING, 68 Value: strconv.FormatBool(isCollapse), 69 } 70 71 if result := <-a.Srv.Store.Preference().Save(&model.Preferences{pref}); result.Err != nil { 72 return &model.CommandResponse{Text: args.T("api.command_expand_collapse.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 73 } 74 75 socketMessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", args.UserId, nil) 76 socketMessage.Add("preference", pref.ToJson()) 77 a.Publish(socketMessage) 78 79 var rmsg string 80 81 if isCollapse { 82 rmsg = args.T("api.command_collapse.success") 83 } else { 84 rmsg = args.T("api.command_expand.success") 85 } 86 return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: rmsg} 87 }