2014-08-12 08:10:52 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-03 10:29:13 -06:00
|
|
|
# Copyright 2014 OpenMarket Ltd
|
2014-08-12 08:10:52 -06:00
|
|
|
#
|
|
|
|
# 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.
|
2014-08-12 20:14:34 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
from synapse.util.jsonobject import JsonEncodedObject
|
|
|
|
|
|
|
|
|
2014-09-15 06:26:05 -06:00
|
|
|
def serialize_event(hs, e):
|
|
|
|
# FIXME(erikj): To handle the case of presence events and the like
|
|
|
|
if not isinstance(e, SynapseEvent):
|
|
|
|
return e
|
|
|
|
|
2014-09-25 09:59:26 -06:00
|
|
|
# Should this strip out None's?
|
|
|
|
d = {k: v for k, v in e.get_dict().items()}
|
2014-09-15 06:26:05 -06:00
|
|
|
if "age_ts" in d:
|
|
|
|
d["age"] = int(hs.get_clock().time_msec()) - d["age_ts"]
|
2014-09-15 09:55:39 -06:00
|
|
|
del d["age_ts"]
|
2014-09-15 06:26:05 -06:00
|
|
|
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
class SynapseEvent(JsonEncodedObject):
|
|
|
|
|
|
|
|
"""Base class for Synapse events. These are JSON objects which must abide
|
|
|
|
by a certain well-defined structure.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Attributes that are currently assumed by the federation side:
|
|
|
|
# Mandatory:
|
|
|
|
# - event_id
|
|
|
|
# - room_id
|
|
|
|
# - type
|
|
|
|
# - is_state
|
|
|
|
#
|
|
|
|
# Optional:
|
|
|
|
# - state_key (mandatory when is_state is True)
|
|
|
|
# - prev_events (these can be filled out by the federation layer itself.)
|
|
|
|
# - prev_state
|
|
|
|
|
|
|
|
valid_keys = [
|
|
|
|
"event_id",
|
|
|
|
"type",
|
|
|
|
"room_id",
|
|
|
|
"user_id", # sender/initiator
|
|
|
|
"content", # HTTP body, JSON
|
2014-08-26 02:40:58 -06:00
|
|
|
"state_key",
|
2014-09-15 06:26:05 -06:00
|
|
|
"age_ts",
|
2014-09-15 10:43:46 -06:00
|
|
|
"prev_content",
|
2014-11-06 08:10:55 -07:00
|
|
|
"replaces_state",
|
2014-09-24 08:27:59 -06:00
|
|
|
"redacted_because",
|
2014-11-03 10:51:42 -07:00
|
|
|
"origin_server_ts",
|
2014-08-12 08:10:52 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
internal_keys = [
|
|
|
|
"is_state",
|
|
|
|
"depth",
|
|
|
|
"destinations",
|
|
|
|
"origin",
|
2014-08-21 08:06:00 -06:00
|
|
|
"outlier",
|
2014-09-24 08:27:59 -06:00
|
|
|
"redacted",
|
2014-10-28 10:42:35 -06:00
|
|
|
"prev_events",
|
|
|
|
"hashes",
|
|
|
|
"signatures",
|
2014-11-06 08:19:00 -07:00
|
|
|
"prev_state",
|
2014-11-07 03:42:44 -07:00
|
|
|
"auth_events",
|
2014-11-07 04:36:40 -07:00
|
|
|
"state_hash",
|
2014-08-12 08:10:52 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
required_keys = [
|
|
|
|
"event_id",
|
|
|
|
"room_id",
|
|
|
|
"content",
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, raises=True, **kwargs):
|
|
|
|
super(SynapseEvent, self).__init__(**kwargs)
|
2014-11-10 03:21:32 -07:00
|
|
|
# if "content" in kwargs:
|
|
|
|
# self.check_json(self.content, raises=raises)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
def get_content_template(self):
|
|
|
|
""" Retrieve the JSON template for this event as a dict.
|
|
|
|
|
|
|
|
The template must be a dict representing the JSON to match. Only
|
|
|
|
required keys should be present. The values of the keys in the template
|
|
|
|
are checked via type() to the values of the same keys in the actual
|
|
|
|
event JSON.
|
|
|
|
|
|
|
|
NB: If loading content via json.loads, you MUST define strings as
|
|
|
|
unicode.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
Content:
|
|
|
|
{
|
|
|
|
"name": u"bob",
|
|
|
|
"age": 18,
|
|
|
|
"friends": [u"mike", u"jill"]
|
|
|
|
}
|
|
|
|
Template:
|
|
|
|
{
|
|
|
|
"name": u"string",
|
|
|
|
"age": 0,
|
|
|
|
"friends": [u"string"]
|
|
|
|
}
|
|
|
|
The values "string" and 0 could be anything, so long as the types
|
|
|
|
are the same as the content.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("get_content_template not implemented.")
|
|
|
|
|
2014-08-28 03:59:15 -06:00
|
|
|
|
|
|
|
class SynapseStateEvent(SynapseEvent):
|
2014-09-05 19:23:36 -06:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2014-08-28 03:59:15 -06:00
|
|
|
if "state_key" not in kwargs:
|
|
|
|
kwargs["state_key"] = ""
|
|
|
|
super(SynapseStateEvent, self).__init__(**kwargs)
|