github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	l4g "github.com/alecthomas/log4go"
    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  	groupChannel, channelErr := a.CreateGroupChannel(targetUsersSlice, args.UserId)
    97  	if channelErr != nil {
    98  		l4g.Error(channelErr.Error())
    99  		return &model.CommandResponse{Text: args.T("api.command_groupmsg.group_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
   100  	}
   101  
   102  	if len(parsedMessage) > 0 {
   103  		post := &model.Post{}
   104  		post.Message = parsedMessage
   105  		post.ChannelId = groupChannel.Id
   106  		post.UserId = args.UserId
   107  		if _, err := a.CreatePostMissingChannel(post, true); err != nil {
   108  			return &model.CommandResponse{Text: args.T("api.command_groupmsg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
   109  		}
   110  	}
   111  
   112  	team, err := a.GetTeam(args.TeamId)
   113  	if err != nil {
   114  		return &model.CommandResponse{Text: args.T("api.command_groupmsg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
   115  	}
   116  
   117  	return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + groupChannel.Name, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
   118  }
   119  
   120  func groupMsgUsernames(message string) ([]string, string) {
   121  	result := []string{}
   122  	resultMessage := ""
   123  	for idx, part := range strings.Split(message, ",") {
   124  		clean := strings.TrimPrefix(strings.TrimSpace(part), "@")
   125  		split := strings.Fields(clean)
   126  		if len(split) > 0 {
   127  			result = append(result, split[0])
   128  		}
   129  		if len(split) > 1 {
   130  			splitted := strings.SplitN(message, ",", idx+1)
   131  			resultMessage = strings.TrimPrefix(strings.TrimSpace(splitted[len(splitted)-1]), "@")
   132  			resultMessage = strings.TrimSpace(strings.TrimPrefix(resultMessage, split[0]))
   133  			break
   134  		}
   135  	}
   136  	return result, resultMessage
   137  }