summary refs log tree commit diff
path: root/synapse/events/__init__.py
blob: e81b995d39b32ffe369b2b90537ae4180fc78a15 (plain) (blame)
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
# -*- coding: utf-8 -*-
# Copyright 2014 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 frozendict import frozendict


def _freeze(o):
    if isinstance(o, dict) or isinstance(o, frozendict):
        return frozendict({k: _freeze(v) for k, v in o.items()})

    if isinstance(o, basestring):
        return o

    try:
        return tuple([_freeze(i) for i in o])
    except TypeError:
        pass

    return o


def _unfreeze(o):
    if isinstance(o, frozendict) or isinstance(o, dict):
        return dict({k: _unfreeze(v) for k, v in o.items()})

    if isinstance(o, basestring):
        return o

    try:
        return [_unfreeze(i) for i in o]
    except TypeError:
        pass

    return o


class _EventInternalMetadata(object):
    def __init__(self, internal_metadata_dict):
        self.__dict__ = internal_metadata_dict

    def get_dict(self):
        return dict(self.__dict__)


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):
    def __init__(self, event_dict, signatures={}, unsigned={},
                 internal_metadata_dict={}):
        self.signatures = signatures
        self.unsigned = unsigned

        self._event_dict = event_dict

        self.internal_metadata = _EventInternalMetadata(
            internal_metadata_dict
        )

    auth_events = _event_dict_property("auth_events")
    depth = _event_dict_property("depth")
    content = _event_dict_property("content")
    event_id = _event_dict_property("event_id")
    hashes = _event_dict_property("hashes")
    origin = _event_dict_property("origin")
    prev_events = _event_dict_property("prev_events")
    prev_state = _event_dict_property("prev_state")
    room_id = _event_dict_property("room_id")
    sender = _event_dict_property("sender")
    state_key = _event_dict_property("state_key")
    type = _event_dict_property("type")
    user_id = _event_dict_property("sender")

    @property
    def membership(self):
        return self.content["membership"]

    def is_state(self):
        return hasattr(self, "state_key")

    def get_dict(self):
        d = dict(self._event_dict)
        d.update({
            "signatures": self.signatures,
            "unsigned": self.unsigned,
        })

        return d

    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"]

        return pdu_json

    def __set__(self, instance, value):
        raise AttributeError("Unrecognized attribute %s" % (instance,))


class FrozenEvent(EventBase):
    def __init__(self, event_dict, signatures={}, unsigned={}):
        event_dict = dict(event_dict)

        signatures.update(event_dict.pop("signatures", {}))
        unsigned.update(event_dict.pop("unsigned", {}))

        frozen_dict = _freeze(event_dict)

        super(FrozenEvent, self).__init__(
            frozen_dict,
            signatures=signatures,
            unsigned=unsigned
        )

    @staticmethod
    def from_event(event):
        e = FrozenEvent(
            event.get_pdu_json()
        )

        e.internal_metadata = event.internal_metadata

        return e

    def get_dict(self):
        # We need to unfreeze what we return

        d = _unfreeze(self._event_dict)
        d.update({
            "signatures": self.signatures,
            "unsigned": self.unsigned,
        })

        return d