github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/app/command_groupmsg.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 "fmt" 8 "strings" 9 10 goi18n "github.com/mattermost/go-i18n/i18n" 11 "github.com/vnforks/kid/v5/model" 12 ) 13 14 type groupmsgProvider struct { 15 } 16 17 const ( 18 CMD_GROUPMSG = "groupmsg" 19 ) 20 21 func init() { 22 RegisterCommandProvider(&groupmsgProvider{}) 23 } 24 25 func (me *groupmsgProvider) GetTrigger() string { 26 return CMD_GROUPMSG 27 } 28 29 func (me *groupmsgProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 30 return &model.Command{ 31 Trigger: CMD_GROUPMSG, 32 AutoComplete: true, 33 AutoCompleteDesc: T("api.command_groupmsg.desc"), 34 AutoCompleteHint: T("api.command_groupmsg.hint"), 35 DisplayName: T("api.command_groupmsg.name"), 36 } 37 } 38 39 func (me *groupmsgProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 40 targetUsers := map[string]*model.User{} 41 targetUsersSlice := []string{args.UserId} 42 invalidUsernames := []string{} 43 44 users, parsedMessage := groupMsgUsernames(message) 45 46 for _, username := range users { 47 username = strings.TrimSpace(username) 48 username = strings.TrimPrefix(username, "@") 49 targetUser, err := a.Srv().Store.User().GetByUsername(username) 50 if err != nil { 51 invalidUsernames = append(invalidUsernames, username) 52 continue 53 } 54 55 canSee, err := a.UserCanSeeOtherUser(args.UserId, targetUser.Id) 56 if err != nil { 57 return &model.CommandResponse{Text: args.T("api.command_groupmsg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 58 } 59 60 if !canSee { 61 invalidUsernames = append(invalidUsernames, username) 62 continue 63 } 64 65 _, exists := targetUsers[targetUser.Id] 66 if !exists && targetUser.Id != args.UserId { 67 targetUsers[targetUser.Id] = targetUser 68 targetUsersSlice = append(targetUsersSlice, targetUser.Id) 69 } 70 } 71 72 if len(invalidUsernames) > 0 { 73 invalidUsersString := map[string]interface{}{ 74 "Users": "@" + strings.Join(invalidUsernames, ", @"), 75 } 76 return &model.CommandResponse{ 77 Text: args.T("api.command_groupmsg.invalid_user.app_error", len(invalidUsernames), invalidUsersString), 78 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 79 } 80 } 81 82 if len(targetUsersSlice) == 2 { 83 return GetCommandProvider("msg").DoCommand(a, args, fmt.Sprintf("%s %s", targetUsers[targetUsersSlice[1]].Username, parsedMessage)) 84 } 85 86 branch, err := a.GetBranch(args.BranchId) 87 if err != nil { 88 return &model.CommandResponse{Text: args.T("api.command_groupmsg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 89 } 90 91 return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + branch.Name + "/classes/", Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 92 } 93 94 func groupMsgUsernames(message string) ([]string, string) { 95 result := []string{} 96 resultMessage := "" 97 for idx, part := range strings.Split(message, ",") { 98 clean := strings.TrimPrefix(strings.TrimSpace(part), "@") 99 split := strings.Fields(clean) 100 if len(split) > 0 { 101 result = append(result, split[0]) 102 } 103 if len(split) > 1 { 104 splitted := strings.SplitN(message, ",", idx+1) 105 resultMessage = strings.TrimPrefix(strings.TrimSpace(splitted[len(splitted)-1]), "@") 106 resultMessage = strings.TrimSpace(strings.TrimPrefix(resultMessage, split[0])) 107 break 108 } 109 } 110 return result, resultMessage 111 }