Debian Codegraph @

event/relations.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package event
8
9 import (
10 "encoding/json"
11
12 "maunium.net/go/mautrix/id"
13 )
14
15 type RelationType string
16
17 const (
18 RelReplace RelationType = "m.replace"
19 RelReference RelationType = "m.reference"
20 RelAnnotation RelationType = "m.annotation"
21 RelReply RelationType = "net.maunium.reply"
22 )
23
24 type RelatesTo struct {
25 Type RelationType
26 EventID id.EventID
27 Key string
28 }
29
30 type serializableInReplyTo struct {
31 EventID id.EventID `json:"event_id,omitempty"`
32 }
33
34 type serializableRelatesTo struct {
35 InReplyTo *serializableInReplyTo `json:"m.in_reply_to,omitempty"`
36
37 Type RelationType `json:"rel_type,omitempty"`
38 EventID id.EventID `json:"event_id,omitempty"`
39 Key string `json:"key,omitempty"`
40 }
41
42 func (rel *RelatesTo) GetReplaceID() id.EventID {
43 if rel.Type == RelReplace {
44 return rel.EventID
45 }
46 return ""
47 }
48
49 func (rel *RelatesTo) GetReferenceID() id.EventID {
50 if rel.Type == RelReference {
51 return rel.EventID
52 }
53 return ""
54 }
55
56 func (rel *RelatesTo) GetReplyID() id.EventID {
57 if rel.Type == RelReply {
58 return rel.EventID
59 }
60 return ""
61 }
62
63 func (rel *RelatesTo) GetAnnotationID() id.EventID {
64 if rel.Type == RelAnnotation {
65 return rel.EventID
66 }
67 return ""
68 }
69
70 func (rel *RelatesTo) GetAnnotationKey() string {
71 if rel.Type == RelAnnotation {
72 return rel.Key
73 }
74 return ""
75 }
76
77 func (rel *RelatesTo) UnmarshalJSON(data []byte) error {
78 var srel serializableRelatesTo
79 if err := json.Unmarshal(data, &srel); err != nil {
80 return err
81 }
82 if len(srel.Type) > 0 {
83 rel.Type = srel.Type
84 rel.EventID = srel.EventID
85 rel.Key = srel.Key
86 } else if srel.InReplyTo != nil && len(srel.InReplyTo.EventID) > 0 {
87 rel.Type = RelReply
88 rel.EventID = srel.InReplyTo.EventID
89 rel.Key = ""
90 }
91 return nil
92 }
93
94 func (rel *RelatesTo) MarshalJSON() ([]byte, error) {
95 srel := serializableRelatesTo{Type: rel.Type, EventID: rel.EventID, Key: rel.Key}
96 if rel.Type == RelReply {
97 srel.InReplyTo = &serializableInReplyTo{rel.EventID}
98 }
99 return json.Marshal(&srel)
100 }
101
102 type RelationChunkItem struct {
103 Type RelationType `json:"type"`
104 EventID string `json:"event_id,omitempty"`
105 Key string `json:"key,omitempty"`
106 Count int `json:"count,omitempty"`
107 }
108
109 type RelationChunk struct {
110 Chunk []RelationChunkItem `json:"chunk"`
111
112 Limited bool `json:"limited"`
113 Count int `json:"count"`
114 }
115
116 type AnnotationChunk struct {
117 RelationChunk
118 Map map[string]int `json:"-"`
119 }
120
121 type serializableAnnotationChunk AnnotationChunk
122
123 func (ac *AnnotationChunk) UnmarshalJSON(data []byte) error {
124 if err := json.Unmarshal(data, (*serializableAnnotationChunk)(ac)); err != nil {
125 return err
126 }
127 ac.Map = make(map[string]int)
128 for _, item := range ac.Chunk {
129 ac.Map[item.Key] += item.Count
130 }
131 return nil
132 }
133
134 func (ac *AnnotationChunk) Serialize() RelationChunk {
135 ac.Chunk = make([]RelationChunkItem, len(ac.Map))
136 i := 0
137 for key, count := range ac.Map {
138 ac.Chunk[i] = RelationChunkItem{
139 Type: RelAnnotation,
140 Key: key,
141 Count: count,
142 }
143 }
144 return ac.RelationChunk
145 }
146
147 type EventIDChunk struct {
148 RelationChunk
149 List []string `json:"-"`
150 }
151
152 type serializableEventIDChunk EventIDChunk
153
154 func (ec *EventIDChunk) UnmarshalJSON(data []byte) error {
155 if err := json.Unmarshal(data, (*serializableEventIDChunk)(ec)); err != nil {
156 return err
157 }
158 for _, item := range ec.Chunk {
159 ec.List = append(ec.List, item.EventID)
160 }
161 return nil
162 }
163
164 func (ec *EventIDChunk) Serialize(typ RelationType) RelationChunk {
165 ec.Chunk = make([]RelationChunkItem, len(ec.List))
166 for i, eventID := range ec.List {
167 ec.Chunk[i] = RelationChunkItem{
168 Type: typ,
169 EventID: eventID,
170 }
171 }
172 return ec.RelationChunk
173 }
174
175 type Relations struct {
176 Raw map[RelationType]RelationChunk `json:"-"`
177
178 Annotations AnnotationChunk `json:"m.annotation,omitempty"`
179 References EventIDChunk `json:"m.reference,omitempty"`
180 Replaces EventIDChunk `json:"m.replace,omitempty"`
181 }
182
183 type serializableRelations Relations
184
185 func (relations *Relations) UnmarshalJSON(data []byte) error {
186 if err := json.Unmarshal(data, &relations.Raw); err != nil {
187 return err
188 }
189 return json.Unmarshal(data, (*serializableRelations)(relations))
190 }
191
192 func (relations *Relations) MarshalJSON() ([]byte, error) {
193 if relations.Raw == nil {
194 relations.Raw = make(map[RelationType]RelationChunk)
195 }
196 relations.Raw[RelAnnotation] = relations.Annotations.Serialize()
197 relations.Raw[RelReference] = relations.References.Serialize(RelReference)
198 relations.Raw[RelReplace] = relations.Replaces.Serialize(RelReplace)
199 return json.Marshal(relations.Raw)
200 }