From 67c1a47b16e823762663ae90457342543c794ab5 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Fri, 2 Nov 2018 18:19:45 +0100 Subject: [PATCH] Message pin/reaction events are now sent standalone (w/o message obj) --- internal/payload/outgoing.go | 38 +++++++++++++++++++--- internal/payload/outgoing/message.go | 48 ++++++++++++++++++++++++---- internal/payload/outgoing/payload.go | 5 +++ sam/service/events.go | 25 +++++++++++++++ sam/service/message.go | 21 ++++++++++-- sam/types/message_flag.go | 3 ++ 6 files changed, 126 insertions(+), 14 deletions(-) diff --git a/internal/payload/outgoing.go b/internal/payload/outgoing.go index ef7627041..ef0fe56c9 100644 --- a/internal/payload/outgoing.go +++ b/internal/payload/outgoing.go @@ -31,7 +31,7 @@ func Message(ctx context.Context, msg *samTypes.Message) *outgoing.Message { User: User(msg.User), Attachment: Attachment(msg.Attachment), - Reactions: MessageReactions(msg.Flags), + Reactions: messageReactionSumSet(msg.Flags), IsPinned: msg.Flags.IsPinned(), IsBookmarked: msg.Flags.IsBookmarked(currentUserID), @@ -54,9 +54,9 @@ func Messages(ctx context.Context, msg samTypes.MessageSet) *outgoing.MessageSet return &retval } -func MessageReactions(flags samTypes.MessageFlagSet) outgoing.ReactionSet { +func messageReactionSumSet(flags samTypes.MessageFlagSet) outgoing.MessageReactionSumSet { var ( - rr = make([]*outgoing.Reaction, 0) + rr = make([]*outgoing.MessageReactionSum, 0) rIndex = map[string]int{} has bool i int @@ -64,7 +64,7 @@ func MessageReactions(flags samTypes.MessageFlagSet) outgoing.ReactionSet { _ = flags.Walk(func(flag *samTypes.MessageFlag) error { if flag.IsReaction() { - r := &outgoing.Reaction{Reaction: flag.Flag, UserIDs: []string{}, Count: 0} + r := &outgoing.MessageReactionSum{Reaction: flag.Flag, UserIDs: []string{}, Count: 0} if i, has = rIndex[flag.Flag]; !has { i, rIndex[flag.Flag] = len(rr), len(rr) @@ -81,6 +81,36 @@ func MessageReactions(flags samTypes.MessageFlagSet) outgoing.ReactionSet { return rr } +func MessageReaction(f *samTypes.MessageFlag) *outgoing.MessageReaction { + return &outgoing.MessageReaction{ + UserID: f.UserID, + MessageID: f.MessageID, + Reaction: f.Flag, + } +} + +func MessageReactionRemoved(f *samTypes.MessageFlag) *outgoing.MessageReactionRemoved { + return &outgoing.MessageReactionRemoved{ + UserID: f.UserID, + MessageID: f.MessageID, + Reaction: f.Flag, + } +} + +func MessagePin(f *samTypes.MessageFlag) *outgoing.MessagePin { + return &outgoing.MessagePin{ + UserID: f.UserID, + MessageID: f.MessageID, + } +} + +func MessagePinRemoved(f *samTypes.MessageFlag) *outgoing.MessagePinRemoved { + return &outgoing.MessagePinRemoved{ + UserID: f.UserID, + MessageID: f.MessageID, + } +} + func Channel(ch *samTypes.Channel) *outgoing.Channel { return &outgoing.Channel{ ID: Uint64toa(ch.ID), diff --git a/internal/payload/outgoing/message.go b/internal/payload/outgoing/message.go index eaaafc43f..41764e0c0 100644 --- a/internal/payload/outgoing/message.go +++ b/internal/payload/outgoing/message.go @@ -14,11 +14,11 @@ type ( ReplyTo uint64 `json:"replyTo,omitempty,string"` Replies uint `json:"replies,omitempty"` - User *User `json:"user"` - Attachment *Attachment `json:"att,omitempty"` - Reactions ReactionSet `json:"reactions,omitempty"` - IsBookmarked bool `json:"isBookmarked"` - IsPinned bool `json:"isPinned"` + User *User `json:"user"` + Attachment *Attachment `json:"att,omitempty"` + Reactions MessageReactionSumSet `json:"reactions,omitempty"` + IsBookmarked bool `json:"isBookmarked"` + IsPinned bool `json:"isPinned"` CanReply bool `json:"canReply"` CanEdit bool `json:"canEdit"` @@ -31,13 +31,31 @@ type ( MessageSet []*Message - Reaction struct { + // Used for single reaction event notification + MessageReactionSum struct { UserIDs []string `json:"userIDs"` Reaction string `json:"reaction"` Count uint `json:"count"` } - ReactionSet []*Reaction + MessageReactionSumSet []*MessageReactionSum + + // Used for single reaction event notification + MessageReaction struct { + MessageID uint64 `json:"messageID,string"` + UserID uint64 `json:"userID,string"` + Reaction string `json:"reaction"` + } + + MessageReactionRemoved MessageReaction + + // Used for single pin/unpin event notification + MessagePin struct { + MessageID uint64 `json:"messageID,string"` + UserID uint64 `json:"userID,string"` + } + + MessagePinRemoved MessagePin ) func (p *Message) EncodeMessage() ([]byte, error) { @@ -47,3 +65,19 @@ func (p *Message) EncodeMessage() ([]byte, error) { func (p *MessageSet) EncodeMessage() ([]byte, error) { return json.Marshal(Payload{MessageSet: p}) } + +func (p *MessageReaction) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{MessageReaction: p}) +} + +func (p *MessageReactionRemoved) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{MessageReactionRemoved: p}) +} + +func (p *MessagePin) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{MessagePin: p}) +} + +func (p *MessagePinRemoved) EncodeMessage() ([]byte, error) { + return json.Marshal(Payload{MessagePinRemoved: p}) +} diff --git a/internal/payload/outgoing/payload.go b/internal/payload/outgoing/payload.go index 92da9df72..c31c7a5ee 100644 --- a/internal/payload/outgoing/payload.go +++ b/internal/payload/outgoing/payload.go @@ -10,6 +10,11 @@ type ( *Message `json:"message,omitempty"` *MessageSet `json:"messages,omitempty"` + *MessageReaction `json:"messageReaction,omitempty"` + *MessageReactionRemoved `json:"messageReactionRemoved,omitempty"` + *MessagePin `json:"messagePin,omitempty"` + *MessagePinRemoved `json:"messagePinRemoved,omitempty"` + *ChannelJoin `json:"channelJoin,omitempty"` *ChannelPart `json:"channelPart,omitempty"` *Channel `json:"channel,omitempty"` diff --git a/sam/service/events.go b/sam/service/events.go index f777ba5ae..c0395c7bb 100644 --- a/sam/service/events.go +++ b/sam/service/events.go @@ -19,6 +19,7 @@ type ( EventService interface { With(ctx context.Context) EventService Message(m *types.Message) error + MessageFlag(m *types.MessageFlag) error Channel(m *types.Channel) error Join(userID, channelID uint64) error Part(userID, channelID uint64) error @@ -42,6 +43,30 @@ func (svc *event) Message(m *types.Message) error { return svc.push(payload.Message(svc.ctx, m), types.EventQueueItemSubTypeChannel, m.ChannelID) } +// MessageFlag sends message flag events to subscribers +func (svc *event) MessageFlag(f *types.MessageFlag) error { + var p outgoing.MessageEncoder + + switch true { + case f.IsBookmark(): + // Leaving this here so it is obvious. + return nil + case f.IsPin() && f.DeletedAt != nil: + p = payload.MessagePinRemoved(f) + case f.IsReaction() && f.DeletedAt != nil: + p = payload.MessageReactionRemoved(f) + case f.IsPin(): + p = payload.MessagePin(f) + case f.IsReaction(): + p = payload.MessageReaction(f) + default: + return nil + } + + return svc.push(p, types.EventQueueItemSubTypeChannel, f.ChannelID) + return nil +} + // Channel notifies subscribers about channel change // // If this is a public channel we notify everyone diff --git a/sam/service/message.go b/sam/service/message.go index cfe5185e4..e9721ced9 100644 --- a/sam/service/message.go +++ b/sam/service/message.go @@ -8,7 +8,6 @@ import ( authService "github.com/crusttech/crust/auth/service" authTypes "github.com/crusttech/crust/auth/types" - "github.com/crusttech/crust/sam/repository" "github.com/crusttech/crust/sam/types" ) @@ -363,8 +362,9 @@ func (svc *message) flag(messageID uint64, flag string, remove bool) error { if remove { err = svc.mflag.DeleteByID(f.ID) + f.DeletedAt = timeNowPtr() } else { - _, err = svc.mflag.Create(&types.MessageFlag{ + f, err = svc.mflag.Create(&types.MessageFlag{ UserID: currentUserID, ChannelID: msg.ChannelID, MessageID: msg.ID, @@ -372,7 +372,11 @@ func (svc *message) flag(messageID uint64, flag string, remove bool) error { }) } - svc.sendEvent(msg) + if err != nil { + return err + } + + svc.sendFlagEvent(f) return }) @@ -485,4 +489,15 @@ func (svc *message) sendEvent(mm ...*types.Message) (err error) { return } +// Sends message to event loop +func (svc *message) sendFlagEvent(ff ...*types.MessageFlag) (err error) { + for _, f := range ff { + if err = svc.evl.MessageFlag(f); err != nil { + return + } + } + + return +} + var _ MessageService = &message{} diff --git a/sam/types/message_flag.go b/sam/types/message_flag.go index 2b8ea5577..ce43fa362 100644 --- a/sam/types/message_flag.go +++ b/sam/types/message_flag.go @@ -12,6 +12,9 @@ type ( ChannelID uint64 `json:"channelId" db:"rel_channel"` Flag string `json:"flag" db:"flag"` CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"` + + // Internal only + DeletedAt *time.Time `json:"-" db:"-"` } MessageFlagSet []*MessageFlag