diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/admin_test.go | 7 | ||||
-rw-r--r-- | api/channel.go | 19 | ||||
-rw-r--r-- | api/command.go | 3 | ||||
-rw-r--r-- | api/command_echo.go | 3 | ||||
-rw-r--r-- | api/command_loadtest.go | 6 | ||||
-rw-r--r-- | api/command_msg.go | 6 | ||||
-rw-r--r-- | api/post.go | 96 | ||||
-rw-r--r-- | api/webhook.go | 2 |
8 files changed, 75 insertions, 67 deletions
diff --git a/api/admin_test.go b/api/admin_test.go index 013e72944..967e3ceb3 100644 --- a/api/admin_test.go +++ b/api/admin_test.go @@ -4,11 +4,12 @@ package api import ( + "strings" + "testing" + "github.com/mattermost/platform/model" "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" - "strings" - "testing" ) func TestGetLogs(t *testing.T) { @@ -198,7 +199,7 @@ func TestGetTeamAnalyticsStandard(t *testing.T) { t.Fatal() } - if rows[2].Value != 1 { + if rows[2].Value != 3 { t.Log(rows.ToJson()) t.Fatal() } diff --git a/api/channel.go b/api/channel.go index 5d3021cee..a07f45d7a 100644 --- a/api/channel.go +++ b/api/channel.go @@ -352,8 +352,9 @@ func PostUpdateChannelHeaderMessage(c *Context, channelId string, oldChannelHead ChannelId: channelId, Message: message, Type: model.POST_HEADER_CHANGE, + UserId: c.Session.UserId, } - if _, err := CreatePost(c, post, false); err != nil { + if _, err := CreatePost(c.TeamId, post, false); err != nil { l4g.Error(utils.T("api.channel.post_update_channel_header_message_and_forget.join_leave.error"), err) } } @@ -543,8 +544,9 @@ func PostUserAddRemoveMessage(c *Context, channelId string, message, postType st ChannelId: channelId, Message: message, Type: postType, + UserId: c.Session.UserId, } - if _, err := CreatePost(c, post, false); err != nil { + if _, err := CreatePost(c.TeamId, post, false); err != nil { l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err) } } @@ -609,6 +611,16 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err } + + post := &model.Post{ + ChannelId: result.Data.(*model.Channel).Id, + Message: fmt.Sprintf(utils.T("api.channel.join_channel.post_and_forget"), user.Username), + Type: model.POST_JOIN_LEAVE, + UserId: user.Id, + } + if _, err := CreatePost(teamId, post, false); err != nil { + l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err) + } } if result := <-Srv.Store.Channel().GetByName(teamId, "off-topic"); result.Err != nil { @@ -780,8 +792,9 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) { ChannelId: channel.Id, Message: fmt.Sprintf(c.T("api.channel.delete_channel.archived"), user.Username), Type: model.POST_CHANNEL_DELETED, + UserId: c.Session.UserId, } - if _, err := CreatePost(c, post, false); err != nil { + if _, err := CreatePost(c.TeamId, post, false); err != nil { l4g.Error(utils.T("api.channel.delete_channel.failed_post.error"), err) } }() diff --git a/api/command.go b/api/command.go index 1e58308ad..45355f092 100644 --- a/api/command.go +++ b/api/command.go @@ -247,7 +247,8 @@ func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandRe if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL { post.Message = response.Text - if _, err := CreatePost(c, post, true); err != nil { + post.UserId = c.Session.UserId + if _, err := CreatePost(c.TeamId, post, true); err != nil { c.Err = model.NewLocAppError("command", "api.command.execute_command.save.app_error", nil, "") } } else if response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL && response.Text != "" { diff --git a/api/command_echo.go b/api/command_echo.go index 805db7ad2..5caa41759 100644 --- a/api/command_echo.go +++ b/api/command_echo.go @@ -77,10 +77,11 @@ func (me *EchoProvider) DoCommand(c *Context, channelId string, message string) post := &model.Post{} post.ChannelId = channelId post.Message = message + post.UserId = c.Session.UserId time.Sleep(time.Duration(delay) * time.Second) - if _, err := CreatePost(c, post, true); err != nil { + if _, err := CreatePost(c.TeamId, post, true); err != nil { l4g.Error(c.T("api.command_echo.create.app_error"), err) } }() diff --git a/api/command_loadtest.go b/api/command_loadtest.go index 26306440f..8028b8352 100644 --- a/api/command_loadtest.go +++ b/api/command_loadtest.go @@ -355,8 +355,9 @@ func (me *LoadTestProvider) UrlCommand(c *Context, channelId string, message str post := &model.Post{} post.Message = string(bytes[:length]) post.ChannelId = channelId + post.UserId = c.Session.UserId - if _, err := CreatePost(c, post, false); err != nil { + if _, err := CreatePost(c.TeamId, post, false); err != nil { return &model.CommandResponse{Text: "Unable to create post", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } @@ -390,11 +391,12 @@ func (me *LoadTestProvider) JsonCommand(c *Context, channelId string, message st post := model.PostFromJson(contents) post.ChannelId = channelId + post.UserId = c.Session.UserId if post.Message == "" { post.Message = message } - if _, err := CreatePost(c, post, false); err != nil { + if _, err := CreatePost(c.TeamId, post, false); err != nil { return &model.CommandResponse{Text: "Unable to create post", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } return &model.CommandResponse{Text: "Loaded data", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} diff --git a/api/command_msg.go b/api/command_msg.go index d32a7ca5e..f54190f58 100644 --- a/api/command_msg.go +++ b/api/command_msg.go @@ -4,8 +4,9 @@ package api import ( - "github.com/mattermost/platform/model" "strings" + + "github.com/mattermost/platform/model" ) type msgProvider struct { @@ -83,7 +84,8 @@ func (me *msgProvider) DoCommand(c *Context, channelId string, message string) * post := &model.Post{} post.Message = parsedMessage post.ChannelId = targetChannelId - if _, err := CreatePost(c, post, true); err != nil { + post.UserId = c.Session.UserId + if _, err := CreatePost(c.TeamId, post, true); err != nil { return &model.CommandResponse{Text: c.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } diff --git a/api/post.go b/api/post.go index dde85a8c8..fa42e6f88 100644 --- a/api/post.go +++ b/api/post.go @@ -56,6 +56,7 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) { c.SetInvalidParam("createPost", "post") return } + post.UserId = c.Session.UserId // Create and save post object to channel cchan := Srv.Store.Channel().CheckPermissionsTo(c.TeamId, post.ChannelId, c.Session.UserId) @@ -64,7 +65,7 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) { return } - if rp, err := CreatePost(c, post, true); err != nil { + if rp, err := CreatePost(c.TeamId, post, true); err != nil { c.Err = err if c.Err.Id == "api.post.create_post.root_id.app_error" || @@ -83,7 +84,7 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) { } } -func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) { +func CreatePost(teamId string, post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) { var pchan store.StoreChannel if len(post.RootId) > 0 { pchan = Srv.Store.Post().Get(post.RootId) @@ -116,8 +117,6 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post post.Hashtags, _ = model.ParseHashtags(post.Message) - post.UserId = c.Session.UserId - if len(post.Filenames) > 0 { doRemove := false for i := len(post.Filenames) - 1; i >= 0; i-- { @@ -157,18 +156,18 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post } else { rpost = result.Data.(*model.Post) - go handlePostEvents(c, rpost, triggerWebhooks) + go handlePostEvents(teamId, rpost, triggerWebhooks) } return rpost, nil } -func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) { +func CreateWebhookPost(userId, channelId, teamId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) { // parse links into Markdown format linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") - post := &model.Post{UserId: c.Session.UserId, ChannelId: channelId, Message: text, Type: postType} + post := &model.Post{UserId: userId, ChannelId: channelId, Message: text, Type: postType} post.AddProp("from_webhook", "true") if utils.Cfg.ServiceSettings.EnablePostUsernameOverride { @@ -229,21 +228,21 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc } } - if _, err := CreatePost(c, post, false); err != nil { + if _, err := CreatePost(teamId, post, false); err != nil { return nil, model.NewLocAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message) } return post, nil } -func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { - tchan := Srv.Store.Team().Get(c.TeamId) +func handlePostEvents(teamId string, post *model.Post, triggerWebhooks bool) { + tchan := Srv.Store.Team().Get(teamId) cchan := Srv.Store.Channel().Get(post.ChannelId) uchan := Srv.Store.User().Get(post.UserId) var team *model.Team if result := <-tchan; result.Err != nil { - l4g.Error(utils.T("api.post.handle_post_events_and_forget.team.error"), c.TeamId, result.Err) + l4g.Error(utils.T("api.post.handle_post_events_and_forget.team.error"), teamId, result.Err) return } else { team = result.Data.(*model.Team) @@ -257,7 +256,7 @@ func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { channel = result.Data.(*model.Channel) } - go sendNotifications(c, post, team, channel) + go sendNotifications(teamId, post, team, channel) var user *model.User if result := <-uchan; result.Err != nil { @@ -268,11 +267,11 @@ func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { } if triggerWebhooks { - go handleWebhookEvents(c, post, team, channel, user) + go handleWebhookEvents(post, team, channel, user) } if channel.Type == model.CHANNEL_DIRECT { - go makeDirectChannelVisible(c.TeamId, post.ChannelId) + go makeDirectChannelVisible(teamId, post.ChannelId) } } @@ -331,7 +330,7 @@ func makeDirectChannelVisible(teamId string, channelId string) { } } -func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel *model.Channel, user *model.User) { +func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Channel, user *model.User) { if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks { return } @@ -340,7 +339,7 @@ func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel return } - hchan := Srv.Store.Webhook().GetOutgoingByTeam(c.TeamId) + hchan := Srv.Store.Webhook().GetOutgoingByTeam(team.Id) result := <-hchan if result.Err != nil { l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.getting.error"), result.Err) @@ -414,29 +413,8 @@ func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel }() respProps := model.MapFromJson(resp.Body) - // copy the context and create a mock session for posting the message - mockSession := model.Session{ - UserId: hook.CreatorId, - TeamMembers: []*model.TeamMember{{TeamId: hook.TeamId, UserId: hook.CreatorId}}, - IsOAuth: false, - } - - newContext := &Context{ - Session: mockSession, - RequestId: model.NewId(), - IpAddress: "", - Path: c.Path, - Err: nil, - teamURLValid: c.teamURLValid, - teamURL: c.teamURL, - siteURL: c.siteURL, - T: c.T, - Locale: c.Locale, - TeamId: hook.TeamId, - } - if text, ok := respProps["text"]; ok { - if _, err := CreateWebhookPost(newContext, post.ChannelId, text, respProps["username"], respProps["icon_url"], post.Props, post.Type); err != nil { + if _, err := CreateWebhookPost(hook.CreatorId, post.ChannelId, hook.TeamId, text, respProps["username"], respProps["icon_url"], post.Props, post.Type); err != nil { l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.create_post.error"), err) } } @@ -547,22 +525,22 @@ func getExplicitMentions(message string, keywords map[string][]string) (map[stri return mentioned, hereMentioned } -func sendNotifications(c *Context, post *model.Post, team *model.Team, channel *model.Channel) { +func sendNotifications(teamId string, post *model.Post, team *model.Team, channel *model.Channel) { // get profiles for all users we could be mentioning - pchan := Srv.Store.User().GetProfiles(c.TeamId) - dpchan := Srv.Store.User().GetDirectProfiles(c.Session.UserId) + pchan := Srv.Store.User().GetProfiles(teamId) + dpchan := Srv.Store.User().GetDirectProfiles(post.UserId) mchan := Srv.Store.Channel().GetMembers(post.ChannelId) var profileMap map[string]*model.User if result := <-pchan; result.Err != nil { - l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), c.TeamId, result.Err) + l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), teamId, result.Err) return } else { profileMap = result.Data.(map[string]*model.User) } if result := <-dpchan; result.Err != nil { - l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), c.TeamId, result.Err) + l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), teamId, result.Err) return } else { dps := result.Data.(map[string]*model.User) @@ -638,7 +616,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } } - go sendOutOfChannelMentions(c, post, profileMap, outOfChannelMentions) + go sendOutOfChannelMentions(teamId, post, profileMap, outOfChannelMentions) // find which users in the channel are set up to always receive mobile notifications for id := range members { @@ -658,9 +636,14 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * mentionedUsersList := make([]string, 0, len(mentionedUserIds)) + T := utils.T + if result := <-Srv.Store.User().Get(post.UserId); result.Err == nil { + T = utils.TfuncWithFallback(result.Data.(*model.User).Locale) + } + senderName := "" if post.IsSystemMessage() { - senderName = c.T("system.message.name") + senderName = T("system.message.name") } else if profile, ok := profileMap[post.UserId]; ok { senderName = profile.Username } @@ -681,7 +664,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } if userAllowsEmails && status.Status != model.STATUS_ONLINE { - sendNotificationEmail(c, post, profileMap[id], channel, team, senderName) + sendNotificationEmail(post, profileMap[id], channel, team, senderName) } } } @@ -733,7 +716,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } } - message := model.NewWebSocketEvent(c.TeamId, post.ChannelId, post.UserId, model.WEBSOCKET_EVENT_POSTED) + message := model.NewWebSocketEvent(teamId, post.ChannelId, post.UserId, model.WEBSOCKET_EVENT_POSTED) message.Add("post", post.ToJson()) message.Add("channel_type", channel.Type) message.Add("channel_display_name", channel.DisplayName) @@ -767,7 +750,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * go Publish(message) } -func sendNotificationEmail(c *Context, post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string) { +func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string) { // skip if inactive if user.DeleteAt > 0 { return @@ -785,7 +768,7 @@ func sendNotificationEmail(c *Context, post *model.Post, user *model.User, chann var bodyText string var subjectText string - teamURL := c.GetSiteURL() + "/" + team.Name + teamURL := *utils.Cfg.ServiceSettings.SiteURL + "/" + team.Name tm := time.Unix(post.CreateAt/1000, 0) userLocale := utils.GetUserTranslations(user.Locale) @@ -812,7 +795,7 @@ func sendNotificationEmail(c *Context, post *model.Post, user *model.User, chann subjectPage.Props["SiteName"] = utils.Cfg.TeamSettings.SiteName bodyPage := utils.NewHTMLTemplate("post_body", user.Locale) - bodyPage.Props["SiteURL"] = c.GetSiteURL() + bodyPage.Props["SiteURL"] = *utils.Cfg.ServiceSettings.SiteURL bodyPage.Props["PostMessage"] = getMessageForNotification(post, userLocale) bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id bodyPage.Props["BodyText"] = bodyText @@ -935,7 +918,7 @@ func sendPushNotification(post *model.Post, user *model.User, channel *model.Cha } } -func sendOutOfChannelMentions(c *Context, post *model.Post, profiles map[string]*model.User, outOfChannelMentions map[string]bool) { +func sendOutOfChannelMentions(teamId string, post *model.Post, profiles map[string]*model.User, outOfChannelMentions map[string]bool) { if len(outOfChannelMentions) == 0 { return } @@ -946,20 +929,25 @@ func sendOutOfChannelMentions(c *Context, post *model.Post, profiles map[string] } sort.Strings(usernames) + T := utils.T + if result := <-Srv.Store.User().Get(post.UserId); result.Err == nil { + T = utils.TfuncWithFallback(result.Data.(*model.User).Locale) + } + var message string if len(usernames) == 1 { - message = c.T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{ + message = T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{ "Username": usernames[0], }) } else { - message = c.T("api.post.check_for_out_of_channel_mentions.message.multiple", map[string]interface{}{ + message = T("api.post.check_for_out_of_channel_mentions.message.multiple", map[string]interface{}{ "Usernames": strings.Join(usernames[:len(usernames)-1], ", "), "LastUsername": usernames[len(usernames)-1], }) } SendEphemeralPost( - c.TeamId, + teamId, post.UserId, &model.Post{ ChannelId: post.ChannelId, diff --git a/api/webhook.go b/api/webhook.go index d57e5be9e..ec96f012e 100644 --- a/api/webhook.go +++ b/api/webhook.go @@ -481,7 +481,7 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) { return } - if _, err := CreateWebhookPost(c, channel.Id, text, overrideUsername, overrideIconUrl, parsedRequest.Props, webhookType); err != nil { + if _, err := CreateWebhookPost(hook.UserId, channel.Id, hook.TeamId, text, overrideUsername, overrideIconUrl, parsedRequest.Props, webhookType); err != nil { c.Err = err return } |