-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotification_topic_conv_chat_message.go
93 lines (83 loc) · 2.76 KB
/
notification_topic_conv_chat_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gcloudcx
import (
"encoding/json"
"time"
"github.com/gildas/go-errors"
"github.com/google/uuid"
)
// ConversationChatMessageTopic describes a Topic about User's Presence
type ConversationChatMessageTopic struct {
ID uuid.UUID
Name string
ConversationID uuid.UUID
Sender *ChatMember
Type string // message, typing-indicator,
Body string
BodyType string // standard,
TimeStamp time.Time
CorrelationID string
Targets []Identifiable
}
func init() {
notificationTopicRegistry.Add(ConversationChatMessageTopic{})
}
// GetType returns the type of this topic
//
// implements core.TypeCarrier
func (topic ConversationChatMessageTopic) GetType() string {
return "v2.conversations.chats.{id}.messages"
}
// GetTargets returns the targets of this topic
func (topic ConversationChatMessageTopic) GetTargets() []Identifiable {
return topic.Targets
}
// With creates a new NotificationTopic with the given targets
func (topic ConversationChatMessageTopic) With(targets ...Identifiable) NotificationTopic {
newTopic := topic
newTopic.Targets = targets
return newTopic
}
// TopicNameWith builds the topicName for the given identifiables
func (topic ConversationChatMessageTopic) TopicNameWith(identifiables ...Identifiable) string {
return topicNameWith(topic, identifiables...)
}
// String gets a string version
//
// implements the fmt.Stringer interface
func (topic ConversationChatMessageTopic) String() string {
if len(topic.Targets) == 0 {
return topic.GetType()
}
return topicNameWith(topic, topic.Targets...)
}
// UnmarshalJSON unmarshals JSON into this
func (topic *ConversationChatMessageTopic) UnmarshalJSON(payload []byte) (err error) {
var inner struct {
TopicName string `json:"topicName"`
EventBody struct {
ID uuid.UUID `json:"id,omitempty"`
Conversation EntityRef `json:"conversation,omitempty"`
Sender *ChatMember `json:"sender,omitempty"`
Body string `json:"body,omitempty"`
BodyType string `json:"bodyType,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
} `json:"eventBody"`
Metadata struct {
CorrelationID string `json:"correlationId,omitempty"`
Type string `json:"type,omitempty"`
} `json:"metadata,omitempty"`
Version string `json:"version"` // all
}
if err = json.Unmarshal(payload, &inner); err != nil {
return errors.JSONUnmarshalError.Wrap(err)
}
topic.Name = inner.TopicName
topic.Type = inner.Metadata.Type
topic.ConversationID = inner.EventBody.Conversation.ID
topic.Sender = inner.EventBody.Sender
topic.BodyType = inner.EventBody.BodyType
topic.Body = inner.EventBody.Body
topic.TimeStamp = inner.EventBody.Timestamp
topic.CorrelationID = inner.Metadata.CorrelationID
return
}