Пример #1
0
def create_other_room_wo_check(self_user_id, user_id):
	room = Room()
	room.save()
	room_id = room.id
	if self_user_id == user_id:
		RoomUsers(user_id=user_id, room_id=room_id).save()
	else:
		RoomUsers.objects.bulk_create([
			RoomUsers(user_id=self_user_id, room_id=room_id),
			RoomUsers(user_id=user_id, room_id=room_id),
		])
	return room_id
Пример #2
0
 def create_user_profile(self, email, name, surname, picture):
     try:
         user_profile = UserProfile.objects.get(email=email)
         self.logger.info("Sign in as %s with id %s", user_profile.username,
                          user_profile.id)
     except UserProfile.DoesNotExist:
         try:
             self.logger.info(
                 "Creating new user with email %s, name %s, surname %s, picture %s",
                 email, name, surname, picture)
             # replace all characters but a valid one with '-' and cut to 15 chars
             username = re.sub('[^0-9a-zA-Z-_]+', '-',
                               email.rsplit('@')[0])[:15]
             check_user(username)
         except ValidationError as e:
             self.logger.info("Can't use username %s because %s", username,
                              e)
             username = id_generator(8)
         self.logger.debug("Generated username: %s", username)
         user_profile = UserProfile(name=name,
                                    surname=surname,
                                    email=email,
                                    username=username)
         self.download_http_photo(picture, user_profile)
         user_profile.save()
         RoomUsers(user_id=user_profile.id,
                   room_id=settings.ALL_ROOM_ID,
                   notifications=False).save()
     return user_profile
Пример #3
0
def create_other_room(self_user_id, user_id, user_rooms):
	rooms_query = RoomUsers.objects.filter(user_id=user_id, room__in=user_rooms)
	query = rooms_query.values('room__id', 'room__disabled')
	try:
		room = do_db(query.get)
		room_id = room['room__id']
		update_room(room_id, room['room__disabled'])
	except RoomUsers.DoesNotExist:
		room = Room()
		room.save()
		room_id = room.id
		RoomUsers.objects.bulk_create([
			RoomUsers(user_id=self_user_id, room_id=room_id),
			RoomUsers(user_id=user_id, room_id=room_id),
		])
	return room_id
Пример #4
0
    def create_user_profile(self,
                            user_profile_query,
                            name,
                            surname,
                            picture=None,
                            email=None,
                            fb_id=None,
                            google_id=None):
        try:
            user_profile = UserProfile.objects.get(**user_profile_query)
            self.logger.info("Sign in as %s with id %s", user_profile.username,
                             user_profile.id)
        except UserProfile.DoesNotExist:
            if email and UserProfile.objects.filter(email=email).exists():
                raise ValidationError(
                    f"User with email {email} already exists. If this is you, please sign in from your account and connect with social auth in settings."
                )

            self.logger.info(
                "Creating new user with email %s, name %s, surname %s, picture %s",
                email, name, surname, picture)
            username = self.get_user_name(email, name, surname)
            user_profile = UserProfile(name=name,
                                       surname=surname,
                                       email=email,
                                       username=username,
                                       facebook_id=fb_id,
                                       google_id=google_id)
            self.download_http_photo(picture, user_profile)
            user_profile.save()
            RoomUsers(user_id=user_profile.id,
                      room_id=settings.ALL_ROOM_ID,
                      notifications=False).save()
        return user_profile
Пример #5
0
def create_user_model(user):
    user.save()
    RoomUsers(user_id=user.id,
              room_id=settings.ALL_ROOM_ID,
              notifications=False).save()
    logger.info('Signed up new user %s, subscribed for channels with id %d',
                user, settings.ALL_ROOM_ID)
    return user
Пример #6
0
	def create_new_room(self, message):
		room_name = message[VarNames.ROOM_NAME]
		if not room_name or len(room_name) > 16:
			raise ValidationError('Incorrect room name "{}"'.format(room_name))
		room = Room(name=room_name)
		self.do_db(room.save)
		RoomUsers(room_id=room.id, user_id=self.user_id).save()
		subscribe_message = self.subscribe_room_channel_message(room.id, room_name)
		self.publish(subscribe_message, self.channel, True)
Пример #7
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()
		if email:
			yield from self.__send_sign_up_email(user_profile)
		return self.__generate_session__(user_profile.id)
