github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/app/command_msg.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 "strings" 8 9 l4g "github.com/alecthomas/log4go" 10 "github.com/mattermost/mattermost-server/model" 11 goi18n "github.com/nicksnyder/go-i18n/i18n" 12 ) 13 14 type msgProvider struct { 15 } 16 17 const ( 18 CMD_MSG = "msg" 19 ) 20 21 func init() { 22 RegisterCommandProvider(&msgProvider{}) 23 } 24 25 func (me *msgProvider) GetTrigger() string { 26 return CMD_MSG 27 } 28 29 func (me *msgProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 30 return &model.Command{ 31 Trigger: CMD_MSG, 32 AutoComplete: true, 33 AutoCompleteDesc: T("api.command_msg.desc"), 34 AutoCompleteHint: T("api.command_msg.hint"), 35 DisplayName: T("api.command_msg.name"), 36 } 37 } 38 39 func (me *msgProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 40 splitMessage := strings.SplitN(message, " ", 2) 41 42 parsedMessage := "" 43 targetUsername := "" 44 45 if len(splitMessage) > 1 { 46 parsedMessage = strings.SplitN(message, " ", 2)[1] 47 } 48 targetUsername = strings.SplitN(message, " ", 2)[0] 49 targetUsername = strings.TrimPrefix(targetUsername, "@") 50 51 var userProfile *model.User 52 if result := <-a.Srv.Store.User().GetByUsername(targetUsername); result.Err != nil { 53 l4g.Error(result.Err.Error()) 54 return &model.CommandResponse{Text: args.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 55 } else { 56 userProfile = result.Data.(*model.User) 57 } 58 59 if userProfile.Id == args.UserId { 60 return &model.CommandResponse{Text: args.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 61 } 62 63 // Find the channel based on this user 64 channelName := model.GetDMNameFromIds(args.UserId, userProfile.Id) 65 66 targetChannelId := "" 67 if channel := <-a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true); channel.Err != nil { 68 if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { 69 if directChannel, err := a.CreateDirectChannel(args.UserId, userProfile.Id); err != nil { 70 l4g.Error(err.Error()) 71 return &model.CommandResponse{Text: args.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 72 } else { 73 targetChannelId = directChannel.Id 74 } 75 } else { 76 l4g.Error(channel.Err.Error()) 77 return &model.CommandResponse{Text: args.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 78 } 79 } else { 80 channel := channel.Data.(*model.Channel) 81 targetChannelId = channel.Id 82 } 83 84 if len(parsedMessage) > 0 { 85 post := &model.Post{} 86 post.Message = parsedMessage 87 post.ChannelId = targetChannelId 88 post.UserId = args.UserId 89 if _, err := a.CreatePostMissingChannel(post, true); err != nil { 90 return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 91 } 92 } 93 94 teamId := args.TeamId 95 if teamId == "" { 96 if len(args.Session.TeamMembers) == 0 { 97 return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 98 } 99 teamId = args.Session.TeamMembers[0].TeamId 100 } 101 102 team, err := a.GetTeam(teamId) 103 if err != nil { 104 return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 105 } 106 107 return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + channelName, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 108 }