github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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 "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 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 mlog.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 !a.SessionHasPermissionTo(args.Session, model.PERMISSION_CREATE_DIRECT_CHANNEL) { 70 return &model.CommandResponse{Text: args.T("api.command_msg.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 71 } 72 73 if directChannel, err := a.CreateDirectChannel(args.UserId, userProfile.Id); err != nil { 74 mlog.Error(err.Error()) 75 return &model.CommandResponse{Text: args.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 76 } else { 77 targetChannelId = directChannel.Id 78 } 79 } else { 80 mlog.Error(channel.Err.Error()) 81 return &model.CommandResponse{Text: args.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 82 } 83 } else { 84 channel := channel.Data.(*model.Channel) 85 targetChannelId = channel.Id 86 } 87 88 if len(parsedMessage) > 0 { 89 post := &model.Post{} 90 post.Message = parsedMessage 91 post.ChannelId = targetChannelId 92 post.UserId = args.UserId 93 if _, err := a.CreatePostMissingChannel(post, true); err != nil { 94 return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 95 } 96 } 97 98 teamId := args.TeamId 99 if teamId == "" { 100 if len(args.Session.TeamMembers) == 0 { 101 return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 102 } 103 teamId = args.Session.TeamMembers[0].TeamId 104 } 105 106 team, err := a.GetTeam(teamId) 107 if err != nil { 108 return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 109 } 110 111 return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + channelName, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 112 }