コード例 #1
0
ファイル: message_handler.py プロジェクト: lmyfzx/pychat
	def save_channels_settings(self, message):
		channel_id = message[VarNames.CHANNEL_ID]
		channel_name = message[VarNames.CHANNEL_NAME]
		new_creator = message[VarNames.CHANNEL_CREATOR_ID]
		if not channel_name or len(channel_name) > 16:
			raise ValidationError('Incorrect channel name name "{}"'.format(channel_name))
		channel = Channel.objects.get(id=channel_id)
		users_id = list(RoomUsers.objects.filter(room__channel_id=channel_id).values_list('user_id', flat=True))
		if channel.creator_id != self.user_id and self.user_id not in users_id:
			raise ValidationError("You are not allowed to edit this channel")
		if new_creator != channel.creator_id:
			if self.user_id != channel.creator_id:
				raise ValidationError("Only admin can change the admin of this channel")
			if new_creator not in users_id:
				raise ValidationError("You can only change admin to one of the users in this channels room")
			channel.creator_id = new_creator
		if self.user_id not in users_id: # if channel has no rooms
			users_id.append(self.user_id)
		channel.name = channel_name
		channel.save()
		m = {
			VarNames.EVENT: Actions.SAVE_CHANNEL_SETTINGS,
			VarNames.CHANNEL_ID: channel_id,
			VarNames.CB_BY_SENDER: self.id,
			VarNames.HANDLER_NAME: HandlerNames.ROOM,
			VarNames.CHANNEL_NAME: channel_name,
			VarNames.CHANNEL_CREATOR_ID: channel.creator_id,
			VarNames.TIME: get_milliseconds(),
			VarNames.JS_MESSAGE_ID: message[VarNames.JS_MESSAGE_ID],
		}
		for user_id in users_id:
			self.publish(m, RedisPrefix.generate_user(user_id))
コード例 #2
0
ファイル: http_handler.py プロジェクト: unyime1/pychat
    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
コード例 #3
0
ファイル: message_handler.py プロジェクト: area55git/pychat
 def create_user_channel(self, message):
     user_id = message[VarNames.USER_ID]
     room_id = create_room(self.user_id, user_id)
     subscribe_message = self.subscribe_direct_channel_message(
         room_id, user_id, self.user_id != user_id)
     self.publish(subscribe_message, self.channel, True)
     other_channel = RedisPrefix.generate_user(user_id)
     if self.channel != other_channel:
         self.publish(subscribe_message, other_channel, True)
コード例 #4
0
	def upload_profile_image(self, files):
		"""
		POST only, validates email during registration
		"""
		up = UserProfile(photo=files['file'], id=self.user_id)
		up.save(update_fields=('photo',))
		url = up.photo.url
		message = json.dumps(MessagesCreator.set_profile_image(url))
		channel = RedisPrefix.generate_user(self.user_id)
		global_redis.sync_redis.publish(channel, message)
		return settings.VALIDATION_IS_OK
コード例 #5
0
ファイル: message_handler.py プロジェクト: lmyfzx/pychat
	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)
コード例 #6
0
ファイル: views.py プロジェクト: ruankranz/pychat
def upload_profile_image(request):
    """
	POST only, validates email during registration
	"""

    up = UserProfile(photo=request.FILES['file'], id=request.user.id)
    up.save(update_fields=('photo', ))
    url = up.photo.url
    message = json.dumps(MessagesCreator.set_profile_image(url))
    channel = RedisPrefix.generate_user(request.user.id)
    global_redis.sync_redis.publish(channel, message)
    return HttpResponse(settings.VALIDATION_IS_OK, content_type='text/plain')
コード例 #7
0
ファイル: message_handler.py プロジェクト: area55git/pychat
 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)
