From 33b8b72a026c54f121f1d0f0370d9ad2e6220a61 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 7 Mar 2017 09:37:00 -0500 Subject: Adding index and cache to reactinos store (#5654) --- app/web_hub.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'app') diff --git a/app/web_hub.go b/app/web_hub.go index 5b7e3623e..65d18481f 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -202,6 +202,18 @@ func InvalidateWebConnSessionCacheForUser(userId string) { } } +func InvalidateCacheForReactions(postId string) { + InvalidateCacheForReactionsSkipClusterSend(postId) + + if cluster := einterfaces.GetClusterInterface(); cluster != nil { + cluster.InvalidateCacheForReactions(postId) + } +} + +func InvalidateCacheForReactionsSkipClusterSend(postId string) { + Srv.Store.Reaction().InvalidateCacheForPost(postId) +} + func (h *Hub) Register(webConn *WebConn) { h.register <- webConn -- cgit v1.2.3-1-g7c22 From 5d62b3661bcf4b912e7809ca05082e364e2b34b1 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Wed, 8 Mar 2017 04:15:33 -0500 Subject: Added additional validation for slack attachment format on server (#5680) --- app/post.go | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) (limited to 'app') diff --git a/app/post.go b/app/post.go index a41da6c90..3d463aade 100644 --- a/app/post.go +++ b/app/post.go @@ -180,40 +180,20 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo // This method only parses and processes the attachments, // all else should be set in the post which is passed -func parseSlackAttachment(post *model.Post, attachments interface{}) { +func parseSlackAttachment(post *model.Post, attachments []*model.SlackAttachment) { post.Type = model.POST_SLACK_ATTACHMENT - if list, success := attachments.([]interface{}); success { - for i, aInt := range list { - attachment := aInt.(map[string]interface{}) - if aText, ok := attachment["text"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["text"] = aText - list[i] = attachment - } - if aText, ok := attachment["pretext"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["pretext"] = aText - list[i] = attachment - } - if fVal, ok := attachment["fields"]; ok { - if fields, ok := fVal.([]interface{}); ok { - // parse attachment field links into Markdown format - for j, fInt := range fields { - field := fInt.(map[string]interface{}) - if fValue, ok := field["value"].(string); ok { - fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") - field["value"] = fValue - fields[j] = field - } - } - attachment["fields"] = fields - list[i] = attachment - } + for _, attachment := range attachments { + attachment.Text = parseSlackLinksToMarkdown(attachment.Text) + attachment.Pretext = parseSlackLinksToMarkdown(attachment.Pretext) + + for _, field := range attachment.Fields { + if value, ok := field.Value.(string); ok { + field.Value = parseSlackLinksToMarkdown(value) } } - post.AddProp("attachments", list) } + post.AddProp("attachments", attachments) } func parseSlackLinksToMarkdown(text string) string { -- cgit v1.2.3-1-g7c22 From 9c13863f484e9741adf4179cb6b95ee4af8ec343 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 9 Mar 2017 14:41:03 -0500 Subject: Fix GMs showing up on refresh after being hidden (#5702) --- app/notification.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/notification.go b/app/notification.go index 469d114ee..1dd3fedb2 100644 --- a/app/notification.go +++ b/app/notification.go @@ -286,7 +286,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POSTED, "", post.ChannelId, "", nil) message.Add("post", post.ToJson()) message.Add("channel_type", channel.Type) - message.Add("channel_display_name", channel.DisplayName) + message.Add("channel_display_name", channelName) message.Add("channel_name", channel.Name) message.Add("sender_name", senderUsername) message.Add("team_id", team.Id) -- cgit v1.2.3-1-g7c22 From a8e68bd8905972ae59de90fa33d5b3e3c274dc47 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Fri, 10 Mar 2017 05:18:18 -0500 Subject: PLT-5765 Passed SiteURL to SendNotifications (#5705) --- app/apptestlib.go | 4 ++-- app/channel.go | 50 ++++++++++++++++++++++++------------------------ app/command.go | 4 ++-- app/import.go | 2 +- app/notification.go | 10 +++++----- app/notification_test.go | 5 +++-- app/post.go | 18 ++++++++--------- app/slackimport.go | 2 +- app/team.go | 24 +++++++++++------------ app/team_test.go | 11 ++++++----- app/user.go | 10 +++++----- app/user_test.go | 6 +++--- app/webhook.go | 12 ++++++------ 13 files changed, 80 insertions(+), 78 deletions(-) (limited to 'app') diff --git a/app/apptestlib.go b/app/apptestlib.go index 41e234130..561c98ed2 100644 --- a/app/apptestlib.go +++ b/app/apptestlib.go @@ -161,7 +161,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.TeamId, false, utils.GetSiteURL()); err != nil { l4g.Error(err.Error()) l4g.Close() time.Sleep(time.Second) @@ -174,7 +174,7 @@ func (me *TestHelper) CreatePost(channel *model.Channel) *model.Post { func LinkUserToTeam(user *model.User, team *model.Team) { utils.DisableDebugLogForTest() - err := JoinUserToTeam(team, user) + err := JoinUserToTeam(team, user, utils.GetSiteURL()) if err != nil { l4g.Error(err.Error()) l4g.Close() diff --git a/app/channel.go b/app/channel.go index f037e64c3..7c63cbc6b 100644 --- a/app/channel.go +++ b/app/channel.go @@ -32,7 +32,7 @@ func CreateDefaultChannels(teamId string) ([]*model.Channel, *model.AppError) { return channels, nil } -func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *model.AppError { +func JoinDefaultChannels(teamId string, user *model.User, channelRole string, siteURL string) *model.AppError { var err *model.AppError = nil if result := <-Srv.Store.Channel().GetByName(teamId, "town-square", true); result.Err != nil { @@ -47,7 +47,7 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m err = cmResult.Err } - if err := postJoinChannelMessage(user, townSquare); err != nil { + if err := postJoinChannelMessage(user, townSquare, siteURL); err != nil { l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err) } @@ -66,7 +66,7 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m err = cmResult.Err } - if err := postJoinChannelMessage(user, offTopic); err != nil { + if err := postJoinChannelMessage(user, offTopic, siteURL); err != nil { l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err) } @@ -310,7 +310,7 @@ func UpdateChannelMemberNotifyProps(data map[string]string, channelId string, us } } -func DeleteChannel(channel *model.Channel, userId string) *model.AppError { +func DeleteChannel(channel *model.Channel, userId string, siteURL string) *model.AppError { uc := Srv.Store.User().Get(userId) ihc := Srv.Store.Webhook().GetIncomingByChannel(channel.Id) ohc := Srv.Store.Webhook().GetOutgoingByChannel(channel.Id) @@ -350,7 +350,7 @@ func DeleteChannel(channel *model.Channel, userId string) *model.AppError { }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel.TeamId, false, siteURL); err != nil { l4g.Error(utils.T("api.channel.delete_channel.failed_post.error"), err) } @@ -482,7 +482,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, channelId string, teamId string, oldChannelHeader, newChannelHeader string, siteURL string) *model.AppError { uc := Srv.Store.User().Get(userId) if uresult := <-uc; uresult.Err != nil { @@ -511,7 +511,7 @@ func PostUpdateChannelHeaderMessage(userId string, channelId string, teamId stri }, } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, teamId, false, siteURL); err != nil { return model.NewLocAppError("", "api.channel.post_update_channel_header_message_and_forget.post.error", nil, err.Error()) } } @@ -519,7 +519,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, channelId string, teamId string, oldChannelPurpose string, newChannelPurpose string, siteURL string) *model.AppError { uc := Srv.Store.User().Get(userId) if uresult := <-uc; uresult.Err != nil { @@ -547,7 +547,7 @@ func PostUpdateChannelPurposeMessage(userId string, channelId string, teamId str "new_purpose": newChannelPurpose, }, } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, teamId, false, siteURL); err != nil { return model.NewLocAppError("", "app.channel.post_update_channel_purpose_message.post.error", nil, err.Error()) } } @@ -555,7 +555,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, channelId string, teamId string, oldChannelDisplayName, newChannelDisplayName string, siteURL string) *model.AppError { uc := Srv.Store.User().Get(userId) if uresult := <-uc; uresult.Err != nil { @@ -577,7 +577,7 @@ func PostUpdateChannelDisplayNameMessage(userId string, channelId string, teamId }, } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, teamId, false, siteURL); err != nil { return model.NewLocAppError("PostUpdateChannelDisplayNameMessage", "api.channel.post_update_channel_displayname_message_and_forget.create_post.error", nil, err.Error()) } } @@ -694,7 +694,7 @@ func GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *mode } } -func JoinChannel(channel *model.Channel, userId string) *model.AppError { +func JoinChannel(channel *model.Channel, userId string, siteURL string) *model.AppError { userChan := Srv.Store.User().Get(userId) memberChan := Srv.Store.Channel().GetMember(channel.Id, userId) @@ -711,7 +711,7 @@ func JoinChannel(channel *model.Channel, userId string) *model.AppError { return err } - if err := postJoinChannelMessage(user, channel); err != nil { + if err := postJoinChannelMessage(user, channel, siteURL); err != nil { return err } } else { @@ -722,7 +722,7 @@ func JoinChannel(channel *model.Channel, userId string) *model.AppError { return nil } -func postJoinChannelMessage(user *model.User, channel *model.Channel) *model.AppError { +func postJoinChannelMessage(user *model.User, channel *model.Channel, siteURL string) *model.AppError { post := &model.Post{ ChannelId: channel.Id, Message: fmt.Sprintf(utils.T("api.channel.join_channel.post_and_forget"), user.Username), @@ -733,14 +733,14 @@ func postJoinChannelMessage(user *model.User, channel *model.Channel) *model.App }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel.TeamId, false, siteURL); err != nil { return model.NewLocAppError("postJoinChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } return nil } -func LeaveChannel(channelId string, userId string) *model.AppError { +func LeaveChannel(channelId string, userId string, siteURL string) *model.AppError { sc := Srv.Store.Channel().Get(channelId, true) uc := Srv.Store.User().Get(userId) ccm := Srv.Store.Channel().GetMemberCount(channelId, false) @@ -772,13 +772,13 @@ func LeaveChannel(channelId string, userId string) *model.AppError { return err } - go postLeaveChannelMessage(user, channel) + go postLeaveChannelMessage(user, channel, siteURL) } return nil } -func postLeaveChannelMessage(user *model.User, channel *model.Channel) *model.AppError { +func postLeaveChannelMessage(user *model.User, channel *model.Channel, siteURL string) *model.AppError { post := &model.Post{ ChannelId: channel.Id, Message: fmt.Sprintf(utils.T("api.channel.leave.left"), user.Username), @@ -789,14 +789,14 @@ func postLeaveChannelMessage(user *model.User, channel *model.Channel) *model.Ap }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel.TeamId, false, siteURL); err != nil { return model.NewLocAppError("postLeaveChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } return nil } -func PostAddToChannelMessage(user *model.User, addedUser *model.User, channel *model.Channel) *model.AppError { +func PostAddToChannelMessage(user *model.User, addedUser *model.User, channel *model.Channel, siteURL string) *model.AppError { post := &model.Post{ ChannelId: channel.Id, Message: fmt.Sprintf(utils.T("api.channel.add_member.added"), addedUser.Username, user.Username), @@ -808,14 +808,14 @@ func PostAddToChannelMessage(user *model.User, addedUser *model.User, channel *m }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel.TeamId, false, siteURL); err != nil { return model.NewLocAppError("postAddToChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } return nil } -func PostRemoveFromChannelMessage(removerUserId string, removedUser *model.User, channel *model.Channel) *model.AppError { +func PostRemoveFromChannelMessage(removerUserId string, removedUser *model.User, channel *model.Channel, siteURL string) *model.AppError { post := &model.Post{ ChannelId: channel.Id, Message: fmt.Sprintf(utils.T("api.channel.remove_member.removed"), removedUser.Username), @@ -826,7 +826,7 @@ func PostRemoveFromChannelMessage(removerUserId string, removedUser *model.User, }, } - if _, err := CreatePost(post, channel.TeamId, false); err != nil { + if _, err := CreatePost(post, channel.TeamId, false, siteURL); err != nil { return model.NewLocAppError("postRemoveFromChannelMessage", "api.channel.post_user_add_remove_message_and_forget.error", nil, err.Error()) } @@ -865,7 +865,7 @@ func removeUserFromChannel(userIdToRemove string, removerUserId string, channel return nil } -func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel *model.Channel) *model.AppError { +func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel *model.Channel, siteURL string) *model.AppError { var err *model.AppError if err = removeUserFromChannel(userIdToRemove, removerUserId, channel); err != nil { return err @@ -876,7 +876,7 @@ func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel return err } - go PostRemoveFromChannelMessage(removerUserId, user, channel) + go PostRemoveFromChannelMessage(removerUserId, user, channel, siteURL) return nil } diff --git a/app/command.go b/app/command.go index 2d5861206..4d344915d 100644 --- a/app/command.go +++ b/app/command.go @@ -7,7 +7,7 @@ import ( "github.com/mattermost/platform/model" ) -func CreateCommandPost(post *model.Post, teamId string, response *model.CommandResponse) (*model.Post, *model.AppError) { +func CreateCommandPost(post *model.Post, teamId string, response *model.CommandResponse, siteURL string) (*model.Post, *model.AppError) { post.Message = parseSlackLinksToMarkdown(response.Text) post.CreateAt = model.GetMillis() @@ -17,7 +17,7 @@ func CreateCommandPost(post *model.Post, teamId string, response *model.CommandR switch response.ResponseType { case model.COMMAND_RESPONSE_TYPE_IN_CHANNEL: - return CreatePost(post, teamId, true) + return CreatePost(post, teamId, true, siteURL) case model.COMMAND_RESPONSE_TYPE_EPHEMERAL: if response.Text == "" { return post, nil diff --git a/app/import.go b/app/import.go index 1ca532902..4d0feb532 100644 --- a/app/import.go +++ b/app/import.go @@ -865,7 +865,7 @@ func OldImportUser(team *model.Team, user *model.User) *model.User { l4g.Error(utils.T("api.import.import_user.set_email.error"), cresult.Err) } - if err := JoinUserToTeam(team, user); err != nil { + if err := JoinUserToTeam(team, user, utils.GetSiteURL()); err != nil { l4g.Error(utils.T("api.import.import_user.join_team.error"), err) } diff --git a/app/notification.go b/app/notification.go index 1dd3fedb2..67959c6bc 100644 --- a/app/notification.go +++ b/app/notification.go @@ -24,7 +24,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, siteURL string) ([]string, *model.AppError) { pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true) cmnchan := Srv.Store.Channel().GetAllChannelMembersNotifyPropsForChannel(channel.Id, true) var fchan store.StoreChannel @@ -171,7 +171,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe } if userAllowsEmails && status.Status != model.STATUS_ONLINE && profileMap[id].DeleteAt == 0 { - sendNotificationEmail(post, profileMap[id], channel, team, senderName, sender) + sendNotificationEmail(post, profileMap[id], channel, team, senderName, sender, siteURL) } } } @@ -317,7 +317,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe return mentionedUsersList, nil } -func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User) *model.AppError { +func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User, siteURL string) *model.AppError { if channel.IsGroupOrDirect() && channel.TeamId != team.Id { // this message is a cross-team DM/GM so we need to find a team that the recipient is on to use in the link if result := <-Srv.Store.Team().GetTeamsByUserId(user.Id); result.Err != nil { @@ -368,7 +368,7 @@ func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Ch var mailTemplate string var mailParameters map[string]interface{} - teamURL := utils.GetSiteURL() + "/" + team.Name + teamURL := siteURL + "/" + team.Name tm := time.Unix(post.CreateAt/1000, 0) userLocale := utils.GetUserTranslations(user.Locale) @@ -406,7 +406,7 @@ func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Ch subject := fmt.Sprintf("[%v] %v", utils.Cfg.TeamSettings.SiteName, userLocale(mailTemplate, mailParameters)) bodyPage := utils.NewHTMLTemplate("post_body", user.Locale) - bodyPage.Props["SiteURL"] = utils.GetSiteURL() + bodyPage.Props["SiteURL"] = siteURL bodyPage.Props["PostMessage"] = GetMessageForNotification(post, userLocale) if team.Name != "select_team" { bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id diff --git a/app/notification_test.go b/app/notification_test.go index 3768a95c7..fd8c4bf57 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) func TestSendNotifications(t *testing.T) { @@ -18,13 +19,13 @@ func TestSendNotifications(t *testing.T) { UserId: th.BasicUser.Id, ChannelId: th.BasicChannel.Id, Message: "@" + th.BasicUser2.Username, - }, th.BasicTeam.Id, true) + }, th.BasicTeam.Id, true, utils.GetSiteURL()) if postErr != nil { t.Fatal(postErr) } - mentions, err := SendNotifications(post1, th.BasicTeam, th.BasicChannel, th.BasicUser) + mentions, err := SendNotifications(post1, th.BasicTeam, th.BasicChannel, th.BasicUser, utils.GetSiteURL()) if err != nil { t.Fatal(err) } else if mentions == nil { diff --git a/app/post.go b/app/post.go index 3d463aade..c4d128399 100644 --- a/app/post.go +++ b/app/post.go @@ -24,7 +24,7 @@ var ( linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) ) -func CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) { +func CreatePostAsUser(post *model.Post, siteURL string) (*model.Post, *model.AppError) { // Check that channel has not been deleted var channel *model.Channel if result := <-Srv.Store.Channel().Get(post.ChannelId, true); result.Err != nil { @@ -41,7 +41,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.TeamId, true, siteURL); 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" { @@ -62,7 +62,7 @@ func CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) { } -func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.Post, *model.AppError) { +func CreatePost(post *model.Post, teamId string, triggerWebhooks bool, siteURL string) (*model.Post, *model.AppError) { var pchan store.StoreChannel if len(post.RootId) > 0 { pchan = Srv.Store.Post().Get(post.RootId) @@ -119,14 +119,14 @@ func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.P } } - if err := handlePostEvents(rpost, teamId, triggerWebhooks); err != nil { + if err := handlePostEvents(rpost, teamId, triggerWebhooks, siteURL); err != nil { return nil, err } return rpost, nil } -func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *model.AppError { +func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool, siteURL string) *model.AppError { var tchan store.StoreChannel if len(teamId) > 0 { tchan = Srv.Store.Team().Get(teamId) @@ -163,13 +163,13 @@ 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, siteURL); err != nil { return err } if triggerWebhooks { go func() { - if err := handleWebhookEvents(post, team, channel, user); err != nil { + if err := handleWebhookEvents(post, team, channel, user, siteURL); err != nil { l4g.Error(err.Error()) } }() @@ -345,7 +345,7 @@ func GetFlaggedPosts(userId string, offset int, limit int) (*model.PostList, *mo } } -func GetPermalinkPost(postId string, userId string) (*model.PostList, *model.AppError) { +func GetPermalinkPost(postId string, userId string, siteURL string) (*model.PostList, *model.AppError) { if result := <-Srv.Store.Post().Get(postId); result.Err != nil { return nil, result.Err } else { @@ -362,7 +362,7 @@ func GetPermalinkPost(postId string, userId string) (*model.PostList, *model.App return nil, err } - if err = JoinChannel(channel, userId); err != nil { + if err = JoinChannel(channel, userId, siteURL); err != nil { return nil, err } diff --git a/app/slackimport.go b/app/slackimport.go index 5512d8c00..ced0f6581 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -167,7 +167,7 @@ func SlackAddUsers(teamId string, slackusers []SlackUser, log *bytes.Buffer) map if result := <-Srv.Store.User().GetByEmail(email); result.Err == nil { existingUser := result.Data.(*model.User) addedUsers[sUser.Id] = existingUser - if err := JoinUserToTeam(team, addedUsers[sUser.Id]); err != nil { + if err := JoinUserToTeam(team, addedUsers[sUser.Id], utils.GetSiteURL()); err != nil { log.WriteString(utils.T("api.slackimport.slack_add_users.merge_existing_failed", map[string]interface{}{"Email": existingUser.Email, "Username": existingUser.Username})) } else { log.WriteString(utils.T("api.slackimport.slack_add_users.merge_existing", map[string]interface{}{"Email": existingUser.Email, "Username": existingUser.Username})) diff --git a/app/team.go b/app/team.go index 60a2f4220..875dedfd0 100644 --- a/app/team.go +++ b/app/team.go @@ -29,7 +29,7 @@ func CreateTeam(team *model.Team) (*model.Team, *model.AppError) { } } -func CreateTeamWithUser(team *model.Team, userId string) (*model.Team, *model.AppError) { +func CreateTeamWithUser(team *model.Team, userId string, siteURL string) (*model.Team, *model.AppError) { var user *model.User var err *model.AppError if user, err = GetUser(userId); err != nil { @@ -47,7 +47,7 @@ func CreateTeamWithUser(team *model.Team, userId string) (*model.Team, *model.Ap return nil, err } - if err = JoinUserToTeam(rteam, user); err != nil { + if err = JoinUserToTeam(rteam, user, siteURL); err != nil { return nil, err } @@ -137,7 +137,7 @@ func UpdateTeamMemberRoles(teamId string, userId string, newRoles string) (*mode return member, nil } -func AddUserToTeam(teamId string, userId string) (*model.Team, *model.AppError) { +func AddUserToTeam(teamId string, userId string, siteURL string) (*model.Team, *model.AppError) { tchan := Srv.Store.Team().Get(teamId) uchan := Srv.Store.User().Get(userId) @@ -155,22 +155,22 @@ func AddUserToTeam(teamId string, userId string) (*model.Team, *model.AppError) user = result.Data.(*model.User) } - if err := JoinUserToTeam(team, user); err != nil { + if err := JoinUserToTeam(team, user, siteURL); err != nil { return nil, err } return team, nil } -func AddUserToTeamByTeamId(teamId string, user *model.User) *model.AppError { +func AddUserToTeamByTeamId(teamId string, user *model.User, siteURL string) *model.AppError { if result := <-Srv.Store.Team().Get(teamId); result.Err != nil { return result.Err } else { - return JoinUserToTeam(result.Data.(*model.Team), user) + return JoinUserToTeam(result.Data.(*model.Team), user, siteURL) } } -func AddUserToTeamByHash(userId string, hash string, data string) (*model.Team, *model.AppError) { +func AddUserToTeamByHash(userId string, hash string, data string, siteURL string) (*model.Team, *model.AppError) { props := model.MapFromJson(strings.NewReader(data)) if !model.ComparePassword(hash, fmt.Sprintf("%v:%v", data, utils.Cfg.EmailSettings.InviteSalt)) { @@ -199,14 +199,14 @@ func AddUserToTeamByHash(userId string, hash string, data string) (*model.Team, user = result.Data.(*model.User) } - if err := JoinUserToTeam(team, user); err != nil { + if err := JoinUserToTeam(team, user, siteURL); err != nil { return nil, err } return team, nil } -func AddUserToTeamByInviteId(inviteId string, userId string) (*model.Team, *model.AppError) { +func AddUserToTeamByInviteId(inviteId string, userId string, siteURL string) (*model.Team, *model.AppError) { tchan := Srv.Store.Team().GetByInviteId(inviteId) uchan := Srv.Store.User().Get(userId) @@ -224,7 +224,7 @@ func AddUserToTeamByInviteId(inviteId string, userId string) (*model.Team, *mode user = result.Data.(*model.User) } - if err := JoinUserToTeam(team, user); err != nil { + if err := JoinUserToTeam(team, user, siteURL); err != nil { return nil, err } @@ -269,7 +269,7 @@ func joinUserToTeam(team *model.Team, user *model.User) (bool, *model.AppError) return false, nil } -func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError { +func JoinUserToTeam(team *model.Team, user *model.User, siteURL string) *model.AppError { if alreadyAdded, err := joinUserToTeam(team, user); err != nil { return err @@ -284,7 +284,7 @@ func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError { } // Soft error if there is an issue joining the default channels - if err := JoinDefaultChannels(team.Id, user, channelRole); err != nil { + if err := JoinDefaultChannels(team.Id, user, channelRole, siteURL); err != nil { l4g.Error(utils.T("api.user.create_user.joining.error"), user.Id, team.Id, err) } diff --git a/app/team_test.go b/app/team_test.go index 64af0c4af..75f09f1ca 100644 --- a/app/team_test.go +++ b/app/team_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) func TestCreateTeam(t *testing.T) { @@ -42,12 +43,12 @@ func TestCreateTeamWithUser(t *testing.T) { Type: model.TEAM_OPEN, } - if _, err := CreateTeamWithUser(team, th.BasicUser.Id); err != nil { + if _, err := CreateTeamWithUser(team, th.BasicUser.Id, utils.GetSiteURL()); err != nil { t.Log(err) t.Fatal("Should create a new team with existing user") } - if _, err := CreateTeamWithUser(team, model.NewId()); err == nil { + if _, err := CreateTeamWithUser(team, model.NewId(), utils.GetSiteURL()); err == nil { t.Fatal("Should not create a new team - user does not exist") } @@ -63,7 +64,7 @@ func TestCreateTeamWithUser(t *testing.T) { } //Fail to create a team with user when user has set email without domain - if _, err := CreateTeamWithUser(team2, ruser.Id); err == nil { + if _, err := CreateTeamWithUser(team2, ruser.Id, utils.GetSiteURL()); err == nil { t.Log(err.Message) t.Fatal("Should not create a team with user when user has set email without domain") } else { @@ -95,7 +96,7 @@ func TestAddUserToTeam(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := CreateUser(&user) - if _, err := AddUserToTeam(th.BasicTeam.Id, ruser.Id); err != nil { + if _, err := AddUserToTeam(th.BasicTeam.Id, ruser.Id, utils.GetSiteURL()); err != nil { t.Log(err) t.Fatal("Should add user to the team") } @@ -107,7 +108,7 @@ func TestAddUserToTeamByTeamId(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := CreateUser(&user) - if err := AddUserToTeamByTeamId(th.BasicTeam.Id, ruser); err != nil { + if err := AddUserToTeamByTeamId(th.BasicTeam.Id, ruser, utils.GetSiteURL()); err != nil { t.Log(err) t.Fatal("Should add user to the team") } diff --git a/app/user.go b/app/user.go index d1ddceb23..d050e1524 100644 --- a/app/user.go +++ b/app/user.go @@ -29,7 +29,7 @@ import ( "github.com/mattermost/platform/utils" ) -func CreateUserWithHash(user *model.User, hash string, data string) (*model.User, *model.AppError) { +func CreateUserWithHash(user *model.User, hash string, data string, siteURL string) (*model.User, *model.AppError) { if err := IsUserSignUpAllowed(); err != nil { return nil, err } @@ -62,7 +62,7 @@ func CreateUserWithHash(user *model.User, hash string, data string) (*model.User return nil, err } - if err := JoinUserToTeam(team, ruser); err != nil { + if err := JoinUserToTeam(team, ruser, siteURL); err != nil { return nil, err } @@ -91,7 +91,7 @@ func CreateUserWithInviteId(user *model.User, inviteId string, siteURL string) ( return nil, err } - if err := JoinUserToTeam(team, ruser); err != nil { + if err := JoinUserToTeam(team, ruser, siteURL); err != nil { return nil, err } @@ -216,7 +216,7 @@ func createUser(user *model.User) (*model.User, *model.AppError) { } } -func CreateOAuthUser(service string, userData io.Reader, teamId string) (*model.User, *model.AppError) { +func CreateOAuthUser(service string, userData io.Reader, teamId string, siteURL string) (*model.User, *model.AppError) { if !utils.Cfg.TeamSettings.EnableUserCreation { return nil, model.NewAppError("CreateOAuthUser", "api.user.create_user.disabled.app_error", nil, "", http.StatusNotImplemented) } @@ -268,7 +268,7 @@ func CreateOAuthUser(service string, userData io.Reader, teamId string) (*model. } if len(teamId) > 0 { - err = AddUserToTeamByTeamId(teamId, user) + err = AddUserToTeamByTeamId(teamId, user, siteURL) if err != nil { return nil, err } diff --git a/app/user_test.go b/app/user_test.go index ec0e2b73c..d8d6bdc79 100644 --- a/app/user_test.go +++ b/app/user_test.go @@ -70,7 +70,7 @@ func TestCreateOAuthUser(t *testing.T) { glUser := oauthgitlab.GitLabUser{Id: int64(r.Intn(1000)), Username: "joram" + model.NewId(), Email: model.NewId() + "@simulator.amazonses.com", Name: "Joram Wilander"} json := glUser.ToJson() - user, err := CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id) + user, err := CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id, utils.GetSiteURL()) if err != nil { t.Fatal(err) } @@ -87,7 +87,7 @@ func TestCreateOAuthUser(t *testing.T) { }() utils.Cfg.TeamSettings.EnableUserCreation = false - _, err = CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id) + _, err = CreateOAuthUser(model.USER_AUTH_SERVICE_GITLAB, strings.NewReader(json), th.BasicTeam.Id, utils.GetSiteURL()) if err == nil { t.Fatal("should have failed - user creation disabled") } @@ -253,7 +253,7 @@ func createGitlabUser(t *testing.T, email string, username string) (*model.User, var user *model.User var err *model.AppError - if user, err = CreateOAuthUser("gitlab", bytes.NewReader(gitlabUser), ""); err != nil { + if user, err = CreateOAuthUser("gitlab", bytes.NewReader(gitlabUser), "", utils.GetSiteURL()); err != nil { t.Fatal("unable to create the user") } diff --git a/app/webhook.go b/app/webhook.go index 6f1cec4a8..f58b267b7 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -24,7 +24,7 @@ const ( TRIGGERWORDS_STARTSWITH = 1 ) -func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Channel, user *model.User) *model.AppError { +func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Channel, user *model.User, siteURL string) *model.AppError { if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks { return nil } @@ -107,7 +107,7 @@ func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Chan 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, hook.TeamId, post.ChannelId, text, respProps["username"], respProps["icon_url"], post.Props, post.Type, siteURL); err != nil { l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.create_post.error"), err) } } @@ -121,7 +121,7 @@ func handleWebhookEvents(post *model.Post, team *model.Team, channel *model.Chan return nil } -func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) { +func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string, siteURL string) (*model.Post, *model.AppError) { // parse links into Markdown format linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") @@ -188,7 +188,7 @@ func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overri } } - if _, err := CreatePost(post, teamId, false); err != nil { + if _, err := CreatePost(post, teamId, false, siteURL); err != nil { return nil, model.NewLocAppError("CreateWebhookPost", "api.post.create_webhook_post.creating.app_error", nil, "err="+err.Message) } @@ -427,7 +427,7 @@ func RegenOutgoingWebhookToken(hook *model.OutgoingWebhook) (*model.OutgoingWebh } } -func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *model.AppError { +func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest, siteURL string) *model.AppError { if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks { return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented) } @@ -518,7 +518,7 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *mo return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.permissions.app_error", nil, "", http.StatusForbidden) } - if _, err := CreateWebhookPost(hook.UserId, hook.TeamId, channel.Id, text, overrideUsername, overrideIconUrl, req.Props, webhookType); err != nil { + if _, err := CreateWebhookPost(hook.UserId, hook.TeamId, channel.Id, text, overrideUsername, overrideIconUrl, req.Props, webhookType, siteURL); err != nil { return err } -- cgit v1.2.3-1-g7c22 From 06a7c3ba8e054237c44e8b1586c76d00c1cffb67 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Fri, 10 Mar 2017 05:22:14 -0500 Subject: PLT-5800 Cleaned up duplicated code for adding slack attachments to posts (#5711) --- app/import.go | 33 ++------------------------------- app/slackimport.go | 39 ++++++++++++--------------------------- app/webhook.go | 35 +++-------------------------------- 3 files changed, 17 insertions(+), 90 deletions(-) (limited to 'app') diff --git a/app/import.go b/app/import.go index 4d0feb532..a5ed69843 100644 --- a/app/import.go +++ b/app/import.go @@ -915,37 +915,8 @@ func OldImportIncomingWebhookPost(post *model.Post, props model.StringInterface) if len(props) > 0 { for key, val := range props { if key == "attachments" { - if list, success := val.([]interface{}); success { - // parse attachment links into Markdown format - for i, aInt := range list { - attachment := aInt.(map[string]interface{}) - if aText, ok := attachment["text"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["text"] = aText - list[i] = attachment - } - if aText, ok := attachment["pretext"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["pretext"] = aText - list[i] = attachment - } - if fVal, ok := attachment["fields"]; ok { - if fields, ok := fVal.([]interface{}); ok { - // parse attachment field links into Markdown format - for j, fInt := range fields { - field := fInt.(map[string]interface{}) - if fValue, ok := field["value"].(string); ok { - fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") - field["value"] = fValue - fields[j] = field - } - } - attachment["fields"] = fields - list[i] = attachment - } - } - } - post.AddProp(key, list) + if attachments, success := val.([]*model.SlackAttachment); success { + parseSlackAttachment(post, attachments) } } else if key != "from_webhook" { post.AddProp(key, val) diff --git a/app/slackimport.go b/app/slackimport.go index ced0f6581..fcdd765bb 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -40,17 +40,17 @@ type SlackFile struct { } type SlackPost struct { - User string `json:"user"` - BotId string `json:"bot_id"` - BotUsername string `json:"username"` - Text string `json:"text"` - TimeStamp string `json:"ts"` - Type string `json:"type"` - SubType string `json:"subtype"` - Comment *SlackComment `json:"comment"` - Upload bool `json:"upload"` - File *SlackFile `json:"file"` - Attachments []SlackAttachment `json:"attachments"` + User string `json:"user"` + BotId string `json:"bot_id"` + BotUsername string `json:"username"` + Text string `json:"text"` + TimeStamp string `json:"ts"` + Type string `json:"type"` + SubType string `json:"subtype"` + Comment *SlackComment `json:"comment"` + Upload bool `json:"upload"` + File *SlackFile `json:"file"` + Attachments []*model.SlackAttachment `json:"attachments"` } type SlackComment struct { @@ -58,13 +58,6 @@ type SlackComment struct { Comment string `json:"comment"` } -type SlackAttachment struct { - Id int `json:"id"` - Text string `json:"text"` - Pretext string `json:"pretext"` - Fields []map[string]interface{} `json:"fields"` -} - func truncateRunes(s string, i int) string { runes := []rune(s) if len(runes) > i { @@ -284,15 +277,7 @@ func SlackAddPosts(teamId string, channel *model.Channel, posts []SlackPost, use props := make(model.StringInterface) props["override_username"] = sPost.BotUsername if len(sPost.Attachments) > 0 { - var mAttachments []interface{} - for _, attachment := range sPost.Attachments { - mAttachments = append(mAttachments, map[string]interface{}{ - "text": attachment.Text, - "pretext": attachment.Pretext, - "fields": attachment.Fields, - }) - } - props["attachments"] = mAttachments + props["attachments"] = sPost.Attachments } post := &model.Post{ diff --git a/app/webhook.go b/app/webhook.go index f58b267b7..98e1080b6 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -150,37 +150,8 @@ func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overri if len(props) > 0 { for key, val := range props { if key == "attachments" { - if list, success := val.([]interface{}); success { - // parse attachment links into Markdown format - for i, aInt := range list { - attachment := aInt.(map[string]interface{}) - if aText, ok := attachment["text"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["text"] = aText - list[i] = attachment - } - if aText, ok := attachment["pretext"].(string); ok { - aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") - attachment["pretext"] = aText - list[i] = attachment - } - if fVal, ok := attachment["fields"]; ok { - if fields, ok := fVal.([]interface{}); ok { - // parse attachment field links into Markdown format - for j, fInt := range fields { - field := fInt.(map[string]interface{}) - if fValue, ok := field["value"].(string); ok { - fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") - field["value"] = fValue - fields[j] = field - } - } - attachment["fields"] = fields - list[i] = attachment - } - } - } - post.AddProp(key, list) + if attachments, success := val.([]*model.SlackAttachment); success { + parseSlackAttachment(post, attachments) } } else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" { post.AddProp(key, val) @@ -452,7 +423,7 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest, sit webhookType := req.Type // attachments is in here for slack compatibility - if req.Attachments != nil { + if len(req.Attachments) > 0 { if len(req.Props) == 0 { req.Props = make(model.StringInterface) } -- cgit v1.2.3-1-g7c22 From ca90fe5cbe43df68a2690077b86377f5823f3284 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 13 Mar 2017 13:20:21 -0400 Subject: Fix permalink in email for DMs/GMs (#5751) --- app/notification.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app') diff --git a/app/notification.go b/app/notification.go index 67959c6bc..7b61de9ac 100644 --- a/app/notification.go +++ b/app/notification.go @@ -318,8 +318,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe } func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Channel, team *model.Team, senderName string, sender *model.User, siteURL string) *model.AppError { - if channel.IsGroupOrDirect() && channel.TeamId != team.Id { - // this message is a cross-team DM/GM so we need to find a team that the recipient is on to use in the link + if channel.IsGroupOrDirect() { if result := <-Srv.Store.Team().GetTeamsByUserId(user.Id); result.Err != nil { return result.Err } else { -- cgit v1.2.3-1-g7c22