github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	"github.com/mattermost/mattermost-server/model"
     8  )
     9  
    10  func (a *App) SaveReactionForPost(reaction *model.Reaction) (*model.Reaction, *model.AppError) {
    11  	post, err := a.GetSinglePost(reaction.PostId)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  
    16  	if result := <-a.Srv.Store.Reaction().Save(reaction); result.Err != nil {
    17  		return nil, result.Err
    18  	} else {
    19  		reaction = result.Data.(*model.Reaction)
    20  
    21  		a.Go(func() {
    22  			a.sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_ADDED, reaction, post)
    23  		})
    24  
    25  		return reaction, nil
    26  	}
    27  }
    28  
    29  func (a *App) GetReactionsForPost(postId string) ([]*model.Reaction, *model.AppError) {
    30  	if result := <-a.Srv.Store.Reaction().GetForPost(postId, true); result.Err != nil {
    31  		return nil, result.Err
    32  	} else {
    33  		return result.Data.([]*model.Reaction), nil
    34  	}
    35  }
    36  
    37  func (a *App) DeleteReactionForPost(reaction *model.Reaction) *model.AppError {
    38  	post, err := a.GetSinglePost(reaction.PostId)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	if result := <-a.Srv.Store.Reaction().Delete(reaction); result.Err != nil {
    44  		return result.Err
    45  	} else {
    46  		a.Go(func() {
    47  			a.sendReactionEvent(model.WEBSOCKET_EVENT_REACTION_REMOVED, reaction, post)
    48  		})
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func (a *App) sendReactionEvent(event string, reaction *model.Reaction, post *model.Post) {
    55  	// send out that a reaction has been added/removed
    56  	message := model.NewWebSocketEvent(event, "", post.ChannelId, "", nil)
    57  	message.Add("reaction", reaction.ToJson())
    58  	a.Publish(message)
    59  
    60  	// The post is always modified since the UpdateAt always changes
    61  	a.InvalidateCacheForChannelPosts(post.ChannelId)
    62  	post.HasReactions = true
    63  	post.UpdateAt = model.GetMillis()
    64  	umessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", post.ChannelId, "", nil)
    65  	umessage.Add("post", a.PostWithProxyAddedToImageURLs(post).ToJson())
    66  	a.Publish(umessage)
    67  }