def test_one_session_join(self): users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertFalse(BaseTest.USER_ID in users.keys()) act = self.activity_for_join() OnJoinHooks.join_room((act, as_parser(act))) users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertTrue(BaseTest.USER_ID in users.keys()) room_sids = self.db.get_rooms_with_sid(user_id=BaseTest.USER_ID) self.assertIn(SESSION_ID_ONE, room_sids.keys())
def test_two_sessions_one_leave(self): # first session joins users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertNotIn(BaseTest.USER_ID, users.keys()) act = self.activity_for_join() OnJoinHooks.join_room((act, as_parser(act))) users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertIn(BaseTest.USER_ID, users.keys()) # second session joins self.env.request = BaseTest.Request(SESSION_ID_TWO) environ.env.request = self.env.request OnJoinHooks.join_room((act, as_parser(act))) room_sids = self.db.get_rooms_with_sid(user_id=BaseTest.USER_ID) self.assertIn(SESSION_ID_ONE, room_sids.keys()) self.assertIn(SESSION_ID_TWO, room_sids.keys()) # second session leaves act = self.act_leave() users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertIn(BaseTest.USER_ID, users.keys()) OnLeaveHooks.leave_room((act, as_parser(act))) users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertIn(BaseTest.USER_ID, users.keys()) room_sids = self.db.get_rooms_with_sid(user_id=BaseTest.USER_ID) self.assertNotIn(SESSION_ID_TWO, room_sids.keys()) self.assertIn(SESSION_ID_ONE, room_sids.keys()) # first session leaves self.env.request = BaseTest.Request(SESSION_ID_ONE) environ.env.request = self.env.request act = self.activity_for_leave() OnLeaveHooks.leave_room((act, as_parser(act))) room_sids = self.db.get_rooms_with_sid(user_id=BaseTest.USER_ID) self.assertNotIn(SESSION_ID_TWO, room_sids.keys()) self.assertNotIn(SESSION_ID_ONE, room_sids.keys()) users = utils.get_users_in_room(BaseTest.ROOM_ID, skip_cache=True) self.assertNotIn(BaseTest.USER_ID, users.keys())
def set_online(user_id: str, user_name: str, image: str = '') -> None: was_invisible = utils.user_is_invisible(user_id) OnStatusHooks.logger.info( 'setting user {} ({}) online (was invisible before? {})'.format( user_id, user_name, was_invisible)) environ.env.db.set_user_online(user_id) rooms = utils.rooms_for_user(user_id) if not was_invisible: for room_id in rooms: room_name = utils.get_room_name(room_id) join_activity = utils.activity_for_user_joined( user_id, user_name, room_id, room_name, image) environ.env.emit('gn_user_joined', join_activity, room=room_id, broadcast=True, include_self=False, namespace='/ws') return visible_activity = utils.activity_for_going_visible(user_id) for room_id in rooms: room_name = utils.get_room_name(room_id) join_activity = utils.activity_for_user_joined( user_id, user_name, room_id, room_name, image) admins_in_room = environ.env.db.get_admins_in_room( room_id, user_id) if admins_in_room is None or len(admins_in_room) == 0: environ.env.emit('gn_user_joined', join_activity, room=room_id, broadcast=True, include_self=False, namespace='/ws') continue users_in_room = utils.get_users_in_room(room_id).copy() for user_id_in_room, _ in users_in_room.items(): if user_id_in_room in admins_in_room: continue environ.env.emit('gn_user_joined', join_activity, room=user_id_in_room, broadcast=True, include_self=False, namespace='/ws') for admin_id in admins_in_room: environ.env.emit('gn_user_visible', visible_activity, room=admin_id, broadcast=False, namespace='/ws')
def set_invisible(user_id: str, user_name: str, stage: str) -> None: OnStatusHooks.logger.info('setting user {} ({}) to invisible'.format( user_id, user_name, )) # when logging in as 'invisible', the rest call can happen after ws # logs in, in which case we don't want to update last_online if stage == 'login': is_offline = True else: is_offline = not utils.user_is_online(user_id) environ.env.db.set_user_invisible(user_id, is_offline=is_offline) if is_offline: # nothing more to do if offline already return disconnect_activity = utils.activity_for_disconnect(user_id, user_name) rooms = utils.rooms_for_user(user_id) for room_id in rooms: admins_in_room = environ.env.db.get_admins_in_room( room_id, user_id) if admins_in_room is None or len(admins_in_room) == 0: environ.env.emit('gn_user_disconnected', disconnect_activity, room=room_id, broadcast=True, include_self=False, namespace='/ws') continue users_in_room = utils.get_users_in_room(room_id) for other_user_id, _ in users_in_room.items(): if other_user_id in admins_in_room: continue environ.env.emit('gn_user_disconnected', disconnect_activity, room=other_user_id, broadcast=True, include_self=False, namespace='/ws') invisible_activity = utils.activity_for_going_invisible(user_id) for admin_id in admins_in_room: environ.env.emit('gn_user_invisible', invisible_activity, room=admin_id, broadcast=False, namespace='/ws')
def set_invisible(user_id: str, user_name: str) -> None: OnStatusHooks.logger.info('setting user {} ({}) to invisible'.format( user_id, user_name, )) is_offline = not utils.user_is_online(user_id) environ.env.db.set_user_invisible(user_id, is_offline=is_offline) if is_offline: # nothing more to do if offline already return disconnect_activity = utils.activity_for_disconnect(user_id, user_name) rooms = utils.rooms_for_user(user_id) for room_id in rooms: admins_in_room = environ.env.db.get_admins_in_room( room_id, user_id) if admins_in_room is None or len(admins_in_room) == 0: environ.env.emit('gn_user_disconnected', disconnect_activity, room=room_id, broadcast=True, include_self=False, namespace='/ws') continue users_in_room = utils.get_users_in_room(room_id) for other_user_id, _ in users_in_room.items(): if other_user_id in admins_in_room: continue environ.env.emit('gn_user_disconnected', disconnect_activity, room=other_user_id, broadcast=True, include_self=False, namespace='/ws') invisible_activity = utils.activity_for_going_invisible(user_id) for admin_id in admins_in_room: environ.env.emit('gn_user_invisible', invisible_activity, room=admin_id, broadcast=False, namespace='/ws')
def _process(self, data: dict, activity: Activity): room_id = activity.target.id user_id = activity.actor.id if utils.is_super_user(user_id): return True, None, None n_users = len(utils.get_users_in_room(room_id)) membership = self.env.session.get(SessionKeys.membership.value, '') if n_users < self.max_users_low: return True, None, None if len(membership.strip() ) > 0 and membership not in self.max_users_exception: return True, None, None if n_users > self.max_users_high: return False, ErrorCodes.ROOM_FULL, 'room is full' return True, None, None
def _do_get(self, room_id): output = list() list_activity = parse_to_as({ "verb": "list", "target": { "id": room_id } }) users = utils.get_users_in_room(room_id, skip_cache=False) activity = utils.activity_for_users_in_room(list_activity, users) for user in activity["object"]["attachments"]: output.append({ 'id': user["id"], 'name': user["displayName"], 'info': { attachment["objectType"]: attachment["content"] for attachment in user["attachments"] }, 'roles': user["content"] }) return output