def test_join_room_local(self): self.room_members = [self.u_apple, self.u_banana] self.assertEquals(self.event_source.get_current_key(), 0) # TODO(paul): Gut-wrenching self.handler._user_cachemap[self.u_clementine] = UserPresenceCache() self.handler._user_cachemap[self.u_clementine].update( { "presence": PresenceState.ONLINE, "last_active": self.clock.time_msec(), }, self.u_clementine) yield self.distributor.fire("user_joined_room", self.u_clementine, self.room_id) self.room_members.append(self.u_clementine) (events, _) = yield self.event_source.get_new_events( user=self.u_apple, from_key=0, ) self.assertEquals(self.event_source.get_current_key(), 1) self.assertEquals(events, [{ "type": "m.presence", "content": { "user_id": "@clementine:test", "presence": ONLINE, "last_active_ago": 0, } }])
def test_push_remote(self): put_json = self.mock_http_client.put_json put_json.expect_call_and_return( call( "farm", path=ANY, # Can't guarantee which txn ID will be which data=_expect_edu("farm", "m.presence", content={ "push": [ { "user_id": "@apple:test", "presence": u"online", "last_active_ago": 0 }, ], }), json_data_callback=ANY, long_retries=True, ), defer.succeed((200, "OK"))) put_json.expect_call_and_return( call( "remote", path=ANY, # Can't guarantee which txn ID will be which data=_expect_edu("remote", "m.presence", content={ "push": [ { "user_id": "@apple:test", "presence": u"online", "last_active_ago": 0 }, ], }), json_data_callback=ANY, long_retries=True, ), defer.succeed((200, "OK"))) self.room_members = [self.u_apple, self.u_onion] self.datastore.set_presence_state.return_value = defer.succeed( {"state": ONLINE}) # TODO(paul): Gut-wrenching self.handler._user_cachemap[self.u_apple] = UserPresenceCache() self.handler._user_cachemap[self.u_apple].update({"presence": OFFLINE}, serial=0) apple_set = self.handler._remote_sendmap.setdefault("apple", set()) apple_set.add(self.u_potato.domain) yield self.handler.set_state(self.u_apple, self.u_apple, {"presence": ONLINE}) yield put_json.await_calls()
def test_push_remote(self): self.presence_list = [ { "observed_user_id": "@potato:remote" }, ] self.datastore.set_presence_state.return_value = defer.succeed( {"state": ONLINE}) # TODO(paul): Gut-wrenching from synapse.handlers.presence import UserPresenceCache self.handlers.presence_handler._user_cachemap[self.u_apple] = ( UserPresenceCache()) self.handlers.presence_handler._user_cachemap[self.u_apple].update( {"presence": OFFLINE}, serial=0) apple_set = self.handlers.presence_handler._remote_sendmap.setdefault( "apple", set()) apple_set.add(self.u_potato.domain) yield self.handlers.presence_handler.set_state(self.u_apple, self.u_apple, {"presence": ONLINE}) self.replication.send_edu.assert_called_with( destination="remote", edu_type="m.presence", content={ "push": [ { "user_id": "@apple:test", "presence": "online", "last_active_ago": 0, "displayname": "Frank", "avatar_url": "http://foo" }, ], }, )
def test_push_local(self): self.room_members = [self.u_apple, self.u_elderberry] self.datastore.set_presence_state.return_value = defer.succeed( {"state": ONLINE}) # TODO(paul): Gut-wrenching self.handler._user_cachemap[self.u_apple] = UserPresenceCache() self.handler._user_cachemap[self.u_apple].update({"presence": OFFLINE}, serial=0) apple_set = self.handler._local_pushmap.setdefault("apple", set()) apple_set.add(self.u_banana) apple_set.add(self.u_clementine) self.assertEquals(self.event_source.get_current_key(), 0) yield self.handler.set_state(self.u_apple, self.u_apple, {"presence": ONLINE}) # Apple sees self-reflection even without room_id (events, _) = yield self.event_source.get_new_events( user=self.u_apple, from_key=0, ) self.assertEquals(self.event_source.get_current_key(), 1) self.assertEquals( events, [ { "type": "m.presence", "content": { "user_id": "@apple:test", "presence": ONLINE, "last_active_ago": 0, } }, ], msg="Presence event should be visible to self-reflection") # Apple sees self-reflection (events, _) = yield self.event_source.get_new_events( user=self.u_apple, from_key=0, room_ids=[self.room_id], ) self.assertEquals(self.event_source.get_current_key(), 1) self.assertEquals( events, [ { "type": "m.presence", "content": { "user_id": "@apple:test", "presence": ONLINE, "last_active_ago": 0, } }, ], msg="Presence event should be visible to self-reflection") config = SourcePaginationConfig(from_key=1, to_key=0) (chunk, _) = yield self.event_source.get_pagination_rows( self.u_apple, config, None) self.assertEquals(chunk, [ { "type": "m.presence", "content": { "user_id": "@apple:test", "presence": ONLINE, "last_active_ago": 0, } }, ]) # Banana sees it because of presence subscription (events, _) = yield self.event_source.get_new_events( user=self.u_banana, from_key=0, room_ids=[self.room_id], ) self.assertEquals(self.event_source.get_current_key(), 1) self.assertEquals( events, [ { "type": "m.presence", "content": { "user_id": "@apple:test", "presence": ONLINE, "last_active_ago": 0, } }, ], msg="Presence event should be visible to explicit subscribers") # Elderberry sees it because of same room (events, _) = yield self.event_source.get_new_events( user=self.u_elderberry, from_key=0, room_ids=[self.room_id], ) self.assertEquals(self.event_source.get_current_key(), 1) self.assertEquals( events, [ { "type": "m.presence", "content": { "user_id": "@apple:test", "presence": ONLINE, "last_active_ago": 0, } }, ], msg="Presence event should be visible to other room members") # Durian is not in the room, should not see this event (events, _) = yield self.event_source.get_new_events( user=self.u_durian, from_key=0, room_ids=[], ) self.assertEquals(self.event_source.get_current_key(), 1) self.assertEquals(events, [], msg="Presence event should not be visible to others") presence = yield self.handler.get_presence_list( observer_user=self.u_apple, accepted=True) self.assertEquals([ { "observed_user": self.u_banana, "presence": OFFLINE, "accepted": True }, { "observed_user": self.u_clementine, "presence": OFFLINE, "accepted": True }, ], presence) # TODO(paul): Gut-wrenching banana_set = self.handler._local_pushmap.setdefault("banana", set()) banana_set.add(self.u_apple) yield self.handler.set_state(self.u_banana, self.u_banana, {"presence": ONLINE}) self.clock.advance_time(2) presence = yield self.handler.get_presence_list( observer_user=self.u_apple, accepted=True) self.assertEquals([ { "observed_user": self.u_banana, "presence": ONLINE, "last_active_ago": 2000, "accepted": True }, { "observed_user": self.u_clementine, "presence": OFFLINE, "accepted": True }, ], presence) (events, _) = yield self.event_source.get_new_events( user=self.u_apple, from_key=1, ) self.assertEquals(self.event_source.get_current_key(), 2) self.assertEquals(events, [ { "type": "m.presence", "content": { "user_id": "@banana:test", "presence": ONLINE, "last_active_ago": 2000 } }, ])
def test_join_room_remote(self): ## Sending local user state to a newly-joined remote user put_json = self.mock_http_client.put_json put_json.expect_call_and_return( call( "remote", path=ANY, # Can't guarantee which txn ID will be which data=_expect_edu("remote", "m.presence", content={ "push": [ { "user_id": "@apple:test", "presence": "online" }, ], }), json_data_callback=ANY, long_retries=True, ), defer.succeed((200, "OK"))) put_json.expect_call_and_return( call( "remote", path=ANY, # Can't guarantee which txn ID will be which data=_expect_edu("remote", "m.presence", content={ "push": [ { "user_id": "@banana:test", "presence": "offline" }, ], }), json_data_callback=ANY, long_retries=True, ), defer.succeed((200, "OK"))) # TODO(paul): Gut-wrenching self.handler._user_cachemap[self.u_apple] = UserPresenceCache() self.handler._user_cachemap[self.u_apple].update( {"presence": PresenceState.ONLINE}, self.u_apple) self.room_members = [self.u_apple, self.u_banana] yield self.distributor.fire("user_joined_room", self.u_potato, self.room_id) yield put_json.await_calls() ## Sending newly-joined local user state to remote users put_json.expect_call_and_return( call( "remote", path="/_matrix/federation/v1/send/1000002/", data=_expect_edu("remote", "m.presence", content={ "push": [ { "user_id": "@clementine:test", "presence": "online" }, ], }), json_data_callback=ANY, long_retries=True, ), defer.succeed((200, "OK"))) self.handler._user_cachemap[self.u_clementine] = UserPresenceCache() self.handler._user_cachemap[self.u_clementine].update( {"presence": ONLINE}, self.u_clementine) self.room_members.append(self.u_potato) yield self.distributor.fire("user_joined_room", self.u_clementine, self.room_id) put_json.await_calls()
def test_push_local(self): def get_joined(*args): return defer.succeed([]) self.mock_get_joined.side_effect = get_joined self.presence_list = [ {"observed_user_id": "@banana:test"}, {"observed_user_id": "@clementine:test"}, ] self.datastore.set_presence_state.return_value = defer.succeed( {"state": ONLINE} ) # TODO(paul): Gut-wrenching from synapse.handlers.presence import UserPresenceCache self.handlers.presence_handler._user_cachemap[self.u_apple] = ( UserPresenceCache() ) self.handlers.presence_handler._user_cachemap[self.u_apple].update( {"presence": OFFLINE}, serial=0 ) apple_set = self.handlers.presence_handler._local_pushmap.setdefault( "apple", set()) apple_set.add(self.u_banana) apple_set.add(self.u_clementine) yield self.handlers.presence_handler.set_state(self.u_apple, self.u_apple, {"presence": ONLINE} ) yield self.handlers.presence_handler.set_state(self.u_banana, self.u_banana, {"presence": ONLINE} ) presence = yield self.handlers.presence_handler.get_presence_list( observer_user=self.u_apple, accepted=True) self.assertEquals([ {"observed_user": self.u_banana, "presence": ONLINE, "last_active_ago": 0, "displayname": "Frank", "avatar_url": "http://foo"}, {"observed_user": self.u_clementine, "presence": OFFLINE} ], presence) self.mock_update_client.assert_has_calls([ call(users_to_push=set([self.u_apple, self.u_banana, self.u_clementine]), room_ids=[], observed_user=self.u_apple, statuscache=ANY), # self-reflection ], any_order=True) statuscache = self.mock_update_client.call_args[1]["statuscache"] self.assertEquals({ "presence": ONLINE, "last_active": 1000000, # MockClock "displayname": "Frank", "avatar_url": "http://foo", }, statuscache.state) self.mock_update_client.reset_mock() self.datastore.set_profile_displayname.return_value = defer.succeed( None) yield self.handlers.profile_handler.set_displayname(self.u_apple, self.u_apple, "I am an Apple") self.mock_update_client.assert_has_calls([ call(users_to_push=set([self.u_apple, self.u_banana, self.u_clementine]), room_ids=[], observed_user=self.u_apple, statuscache=ANY), # self-reflection ], any_order=True) statuscache = self.mock_update_client.call_args[1]["statuscache"] self.assertEquals({ "presence": ONLINE, "last_active": 1000000, # MockClock "displayname": "I am an Apple", "avatar_url": "http://foo", }, statuscache.state)