2016-04-06 07:12:51 -06:00
|
|
|
# Copyright 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 ._base import BaseSlavedStoreTestCase
|
|
|
|
|
2016-04-07 09:41:37 -06:00
|
|
|
from synapse.events import FrozenEvent, _EventInternalMetadata
|
2016-04-06 07:12:51 -06:00
|
|
|
from synapse.events.snapshot import EventContext
|
2016-04-19 10:11:44 -06:00
|
|
|
from synapse.replication.slave.storage.events import SlavedEventStore
|
2016-04-06 09:17:15 -06:00
|
|
|
from synapse.storage.roommember import RoomsForUser
|
2016-04-06 07:12:51 -06:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2016-04-07 09:41:37 -06:00
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
USER_ID = "@feeling:blue"
|
2016-04-06 09:17:15 -06:00
|
|
|
USER_ID_2 = "@bright:blue"
|
2016-04-06 07:12:51 -06:00
|
|
|
OUTLIER = {"outlier": True}
|
|
|
|
ROOM_ID = "!room:blue"
|
|
|
|
|
|
|
|
|
2016-04-07 09:41:37 -06:00
|
|
|
def dict_equals(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
|
|
|
|
|
|
|
|
|
|
|
def patch__eq__(cls):
|
|
|
|
eq = getattr(cls, "__eq__", None)
|
|
|
|
cls.__eq__ = dict_equals
|
|
|
|
|
|
|
|
def unpatch():
|
|
|
|
if eq is not None:
|
|
|
|
cls.__eq__ = eq
|
|
|
|
return unpatch
|
|
|
|
|
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
|
|
|
|
|
2016-04-19 10:11:44 -06:00
|
|
|
STORE_TYPE = SlavedEventStore
|
|
|
|
|
2016-04-07 09:41:37 -06:00
|
|
|
def setUp(self):
|
|
|
|
# Patch up the equality operator for events so that we can check
|
|
|
|
# whether lists of events match using assertEquals
|
|
|
|
self.unpatches = [
|
|
|
|
patch__eq__(_EventInternalMetadata),
|
|
|
|
patch__eq__(FrozenEvent),
|
|
|
|
]
|
|
|
|
return super(SlavedEventStoreTestCase, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
[unpatch() for unpatch in self.unpatches]
|
|
|
|
|
2016-04-06 09:17:15 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_room_members(self):
|
|
|
|
create = yield self.persist(type="m.room.create", key="", creator=USER_ID)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_rooms_for_user", (USER_ID,), [])
|
|
|
|
yield self.check("get_users_in_room", (ROOM_ID,), [])
|
|
|
|
|
|
|
|
# Join the room.
|
|
|
|
join = yield self.persist(type="m.room.member", key=USER_ID, membership="join")
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_rooms_for_user", (USER_ID,), [RoomsForUser(
|
|
|
|
room_id=ROOM_ID,
|
|
|
|
sender=USER_ID,
|
|
|
|
membership="join",
|
|
|
|
event_id=join.event_id,
|
|
|
|
stream_ordering=join.internal_metadata.stream_ordering,
|
|
|
|
)])
|
|
|
|
yield self.check("get_users_in_room", (ROOM_ID,), [USER_ID])
|
|
|
|
|
|
|
|
# Leave the room.
|
|
|
|
yield self.persist(type="m.room.member", key=USER_ID, membership="leave")
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_rooms_for_user", (USER_ID,), [])
|
|
|
|
yield self.check("get_users_in_room", (ROOM_ID,), [])
|
|
|
|
|
|
|
|
# Add some other user to the room.
|
|
|
|
join = yield self.persist(type="m.room.member", key=USER_ID_2, membership="join")
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_rooms_for_user", (USER_ID_2,), [RoomsForUser(
|
|
|
|
room_id=ROOM_ID,
|
|
|
|
sender=USER_ID,
|
|
|
|
membership="join",
|
|
|
|
event_id=join.event_id,
|
|
|
|
stream_ordering=join.internal_metadata.stream_ordering,
|
|
|
|
)])
|
|
|
|
yield self.check("get_users_in_room", (ROOM_ID,), [USER_ID_2])
|
|
|
|
|
|
|
|
# Join the room clobbering the state.
|
|
|
|
# This should remove any evidence of the other user being in the room.
|
|
|
|
yield self.persist(
|
|
|
|
type="m.room.member", key=USER_ID, membership="join",
|
|
|
|
reset_state=[create]
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_users_in_room", (ROOM_ID,), [USER_ID])
|
|
|
|
yield self.check("get_rooms_for_user", (USER_ID_2,), [])
|
|
|
|
|
2016-04-07 06:17:56 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_latest_event_ids_in_room(self):
|
|
|
|
create = yield self.persist(type="m.room.create", key="", creator=USER_ID)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_latest_event_ids_in_room", (ROOM_ID,), [create.event_id]
|
|
|
|
)
|
|
|
|
|
|
|
|
join = yield self.persist(
|
|
|
|
type="m.room.member", key=USER_ID, membership="join",
|
|
|
|
prev_events=[(create.event_id, {})],
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_latest_event_ids_in_room", (ROOM_ID,), [join.event_id]
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_current_state(self):
|
|
|
|
# Create the room.
|
|
|
|
create = yield self.persist(type="m.room.create", key="", creator=USER_ID)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_current_state_for_key", (ROOM_ID, "m.room.member", USER_ID), []
|
|
|
|
)
|
|
|
|
|
|
|
|
# Join the room.
|
|
|
|
join1 = yield self.persist(
|
|
|
|
type="m.room.member", key=USER_ID, membership="join",
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_current_state_for_key", (ROOM_ID, "m.room.member", USER_ID),
|
|
|
|
[join1]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add some other user to the room.
|
|
|
|
join2 = yield self.persist(
|
|
|
|
type="m.room.member", key=USER_ID_2, membership="join",
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_current_state_for_key", (ROOM_ID, "m.room.member", USER_ID_2),
|
|
|
|
[join2]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Leave the room, then rejoin the room clobbering state.
|
|
|
|
yield self.persist(type="m.room.member", key=USER_ID, membership="leave")
|
|
|
|
join3 = yield self.persist(
|
|
|
|
type="m.room.member", key=USER_ID, membership="join",
|
|
|
|
reset_state=[create]
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_current_state_for_key", (ROOM_ID, "m.room.member", USER_ID_2),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
yield self.check(
|
|
|
|
"get_current_state_for_key", (ROOM_ID, "m.room.member", USER_ID),
|
|
|
|
[join3]
|
|
|
|
)
|
|
|
|
|
2016-04-07 09:26:52 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_redactions(self):
|
|
|
|
yield self.persist(type="m.room.create", key="", creator=USER_ID)
|
|
|
|
yield self.persist(type="m.room.member", key=USER_ID, membership="join")
|
|
|
|
|
|
|
|
msg = yield self.persist(
|
|
|
|
type="m.room.message", msgtype="m.text", body="Hello"
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_event", [msg.event_id], msg)
|
|
|
|
|
|
|
|
redaction = yield self.persist(
|
|
|
|
type="m.room.redaction", redacts=msg.event_id
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
|
|
|
|
msg_dict = msg.get_dict()
|
|
|
|
msg_dict["content"] = {}
|
|
|
|
msg_dict["unsigned"]["redacted_by"] = redaction.event_id
|
|
|
|
msg_dict["unsigned"]["redacted_because"] = redaction
|
|
|
|
redacted = FrozenEvent(msg_dict, msg.internal_metadata.get_dict())
|
|
|
|
yield self.check("get_event", [msg.event_id], redacted)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_backfilled_redactions(self):
|
|
|
|
yield self.persist(type="m.room.create", key="", creator=USER_ID)
|
|
|
|
yield self.persist(type="m.room.member", key=USER_ID, membership="join")
|
|
|
|
|
|
|
|
msg = yield self.persist(
|
|
|
|
type="m.room.message", msgtype="m.text", body="Hello"
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_event", [msg.event_id], msg)
|
|
|
|
|
|
|
|
redaction = yield self.persist(
|
|
|
|
type="m.room.redaction", redacts=msg.event_id, backfill=True
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
|
|
|
|
msg_dict = msg.get_dict()
|
|
|
|
msg_dict["content"] = {}
|
|
|
|
msg_dict["unsigned"]["redacted_by"] = redaction.event_id
|
|
|
|
msg_dict["unsigned"]["redacted_because"] = redaction
|
|
|
|
redacted = FrozenEvent(msg_dict, msg.internal_metadata.get_dict())
|
|
|
|
yield self.check("get_event", [msg.event_id], redacted)
|
|
|
|
|
2016-04-19 08:22:14 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_invites(self):
|
|
|
|
yield self.check("get_invited_rooms_for_user", [USER_ID_2], [])
|
|
|
|
event = yield self.persist(
|
|
|
|
type="m.room.member", key=USER_ID_2, membership="invite"
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check("get_invited_rooms_for_user", [USER_ID_2], [RoomsForUser(
|
|
|
|
ROOM_ID, USER_ID, "invite", event.event_id,
|
|
|
|
event.internal_metadata.stream_ordering
|
|
|
|
)])
|
|
|
|
|
2016-04-21 08:25:47 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_push_actions_for_user(self):
|
|
|
|
yield self.persist(type="m.room.create", creator=USER_ID)
|
|
|
|
yield self.persist(type="m.room.join", key=USER_ID, membership="join")
|
|
|
|
yield self.persist(
|
|
|
|
type="m.room.join", sender=USER_ID, key=USER_ID_2, membership="join"
|
|
|
|
)
|
|
|
|
event1 = yield self.persist(
|
|
|
|
type="m.room.message", msgtype="m.text", body="hello"
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_unread_event_push_actions_by_room_for_user",
|
|
|
|
[ROOM_ID, USER_ID_2, event1.event_id],
|
|
|
|
{"highlight_count": 0, "notify_count": 0}
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.persist(
|
|
|
|
type="m.room.message", msgtype="m.text", body="world",
|
|
|
|
push_actions=[(USER_ID_2, ["notify"])],
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_unread_event_push_actions_by_room_for_user",
|
|
|
|
[ROOM_ID, USER_ID_2, event1.event_id],
|
|
|
|
{"highlight_count": 0, "notify_count": 1}
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.persist(
|
|
|
|
type="m.room.message", msgtype="m.text", body="world",
|
|
|
|
push_actions=[(USER_ID_2, [
|
|
|
|
"notify", {"set_tweak": "highlight", "value": True}
|
|
|
|
])],
|
|
|
|
)
|
|
|
|
yield self.replicate()
|
|
|
|
yield self.check(
|
|
|
|
"get_unread_event_push_actions_by_room_for_user",
|
|
|
|
[ROOM_ID, USER_ID_2, event1.event_id],
|
|
|
|
{"highlight_count": 1, "notify_count": 2}
|
|
|
|
)
|
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
event_id = 0
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def persist(
|
2016-04-06 09:17:15 -06:00
|
|
|
self, sender=USER_ID, room_id=ROOM_ID, type={}, key=None, internal={},
|
|
|
|
state=None, reset_state=False, backfill=False,
|
2016-04-07 09:26:52 -06:00
|
|
|
depth=None, prev_events=[], auth_events=[], prev_state=[], redacts=None,
|
2016-04-21 08:25:47 -06:00
|
|
|
push_actions=[],
|
2016-04-06 09:17:15 -06:00
|
|
|
**content
|
2016-04-06 07:12:51 -06:00
|
|
|
):
|
2016-04-06 09:17:15 -06:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
synapse.events.FrozenEvent: The event that was persisted.
|
|
|
|
"""
|
2016-04-06 07:12:51 -06:00
|
|
|
if depth is None:
|
|
|
|
depth = self.event_id
|
|
|
|
|
|
|
|
event_dict = {
|
|
|
|
"sender": sender,
|
|
|
|
"type": type,
|
|
|
|
"content": content,
|
|
|
|
"event_id": "$%d:blue" % (self.event_id,),
|
|
|
|
"room_id": room_id,
|
|
|
|
"depth": depth,
|
|
|
|
"origin_server_ts": self.event_id,
|
|
|
|
"prev_events": prev_events,
|
|
|
|
"auth_events": auth_events,
|
|
|
|
}
|
|
|
|
if key is not None:
|
|
|
|
event_dict["state_key"] = key
|
|
|
|
event_dict["prev_state"] = prev_state
|
|
|
|
|
2016-04-07 09:26:52 -06:00
|
|
|
if redacts is not None:
|
|
|
|
event_dict["redacts"] = redacts
|
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
event = FrozenEvent(event_dict, internal_metadata_dict=internal)
|
|
|
|
|
|
|
|
self.event_id += 1
|
|
|
|
|
2016-08-25 10:32:22 -06:00
|
|
|
if state is not None:
|
|
|
|
state_ids = {
|
|
|
|
key: e.event_id for key, e in state.items()
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
state_ids = None
|
|
|
|
|
2016-08-31 06:55:02 -06:00
|
|
|
context = EventContext()
|
|
|
|
context.current_state_ids = state_ids
|
|
|
|
context.prev_state_ids = state_ids
|
2016-04-21 08:25:47 -06:00
|
|
|
context.push_actions = push_actions
|
2016-04-06 07:12:51 -06:00
|
|
|
|
2016-04-06 09:17:15 -06:00
|
|
|
ordering = None
|
2016-04-06 07:12:51 -06:00
|
|
|
if backfill:
|
|
|
|
yield self.master_store.persist_events(
|
|
|
|
[(event, context)], backfilled=True
|
|
|
|
)
|
|
|
|
else:
|
2016-04-06 09:17:15 -06:00
|
|
|
ordering, _ = yield self.master_store.persist_event(
|
2016-04-06 07:12:51 -06:00
|
|
|
event, context, current_state=reset_state
|
|
|
|
)
|
2016-04-06 09:17:15 -06:00
|
|
|
|
|
|
|
if ordering:
|
|
|
|
event.internal_metadata.stream_ordering = ordering
|
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
defer.returnValue(event)
|