Exemplo n.º 1
0
    def upload_profile_image(self, files):
        """
		POST only, validates email during registration
		"""
        input_file = files['file']
        up = UserProfile(photo=input_file, id=self.user_id)
        create_thumbnail(input_file, up)
        up.save(update_fields=('photo', 'thumbnail'))
        up = UserProfile.objects.values('sex', 'thumbnail', 'photo',
                                        'username').get(id=self.user_id)
        payload = {
            VarNames.HANDLER_NAME: HandlerNames.WS,
            VarNames.EVENT: Actions.USER_PROFILE_CHANGED
        }
        payload.update(
            RedisPrefix.set_js_user_structure(
                self.user_id,
                up['username'],
                up['sex'],
                get_thumbnail_url(up['thumbnail']),
            ))
        global_redis.sync_redis.publish(settings.ALL_ROOM_ID,
                                        json.dumps(payload))
        global_redis.sync_redis.publish(
            RedisPrefix.generate_user(self.user_id),
            json.dumps({
                VarNames.HANDLER_NAME: HandlerNames.WS,
                VarNames.EVENT: Actions.SET_PROFILE_IMAGE,
                VarNames.CONTENT: get_thumbnail_url(up['photo']),
            }))
        return settings.VALIDATION_IS_OK
Exemplo n.º 2
0
def get_users_in_current_user_rooms(user_id):
	user_rooms = Room.objects.filter(users__id=user_id, disabled=False).values('id', 'name', 'roomusers__notifications', 'roomusers__volume')
	res = MessagesCreator.create_user_rooms(user_rooms)
	room_ids = (room_id for room_id in res)
	rooms_users = User.objects.filter(rooms__in=room_ids).values('id', 'username', 'sex', 'rooms__id')
	for user in rooms_users:
		dict = res[user['rooms__id']][VarNames.ROOM_USERS]
		dict[user['id']] = RedisPrefix.set_js_user_structure(user['username'], user['sex'])
	return res
Exemplo n.º 3
0
 def invite_user(self, message):
     room_id = message[VarNames.ROOM_ID]
     user_id = message[VarNames.USER_ID]
     room = get_or_create_room(self.channels, room_id, user_id)
     users_in_room = {
         user.id: RedisPrefix.set_js_user_structure(user.username, user.sex)
         for user in room.users.all()
     }
     self.publish(
         self.add_user_to_room(room_id, user_id, users_in_room[user_id]),
         room_id)
     subscribe_message = self.invite_room_channel_message(
         room_id, user_id, room.name, users_in_room)
     self.publish(subscribe_message, RedisPrefix.generate_user(user_id),
                  True)
Exemplo n.º 4
0
    def register(self, username, password, email, sex):
        check_user(username)
        self.__check_password(password)
        self.__check_email__(email)
        user_profile = UserProfile(username=username, email=email, sex_str=sex)
        user_profile.set_password(password)
        user_profile.save()
        RoomUsers(user_id=user_profile.id,
                  room_id=settings.ALL_ROOM_ID,
                  notifications=False).save()

        user_data = {
            VarNames.ROOMS: [{
                VarNames.ROOM_ID:
                settings.ALL_ROOM_ID,
                VarNames.ROOM_USERS:
                list(
                    RoomUsers.objects.filter(room_id=ALL_ROOM_ID).values_list(
                        'user_id', flat=True))
            }],
            VarNames.EVENT:
            Actions.CREATE_NEW_USER,
            VarNames.HANDLER_NAME:
            HandlerNames.ROOM,
        }
        user_data.update(
            RedisPrefix.set_js_user_structure(user_profile.id,
                                              user_profile.username,
                                              user_profile.sex, None))
        global_redis.async_redis_publisher.publish(
            settings.ALL_ROOM_ID,
            json.dumps(user_data),
        )

        if email:
            yield from self.__send_sign_up_email(user_profile)
        return MessagesCreator.get_session(
            self.__generate_session__(user_profile.id))
