github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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 "github.com/mattermost/mattermost-server/model" 8 goi18n "github.com/nicksnyder/go-i18n/i18n" 9 ) 10 11 type PurposeProvider struct { 12 } 13 14 const ( 15 CMD_PURPOSE = "purpose" 16 ) 17 18 func init() { 19 RegisterCommandProvider(&PurposeProvider{}) 20 } 21 22 func (me *PurposeProvider) GetTrigger() string { 23 return CMD_PURPOSE 24 } 25 26 func (me *PurposeProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 27 return &model.Command{ 28 Trigger: CMD_PURPOSE, 29 AutoComplete: true, 30 AutoCompleteDesc: T("api.command_channel_purpose.desc"), 31 AutoCompleteHint: T("api.command_channel_purpose.hint"), 32 DisplayName: T("api.command_channel_purpose.name"), 33 } 34 } 35 36 func (me *PurposeProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 37 channel, err := a.GetChannel(args.ChannelId) 38 if err != nil { 39 return &model.CommandResponse{Text: args.T("api.command_channel_purpose.channel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 40 } 41 42 if channel.Type == model.CHANNEL_OPEN && !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) { 43 return &model.CommandResponse{Text: args.T("api.command_channel_purpose.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 44 } 45 46 if channel.Type == model.CHANNEL_PRIVATE && !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_PROPERTIES) { 47 return &model.CommandResponse{Text: args.T("api.command_channel_purpose.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 48 } 49 50 if channel.Type == model.CHANNEL_GROUP || channel.Type == model.CHANNEL_DIRECT { 51 return &model.CommandResponse{Text: args.T("api.command_channel_purpose.direct_group.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 52 } 53 54 if len(message) == 0 { 55 return &model.CommandResponse{Text: args.T("api.command_channel_purpose.message.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 56 } 57 58 patch := &model.ChannelPatch{ 59 Purpose: new(string), 60 } 61 *patch.Purpose = message 62 63 _, err = a.PatchChannel(channel, patch, args.UserId) 64 if err != nil { 65 return &model.CommandResponse{Text: args.T("api.command_channel_purpose.update_channel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 66 } 67 68 return &model.CommandResponse{} 69 }