Пример #8
0
	def invite_user(self, message):
		room_id = message[VarNames.ROOM_ID]
		if room_id not in self.channels:
			raise ValidationError("Access denied, only allowed for channels {}".format(self.channels))
		room = Room.objects.get(id=room_id)
		if room.is_private:
			raise ValidationError("You can't add users to direct room, create a new room instead")
		users = message.get(VarNames.ROOM_USERS)
		users_in_room = list(RoomUsers.objects.filter(room_id=room_id).values_list('user_id', flat=True))
		intersect = set(users_in_room) & set(users)
		if bool(intersect):
			raise ValidationError("Users %s are already in the room", intersect)
		users_in_room.extend(users)

		max_id = Message.objects.filter(room_id=room_id).aggregate(Max('id'))['id__max']
		if not max_id:
			max_id = Message.objects.all().aggregate(Max('id'))['id__max']
		ru = [RoomUsers(
			user_id=user_id,
			room_id=room_id,
			last_read_message_id=max_id,
			volume=1,
			notifications=False
		) for user_id in users]
		RoomUsers.objects.bulk_create(ru)

		add_invitee = {
			VarNames.EVENT: Actions.ADD_INVITE,
			VarNames.ROOM_ID: room_id,
			VarNames.ROOM_USERS: users_in_room,
			VarNames.ROOM_NAME: room.name,
			VarNames.INVITEE_USER_ID: users,
			VarNames.INVITER_USER_ID: self.user_id,
			VarNames.HANDLER_NAME: HandlerNames.ROOM,
			VarNames.TIME: get_milliseconds(),
			VarNames.VOLUME: 1,
			VarNames.NOTIFICATIONS: False,
		}
		add_invitee_dumped = encode_message(add_invitee, True)
		for user in users:
			self.raw_publish(add_invitee_dumped, RedisPrefix.generate_user(user))

		invite = {
			VarNames.EVENT: Actions.INVITE_USER,
			VarNames.ROOM_ID: room_id,
			VarNames.INVITEE_USER_ID: users,
			VarNames.INVITER_USER_ID: self.user_id,
			VarNames.HANDLER_NAME: HandlerNames.ROOM,
			VarNames.ROOM_USERS: users_in_room,
			VarNames.TIME: get_milliseconds(),
			VarNames.CB_BY_SENDER: self.id,
			VarNames.JS_MESSAGE_ID: message[VarNames.JS_MESSAGE_ID]
		}
		self.publish(invite, room_id, True)
Пример #9
0
def create_self_room(self_user_id, user_rooms):
	room_ids = list([room['room_id'] for room in evaluate(user_rooms)])
	query = execute_query(settings.SELECT_SELF_ROOM, [room_ids, ])
	if query:
		room_id = query[0]['room__id']
		update_room(room_id, query[0]['room__disabled'])
	else:
		room = Room()
		room.save()
		room_id = room.id
		RoomUsers(user_id=self_user_id, room_id=room_id, notifications=False).save()
	return room_id
Пример #10
0
	def create_room(self, user_rooms, user_id):
		if self.user_id == user_id:
			room_ids = list([room['room_id'] for room in user_rooms])
			query_res = self.execute_query(SELECT_SELF_ROOM, [room_ids, ])
		else:
			rooms_query = Room.users.through.objects.filter(user_id=user_id, room__in=user_rooms)
			query_res = rooms_query.values('room__id', 'room__disabled')
		if len(query_res) > 0:
			room = query_res[0]
			room_id = room['room__id']
			self.update_room(room_id, room['room__disabled'])
		else:
			room = Room()
			room.save()
			room_id = room.id
			if self.user_id == user_id:
				RoomUsers(user_id=self.user_id, room_id=room_id).save()
			else:
				RoomUsers.objects.bulk_create([
					RoomUsers(user_id=user_id, room_id=room_id),
					RoomUsers(user_id=self.user_id, room_id=room_id),
				])
		return room_id
Пример #11
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))
Пример #12
0
def create_user_model(user):
    user.save()
    RoomUsers(user_id=user.id, room_id=ALL_ROOM_ID).save()
    logger.info('Signed up new user %s, subscribed for channels with id %d',
                user, ALL_ROOM_ID)
    return user
