github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/command_channel_purpose.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 goi18n "github.com/nicksnyder/go-i18n/i18n" 8 9 "github.com/mattermost/mattermost-server/model" 10 ) 11 12 type PurposeProvider struct { 13 } 14 15 const ( 16 CMD_PURPOSE = "purpose" 17 ) 18 19 func init() { 20 RegisterCommandProvider(&PurposeProvider{}) 21 } 22 23 func (me *PurposeProvider) GetTrigger() string { 24 return CMD_PURPOSE 25 } 26 27 func (me *PurposeProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 28 return &model.Command{ 29 Trigger: CMD_PURPOSE, 30 AutoComplete: true, 31 AutoCompleteDesc: T("api.command_channel_purpose.desc"), 32 AutoCompleteHint: T("api.command_channel_purpose.hint"), 33 DisplayName: T("api.command_channel_purpose.name"), 34 } 35 } 36 37 func (me *PurposeProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 38 channel, err := a.GetChannel(args.ChannelId) 39 if err != nil { 40 return &model.CommandResponse{ 41 Text: args.T("api.command_channel_purpose.channel.app_error"), 42 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 43 } 44 } 45 46 switch channel.Type { 47 case model.CHANNEL_OPEN: 48 if !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) { 49 return &model.CommandResponse{ 50 Text: args.T("api.command_channel_purpose.permission.app_error"), 51 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 52 } 53 } 54 case model.CHANNEL_PRIVATE: 55 if !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_PROPERTIES) { 56 return &model.CommandResponse{ 57 Text: args.T("api.command_channel_purpose.permission.app_error"), 58 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 59 } 60 } 61 default: 62 return &model.CommandResponse{ 63 Text: args.T("api.command_channel_purpose.direct_group.app_error"), 64 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 65 } 66 } 67 68 if len(message) == 0 { 69 return &model.CommandResponse{ 70 Text: args.T("api.command_channel_purpose.message.app_error"), 71 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 72 } 73 } 74 75 patch := &model.ChannelPatch{ 76 Purpose: new(string), 77 } 78 *patch.Purpose = message 79 80 _, err = a.PatchChannel(channel, patch, args.UserId) 81 if err != nil { 82 return &model.CommandResponse{ 83 Text: args.T("api.command_channel_purpose.update_channel.app_error"), 84 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 85 } 86 } 87 88 return &model.CommandResponse{} 89 }