Exemplo n.º 5
0
    def open(self):
        session_key = self.get_argument('sessionId', None)
        user_id = self.sync_redis.hget('sessions', session_key)
        if user_id is None:
            self.logger.warning('!! Session key %s has been rejected' %
                                session_key)
            self.close(403, "Session key %s has been rejected" % session_key)
            return
        self.user_id = int(user_id)
        self.ip = self.get_client_ip()
        user_db = UserProfile.objects.get(id=self.user_id)
        self.generate_self_id()
        self.message_creator = WebRtcMessageCreator(self.user_id, self.id)
        self._logger = logging.LoggerAdapter(parent_logger, {
            'id': self.id,
            'ip': self.ip
        })
        self.logger.debug("!! Incoming connection, session %s, thread hash %s",
                          session_key, self.id)
        self.async_redis.connect()
        self.async_redis_publisher.sadd(RedisPrefix.ONLINE_VAR, self.id)
        # since we add user to online first, latest trigger will always show correct online

        online = self.get_dict_users_from_redis()
        # current user is already online
        my_online = online.setdefault(self.user_id, [])
        if self.id not in my_online:
            my_online.append(self.id)

        was_online = len(online.get(
            self.user_id)) > 1  # if other tabs are opened
        user_rooms_query = Room.objects.filter(users__id=self.user_id, disabled=False) \
         .values('id', 'name', 'creator_id', 'channel_id', 'p2p', 'roomusers__notifications', 'roomusers__volume')
        room_users = [{
            VarNames.ROOM_ID: room['id'],
            VarNames.ROOM_NAME: room['name'],
            VarNames.CHANNEL_ID: room['channel_id'],
            VarNames.ROOM_CREATOR_ID: room['creator_id'],
            VarNames.NOTIFICATIONS: room['roomusers__notifications'],
            VarNames.P2P: room['p2p'],
            VarNames.VOLUME: room['roomusers__volume'],
            VarNames.ROOM_USERS: []
        } for room in user_rooms_query]
        user_rooms_dict = {room[VarNames.ROOM_ID]: room for room in room_users}
        channels_ids = [
            channel[VarNames.CHANNEL_ID] for channel in room_users
            if channel[VarNames.CHANNEL_ID]
        ]
        channels_db = Channel.objects.filter(Q(id__in=channels_ids)
                                             | Q(creator=self.user_id),
                                             disabled=False)
        channels = [{
            VarNames.CHANNEL_ID: channel.id,
            VarNames.CHANNEL_NAME: channel.name,
            VarNames.CHANNEL_CREATOR_ID: channel.creator_id
        } for channel in channels_db]
        room_ids = [room_id[VarNames.ROOM_ID] for room_id in room_users]
        rooms_users = RoomUsers.objects.filter(room_id__in=room_ids).values(
            'user_id', 'room_id')
        for ru in rooms_users:
            user_rooms_dict[ru['room_id']][VarNames.ROOM_USERS].append(
                ru['user_id'])
        # get all missed messages
        self.channels = room_ids  # py2 doesn't support clear()
        self.channels.append(self.channel)
        self.channels.append(self.id)
        self.listen(self.channels)
        # this was replaced to syncHistory method that's called from browser and passes existing ids
        # off_messages, history = self.get_offline_messages(room_users, was_online, self.get_argument('history', False))
        # for room in room_users:
        # 	room_id = room[VarNames.ROOM_ID]
        # 	h = history.get(room_id)
        # 	o = off_messages.get(room_id)
        # 	if h:
        # 		room[VarNames.LOAD_MESSAGES_HISTORY] = h
        # 	if o:
        # 		room[VarNames.LOAD_MESSAGES_OFFLINE] = o

        if settings.SHOW_COUNTRY_CODE:
            fetched_users = User.objects.annotate(user_c=Count('id')).values(
                'id', 'username', 'sex', 'userjoinedinfo__ip__country_code',
                'userjoinedinfo__ip__country', 'userjoinedinfo__ip__region',
                'userjoinedinfo__ip__city')
            user_dict = [
                RedisPrefix.set_js_user_structure_flag(
                    user['id'], user['username'], user['sex'],
                    user['userjoinedinfo__ip__country_code'],
                    user['userjoinedinfo__ip__country'],
                    user['userjoinedinfo__ip__region'],
                    user['userjoinedinfo__ip__city']) for user in fetched_users
            ]
        else:
            fetched_users = User.objects.values('id', 'username', 'sex')
            user_dict = [
                RedisPrefix.set_js_user_structure(user['id'], user['username'],
                                                  user['sex'])
                for user in fetched_users
            ]

        self.ws_write(
            self.message_creator.set_room(room_users, user_dict, online,
                                          user_db, channels))
        online_user_names_mes = self.message_creator.room_online_login(
            online, user_db.username, user_db.sex_str)
        self.logger.info('!! First tab, sending refresh online for all')
        self.publish(online_user_names_mes, settings.ALL_ROOM_ID)
        self.logger.info("!! User %s subscribes for %s", self.user_id,
                         self.channels)
        self.connected = True
