github.com/nhannv/mattermost-server@v5.11.1+incompatible/app/command_groupmsg.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/mattermost/mattermost-server/mlog" 11 "github.com/mattermost/mattermost-server/model" 12 goi18n "github.com/nicksnyder/go-i18n/i18n" 13 ) 14 15 type groupmsgProvider struct { 16 } 17 18 const ( 19 CMD_GROUPMSG = "groupmsg" 20 ) 21 22 func init() { 23 RegisterCommandProvider(&groupmsgProvider{}) 24 } 25 26 func (me *groupmsgProvider) GetTrigger() string { 27 return CMD_GROUPMSG 28 } 29 30 func (me *groupmsgProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 31 return &model.Command{ 32 Trigger: CMD_GROUPMSG, 33 AutoComplete: true, 34 AutoCompleteDesc: T("api.command_groupmsg.desc"), 35 AutoCompleteHint: T("api.command_groupmsg.hint"), 36 DisplayName: T("api.command_groupmsg.name"), 37 } 38 } 39 40 func (me *groupmsgProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 41 targetUsers := map[string]*model.User{} 42 targetUsersSlice := []string{args.UserId} 43 invalidUsernames := []string{} 44 45 users, parsedMessage := groupMsgUsernames(message) 46 47 for _, username := range users { 48 username = strings.TrimSpace(username) 49 username = strings.TrimPrefix(username, "@") 50 if result := <-a.Srv.Store.User().GetByUsername(username); result.Err != nil { 51 invalidUsernames = append(invalidUsernames, username) 52 } else { 53 targetUser := result.Data.(*model.User) 54 _, exists := targetUsers[targetUser.Id] 55 if !exists && targetUser.Id != args.UserId { 56 targetUsers[targetUser.Id] = targetUser 57 targetUsersSlice = append(targetUsersSlice, targetUser.Id) 58 } 59 } 60 } 61 62 if len(invalidUsernames) > 0 { 63 invalidUsersString := map[string]interface{}{ 64 "Users": "@" + strings.Join(invalidUsernames, ", @"), 65 } 66 return &model.CommandResponse{ 67 Text: args.T("api.command_groupmsg.invalid_user.app_error", len(invalidUsernames), invalidUsersString), 68 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 69 } 70 } 71 72 if len(targetUsersSlice) == 2 { 73 return GetCommandProvider("msg").DoCommand(a, args, fmt.Sprintf("%s %s", targetUsers[targetUsersSlice[1]].Username, parsedMessage)) 74 } 75 76 if len(targetUsersSlice) < model.CHANNEL_GROUP_MIN_USERS { 77 minUsers := map[string]interface{}{ 78 "MinUsers": model.CHANNEL_GROUP_MIN_USERS - 1, 79 } 80 return &model.CommandResponse{ 81 Text: args.T("api.command_groupmsg.min_users.app_error", minUsers), 82 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 83 } 84 } 85 86 if len(targetUsersSlice) > model.CHANNEL_GROUP_MAX_USERS { 87 maxUsers := map[string]interface{}{ 88 "MaxUsers": model.CHANNEL_GROUP_MAX_USERS - 1, 89 } 90 return &model.CommandResponse{ 91 Text: args.T("api.command_groupmsg.max_users.app_error", maxUsers), 92 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 93 } 94 } 95 96 var groupChannel *model.Channel 97 var channelErr *model.AppError 98 99 if a.SessionHasPermissionTo(args.Session, model.PERMISSION_CREATE_GROUP_CHANNEL) { 100 groupChannel, channelErr = a.CreateGroupChannel(targetUsersSlice, args.UserId) 101 if channelErr != nil { 102 mlog.Error(channelErr.Error()) 103 return &model.CommandResponse{Text: args.T("api.command_groupmsg.group_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 104 } 105 } else { 106 groupChannel, channelErr = a.GetGroupChannel(targetUsersSlice) 107 if channelErr != nil { 108 return &model.CommandResponse{Text: args.T("api.command_groupmsg.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 109 } 110 } 111 112 if len(parsedMessage) > 0 { 113 post := &model.Post{} 114 post.Message = parsedMessage 115 post.ChannelId = groupChannel.Id 116 post.UserId = args.UserId 117 if _, err := a.CreatePostMissingChannel(post, true); err != nil { 118 return &model.CommandResponse{Text: args.T("api.command_groupmsg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 119 } 120 } 121 122 team, err := a.GetTeam(args.TeamId) 123 if err != nil { 124 return &model.CommandResponse{Text: args.T("api.command_groupmsg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 125 } 126 127 return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + groupChannel.Name, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 128 } 129 130 func groupMsgUsernames(message string) ([]string, string) { 131 result := []string{} 132 resultMessage := "" 133 for idx, part := range strings.Split(message, ",") { 134 clean := strings.TrimPrefix(strings.TrimSpace(part), "@") 135 split := strings.Fields(clean) 136 if len(split) > 0 { 137 result = append(result, split[0]) 138 } 139 if len(split) > 1 { 140 splitted := strings.SplitN(message, ",", idx+1) 141 resultMessage = strings.TrimPrefix(strings.TrimSpace(splitted[len(splitted)-1]), "@") 142 resultMessage = strings.TrimSpace(strings.TrimPrefix(resultMessage, split[0])) 143 break 144 } 145 } 146 return result, resultMessage 147 }