diff options
Diffstat (limited to 'api/post.go')
-rw-r--r-- | api/post.go | 96 |
1 files changed, 42 insertions, 54 deletions
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, |