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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
# -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from synapse.util.frozenutils import freeze, unfreeze
from synapse.util.caches import intern_dict, intern_string
import abc
import simplejson as json
# Whether we should use frozen_dict in FrozenEvent. Using frozen_dicts prevents
# bugs where we accidentally share e.g. signature dicts. However, converting
# a dict to frozen_dicts is expensive.
USE_FROZEN_DICTS = True
class _EventInternalMetadata(object):
def __init__(self, internal_metadata_dict):
self.__dict__ = dict(internal_metadata_dict)
def get_dict(self):
return dict(self.__dict__)
def is_outlier(self):
return getattr(self, "outlier", False)
def is_invite_from_remote(self):
return getattr(self, "invite_from_remote", False)
def get_send_on_behalf_of(self):
"""Whether this server should send the event on behalf of another server.
This is used by the federation "send_join" API to forward the initial join
event for a server in the room.
returns a str with the name of the server this event is sent on behalf of.
"""
return getattr(self, "send_on_behalf_of", None)
def _event_dict_property(key):
def getter(self):
return self._event_dict[key]
def setter(self, v):
self._event_dict[key] = v
def delete(self):
del self._event_dict[key]
return property(
getter,
setter,
delete,
)
class EventBase(object):
__metaclass__ = abc.ABCMeta
def __init__(self, signatures={}, unsigned={},
internal_metadata_dict={}, rejected_reason=None):
self.signatures = signatures
self.unsigned = unsigned
self.rejected_reason = rejected_reason
self.internal_metadata = _EventInternalMetadata(
internal_metadata_dict
)
auth_events = abc.abstractproperty()
depth = abc.abstractproperty()
content = abc.abstractproperty()
hashes = abc.abstractproperty()
origin = abc.abstractproperty()
origin_server_ts = abc.abstractproperty()
prev_events = abc.abstractproperty()
prev_state = abc.abstractproperty()
redacts = abc.abstractproperty()
room_id = abc.abstractproperty()
sender = abc.abstractproperty()
user_id = abc.abstractproperty()
event_id = abc.abstractproperty()
state_key = abc.abstractproperty()
type = abc.abstractproperty()
@abc.abstractmethod
def get_dict(self):
raise NotImplementedError()
@abc.abstractmethod
def get(self, key, default=None):
raise NotImplementedError()
@abc.abstractmethod
def iteritems(self):
raise NotImplementedError()
@property
def membership(self):
return self.content["membership"]
def is_state(self):
return hasattr(self, "state_key") and self.state_key is not None
def get_internal_metadata_dict(self):
return self.internal_metadata.get_dict()
def get_pdu_json(self, time_now=None):
pdu_json = self.get_dict()
if time_now is not None and "age_ts" in pdu_json["unsigned"]:
age = time_now - pdu_json["unsigned"]["age_ts"]
pdu_json.setdefault("unsigned", {})["age"] = int(age)
del pdu_json["unsigned"]["age_ts"]
# This may be a frozen event
pdu_json["unsigned"].pop("redacted_because", None)
return pdu_json
def __set__(self, instance, value):
raise AttributeError("Unrecognized attribute %s" % (instance,))
class FrozenEvent(EventBase):
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
event_dict = dict(event_dict)
# Signatures is a dict of dicts, and this is faster than doing a
# copy.deepcopy
signatures = {
name: {sig_id: sig for sig_id, sig in sigs.items()}
for name, sigs in event_dict.pop("signatures", {}).items()
}
unsigned = dict(event_dict.pop("unsigned", {}))
# We intern these strings because they turn up a lot (especially when
# caching).
event_dict = intern_dict(event_dict)
if USE_FROZEN_DICTS:
frozen_dict = freeze(event_dict)
else:
frozen_dict = event_dict
self._event_id = event_dict["event_id"]
self._type = event_dict["type"]
if "state_key" in event_dict:
self._state_key = event_dict["state_key"]
self._event_dict = frozen_dict
super(FrozenEvent, self).__init__(
signatures=signatures,
unsigned=unsigned,
internal_metadata_dict=internal_metadata_dict,
rejected_reason=rejected_reason,
)
auth_events = _event_dict_property("auth_events")
depth = _event_dict_property("depth")
content = _event_dict_property("content")
hashes = _event_dict_property("hashes")
origin = _event_dict_property("origin")
origin_server_ts = _event_dict_property("origin_server_ts")
prev_events = _event_dict_property("prev_events")
prev_state = _event_dict_property("prev_state")
redacts = _event_dict_property("redacts")
room_id = _event_dict_property("room_id")
sender = _event_dict_property("sender")
user_id = _event_dict_property("sender")
@property
def event_id(self):
return self._event_id
@property
def type(self):
return self._type
@property
def state_key(self):
return self._state_key
def get_dict(self):
d = dict(self._event_dict)
d.update({
"signatures": self.signatures,
"unsigned": dict(self.unsigned),
})
return d
def get(self, key, default=None):
return self._event_dict.get(key, default)
def iteritems(self):
return self._event_dict.iteritems()
@staticmethod
def from_event(event):
e = FrozenEvent(
event.get_pdu_json()
)
e.internal_metadata = event.internal_metadata
return e
def __str__(self):
return self.__repr__()
def __repr__(self):
return "<FrozenEvent event_id='%s', type='%s', state_key='%s'>" % (
self.get("event_id", None),
self.get("type", None),
self.get("state_key", None),
)
def _compact_property(key):
def getter(self):
try:
return self[key]
except KeyError:
raise AttributeError(
"AttributeError: '%s' object has no attribute '%s'" % (
self.__name__, key,
)
)
return property(getter)
class _Unsigned(object):
__slots__ = [
"age_ts",
"replaces_state",
"redacted_because",
"invite_room_state",
"prev_content",
"prev_sender",
"redacted_by",
]
def __init__(self, **kwargs):
for s in self.__slots__:
try:
setattr(self, s, kwargs[s])
except KeyError:
continue
def __getitem__(self, field):
try:
return getattr(self, field)
except AttributeError:
raise KeyError(field)
def __setitem__(self, field, value):
try:
setattr(self, field, value)
except AttributeError:
raise KeyError(field)
def __delitem__(self, field):
try:
return delattr(self, field)
except AttributeError:
raise KeyError(field)
def __contains__(self, field):
return hasattr(self, field)
def get(self, key, default=None):
return getattr(self, key, default)
def pop(self, key, default):
r = self.get(key, default)
try:
delattr(self, key)
except AttributeError:
pass
return r
def __iter__(self):
for key in self.__slots__:
if hasattr(self, key):
yield (key, getattr(self, key))
class CompactEvent(EventBase):
__slots__ = [
"event_json",
"internal_metadata",
"rejected_reason",
"signatures",
"unsigned",
"event_id",
"room_id",
"type",
"state_key",
"sender",
]
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
event_dict = dict(unfreeze(event_dict))
object.__setattr__(self, "unsigned", _Unsigned(**event_dict.pop("unsigned", {})))
signatures = {
intern_string(name): {
intern_string(sig_id): sig.encode("utf-8")
for sig_id, sig in sigs.iteritems()
}
for name, sigs in event_dict.pop("signatures", {}).iteritems()
}
object.__setattr__(self, "signatures", signatures)
object.__setattr__(self, "event_json", json.dumps(event_dict))
object.__setattr__(self, "rejected_reason", rejected_reason)
object.__setattr__(self, "internal_metadata", _EventInternalMetadata(
internal_metadata_dict
))
object.__setattr__(self, "event_id", event_dict["event_id"])
object.__setattr__(self, "room_id", event_dict["room_id"])
object.__setattr__(self, "type", event_dict["type"])
if "state_key" in event_dict:
object.__setattr__(self, "state_key", event_dict["state_key"])
object.__setattr__(self, "sender", event_dict["sender"])
auth_events = _compact_property("auth_events")
depth = _compact_property("depth")
content = _compact_property("content")
hashes = _compact_property("hashes")
origin = _compact_property("origin")
origin_server_ts = _compact_property("origin_server_ts")
prev_events = _compact_property("prev_events")
prev_state = _compact_property("prev_state")
redacts = _compact_property("redacts")
@property
def user_id(self):
return self.sender
@property
def membership(self):
return self.content["membership"]
def is_state(self):
return hasattr(self, "state_key") and self.state_key is not None
def get_dict(self):
d = json.loads(self.event_json)
d.update({
"signatures": dict(self.signatures),
"unsigned": dict(self.unsigned),
})
return d
def get(self, key, default=None):
if key in self.__slots__:
return freeze(getattr(self, key, default))
d = json.loads(self.event_json)
return d.get(key, default)
def get_internal_metadata_dict(self):
return self.internal_metadata.get_dict()
def __getitem__(self, field):
if field in self.__slots__:
try:
return freeze(getattr(self, field))
except AttributeError:
raise KeyError(field)
d = json.loads(self.event_json)
return d[field]
def __contains__(self, field):
if field in self.__slots__:
return hasattr(self, field)
d = json.loads(self.event_json)
return field in d
@staticmethod
def from_event(event):
return CompactEvent(
event.get_pdu_json(),
event.get_internal_metadata_dict(),
event.rejected_reason,
)
def __str__(self):
return self.__repr__()
def __repr__(self):
return "<CompactEvent event_id='%s', type='%s', state_key='%s'>" % (
self.get("event_id", None),
self.get("type", None),
self.get("state_key", None),
)
def iteritems(self):
return json.loads(self.event_json).iteritems()
def __eq__(self, other):
return self.event_id == other.event_id
|