summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorDavid Lu <david.lu97@outlook.com>2016-08-29 09:51:29 -0400
committerChristopher Speller <crspeller@gmail.com>2016-08-29 09:51:29 -0400
commitd252e61c662479155081aeaf34fe0c6e4c3705d1 (patch)
tree215df6f5c73009bebcae4a0fdfacdb2b3252c23b /api
parent167dd22eefeeeb9c1eaebd990a4f5902bd366302 (diff)
downloadchat-d252e61c662479155081aeaf34fe0c6e4c3705d1.tar.gz
chat-d252e61c662479155081aeaf34fe0c6e4c3705d1.tar.bz2
chat-d252e61c662479155081aeaf34fe0c6e4c3705d1.zip
Revert/Fix PLT-2805 (#3873)
Diffstat (limited to 'api')
-rw-r--r--api/channel.go17
-rw-r--r--api/command.go2
-rw-r--r--api/command_echo.go2
-rw-r--r--api/command_loadtest.go4
-rw-r--r--api/command_msg.go2
-rw-r--r--api/post.go93
-rw-r--r--api/webhook.go2
7 files changed, 71 insertions, 51 deletions
diff --git a/api/channel.go b/api/channel.go
index a07f45d7a..faa39e13c 100644
--- a/api/channel.go
+++ b/api/channel.go
@@ -354,7 +354,7 @@ func PostUpdateChannelHeaderMessage(c *Context, channelId string, oldChannelHead
Type: model.POST_HEADER_CHANGE,
UserId: c.Session.UserId,
}
- if _, err := CreatePost(c.TeamId, post, false); err != nil {
+ if _, err := CreatePost(c, post, false); err != nil {
l4g.Error(utils.T("api.channel.post_update_channel_header_message_and_forget.join_leave.error"), err)
}
}
@@ -546,7 +546,7 @@ func PostUserAddRemoveMessage(c *Context, channelId string, message, postType st
Type: postType,
UserId: c.Session.UserId,
}
- if _, err := CreatePost(c.TeamId, post, false); err != nil {
+ if _, err := CreatePost(c, post, false); err != nil {
l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err)
}
}
@@ -618,7 +618,16 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m
Type: model.POST_JOIN_LEAVE,
UserId: user.Id,
}
- if _, err := CreatePost(teamId, post, false); err != nil {
+
+ fakeContext := &Context{
+ Session: model.Session{
+ UserId: user.Id,
+ },
+ TeamId: teamId,
+ T: utils.TfuncWithFallback(user.Locale),
+ }
+
+ if _, err := CreatePost(fakeContext, post, false); err != nil {
l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err)
}
}
@@ -794,7 +803,7 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) {
Type: model.POST_CHANNEL_DELETED,
UserId: c.Session.UserId,
}
- if _, err := CreatePost(c.TeamId, post, false); err != nil {
+ if _, err := CreatePost(c, 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 7e0708a4d..5556ed817 100644
--- a/api/command.go
+++ b/api/command.go
@@ -248,7 +248,7 @@ func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandRe
if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
post.Message = response.Text
post.UserId = c.Session.UserId
- if _, err := CreatePost(c.TeamId, post, true); err != nil {
+ if _, err := CreatePost(c, 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 5caa41759..997ee0751 100644
--- a/api/command_echo.go
+++ b/api/command_echo.go
@@ -81,7 +81,7 @@ func (me *EchoProvider) DoCommand(c *Context, channelId string, message string)
time.Sleep(time.Duration(delay) * time.Second)
- if _, err := CreatePost(c.TeamId, post, true); err != nil {
+ if _, err := CreatePost(c, 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 8028b8352..beb831b22 100644
--- a/api/command_loadtest.go
+++ b/api/command_loadtest.go
@@ -357,7 +357,7 @@ func (me *LoadTestProvider) UrlCommand(c *Context, channelId string, message str
post.ChannelId = channelId
post.UserId = c.Session.UserId
- if _, err := CreatePost(c.TeamId, post, false); err != nil {
+ if _, err := CreatePost(c, post, false); err != nil {
return &model.CommandResponse{Text: "Unable to create post", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
}
@@ -396,7 +396,7 @@ func (me *LoadTestProvider) JsonCommand(c *Context, channelId string, message st
post.Message = message
}
- if _, err := CreatePost(c.TeamId, post, false); err != nil {
+ if _, err := CreatePost(c, 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 f54190f58..d7208f121 100644
--- a/api/command_msg.go
+++ b/api/command_msg.go
@@ -85,7 +85,7 @@ func (me *msgProvider) DoCommand(c *Context, channelId string, message string) *
post.Message = parsedMessage
post.ChannelId = targetChannelId
post.UserId = c.Session.UserId
- if _, err := CreatePost(c.TeamId, post, true); err != nil {
+ if _, err := CreatePost(c, 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 778f930c4..e49be2248 100644
--- a/api/post.go
+++ b/api/post.go
@@ -65,7 +65,7 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
- if rp, err := CreatePost(c.TeamId, post, true); err != nil {
+ if rp, err := CreatePost(c, post, true); err != nil {
c.Err = err
if c.Err.Id == "api.post.create_post.root_id.app_error" ||
@@ -84,7 +84,7 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
-func CreatePost(teamId string, post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) {
+func CreatePost(c *Context, 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)
@@ -156,18 +156,18 @@ func CreatePost(teamId string, post *model.Post, triggerWebhooks bool) (*model.P
} else {
rpost = result.Data.(*model.Post)
- go handlePostEvents(teamId, rpost, triggerWebhooks)
+ go handlePostEvents(c, rpost, triggerWebhooks)
}
return rpost, nil
}
-func CreateWebhookPost(userId, channelId, teamId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) {
+func CreateWebhookPost(c *Context, channelId, 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: userId, ChannelId: channelId, Message: text, Type: postType}
+ post := &model.Post{UserId: c.Session.UserId, ChannelId: channelId, Message: text, Type: postType}
post.AddProp("from_webhook", "true")
if utils.Cfg.ServiceSettings.EnablePostUsernameOverride {
@@ -228,21 +228,21 @@ func CreateWebhookPost(userId, channelId, teamId, text, overrideUsername, overri
}
}
- if _, err := CreatePost(teamId, post, false); err != nil {
+ if _, err := CreatePost(c, 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(teamId string, post *model.Post, triggerWebhooks bool) {
- tchan := Srv.Store.Team().Get(teamId)
+func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) {
+ tchan := Srv.Store.Team().Get(c.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"), teamId, result.Err)
+ l4g.Error(utils.T("api.post.handle_post_events_and_forget.team.error"), c.TeamId, result.Err)
return
} else {
team = result.Data.(*model.Team)
@@ -256,7 +256,7 @@ func handlePostEvents(teamId string, post *model.Post, triggerWebhooks bool) {
channel = result.Data.(*model.Channel)
}
- go sendNotifications(teamId, post, team, channel)
+ go sendNotifications(c, post, team, channel)
var user *model.User
if result := <-uchan; result.Err != nil {
@@ -267,11 +267,11 @@ func handlePostEvents(teamId string, post *model.Post, triggerWebhooks bool) {
}
if triggerWebhooks {
- go handleWebhookEvents(post, team, channel, user)
+ go handleWebhookEvents(c, post, team, channel, user)
}
if channel.Type == model.CHANNEL_DIRECT {
- go makeDirectChannelVisible(teamId, post.ChannelId)
+ go makeDirectChannelVisible(c.TeamId, post.ChannelId)
}
}
@@ -330,7 +330,7 @@ func makeDirectChannelVisible(teamId string, channelId string) {
}
}
-func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Channel, user *model.User) {
+func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel *model.Channel, user *model.User) {
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {
return
}
@@ -339,7 +339,7 @@ func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Chan
return
}
- hchan := Srv.Store.Webhook().GetOutgoingByTeam(team.Id)
+ hchan := Srv.Store.Webhook().GetOutgoingByTeam(c.TeamId)
result := <-hchan
if result.Err != nil {
l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.getting.error"), result.Err)
@@ -413,8 +413,29 @@ func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Chan
}()
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(hook.CreatorId, post.ChannelId, hook.TeamId, text, respProps["username"], respProps["icon_url"], post.Props, post.Type); err != nil {
+ if _, err := CreateWebhookPost(newContext, post.ChannelId, 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)
}
}
@@ -525,22 +546,22 @@ func getExplicitMentions(message string, keywords map[string][]string) (map[stri
return mentioned, hereMentioned
}
-func sendNotifications(teamId string, post *model.Post, team *model.Team, channel *model.Channel) {
+func sendNotifications(c *Context, post *model.Post, team *model.Team, channel *model.Channel) {
// get profiles for all users we could be mentioning
- pchan := Srv.Store.User().GetProfiles(teamId)
- dpchan := Srv.Store.User().GetDirectProfiles(post.UserId)
+ pchan := Srv.Store.User().GetProfiles(c.TeamId)
+ dpchan := Srv.Store.User().GetDirectProfiles(c.Session.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"), teamId, result.Err)
+ l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), c.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"), teamId, result.Err)
+ l4g.Error(utils.T("api.post.handle_post_events_and_forget.profiles.error"), c.TeamId, result.Err)
return
} else {
dps := result.Data.(map[string]*model.User)
@@ -616,7 +637,7 @@ func sendNotifications(teamId string, post *model.Post, team *model.Team, channe
}
}
- go sendOutOfChannelMentions(teamId, post, profileMap, outOfChannelMentions)
+ go sendOutOfChannelMentions(c, post, profileMap, outOfChannelMentions)
// find which users in the channel are set up to always receive mobile notifications
for id := range members {
@@ -636,16 +657,11 @@ func sendNotifications(teamId string, post *model.Post, team *model.Team, channe
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 := ""
var sender *model.User
if post.IsSystemMessage() {
- senderName = T("system.message.name")
+ senderName = c.T("system.message.name")
} else if profile, ok := profileMap[post.UserId]; ok {
senderName = profile.Username
sender = profile
@@ -667,7 +683,7 @@ func sendNotifications(teamId string, post *model.Post, team *model.Team, channe
}
if userAllowsEmails && status.Status != model.STATUS_ONLINE {
- sendNotificationEmail(post, profileMap[id], channel, team, senderName, sender)
+ sendNotificationEmail(c, post, profileMap[id], channel, team, senderName, sender)
}
}
}
@@ -734,7 +750,7 @@ func sendNotifications(teamId string, post *model.Post, team *model.Team, channe
}
}
- message := model.NewWebSocketEvent(teamId, post.ChannelId, post.UserId, model.WEBSOCKET_EVENT_POSTED)
+ message := model.NewWebSocketEvent(c.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)
@@ -768,7 +784,7 @@ func sendNotifications(teamId string, post *model.Post, team *model.Team, channe
go Publish(message)
}
-func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User) {
+func sendNotificationEmail(c *Context, post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User) {
// skip if inactive
if user.DeleteAt > 0 {
return
@@ -788,7 +804,7 @@ func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Ch
var mailTemplate string
var mailParameters map[string]interface{}
- teamURL := *utils.Cfg.ServiceSettings.SiteURL + "/" + team.Name
+ teamURL := c.GetSiteURL() + "/" + team.Name
tm := time.Unix(post.CreateAt/1000, 0)
userLocale := utils.GetUserTranslations(user.Locale)
@@ -828,7 +844,7 @@ func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Ch
subjectPage.Props["SiteName"] = utils.Cfg.TeamSettings.SiteName
bodyPage := utils.NewHTMLTemplate("post_body", user.Locale)
- bodyPage.Props["SiteURL"] = *utils.Cfg.ServiceSettings.SiteURL
+ bodyPage.Props["SiteURL"] = c.GetSiteURL()
bodyPage.Props["PostMessage"] = getMessageForNotification(post, userLocale)
bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id
bodyPage.Props["BodyText"] = bodyText
@@ -951,7 +967,7 @@ func sendPushNotification(post *model.Post, user *model.User, channel *model.Cha
}
}
-func sendOutOfChannelMentions(teamId string, post *model.Post, profiles map[string]*model.User, outOfChannelMentions map[string]bool) {
+func sendOutOfChannelMentions(c *Context, post *model.Post, profiles map[string]*model.User, outOfChannelMentions map[string]bool) {
if len(outOfChannelMentions) == 0 {
return
}
@@ -962,25 +978,20 @@ func sendOutOfChannelMentions(teamId string, post *model.Post, profiles map[stri
}
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 = T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{
+ message = c.T("api.post.check_for_out_of_channel_mentions.message.one", map[string]interface{}{
"Username": usernames[0],
})
} else {
- message = T("api.post.check_for_out_of_channel_mentions.message.multiple", map[string]interface{}{
+ message = c.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(
- teamId,
+ c.TeamId,
post.UserId,
&model.Post{
ChannelId: post.ChannelId,
diff --git a/api/webhook.go b/api/webhook.go
index 072a40559..a9eded2ac 100644
--- a/api/webhook.go
+++ b/api/webhook.go
@@ -482,7 +482,7 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
}
c.Err = nil
- if _, err := CreateWebhookPost(hook.UserId, channel.Id, hook.TeamId, text, overrideUsername, overrideIconUrl, parsedRequest.Props, webhookType); err != nil {
+ if _, err := CreateWebhookPost(c, channel.Id, text, overrideUsername, overrideIconUrl, parsedRequest.Props, webhookType); err != nil {
c.Err = err
return
}