github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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 "github.com/mattermost/mattermost-server/mlog" 10 "github.com/mattermost/mattermost-server/model" 11 goi18n "github.com/nicksnyder/go-i18n/i18n" 12 ) 13 14 type InviteProvider struct { 15 } 16 17 const ( 18 CMD_INVITE = "invite" 19 ) 20 21 func init() { 22 RegisterCommandProvider(&InviteProvider{}) 23 } 24 25 func (me *InviteProvider) GetTrigger() string { 26 return CMD_INVITE 27 } 28 29 func (me *InviteProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 30 return &model.Command{ 31 Trigger: CMD_INVITE, 32 AutoComplete: true, 33 AutoCompleteDesc: T("api.command_invite.desc"), 34 AutoCompleteHint: T("api.command_invite.hint"), 35 DisplayName: T("api.command_invite.name"), 36 } 37 } 38 39 func (me *InviteProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 40 if message == "" { 41 return &model.CommandResponse{Text: args.T("api.command_invite.missing_message.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 42 } 43 44 splitMessage := strings.SplitN(message, " ", 2) 45 targetUsername := splitMessage[0] 46 targetUsername = strings.TrimPrefix(targetUsername, "@") 47 48 var userProfile *model.User 49 if result := <-a.Srv.Store.User().GetByUsername(targetUsername); result.Err != nil { 50 mlog.Error(result.Err.Error()) 51 return &model.CommandResponse{Text: args.T("api.command_invite.missing_user.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 52 } else { 53 userProfile = result.Data.(*model.User) 54 } 55 56 var channelToJoin *model.Channel 57 var err *model.AppError 58 // User set a channel to add the invited user 59 if len(splitMessage) > 1 && splitMessage[1] != "" { 60 targetChannelName := strings.TrimPrefix(strings.TrimSpace(splitMessage[1]), "~") 61 62 if channelToJoin, err = a.GetChannelByName(targetChannelName, args.TeamId); err != nil { 63 return &model.CommandResponse{Text: args.T("api.command_invite.channel.error", map[string]interface{}{"Channel": targetChannelName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 64 } 65 } else { 66 channelToJoin, err = a.GetChannel(args.ChannelId) 67 if err != nil { 68 return &model.CommandResponse{Text: args.T("api.command_invite.channel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 69 } 70 } 71 72 // Check if is a Direct Channel 73 if channelToJoin.Type == model.CHANNEL_DIRECT || channelToJoin.Type == model.CHANNEL_GROUP { 74 return &model.CommandResponse{Text: args.T("api.command_invite.directchannel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 75 } 76 77 // Check Permissions 78 if channelToJoin.Type == model.CHANNEL_OPEN && !a.SessionHasPermissionToChannel(args.Session, channelToJoin.Id, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS) { 79 return &model.CommandResponse{Text: args.T("api.command_invite.permission.app_error", map[string]interface{}{"User": userProfile.Username, "Channel": channelToJoin.Name}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 80 } 81 82 if channelToJoin.Type == model.CHANNEL_PRIVATE && !a.SessionHasPermissionToChannel(args.Session, channelToJoin.Id, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS) { 83 return &model.CommandResponse{Text: args.T("api.command_invite.permission.app_error", map[string]interface{}{"User": userProfile.Username, "Channel": channelToJoin.Name}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 84 } 85 86 // Check if user is already in the channel 87 _, err = a.GetChannelMember(channelToJoin.Id, userProfile.Id) 88 if err == nil { 89 return &model.CommandResponse{Text: args.T("api.command_invite.user_already_in_channel.app_error", map[string]interface{}{"User": userProfile.Username}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 90 } 91 92 if _, err := a.AddChannelMember(userProfile.Id, channelToJoin, args.Session.UserId, ""); err != nil { 93 return &model.CommandResponse{Text: args.T("api.command_invite.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 94 } 95 96 if args.ChannelId != channelToJoin.Id { 97 return &model.CommandResponse{Text: args.T("api.command_invite.success", map[string]interface{}{"User": userProfile.Username, "Channel": channelToJoin.Name}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 98 } 99 100 return &model.CommandResponse{} 101 }