Exemplo n.º 6
0
    def open(self):
        session_key = self.get_argument('sessionId', None)
        user_id = self.sync_redis.hget('sessions', session_key)
        if user_id is None:
            self.logger.warning('!! Session key %s has been rejected' %
                                session_key)
            self.close(403, "Session key %s has been rejected" % session_key)
            return
        self.user_id = int(user_id)
        self.ip = self.get_client_ip()
        user_db = UserProfile.objects.get(id=self.user_id)
        self.generate_self_id()
        self._logger = logging.LoggerAdapter(parent_logger, {
            'id': self.id,
            'ip': self.ip
        })
        self.logger.debug("!! Incoming connection, session %s, thread hash %s",
                          session_key, self.id)
        self.async_redis.connect()
        self.async_redis_publisher.sadd(RedisPrefix.ONLINE_VAR, self.id)
        # since we add user to online first, latest trigger will always show correct online
        was_online, online = self.get_online_and_status_from_redis()
        user_rooms_query = Room.objects.filter(users__id=self.user_id, disabled=False) \
         .values('id', 'name', 'roomusers__notifications', 'roomusers__volume')
        room_users = [{
            VarNames.ROOM_ID: room['id'],
            VarNames.ROOM_NAME: room['name'],
            VarNames.NOTIFICATIONS: room['roomusers__notifications'],
            VarNames.VOLUME: room['roomusers__volume'],
            VarNames.ROOM_USERS: []
        } for room in user_rooms_query]
        user_rooms_dict = {room[VarNames.ROOM_ID]: room for room in room_users}
        room_ids = [room_id[VarNames.ROOM_ID] for room_id in room_users]
        rooms_users = RoomUsers.objects.filter(room_id__in=room_ids).values(
            'user_id', 'room_id')
        for ru in rooms_users:
            user_rooms_dict[ru['room_id']][VarNames.ROOM_USERS].append(
                ru['user_id'])
        # get all missed messages
        self.channels = room_ids  # py2 doesn't support clear()
        self.channels.append(self.channel)
        self.channels.append(self.id)
        self.listen(self.channels)
        off_messages, history = self.get_offline_messages(
            room_users, was_online, self.get_argument('history', False))
        for room in room_users:
            room_id = room[VarNames.ROOM_ID]
            h = history.get(room_id)
            o = off_messages.get(room_id)
            if h:
                room[VarNames.LOAD_MESSAGES_HISTORY] = h
            if o:
                room[VarNames.LOAD_MESSAGES_OFFLINE] = o

        if settings.SHOW_COUNTRY_CODE:
            fetched_users = User.objects.annotate(user_c=Count('id')).values(
                'id', 'username', 'sex', 'userjoinedinfo__ip__country_code',
                'userjoinedinfo__ip__country', 'userjoinedinfo__ip__region',
                'userjoinedinfo__ip__city')
            user_dict = [
                RedisPrefix.set_js_user_structure_flag(
                    user['id'], user['username'], user['sex'],
                    user['userjoinedinfo__ip__country_code'],
                    user['userjoinedinfo__ip__country'],
                    user['userjoinedinfo__ip__region'],
                    user['userjoinedinfo__ip__city']) for user in fetched_users
            ]
        else:
            fetched_users = User.objects.values('id', 'username', 'sex')
            user_dict = [
                RedisPrefix.set_js_user_structure(user['id'], user['username'],
                                                  user['sex'])
                for user in fetched_users
            ]
        if self.user_id not in online:
            online.append(self.user_id)

        self.ws_write(self.set_room(room_users, user_dict, online, user_db))
        if not was_online:  # if a new tab has been opened
            online_user_names_mes = self.room_online_login(
                online, user_db.username, user_db.sex_str)
            self.logger.info('!! First tab, sending refresh online for all')
            self.publish(online_user_names_mes, settings.ALL_ROOM_ID)
        self.logger.info("!! User %s subscribes for %s", self.user_id,
                         self.channels)
        self.connected = True