github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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 goi18n "github.com/nicksnyder/go-i18n/i18n" 10 11 "github.com/mattermost/mattermost-server/mlog" 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{ 71 Text: args.T("api.command_channel_remove.channel.app_error"), 72 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 73 } 74 } 75 76 switch channel.Type { 77 case model.CHANNEL_OPEN: 78 if !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS) { 79 return &model.CommandResponse{ 80 Text: args.T("api.command_remove.permission.app_error"), 81 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 82 } 83 } 84 case model.CHANNEL_PRIVATE: 85 if !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS) { 86 return &model.CommandResponse{ 87 Text: args.T("api.command_remove.permission.app_error"), 88 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 89 } 90 } 91 default: 92 return &model.CommandResponse{ 93 Text: args.T("api.command_remove.direct_group.app_error"), 94 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 95 } 96 } 97 98 if len(message) == 0 { 99 return &model.CommandResponse{ 100 Text: args.T("api.command_remove.message.app_error"), 101 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 102 } 103 } 104 105 targetUsername := "" 106 107 targetUsername = strings.SplitN(message, " ", 2)[0] 108 targetUsername = strings.TrimPrefix(targetUsername, "@") 109 110 result := <-a.Srv.Store.User().GetByUsername(targetUsername) 111 if result.Err != nil { 112 mlog.Error(result.Err.Error()) 113 return &model.CommandResponse{ 114 Text: args.T("api.command_remove.missing.app_error"), 115 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 116 } 117 } 118 userProfile := result.Data.(*model.User) 119 if userProfile.DeleteAt != 0 { 120 return &model.CommandResponse{ 121 Text: args.T("api.command_remove.missing.app_error"), 122 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 123 } 124 } 125 126 _, err = a.GetChannelMember(args.ChannelId, userProfile.Id) 127 if err != nil { 128 nameFormat := *a.Config().TeamSettings.TeammateNameDisplay 129 return &model.CommandResponse{ 130 Text: args.T("api.command_remove.user_not_in_channel", map[string]interface{}{ 131 "Username": userProfile.GetDisplayName(nameFormat), 132 }), 133 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 134 } 135 } 136 137 if err = a.RemoveUserFromChannel(userProfile.Id, args.UserId, channel); err != nil { 138 return &model.CommandResponse{ 139 Text: args.T(err.Id, map[string]interface{}{ 140 "Channel": model.DEFAULT_CHANNEL, 141 }), 142 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 143 } 144 } 145 146 return &model.CommandResponse{} 147 }