Пример #13
0
    def create_new_room(self, message):
        room_name = message.get(VarNames.ROOM_NAME)
        users = message.get(VarNames.ROOM_USERS)
        channel_id = message.get(VarNames.CHANNEL_ID)
        users.append(self.user_id)
        users = list(set(users))
        if room_name and len(room_name) > 16:
            raise ValidationError('Incorrect room name "{}"'.format(room_name))
        create_room = True
        if not room_name and len(users) == 2:
            user_rooms = evaluate(
                Room.users.through.objects.filter(
                    user_id=self.user_id,
                    room__name__isnull=True).values('room_id'))
            user_id = users[0] if users[1] == self.user_id else users[1]
            try:
                room = RoomUsers.objects.filter(user_id=user_id,
                                                room__in=user_rooms).values(
                                                    'room__id',
                                                    'room__disabled').get()
                room_id = room['room__id']
                if room['room__disabled']:  # only a private room can be disabled
                    Room.objects.filter(id=room_id).update(
                        disabled=False, p2p=message[VarNames.P2P])
                    RoomUsers.objects.filter(
                        user_id=self.user_id, room_id=room_id).update(
                            volume=message[VarNames.VOLUME],
                            notifications=message[VarNames.NOTIFICATIONS])
                else:
                    raise ValidationError('This room already exist')
                create_room = False
            except RoomUsers.DoesNotExist:
                pass
        elif not room_name:
            raise ValidationError(
                'At least one user should be selected, or room should be public'
            )

        if channel_id and channel_id not in self.get_users_channels_ids():
            raise ValidationError("You don't have access to this channel")
        if channel_id:
            channel = Channel.objects.get(id=channel_id)
            channel_name = channel.name
            channel_creator_id = channel.creator_id
        else:
            channel_name = None
            channel_creator_id = None
        if create_room:
            room = Room(name=room_name,
                        channel_id=channel_id,
                        p2p=message[VarNames.P2P])
            if not room_name:
                room.creator_id = self.user_id
            room.save()
            room_id = room.id
            max_id = Message.objects.all().aggregate(Max('id'))['id__max']
            ru = [
                RoomUsers(user_id=user_id,
                          room_id=room_id,
                          last_read_message_id=max_id,
                          volume=message[VarNames.VOLUME],
                          notifications=message[VarNames.NOTIFICATIONS])
                for user_id in users
            ]
            RoomUsers.objects.bulk_create(ru)

        m = {
            VarNames.EVENT: Actions.CREATE_ROOM,
            VarNames.ROOM_ID: room_id,
            VarNames.ROOM_USERS: users,
            VarNames.CB_BY_SENDER: self.id,
            VarNames.INVITER_USER_ID: self.user_id,
            VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
            VarNames.VOLUME: message[VarNames.VOLUME],
            VarNames.P2P: message[VarNames.P2P],
            VarNames.NOTIFICATIONS: message[VarNames.NOTIFICATIONS],
            VarNames.ROOM_NAME: room_name,
            VarNames.TIME: get_milliseconds(),
            VarNames.JS_MESSAGE_ID: message[VarNames.JS_MESSAGE_ID],
        }
        if channel_id:
            m[VarNames.CHANNEL_NAME] = channel_name
            m[VarNames.CHANNEL_ID] = channel_id
            m[VarNames.CHANNEL_CREATOR_ID] = channel_creator_id
        jsoned_mess = encode_message(m, True)
        for user in users:
            self.raw_publish(jsoned_mess, RedisPrefix.generate_user(user))
Пример #14
0
    def create_new_room(self, message):
        room_name = message.get(VarNames.ROOM_NAME)
        users = message.get(VarNames.ROOM_USERS)
        users.append(self.user_id)
        users = list(set(users))
        if room_name and len(room_name) > 16:
            raise ValidationError('Incorrect room name "{}"'.format(room_name))
        create_user_rooms = True
        if not room_name and len(users) == 2:
            user_rooms = evaluate(
                Room.users.through.objects.filter(
                    user_id=self.user_id,
                    room__name__isnull=True).values('room_id'))
            user_id = users[0] if users[1] == self.user_id else users[1]
            try:
                room = RoomUsers.objects.filter(user_id=user_id,
                                                room__in=user_rooms).values(
                                                    'room__id',
                                                    'room__disabled').get()
                room_id = room['room__id']
                if room['room__disabled']:
                    Room.objects.filter(id=room_id).update(disabled=False)
                else:
                    raise ValidationError('This room already exist')
                create_user_rooms = False
            except RoomUsers.DoesNotExist:
                pass
        elif not room_name:
            raise ValidationError(
                'At least one user should be selected, or room should be public'
            )
        if create_user_rooms:
            room = Room(name=room_name)
            do_db(room.save)
            room_id = room.id
            max_id = Message.objects.all().aggregate(Max('id'))['id__max']
            ru = [
                RoomUsers(user_id=user_id,
                          room_id=room_id,
                          last_read_message_id=max_id,
                          volume=message[VarNames.VOLUME],
                          notifications=message[VarNames.NOTIFICATIONS])
                for user_id in users
            ]
            RoomUsers.objects.bulk_create(ru)

        m = {
            VarNames.EVENT: Actions.CREATE_ROOM_CHANNEL,
            VarNames.ROOM_ID: room_id,
            VarNames.ROOM_USERS: users,
            VarNames.CB_BY_SENDER: self.id,
            VarNames.INVITER_USER_ID: self.user_id,
            VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
            VarNames.VOLUME: message[VarNames.VOLUME],
            VarNames.NOTIFICATIONS: message[VarNames.NOTIFICATIONS],
            VarNames.ROOM_NAME: room_name,
            VarNames.TIME: get_milliseconds(),
            VarNames.JS_MESSAGE_ID: message[VarNames.JS_MESSAGE_ID],
        }
        jsoned_mess = encode_message(m, True)
        for user in users:
            self.raw_publish(jsoned_mess, RedisPrefix.generate_user(user))