github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/reaction.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "net/http" 8 9 "github.com/mattermost/mattermost-server/model" 10 ) 11 12 func (a *App) SaveReactionForPost(reaction *model.Reaction) (*model.Reaction, *model.AppError) { 13 post, err := a.GetSinglePost(reaction.PostId) 14 if err != nil { 15 return nil, err 16 } 17 18 channel, err := a.GetChannel(post.ChannelId) 19 if err != nil { 20 return nil, err 21 } 22 23 if channel.DeleteAt > 0 { 24 return nil, model.NewAppError("deleteReactionForPost", "api.reaction.save.archived_channel.app_error", nil, "", http.StatusForbidden) 25 } 26 27 if a.License() != nil && *a.Config().TeamSettings.ExperimentalTownSquareIsReadOnly && channel.Name == model.DEFAULT_CHANNEL { 28 user, err := a.GetUser(reaction.UserId) 29 if err != nil { 30 return nil, err 31 } 32 33 if !a.RolesGrantPermission(user.GetRoles(), model.PERMISSION_MANAGE_SYSTEM.Id) { 34 return nil, model.NewAppError("saveReactionForPost", "api.reaction.town_square_read_only", nil, "", http.StatusForbidden) 35 } 36 } 37 38 result := <-a.Srv.Store.Reaction().Save(reaction) 39 if result.Err != nil { 40 return nil, result.Err 41 } 42 43 reaction = result.Data.(*model.Reaction) 44 45 a.Go(func() { 46 a.sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_ADDED, reaction, post, true) 47 }) 48 49 return reaction, nil 50 } 51 52 func (a *App) GetReactionsForPost(postId string) ([]*model.Reaction, *model.AppError) { 53 result := <-a.Srv.Store.Reaction().GetForPost(postId, true) 54 if result.Err != nil { 55 return nil, result.Err 56 } 57 return result.Data.([]*model.Reaction), nil 58 } 59 60 func (a *App) DeleteReactionForPost(reaction *model.Reaction) *model.AppError { 61 post, err := a.GetSinglePost(reaction.PostId) 62 if err != nil { 63 return err 64 } 65 66 channel, err := a.GetChannel(post.ChannelId) 67 if err != nil { 68 return err 69 } 70 71 if channel.DeleteAt > 0 { 72 return model.NewAppError("deleteReactionForPost", "api.reaction.delete.archived_channel.app_error", nil, "", http.StatusForbidden) 73 } 74 75 if a.License() != nil && *a.Config().TeamSettings.ExperimentalTownSquareIsReadOnly && channel.Name == model.DEFAULT_CHANNEL { 76 user, err := a.GetUser(reaction.UserId) 77 if err != nil { 78 return err 79 } 80 81 if !a.RolesGrantPermission(user.GetRoles(), model.PERMISSION_MANAGE_SYSTEM.Id) { 82 return model.NewAppError("deleteReactionForPost", "api.reaction.town_square_read_only", nil, "", http.StatusForbidden) 83 } 84 } 85 86 hasReactions := true 87 if reactions, _ := a.GetReactionsForPost(post.Id); len(reactions) <= 1 { 88 hasReactions = false 89 } 90 91 if result := <-a.Srv.Store.Reaction().Delete(reaction); result.Err != nil { 92 return result.Err 93 } 94 95 a.Go(func() { 96 a.sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_REMOVED, reaction, post, hasReactions) 97 }) 98 99 return nil 100 } 101 102 func (a *App) sendReactionEvent(event string, reaction *model.Reaction, post *model.Post, hasReactions bool) { 103 // send out that a reaction has been added/removed 104 message := model.NewWebSocketEvent(event, "", post.ChannelId, "", nil) 105 message.Add("reaction", reaction.ToJson()) 106 a.Publish(message) 107 108 // The post is always modified since the UpdateAt always changes 109 a.InvalidateCacheForChannelPosts(post.ChannelId) 110 post.HasReactions = hasReactions 111 post.UpdateAt = model.GetMillis() 112 umessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", post.ChannelId, "", nil) 113 umessage.Add("post", a.PostWithProxyAddedToImageURLs(post).ToJson()) 114 a.Publish(umessage) 115 }