From 1709b94227ed33e789f1be00d26ebbb972ce86b7 Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Mon, 28 Aug 2017 07:08:37 -0700 Subject: [PATCH] PLT-5745 Cleaning up duplicate calls (#7298) * PLT-5745 Cleaning up duplicate calls * Fixing missing calls * Fixing broken test --- api/channel.go | 6 +++--- api4/channel.go | 2 +- app/apptestlib.go | 2 +- app/channel.go | 34 +++++++++++++++--------------- app/command.go | 2 +- app/command_echo.go | 2 +- app/command_loadtest.go | 4 ++-- app/command_msg.go | 2 +- app/notification.go | 18 ++++++---------- app/notification_test.go | 18 ++++++++-------- app/plugin/api.go | 2 +- app/plugin/jira/plugin.go | 2 +- app/plugins.go | 4 ++-- app/post.go | 44 ++++++++++++++++++++++----------------- app/webhook.go | 14 ++++++------- app/webhook_test.go | 2 +- 16 files changed, 79 insertions(+), 79 deletions(-) diff --git a/api/channel.go b/api/channel.go index 50dc840ff9d..0d2df7c5dec 100644 --- a/api/channel.go +++ b/api/channel.go @@ -207,7 +207,7 @@ func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } else { if oldChannelDisplayName != channel.DisplayName { - if err := app.PostUpdateChannelDisplayNameMessage(c.Session.UserId, channel.Id, c.TeamId, oldChannelDisplayName, channel.DisplayName); err != nil { + if err := app.PostUpdateChannelDisplayNameMessage(c.Session.UserId, channel, oldChannelDisplayName, channel.DisplayName); err != nil { l4g.Error(err.Error()) } } @@ -255,7 +255,7 @@ func updateChannelHeader(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = err return } else { - if err := app.PostUpdateChannelHeaderMessage(c.Session.UserId, channel.Id, c.TeamId, oldChannelHeader, channelHeader); err != nil { + if err := app.PostUpdateChannelHeaderMessage(c.Session.UserId, channel, oldChannelHeader, channelHeader); err != nil { l4g.Error(err.Error()) } c.LogAudit("name=" + channel.Name) @@ -301,7 +301,7 @@ func updateChannelPurpose(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = err return } else { - if err := app.PostUpdateChannelPurposeMessage(c.Session.UserId, channel.Id, c.TeamId, oldChannelPurpose, channelPurpose); err != nil { + if err := app.PostUpdateChannelPurposeMessage(c.Session.UserId, channel, oldChannelPurpose, channelPurpose); err != nil { l4g.Error(err.Error()) } c.LogAudit("name=" + channel.Name) diff --git a/api4/channel.go b/api4/channel.go index 281fb6ac49c..8e9a6d9d655 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -141,7 +141,7 @@ func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } else { if oldChannelDisplayName != channel.DisplayName { - if err := app.PostUpdateChannelDisplayNameMessage(c.Session.UserId, channel.Id, c.Params.TeamId, oldChannelDisplayName, channel.DisplayName); err != nil { + if err := app.PostUpdateChannelDisplayNameMessage(c.Session.UserId, channel, oldChannelDisplayName, channel.DisplayName); err != nil { l4g.Error(err.Error()) } } diff --git a/app/apptestlib.go b/app/apptestlib.go index 3966bdeea9d..3cf831f60d4 100644 --- a/app/apptestlib.go +++ b/app/apptestlib.go @@ -169,7 +169,7 @@ func (me *TestHelper) CreatePost(channel *model.Channel) *model.Post { utils.DisableDebugLogForTest() var err *model.AppError - if post, err = CreatePost(post, channel.TeamId, false); err != nil { + if post, err = CreatePost(post, channel, false); err != nil { l4g.Error(err.Error()) l4g.Close() time.Sleep(time.Second) diff --git a/app/channel.go b/app/channel.go index 4a0f94b4240..417659c49f1 100644 --- a/app/channel.go +++ b/app/channel.go @@ -335,19 +335,19 @@ func PatchChannel(channel *model.Channel, patch *model.ChannelPatch, userId stri } if oldChannelDisplayName != channel.DisplayName { - if err := PostUpdateChannelDisplayNameMessage(userId, channel.Id, channel.TeamId, oldChannelDisplayName, channel.DisplayName); err != nil { + if err := PostUpdateChannelDisplayNameMessage(userId, channel, oldChannelDisplayName, channel.DisplayName); err != nil { l4g.Error(err.Error()) } } if channel.Header != oldChannelHeader { - if err := PostUpdateChannelHeaderMessage(userId, channel.Id, channel.TeamId, oldChannelHeader, channel.Header); err != nil { + if err := PostUpdateChannelHeaderMessage(userId, channel, oldChannelHeader, channel.Header); err != nil { l4g.Error(err.Error()) } } if channel.Purpose != oldChannelPurpose { - if err := PostUpdateChannelPurposeMessage(userId, channel.Id, channel.TeamId, oldChannelPurpose, channel.Purpose); err != nil { + if err := PostUpdateChannelPurposeMessage(userId, channel, oldChannelPurpose, channel.Purpose); err != nil { l4g.Error(err.Error()) } } @@ -445,7 +445,7 @@ func DeleteChannel(channel *model.Channel, userId string) *model.AppError { }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { l4g.Error(utils.T("api.channel.delete_channel.failed_post.error"), err) } @@ -615,7 +615,7 @@ func AddDirectChannels(teamId string, user *model.User) *model.AppError { return nil } -func PostUpdateChannelHeaderMessage(userId string, channelId string, teamId string, oldChannelHeader, newChannelHeader string) *model.AppError { +func PostUpdateChannelHeaderMessage(userId string, channel *model.Channel, oldChannelHeader, newChannelHeader string) *model.AppError { uc := Srv.Store.User().Get(userId) if uresult := <-uc; uresult.Err != nil { @@ -633,7 +633,7 @@ func PostUpdateChannelHeaderMessage(userId string, channelId string, teamId stri } post := &model.Post{ - ChannelId: channelId, + ChannelId: channel.Id, Message: message, Type: model.POST_HEADER_CHANGE, UserId: userId, @@ -644,7 +644,7 @@ func PostUpdateChannelHeaderMessage(userId string, channelId string, teamId stri }, } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("", "api.channel.post_update_channel_header_message_and_forget.post.error", nil, err.Error()) } } @@ -652,7 +652,7 @@ func PostUpdateChannelHeaderMessage(userId string, channelId string, teamId stri return nil } -func PostUpdateChannelPurposeMessage(userId string, channelId string, teamId string, oldChannelPurpose string, newChannelPurpose string) *model.AppError { +func PostUpdateChannelPurposeMessage(userId string, channel *model.Channel, oldChannelPurpose string, newChannelPurpose string) *model.AppError { uc := Srv.Store.User().Get(userId) if uresult := <-uc; uresult.Err != nil { @@ -670,7 +670,7 @@ func PostUpdateChannelPurposeMessage(userId string, channelId string, teamId str } post := &model.Post{ - ChannelId: channelId, + ChannelId: channel.Id, Message: message, Type: model.POST_PURPOSE_CHANGE, UserId: userId, @@ -680,7 +680,7 @@ func PostUpdateChannelPurposeMessage(userId string, channelId string, teamId str "new_purpose": newChannelPurpose, }, } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("", "app.channel.post_update_channel_purpose_message.post.error", nil, err.Error()) } } @@ -688,7 +688,7 @@ func PostUpdateChannelPurposeMessage(userId string, channelId string, teamId str return nil } -func PostUpdateChannelDisplayNameMessage(userId string, channelId string, teamId string, oldChannelDisplayName, newChannelDisplayName string) *model.AppError { +func PostUpdateChannelDisplayNameMessage(userId string, channel *model.Channel, oldChannelDisplayName, newChannelDisplayName string) *model.AppError { uc := Srv.Store.User().Get(userId) if uresult := <-uc; uresult.Err != nil { @@ -699,7 +699,7 @@ func PostUpdateChannelDisplayNameMessage(userId string, channelId string, teamId message := fmt.Sprintf(utils.T("api.channel.post_update_channel_displayname_message_and_forget.updated_from"), user.Username, oldChannelDisplayName, newChannelDisplayName) post := &model.Post{ - ChannelId: channelId, + ChannelId: channel.Id, Message: message, Type: model.POST_DISPLAYNAME_CHANGE, UserId: userId, @@ -710,7 +710,7 @@ func PostUpdateChannelDisplayNameMessage(userId string, channelId string, teamId }, } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("PostUpdateChannelDisplayNameMessage", "api.channel.post_update_channel_displayname_message_and_forget.create_post.error", nil, err.Error()) } } @@ -908,7 +908,7 @@ func postJoinChannelMessage(user *model.User, channel *model.Channel) *model.App }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("postJoinChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } @@ -964,7 +964,7 @@ func postLeaveChannelMessage(user *model.User, channel *model.Channel) *model.Ap }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("postLeaveChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } @@ -983,7 +983,7 @@ func PostAddToChannelMessage(user *model.User, addedUser *model.User, channel *m }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("postAddToChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } @@ -1001,7 +1001,7 @@ func PostRemoveFromChannelMessage(removerUserId string, removedUser *model.User, }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel, false); err != nil { return model.NewLocAppError("postRemoveFromChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } diff --git a/app/command.go b/app/command.go index 83500cc1f0b..b9d6748fb64 100644 --- a/app/command.go +++ b/app/command.go @@ -46,7 +46,7 @@ func CreateCommandPost(post *model.Post, teamId string, response *model.CommandR } if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL { - return CreatePost(post, teamId, true) + return CreatePostMissingChannel(post, true) } else if response.ResponseType == "" || response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL { if response.Text == "" { return post, nil diff --git a/app/command_echo.go b/app/command_echo.go index ef70cb609ee..3ab5a787368 100644 --- a/app/command_echo.go +++ b/app/command_echo.go @@ -88,7 +88,7 @@ func (me *EchoProvider) DoCommand(args *model.CommandArgs, message string) *mode time.Sleep(time.Duration(delay) * time.Second) - if _, err := CreatePost(post, args.TeamId, true); err != nil { + if _, err := CreatePostMissingChannel(post, true); err != nil { l4g.Error(args.T("api.command_echo.create.app_error"), err) } }() diff --git a/app/command_loadtest.go b/app/command_loadtest.go index 20e6e0cdda4..8a37014fa2e 100644 --- a/app/command_loadtest.go +++ b/app/command_loadtest.go @@ -357,7 +357,7 @@ func (me *LoadTestProvider) UrlCommand(args *model.CommandArgs, message string) post.ChannelId = args.ChannelId post.UserId = args.UserId - if _, err := CreatePost(post, args.TeamId, false); err != nil { + if _, err := CreatePostMissingChannel(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(args *model.CommandArgs, message string) post.Message = message } - if _, err := CreatePost(post, args.TeamId, false); err != nil { + if _, err := CreatePostMissingChannel(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/app/command_msg.go b/app/command_msg.go index 977e403d545..67bb425d2ee 100644 --- a/app/command_msg.go +++ b/app/command_msg.go @@ -89,7 +89,7 @@ func (me *msgProvider) DoCommand(args *model.CommandArgs, message string) *model post.Message = parsedMessage post.ChannelId = targetChannelId post.UserId = args.UserId - if _, err := CreatePost(post, args.TeamId, true); err != nil { + if _, err := CreatePostMissingChannel(post, true); err != nil { return &model.CommandResponse{Text: args.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } diff --git a/app/notification.go b/app/notification.go index 21271c716f1..1d3416794cb 100644 --- a/app/notification.go +++ b/app/notification.go @@ -25,7 +25,7 @@ import ( "github.com/nicksnyder/go-i18n/i18n" ) -func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) { +func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User, parentPostList *model.PostList) ([]string, *model.AppError) { pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true) cmnchan := Srv.Store.Channel().GetAllChannelMembersNotifyPropsForChannel(channel.Id, true) var fchan store.StoreChannel @@ -77,17 +77,11 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe mentionedUserIds, potentialOtherMentions, hereNotification, channelNotification, allNotification = GetExplicitMentions(post.Message, keywords) // get users that have comment thread mentions enabled - if len(post.RootId) > 0 { - if result := <-Srv.Store.Post().Get(post.RootId); result.Err != nil { - return nil, result.Err - } else { - list := result.Data.(*model.PostList) - - for _, threadPost := range list.Posts { - profile := profileMap[threadPost.UserId] - if profile != nil && (profile.NotifyProps["comments"] == "any" || (profile.NotifyProps["comments"] == "root" && threadPost.Id == list.Order[0])) { - mentionedUserIds[threadPost.UserId] = true - } + if len(post.RootId) > 0 && parentPostList != nil { + for _, threadPost := range parentPostList.Posts { + profile := profileMap[threadPost.UserId] + if profile != nil && (profile.NotifyProps["comments"] == "any" || (profile.NotifyProps["comments"] == "root" && threadPost.Id == parentPostList.Order[0])) { + mentionedUserIds[threadPost.UserId] = true } } } diff --git a/app/notification_test.go b/app/notification_test.go index 5f57290e733..ae6d0504a6e 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -16,17 +16,17 @@ func TestSendNotifications(t *testing.T) { AddUserToChannel(th.BasicUser2, th.BasicChannel) - post1, err := CreatePost(&model.Post{ + post1, err := CreatePostMissingChannel(&model.Post{ UserId: th.BasicUser.Id, ChannelId: th.BasicChannel.Id, Message: "@" + th.BasicUser2.Username, - }, th.BasicTeam.Id, true) + }, true) if err != nil { t.Fatal(err) } - mentions, err := SendNotifications(post1, th.BasicTeam, th.BasicChannel, th.BasicUser) + mentions, err := SendNotifications(post1, th.BasicTeam, th.BasicChannel, th.BasicUser, nil) if err != nil { t.Fatal(err) } else if mentions == nil { @@ -42,17 +42,17 @@ func TestSendNotifications(t *testing.T) { t.Fatal(err) } - post2, err := CreatePost(&model.Post{ + post2, err := CreatePostMissingChannel(&model.Post{ UserId: th.BasicUser.Id, ChannelId: dm.Id, Message: "dm message", - }, th.BasicTeam.Id, true) + }, true) if err != nil { t.Fatal(err) } - _, err = SendNotifications(post2, th.BasicTeam, dm, th.BasicUser) + _, err = SendNotifications(post2, th.BasicTeam, dm, th.BasicUser, nil) if err != nil { t.Fatal(err) } @@ -60,17 +60,17 @@ func TestSendNotifications(t *testing.T) { UpdateActive(th.BasicUser2, false) InvalidateAllCaches() - post3, err := CreatePost(&model.Post{ + post3, err := CreatePostMissingChannel(&model.Post{ UserId: th.BasicUser.Id, ChannelId: dm.Id, Message: "dm message", - }, th.BasicTeam.Id, true) + }, true) if err != nil { t.Fatal(err) } - _, err = SendNotifications(post3, th.BasicTeam, dm, th.BasicUser) + _, err = SendNotifications(post3, th.BasicTeam, dm, th.BasicUser, nil) if err != nil { t.Fatal(err) } diff --git a/app/plugin/api.go b/app/plugin/api.go index ceea51969e6..41838b818a8 100644 --- a/app/plugin/api.go +++ b/app/plugin/api.go @@ -30,7 +30,7 @@ type API interface { GetDirectChannel(userId1, userId2 string) (*model.Channel, *model.AppError) // Creates a post - CreatePost(post *model.Post, teamId string) (*model.Post, *model.AppError) + CreatePost(post *model.Post) (*model.Post, *model.AppError) // Returns a localized string. If a request is given, its headers will be used to pick a locale. I18n(id string, r *http.Request) string diff --git a/app/plugin/jira/plugin.go b/app/plugin/jira/plugin.go index ad51e723c21..94922321c47 100644 --- a/app/plugin/jira/plugin.go +++ b/app/plugin/jira/plugin.go @@ -72,7 +72,7 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) { "from_webhook": "true", "attachments": []*model.SlackAttachment{attachment}, }, - }, channel.TeamId); err != nil { + }); err != nil { http.Error(w, p.api.I18n(err.Message, r), err.StatusCode) } } diff --git a/app/plugins.go b/app/plugins.go index d569dcba229..82eda067c31 100644 --- a/app/plugins.go +++ b/app/plugins.go @@ -50,8 +50,8 @@ func (api *PluginAPI) GetDirectChannel(userId1, userId2 string) (*model.Channel, return GetDirectChannel(userId1, userId2) } -func (api *PluginAPI) CreatePost(post *model.Post, teamId string) (*model.Post, *model.AppError) { - return CreatePost(post, teamId, true) +func (api *PluginAPI) CreatePost(post *model.Post) (*model.Post, *model.AppError) { + return CreatePostMissingChannel(post, true) } func (api *PluginAPI) I18n(id string, r *http.Request) string { diff --git a/app/post.go b/app/post.go index 068266120fa..5b83ab7a2f9 100644 --- a/app/post.go +++ b/app/post.go @@ -34,7 +34,7 @@ func CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) { return nil, err } - if rp, err := CreatePost(post, channel.TeamId, true); err != nil { + if rp, err := CreatePost(post, channel, true); err != nil { if err.Id == "api.post.create_post.root_id.app_error" || err.Id == "api.post.create_post.channel_root_id.app_error" || err.Id == "api.post.create_post.parent_id.app_error" { @@ -61,19 +61,32 @@ func CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) { } -func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.Post, *model.AppError) { +func CreatePostMissingChannel(post *model.Post, triggerWebhooks bool) (*model.Post, *model.AppError) { + var channel *model.Channel + cchan := Srv.Store.Channel().Get(post.ChannelId, true) + if result := <-cchan; result.Err != nil { + return nil, result.Err + } else { + channel = result.Data.(*model.Channel) + } + + return CreatePost(post, channel, triggerWebhooks) +} + +func CreatePost(post *model.Post, channel *model.Channel, triggerWebhooks bool) (*model.Post, *model.AppError) { var pchan store.StoreChannel if len(post.RootId) > 0 { pchan = Srv.Store.Post().Get(post.RootId) } // Verify the parent/child relationships are correct + var parentPostList *model.PostList if pchan != nil { if presult := <-pchan; presult.Err != nil { return nil, model.NewLocAppError("createPost", "api.post.create_post.root_id.app_error", nil, "") } else { - list := presult.Data.(*model.PostList) - if len(list.Posts) == 0 || !list.IsChannelId(post.ChannelId) { + parentPostList = presult.Data.(*model.PostList) + if len(parentPostList.Posts) == 0 || !parentPostList.IsChannelId(post.ChannelId) { return nil, model.NewLocAppError("createPost", "api.post.create_post.channel_root_id.app_error", nil, "") } @@ -82,7 +95,7 @@ func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.P } if post.RootId != post.ParentId { - parent := list.Posts[post.ParentId] + parent := parentPostList.Posts[post.ParentId] if parent == nil { return nil, model.NewLocAppError("createPost", "api.post.create_post.parent_id.app_error", nil, "") } @@ -101,7 +114,7 @@ func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.P esInterface := einterfaces.GetElasticsearchInterface() if esInterface != nil && *utils.Cfg.ElasticsearchSettings.EnableIndexing { - go esInterface.IndexPost(rpost, teamId) + go esInterface.IndexPost(rpost, channel.TeamId) } if einterfaces.GetMetricsInterface() != nil { @@ -123,19 +136,19 @@ func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.P } } - if err := handlePostEvents(rpost, teamId, triggerWebhooks); err != nil { + if err := handlePostEvents(rpost, channel, triggerWebhooks, parentPostList); err != nil { return nil, err } return rpost, nil } -func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *model.AppError { +func handlePostEvents(post *model.Post, channel *model.Channel, triggerWebhooks bool, parentPostList *model.PostList) *model.AppError { var tchan store.StoreChannel - if len(teamId) > 0 { - tchan = Srv.Store.Team().Get(teamId) + if len(channel.TeamId) > 0 { + tchan = Srv.Store.Team().Get(channel.TeamId) } - cchan := Srv.Store.Channel().Get(post.ChannelId, true) + uchan := Srv.Store.User().Get(post.UserId) var team *model.Team @@ -150,13 +163,6 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo team = &model.Team{} } - var channel *model.Channel - if result := <-cchan; result.Err != nil { - return result.Err - } else { - channel = result.Data.(*model.Channel) - } - InvalidateCacheForChannel(channel) InvalidateCacheForChannelPosts(channel.Id) @@ -167,7 +173,7 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo user = result.Data.(*model.User) } - if _, err := SendNotifications(post, team, channel, user); err != nil { + if _, err := SendNotifications(post, team, channel, user, parentPostList); err != nil { return err } diff --git a/app/webhook.go b/app/webhook.go index f84086d94b4..ce154ff70f3 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -80,13 +80,13 @@ func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Chan TriggerWord: triggerWord, FileIds: strings.Join(post.FileIds, ","), } - go TriggerWebhook(payload, hook, post) + go TriggerWebhook(payload, hook, post, channel) } return nil } -func TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingWebhook, post *model.Post) { +func TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingWebhook, post *model.Post, channel *model.Channel) { var body io.Reader var contentType string if hook.ContentType == "application/json" { @@ -109,7 +109,7 @@ func TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingW respProps := model.MapFromJson(resp.Body) if text, ok := respProps["text"]; ok { - if _, err := CreateWebhookPost(hook.CreatorId, hook.TeamId, post.ChannelId, text, respProps["username"], respProps["icon_url"], post.Props, post.Type); err != nil { + if _, err := CreateWebhookPost(hook.CreatorId, channel, 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) } } @@ -118,12 +118,12 @@ func TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingW } } -func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) { +func CreateWebhookPost(userId string, channel *model.Channel, 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: userId, ChannelId: channel.Id, Message: text, Type: postType} post.AddProp("from_webhook", "true") if metrics := einterfaces.GetMetricsInterface(); metrics != nil { @@ -173,7 +173,7 @@ func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overri post.UpdateAt = 0 post.CreateAt = 0 post.Message = txt - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePostMissingChannel(post, false); err != nil { return nil, model.NewLocAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message) } @@ -527,7 +527,7 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *mo overrideUsername := req.Username overrideIconUrl := req.IconURL - if _, err := CreateWebhookPost(hook.UserId, hook.TeamId, channel.Id, text, overrideUsername, overrideIconUrl, req.Props, webhookType); err != nil { + if _, err := CreateWebhookPost(hook.UserId, channel, text, overrideUsername, overrideIconUrl, req.Props, webhookType); err != nil { return err } diff --git a/app/webhook_test.go b/app/webhook_test.go index ae97454a652..8333de0ee7c 100644 --- a/app/webhook_test.go +++ b/app/webhook_test.go @@ -28,7 +28,7 @@ func TestCreateWebhookPost(t *testing.T) { } defer DeleteIncomingWebhook(hook.Id) - post, err := CreateWebhookPost(hook.UserId, hook.TeamId, th.BasicChannel.Id, "foo", "user", "http://iconurl", model.StringInterface{ + post, err := CreateWebhookPost(hook.UserId, th.BasicChannel, "foo", "user", "http://iconurl", model.StringInterface{ "attachments": []*model.SlackAttachment{ &model.SlackAttachment{ Text: "text",