コード例 #8
0
    def save_room_settings(self, message):
        """
		POST only, validates email during registration
		"""
        room_id = message[VarNames.ROOM_ID]
        room_name = message[VarNames.ROOM_NAME]
        creator_id = message.get(
            VarNames.ROOM_CREATOR_ID)  # will be none for private room
        updated = RoomUsers.objects.filter(
            room_id=room_id, user_id=self.user_id).update(
                volume=message[VarNames.VOLUME],
                notifications=message[VarNames.NOTIFICATIONS])
        if updated != 1:
            raise ValidationError("You don't have access to this room")
        room = Room.objects.get(id=room_id)
        update_all = False
        if not room.name:
            if room.p2p != message[VarNames.P2P]:
                room.p2p = message[VarNames.P2P]
                update_all = True
        elif room_id != settings.ALL_ROOM_ID:
            if room_name != room.name:
                room.name = room_name
                update_all = True

            if room.channel_id != message[VarNames.CHANNEL_ID]:
                room.channel_id = message[VarNames.CHANNEL_ID]
                if room.channel_id and room.channel_id not in self.get_users_channels_ids(
                ):
                    raise ValidationError(
                        "You don't have access to this channel")
                update_all = True
            if creator_id != room.creator_id:
                if room.creator_id != self.user_id:
                    raise ValidationError(
                        "Only an owner of this room can change its admin")
                users_id = RoomUsers.objects.filter(
                    room_id=room.id).values_list('user_id', flat=True)
                if creator_id not in users_id:
                    raise ValidationError(
                        "You can only change admin to one of the users in this channels room"
                    )
                room.creator_id = creator_id
                update_all = True
        if message.get(VarNames.CHANNEL_ID):  # will be nOne for private room
            channel = Channel.objects.get(id=message[VarNames.CHANNEL_ID])
            channel_name = channel.name
            channel_creator_id = channel.creator_id
        else:
            channel_name = None
            channel_creator_id = None
        if update_all:
            room.save()
            room_users = list(RoomUsers.objects.filter(room_id=room_id))
            for room_user in room_users:
                self.publish(
                    {
                        VarNames.EVENT: Actions.SAVE_ROOM_SETTINGS,
                        VarNames.CHANNEL_ID: room.channel_id,
                        VarNames.CB_BY_SENDER: self.id,
                        VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
                        VarNames.CHANNEL_NAME: channel_name,
                        VarNames.CHANNEL_CREATOR_ID: channel_creator_id,
                        VarNames.ROOM_CREATOR_ID: room.creator_id,
                        VarNames.ROOM_ID: room.id,
                        VarNames.VOLUME: room_user.volume,
                        VarNames.NOTIFICATIONS: room_user.notifications,
                        VarNames.P2P: message[VarNames.P2P],
                        VarNames.ROOM_NAME: room_name,
                        VarNames.TIME: get_milliseconds(),
                        VarNames.JS_MESSAGE_ID:
                        message[VarNames.JS_MESSAGE_ID],
                    }, RedisPrefix.generate_user(room_user.user_id))
        else:
            self.publish(
                {
                    VarNames.EVENT: Actions.SAVE_ROOM_SETTINGS,
                    VarNames.CHANNEL_ID: room.channel_id,
                    VarNames.CB_BY_SENDER: self.id,
                    VarNames.CHANNEL_CREATOR_ID: channel_creator_id,
                    VarNames.ROOM_CREATOR_ID: room.creator_id,
                    VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
                    VarNames.CHANNEL_NAME: channel_name,
                    VarNames.ROOM_ID: room.id,
                    VarNames.VOLUME: message[VarNames.VOLUME],
                    VarNames.NOTIFICATIONS: message[VarNames.NOTIFICATIONS],
                    VarNames.P2P: message[VarNames.P2P],
                    VarNames.ROOM_NAME: room_name,
                    VarNames.TIME: get_milliseconds(),
                    VarNames.JS_MESSAGE_ID: message[VarNames.JS_MESSAGE_ID],
                }, self.channel)
コード例 #9
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))
コード例 #10
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))
コード例 #11
0
ファイル: message_handler.py プロジェクト: lmyfzx/pychat
	def channel(self):
		return RedisPrefix.generate_user(self.user_id)