github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/command_invite.go (about) 1 // Copyright (c) 2015-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 InviteProvider struct { 16 } 17 18 const ( 19 CMD_INVITE = "invite" 20 ) 21 22 func init() { 23 RegisterCommandProvider(&InviteProvider{}) 24 } 25 26 func (me *InviteProvider) GetTrigger() string { 27 return CMD_INVITE 28 } 29 30 func (me *InviteProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 31 return &model.Command{ 32 Trigger: CMD_INVITE, 33 AutoComplete: true, 34 AutoCompleteDesc: T("api.command_invite.desc"), 35 AutoCompleteHint: T("api.command_invite.hint"), 36 DisplayName: T("api.command_invite.name"), 37 } 38 } 39 40 func (me *InviteProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 41 if message == "" { 42 return &model.CommandResponse{ 43 Text: args.T("api.command_invite.missing_message.app_error"), 44 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 45 } 46 } 47 48 splitMessage := strings.SplitN(message, " ", 2) 49 targetUsername := splitMessage[0] 50 targetUsername = strings.TrimPrefix(targetUsername, "@") 51 52 result := <-a.Srv.Store.User().GetByUsername(targetUsername) 53 if result.Err != nil { 54 mlog.Error(result.Err.Error()) 55 return &model.CommandResponse{ 56 Text: args.T("api.command_invite.missing_user.app_error"), 57 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 58 } 59 } 60 61 userProfile := result.Data.(*model.User) 62 if userProfile.DeleteAt != 0 { 63 return &model.CommandResponse{ 64 Text: args.T("api.command_invite.missing_user.app_error"), 65 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 66 } 67 } 68 69 var channelToJoin *model.Channel 70 var err *model.AppError 71 // User set a channel to add the invited user 72 if len(splitMessage) > 1 && splitMessage[1] != "" { 73 targetChannelName := strings.TrimPrefix(strings.TrimSpace(splitMessage[1]), "~") 74 75 if channelToJoin, err = a.GetChannelByName(targetChannelName, args.TeamId, false); err != nil { 76 return &model.CommandResponse{ 77 Text: args.T("api.command_invite.channel.error", map[string]interface{}{ 78 "Channel": targetChannelName, 79 }), 80 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 81 } 82 } 83 } else { 84 channelToJoin, err = a.GetChannel(args.ChannelId) 85 if err != nil { 86 return &model.CommandResponse{ 87 Text: args.T("api.command_invite.channel.app_error"), 88 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 89 } 90 } 91 } 92 93 // Permissions Check 94 switch channelToJoin.Type { 95 case model.CHANNEL_OPEN: 96 if !a.SessionHasPermissionToChannel(args.Session, channelToJoin.Id, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS) { 97 return &model.CommandResponse{ 98 Text: args.T("api.command_invite.permission.app_error", map[string]interface{}{ 99 "User": userProfile.Username, 100 "Channel": channelToJoin.Name, 101 }), 102 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 103 } 104 } 105 case model.CHANNEL_PRIVATE: 106 if !a.SessionHasPermissionToChannel(args.Session, channelToJoin.Id, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS) { 107 if _, err = a.GetChannelMember(channelToJoin.Id, args.UserId); err == nil { 108 // User doing the inviting is a member of the channel. 109 return &model.CommandResponse{ 110 Text: args.T("api.command_invite.permission.app_error", map[string]interface{}{ 111 "User": userProfile.Username, 112 "Channel": channelToJoin.Name, 113 }), 114 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 115 } 116 } else { 117 // User doing the inviting is *not* a member of the channel. 118 return &model.CommandResponse{ 119 Text: args.T("api.command_invite.private_channel.app_error", map[string]interface{}{ 120 "Channel": channelToJoin.Name, 121 }), 122 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 123 } 124 } 125 } 126 default: 127 return &model.CommandResponse{ 128 Text: args.T("api.command_invite.directchannel.app_error"), 129 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 130 } 131 } 132 133 // Check if user is already in the channel 134 _, err = a.GetChannelMember(channelToJoin.Id, userProfile.Id) 135 if err == nil { 136 return &model.CommandResponse{ 137 Text: args.T("api.command_invite.user_already_in_channel.app_error", map[string]interface{}{ 138 "User": userProfile.Username, 139 }), 140 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 141 } 142 } 143 144 if _, err := a.AddChannelMember(userProfile.Id, channelToJoin, args.Session.UserId, "", !args.Session.IsMobileApp()); err != nil { 145 return &model.CommandResponse{ 146 Text: args.T("api.command_invite.fail.app_error"), 147 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 148 } 149 } 150 151 if args.ChannelId != channelToJoin.Id { 152 return &model.CommandResponse{ 153 Text: args.T("api.command_invite.success", map[string]interface{}{ 154 "User": userProfile.Username, 155 "Channel": channelToJoin.Name, 156 }), 157 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 158 } 159 } 160 161 return &model.CommandResponse{} 162 }