def send_message(message, giphy=None): if message[VarNames.TIME_DIFF] < 0: raise ValidationError("Back to the future?") files = UploadedFile.objects.filter(id__in=message.get( VarNames.FILES), user_id=self.user_id) symbol = get_max_key(files) channel = message[VarNames.ROOM_ID] js_id = message[VarNames.JS_MESSAGE_ID] message_db = Message(sender_id=self.user_id, content=message[VarNames.CONTENT], symbol=symbol, giphy=giphy, room_id=channel) message_db.time -= message[VarNames.TIME_DIFF] res_files = [] message_db.save() if files: images = up_files_to_img(files, message_db.id) res_files = MessagesCreator.prepare_img_video( images, message_db.id) prepared_message = self.create_send_message( message_db, Actions.PRINT_MESSAGE, res_files, ) prepared_message[VarNames.JS_MESSAGE_ID] = js_id self.publish(prepared_message, channel) self.notify_offline(channel, message_db.id)
def edit_message_edit(self, data, message): action = Actions.EDIT_MESSAGE message.giphy = None tags = data[VarNames.MESSAGE_TAGS] files = UploadedFile.objects.filter(id__in=data.get(VarNames.FILES), user_id=self.user_id) if files or tags: update_symbols(files, tags, message) if tags: db_tags = MessageMention.objects.filter(message_id=message.id) update_or_create = [] update_or_create_dict = {} for db_tag in db_tags: if tags.get(db_tag.symbol) and tags.get(db_tag.symbol) != db_tag.user_id: update_or_create.append(MessageMention(message_id=message.id, symbol=db_tag.symbol, user_id=tags[db_tag.symbol])) update_or_create_dict[db_tag.symbol] = True if update_or_create: MessageMention.objects.bulk_update(update_or_create) create_tags = [] for (k, v) in tags.items(): if not update_or_create_dict.get(k): create_tags.append(MessageMention(message_id=message.id, symbol=k, user_id=v)) if create_tags: MessageMention.objects.bulk_create(create_tags) if files: up_files_to_img(files, message.id) if message.symbol: # fetch all, including that we just store db_images = Image.objects.filter(message_id=message.id) prep_files = MessagesCreator.prepare_img_video(db_images, message.id) else: prep_files = None Message.objects.filter(id=message.id).update(content=message.content, symbol=message.symbol, giphy=None, edited_times=get_milliseconds()) self.publish(self.message_creator.create_send_message(message, action, prep_files, tags), message.room_id)
def search_messages(self, data, room, offset): offset = int(offset) if not RoomUsers.objects.filter(room_id=room, user_id=self.user_id).exists(): raise ValidationError("You can't access this room") messages = Message.objects.filter(content__icontains=data, room_id=room).order_by('-id')[ offset:offset + settings.MESSAGES_PER_SEARCH] imv = get_message_images_videos(messages) result = [] for message in messages: files = MessagesCreator.prepare_img_video(imv, message.id) prep_m = MessagesCreator.create_message(message, files) result.append(prep_m) return result
def send_message(message, giphy=None): if message[VarNames.TIME_DIFF] < 0: raise ValidationError("Back to the future?") tags_users = message[VarNames.MESSAGE_TAGS] files = UploadedFile.objects.filter(id__in=message.get(VarNames.FILES), user_id=self.user_id) symbol = max_from_2(get_max_symbol(files), get_max_symbol_dict(tags_users)) channel = message[VarNames.ROOM_ID] js_id = message[VarNames.JS_MESSAGE_ID] parent_message_id = message[VarNames.PARENT_MESSAGE] if parent_message_id: parent_room_id = Message.objects.get(id=parent_message_id).room_id if parent_room_id not in self.channels: raise ValidationError("You don't have access to this room message") message_db = Message( sender_id=self.user_id, content=message[VarNames.CONTENT], symbol=symbol, parent_message_id=parent_message_id, giphy=giphy, room_id=channel ) message_db.time -= message[VarNames.TIME_DIFF] res_files = [] message_db.save() if tags_users: mes_ment = [MessageMention( user_id=userId, message_id=message_db.id, symbol=symb, ) for symb, userId in tags_users.items()] MessageMention.objects.bulk_create(mes_ment) if files: images = up_files_to_img(files, message_db.id) res_files = MessagesCreator.prepare_img_video(images, message_db.id) prepared_message = self.message_creator.create_send_message( message_db, Actions.PRINT_MESSAGE, res_files, tags_users ) prepared_message[VarNames.JS_MESSAGE_ID] = js_id self.publish(prepared_message, channel) self.notify_offline(channel, message_db.id)
def search_messages(request): data = request.POST['data'] room_id = request.POST['room'] offset = int(request.POST['offset']) if not RoomUsers.objects.filter(room_id=room_id, user_id=request.user.id).exists(): raise ValidationError("You can't access this room") messages = Message.objects.filter( content__icontains=data, room_id=room_id).order_by('-id')[offset:offset + settings.MESSAGES_PER_SEARCH] imv = get_message_images_videos(messages) result = [] for message in messages: files = MessagesCreator.prepare_img_video(imv, message.id) prep_m = MessagesCreator.create_message(message, files) result.append(prep_m) response = json.dumps(result) return HttpResponse(response, content_type='application/json')
def edit_message_edit(self, data, message): action = Actions.EDIT_MESSAGE message.giphy = None files = UploadedFile.objects.filter(id__in=data.get(VarNames.FILES), user_id=self.user_id) if files: update_symbols(files, message) up_files_to_img(files, message.id) if message.symbol: # fetch all, including that we just store db_images = Image.objects.filter(message_id=message.id) prep_files = MessagesCreator.prepare_img_video( db_images, message.id) else: prep_files = None Message.objects.filter(id=message.id).update( content=message.content, symbol=message.symbol, giphy=None, edited_times=message.edited_times) self.publish(self.create_send_message(message, action, prep_files), message.room_id)
def send_message(message, giphy=None): files = UploadedFile.objects.filter(id__in=message.get( VarNames.FILES), user_id=self.user_id) symbol = get_max_key(files) channel = message[VarNames.CHANNEL] js_id = message[VarNames.JS_MESSAGE_ID] message_db = Message(sender_id=self.user_id, content=message[VarNames.CONTENT], symbol=symbol, giphy=giphy, room_id=channel) res_files = [] do_db(message_db.save) if files: images = up_files_to_img(files, message_db.id) res_files = MessagesCreator.prepare_img_video( images, message_db.id) prepared_message = self.create_send_message( message_db, Actions.PRINT_MESSAGE, res_files, js_id) self.publish(prepared_message, channel) self.notify_offline(channel, message_db.id)
def set_video_images_messages(self, imv, inm, outm): for message in inm: files = MessagesCreator.prepare_img_video(imv, message.id) prep_m = self.create_message(message, files) outm.setdefault(message.room_id, []).append(prep_m)