Skip to content

Commit

Permalink
PLT-5745 Cleaning up duplicate calls (mattermost#7298)
Browse files Browse the repository at this point in the history
* PLT-5745 Cleaning up duplicate calls

* Fixing missing calls

* Fixing broken test
  • Loading branch information
coreyhulen authored and hmhealey committed Aug 28, 2017
1 parent a12cf54 commit 1709b94
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 79 deletions.
6 changes: 3 additions & 3 deletions api/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api4/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/apptestlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 17 additions & 17 deletions app/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -644,15 +644,15 @@ 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())
}
}

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 {
Expand All @@ -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,
Expand All @@ -680,15 +680,15 @@ 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())
}
}

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 {
Expand All @@ -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,
Expand All @@ -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())
}
}
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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())
}

Expand All @@ -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())
}

Expand All @@ -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())
}

Expand Down
2 changes: 1 addition & 1 deletion app/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/command_echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
Expand Down
4 changes: 2 additions & 2 deletions app/command_loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion app/command_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}
Expand Down
18 changes: 6 additions & 12 deletions app/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions app/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,35 +42,35 @@ 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)
}

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)
}
Expand Down
2 changes: 1 addition & 1 deletion app/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/plugin/jira/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions app/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 1709b94

Please sign in to comment.