Skip to content

Commit

Permalink
Fixing emoji reactions and Aurora read replica issue (#5497)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhulen committed Feb 22, 2017
1 parent d9de220 commit 3b8ed35
Showing 1 changed file with 22 additions and 42 deletions.
64 changes: 22 additions & 42 deletions api/reaction.go
Expand Up @@ -50,29 +50,24 @@ func saveReaction(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

pchan := Srv.Store.Post().Get(reaction.PostId)
var post *model.Post

var postHadReactions bool
if result := <-pchan; result.Err != nil {
if result := <-Srv.Store.Post().Get(reaction.PostId); result.Err != nil {
c.Err = result.Err
return
} else if post := result.Data.(*model.PostList).Posts[postId]; post.ChannelId != channelId {
} else if post = result.Data.(*model.PostList).Posts[postId]; post.ChannelId != channelId {
c.Err = model.NewLocAppError("saveReaction", "api.reaction.save_reaction.mismatched_channel_id.app_error",
nil, "channelId="+channelId+", post.ChannelId="+post.ChannelId+", postId="+postId)
c.Err.StatusCode = http.StatusBadRequest
return
} else {
postHadReactions = post.HasReactions
}

if result := <-Srv.Store.Reaction().Save(reaction); result.Err != nil {
c.Err = result.Err
return
} else {
go sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_ADDED, channelId, reaction, postHadReactions)

go sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_ADDED, channelId, reaction, post)
reaction := result.Data.(*model.Reaction)

w.Write([]byte(reaction.ToJson()))
}
}
Expand Down Expand Up @@ -108,57 +103,42 @@ func deleteReaction(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

pchan := Srv.Store.Post().Get(reaction.PostId)
var post *model.Post

var postHadReactions bool
if result := <-pchan; result.Err != nil {
if result := <-Srv.Store.Post().Get(reaction.PostId); result.Err != nil {
c.Err = result.Err
return
} else if post := result.Data.(*model.PostList).Posts[postId]; post.ChannelId != channelId {
} else if post = result.Data.(*model.PostList).Posts[postId]; post.ChannelId != channelId {
c.Err = model.NewLocAppError("deleteReaction", "api.reaction.delete_reaction.mismatched_channel_id.app_error",
nil, "channelId="+channelId+", post.ChannelId="+post.ChannelId+", postId="+postId)
c.Err.StatusCode = http.StatusBadRequest
return
} else {
postHadReactions = post.HasReactions
}

if result := <-Srv.Store.Reaction().Delete(reaction); result.Err != nil {
c.Err = result.Err
return
} else {
go sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_REMOVED, channelId, reaction, postHadReactions)

go sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_REMOVED, channelId, reaction, post)
ReturnStatusOK(w)
}
}

func sendReactionEvent(event string, channelId string, reaction *model.Reaction, postHadReactions bool) {
func sendReactionEvent(event string, channelId string, reaction *model.Reaction, post *model.Post) {
// send out that a reaction has been added/removed
go func() {
message := model.NewWebSocketEvent(event, "", channelId, "", nil)
message.Add("reaction", reaction.ToJson())

Publish(message)
}()

// send out that a post was updated if post.HasReactions has changed
go func() {
var post *model.Post
if result := <-Srv.Store.Post().Get(reaction.PostId); result.Err != nil {
l4g.Warn(utils.T("api.reaction.send_reaction_event.post.app_error"))
return
} else {
post = result.Data.(*model.PostList).Posts[reaction.PostId]
}

if post.HasReactions != postHadReactions {
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", channelId, "", nil)
message.Add("post", post.ToJson())

Publish(message)
}
}()

message := model.NewWebSocketEvent(event, "", channelId, "", nil)
message.Add("reaction", reaction.ToJson())
Publish(message)

// THe post is always modified since the UpdateAt always changes
InvalidateCacheForChannelPosts(post.ChannelId)
post.HasReactions = true
post.UpdateAt = model.GetMillis()
umessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", channelId, "", nil)
umessage.Add("post", post.ToJson())
Publish(umessage)

}

func listReactions(c *Context, w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 3b8ed35

Please sign in to comment.