github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/app/command_remove.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 "strings" 8 9 l4g "github.com/alecthomas/log4go" 10 goi18n "github.com/nicksnyder/go-i18n/i18n" 11 12 "github.com/mattermost/mattermost-server/model" 13 ) 14 15 type RemoveProvider struct { 16 } 17 18 type KickProvider struct { 19 } 20 21 const ( 22 CMD_REMOVE = "remove" 23 CMD_KICK = "kick" 24 ) 25 26 func init() { 27 RegisterCommandProvider(&RemoveProvider{}) 28 RegisterCommandProvider(&KickProvider{}) 29 } 30 31 func (me *RemoveProvider) GetTrigger() string { 32 return CMD_REMOVE 33 } 34 35 func (me *KickProvider) GetTrigger() string { 36 return CMD_KICK 37 } 38 39 func (me *RemoveProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 40 return &model.Command{ 41 Trigger: CMD_REMOVE, 42 AutoComplete: true, 43 AutoCompleteDesc: T("api.command_remove.desc"), 44 AutoCompleteHint: T("api.command_remove.hint"), 45 DisplayName: T("api.command_remove.name"), 46 } 47 } 48 49 func (me *KickProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 50 return &model.Command{ 51 Trigger: CMD_KICK, 52 AutoComplete: true, 53 AutoCompleteDesc: T("api.command_remove.desc"), 54 AutoCompleteHint: T("api.command_remove.hint"), 55 DisplayName: T("api.command_kick.name"), 56 } 57 } 58 59 func (me *RemoveProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 60 return doCommand(a, args, message) 61 } 62 63 func (me *KickProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 64 return doCommand(a, args, message) 65 } 66 67 func doCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 68 channel, err := a.GetChannel(args.ChannelId) 69 if err != nil { 70 return &model.CommandResponse{Text: args.T("api.command_channel_rename.channel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 71 } 72 73 if channel.Type == model.CHANNEL_OPEN && !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS) { 74 return &model.CommandResponse{Text: args.T("api.command_remove.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 75 } 76 77 if channel.Type == model.CHANNEL_PRIVATE && !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS) { 78 return &model.CommandResponse{Text: args.T("api.command_remove.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 79 } 80 81 if channel.Type == model.CHANNEL_GROUP || channel.Type == model.CHANNEL_DIRECT { 82 return &model.CommandResponse{Text: args.T("api.command_remove.direct_group.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 83 } 84 85 if len(message) == 0 { 86 return &model.CommandResponse{Text: args.T("api.command_remove.message.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 87 } 88 89 targetUsername := "" 90 91 targetUsername = strings.SplitN(message, " ", 2)[0] 92 targetUsername = strings.TrimPrefix(targetUsername, "@") 93 94 var userProfile *model.User 95 if result := <-a.Srv.Store.User().GetByUsername(targetUsername); result.Err != nil { 96 l4g.Error(result.Err.Error()) 97 return &model.CommandResponse{Text: args.T("api.command_remove.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 98 } else { 99 userProfile = result.Data.(*model.User) 100 } 101 102 _, err = a.GetChannelMember(args.ChannelId, userProfile.Id) 103 if err != nil { 104 nameFormat := *a.Config().TeamSettings.TeammateNameDisplay 105 return &model.CommandResponse{Text: args.T("api.command_remove.user_not_in_channel", map[string]interface{}{"Username": userProfile.GetDisplayName(nameFormat)}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 106 } 107 108 if err = a.RemoveUserFromChannel(userProfile.Id, args.UserId, channel); err != nil { 109 return &model.CommandResponse{Text: args.T(err.Id, map[string]interface{}{"Channel": model.DEFAULT_CHANNEL}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 110 } 111 112 return &model.CommandResponse{} 113 }