summary refs log tree commit diff
path: root/vendor/maunium.net/go/mautrix/event/relations.go
blob: ea40cc066e879440c38c685b2aac8f5f42dd0d30 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2020 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package event

import (
	"encoding/json"

	"maunium.net/go/mautrix/id"
)

type RelationType string

const (
	RelReplace    RelationType = "m.replace"
	RelReference  RelationType = "m.reference"
	RelAnnotation RelationType = "m.annotation"
	RelThread     RelationType = "m.thread"
)

type RelatesTo struct {
	Type    RelationType `json:"rel_type,omitempty"`
	EventID id.EventID   `json:"event_id,omitempty"`
	Key     string       `json:"key,omitempty"`

	InReplyTo     *InReplyTo `json:"m.in_reply_to,omitempty"`
	IsFallingBack bool       `json:"is_falling_back,omitempty"`
}

type InReplyTo struct {
	EventID id.EventID `json:"event_id,omitempty"`

	UnstableRoomID id.RoomID `json:"room_id,omitempty"`
}

func (rel *RelatesTo) Copy() *RelatesTo {
	if rel == nil {
		return nil
	}
	cp := *rel
	return &cp
}

func (rel *RelatesTo) GetReplaceID() id.EventID {
	if rel != nil && rel.Type == RelReplace {
		return rel.EventID
	}
	return ""
}

func (rel *RelatesTo) GetReferenceID() id.EventID {
	if rel != nil && rel.Type == RelReference {
		return rel.EventID
	}
	return ""
}

func (rel *RelatesTo) GetThreadParent() id.EventID {
	if rel != nil && rel.Type == RelThread {
		return rel.EventID
	}
	return ""
}

func (rel *RelatesTo) GetReplyTo() id.EventID {
	if rel != nil && rel.InReplyTo != nil {
		return rel.InReplyTo.EventID
	}
	return ""
}

func (rel *RelatesTo) GetNonFallbackReplyTo() id.EventID {
	if rel != nil && rel.InReplyTo != nil && (rel.Type != RelThread || !rel.IsFallingBack) {
		return rel.InReplyTo.EventID
	}
	return ""
}

func (rel *RelatesTo) GetAnnotationID() id.EventID {
	if rel != nil && rel.Type == RelAnnotation {
		return rel.EventID
	}
	return ""
}

func (rel *RelatesTo) GetAnnotationKey() string {
	if rel != nil && rel.Type == RelAnnotation {
		return rel.Key
	}
	return ""
}

func (rel *RelatesTo) SetReplace(mxid id.EventID) *RelatesTo {
	rel.Type = RelReplace
	rel.EventID = mxid
	return rel
}

func (rel *RelatesTo) SetReplyTo(mxid id.EventID) *RelatesTo {
	rel.InReplyTo = &InReplyTo{EventID: mxid}
	rel.IsFallingBack = false
	return rel
}

func (rel *RelatesTo) SetThread(mxid, fallback id.EventID) *RelatesTo {
	rel.Type = RelThread
	rel.EventID = mxid
	if fallback != "" && rel.GetReplyTo() == "" {
		rel.SetReplyTo(fallback)
		rel.IsFallingBack = true
	}
	return rel
}

func (rel *RelatesTo) SetAnnotation(mxid id.EventID, key string) *RelatesTo {
	rel.Type = RelAnnotation
	rel.EventID = mxid
	rel.Key = key
	return rel
}

type RelationChunkItem struct {
	Type    RelationType `json:"type"`
	EventID string       `json:"event_id,omitempty"`
	Key     string       `json:"key,omitempty"`
	Count   int          `json:"count,omitempty"`
}

type RelationChunk struct {
	Chunk []RelationChunkItem `json:"chunk"`

	Limited bool `json:"limited"`
	Count   int  `json:"count"`
}

type AnnotationChunk struct {
	RelationChunk
	Map map[string]int `json:"-"`
}

type serializableAnnotationChunk AnnotationChunk

func (ac *AnnotationChunk) UnmarshalJSON(data []byte) error {
	if err := json.Unmarshal(data, (*serializableAnnotationChunk)(ac)); err != nil {
		return err
	}
	ac.Map = make(map[string]int)
	for _, item := range ac.Chunk {
		if item.Key != "" {
			ac.Map[item.Key] += item.Count
		}
	}
	return nil
}

func (ac *AnnotationChunk) Serialize() RelationChunk {
	ac.Chunk = make([]RelationChunkItem, len(ac.Map))
	i := 0
	for key, count := range ac.Map {
		ac.Chunk[i] = RelationChunkItem{
			Type:  RelAnnotation,
			Key:   key,
			Count: count,
		}
		i++
	}
	return ac.RelationChunk
}

type EventIDChunk struct {
	RelationChunk
	List []string `json:"-"`
}

type serializableEventIDChunk EventIDChunk

func (ec *EventIDChunk) UnmarshalJSON(data []byte) error {
	if err := json.Unmarshal(data, (*serializableEventIDChunk)(ec)); err != nil {
		return err
	}
	for _, item := range ec.Chunk {
		ec.List = append(ec.List, item.EventID)
	}
	return nil
}

func (ec *EventIDChunk) Serialize(typ RelationType) RelationChunk {
	ec.Chunk = make([]RelationChunkItem, len(ec.List))
	for i, eventID := range ec.List {
		ec.Chunk[i] = RelationChunkItem{
			Type:    typ,
			EventID: eventID,
		}
	}
	return ec.RelationChunk
}

type Relations struct {
	Raw map[RelationType]RelationChunk `json:"-"`

	Annotations AnnotationChunk `json:"m.annotation,omitempty"`
	References  EventIDChunk    `json:"m.reference,omitempty"`
	Replaces    EventIDChunk    `json:"m.replace,omitempty"`
}

type serializableRelations Relations

func (relations *Relations) UnmarshalJSON(data []byte) error {
	if err := json.Unmarshal(data, &relations.Raw); err != nil {
		return err
	}
	return json.Unmarshal(data, (*serializableRelations)(relations))
}

func (relations *Relations) MarshalJSON() ([]byte, error) {
	if relations.Raw == nil {
		relations.Raw = make(map[RelationType]RelationChunk)
	}
	relations.Raw[RelAnnotation] = relations.Annotations.Serialize()
	relations.Raw[RelReference] = relations.References.Serialize(RelReference)
	relations.Raw[RelReplace] = relations.Replaces.Serialize(RelReplace)
	for key, item := range relations.Raw {
		if !item.Limited {
			item.Count = len(item.Chunk)
		}
		if item.Count == 0 {
			delete(relations.Raw, key)
		}
	}
	return json.Marshal(relations.Raw)
}