Message pin/reaction events are now sent standalone (w/o message obj)

This commit is contained in:
Denis Arh
2018-11-02 18:19:45 +01:00
parent 242408e83e
commit 67c1a47b16
6 changed files with 126 additions and 14 deletions
+34 -4
View File
@@ -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),
+41 -7
View File
@@ -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})
}
+5
View File
@@ -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"`
+25
View File
@@ -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
+18 -3
View File
@@ -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{}
+3
View File
@@ -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