def first_message_to_user(content, email, device): try: user = db.session.query(UserModel).filter( UserModel.email == email).first() if user is None: raise e_account.UserNotFound elif not device.circle.has_member(user): raise e_cirle.UserNotPartOfCircle conversation = Conversation(device_access=True) conversation.circle = device.circle conversation.name = "NewConversation" link = UserToConversation(privilege="ADMIN") link.user = user link.conversation = conversation message = Message(content=content["text_message"] if "text_message" in content else "", is_user=False) message.conversation = conversation message.device = device media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() sockets.notify_user(user, False, 'conversation', { "conversation_id": conversation.id, "event": 'invite' }) info_and_message = "[" + conversation.name + "] " + device.name + " : " + str( message.text_content) try: messenger_conversation_model_send(0, conversation, info_and_message) except Exception: pass try: hangout_conversation_model_send(0, conversation, info_and_message) except Exception: pass response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id }, "status_code": 200 } except (e_cirle.CircleException, e_account.AccountException) as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def core_message_send(content, conversation_id, user): try: link = db.session.query(UserToConversation).filter( UserToConversation.conversation_id == conversation_id, UserToConversation.user_id == user.id).first() if link is None: raise e_conversation.ConversationNotFound message = Message(content=content["text_message"] if "text_message" in content else "") message.conversation = link.conversation message.link = link media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id }, "status_code": 200 } except e_conversation.ConversationException as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def first_message_to_user(content, email, circle_id, user): try: dest = db.session.query(UserModel).filter( UserModel.email == email).first() circle = db.session.query(Circle).filter( Circle.id == circle_id).first() if dest is None: raise e_message.TargetUserNotFound if circle is None: raise e_circle.CircleNotFound if not circle.has_member(user): raise e_circle.UserNotPartOfCircle if not circle.has_member(dest) and circle.has_member(user): raise e_circle.TargetUserNotPartOfCircle conversation = Conversation() conversation.circle = circle UserToConversation(privilege="ADMIN", user=user, conversation=conversation) UserToConversation(privilege="STANDARD", user=dest, conversation=conversation) message = Message(content=content["text_message"] if "text_message" in content else "") message.conversation = conversation media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() sockets.notify_user(dest, False, 'conversation', { "conversation_id": conversation.id, "event": 'invite' }) message.conversation.mobile_notification( title="Conversation", body="Nouvelle conversation avec " + user.first_name) info_sender = "[" + conversation.name + "] " + user.first_name + " : " try: messenger_conversation_model_send( user.id, conversation, info_sender + message.text_content) except Exception: pass try: hangout_conversation_model_send(user.id, conversation, info_sender + message.text_content) except Exception: pass response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id, 'conversation_id': message.conversation_id }, "status_code": 200 } except (e_message.MessageException, e_circle.CircleException) as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def message_send(content, conversation_id, user, standalone=False): try: link = db.session.query(UserToConversation).filter( UserToConversation.conversation_id == conversation_id, UserToConversation.user_id == user.id).first() if link is None: raise e_conversation.ConversationNotFound message = Message(content=content["text_message"] if "text_message" in content else "") message.conversation = link.conversation message.link = link media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() if standalone is False: if not media_list: emit('message', { 'conversation_id': message.conversation_id, 'message': message.get_simple_json_compliant_content(), 'time': message.sent.isoformat(), 'sender': user.get_simple_json_compliant_content(), 'media_list': media_list, 'status': 'done' }, room='conversation_' + str(message.conversation_id), namespace='/') else: emit('message', { 'conversation_id': message.conversation_id, 'message': message.get_simple_json_compliant_content(), 'status': 'pending' }, room='conversation_' + str(message.conversation_id), namespace='/') message.conversation.mobile_notification( title="Message", body=user.first_name + " vous à envoyer un message.") conversation = db.session.query(Conversation).filter( link.conversation_id == Conversation.id).first() info_sender = "[" + link.conversation.name + "] " + user.first_name + " : " try: messenger_conversation_model_send( link.user_id, conversation, info_sender + message.text_content) except Exception: pass try: hangout_conversation_model_send( link.user_id, conversation, info_sender + message.text_content) except Exception: pass response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id }, "status_code": 200 } except e_conversation.ConversationException as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def message_send(conversation_id, content, socket): client = socket.get_client() conv = db.session.query(Conversation).filter( Conversation.id == conversation_id).first() if conv is None: raise Exception('Conversation introuvable') if socket.is_device: message = Message(content=content["text_message"] if "text_message" in content else "", is_user=False) message.conversation = conv message.device = client info_sender = "[" + conv.name + "] " + client.name + " : " else: link = db.session.query(UserToConversation). \ filter(UserToConversation.user_id == socket.client_id, UserToConversation.conversation_id == conv.id).first() if link is None: raise Exception("Vous ne faites pas partie de cette conversation") message = Message(content=content['text_message'] if 'text_message' in content else '') message.conversation = conv message.link = link info_sender = "[" + conv.name + "] " + client.first_name + " : " db.session.commit() user = db.session.query(User).filter(socket.client_id == User.id).first() message.conversation.mobile_notification(title="Message", body=user.first_name + " vous à envoyer un message.") try: messenger_conversation_model_send( 0 if socket.is_device else socket.client_id, conv, info_sender + content["text_message"]) except Exception as e: logger.info("[%s] [%s] [%s] [%s] [%s] [%s]", "SOCKET", "", "message", type(content), content, "FACEBOOK FAILED : " + str(e)) try: hangout_conversation_model_send( 0 if socket.is_device else socket.client_id, conv, info_sender + content["text_message"]) except Exception as e: logger.info("[%s] [%s] [%s] [%s] [%s] [%s]", "SOCKET", "", "message", type(content), content, "HANGOUT FAILED : " + str(e)) media_list = [] if 'files' in content: for file in content["files"]: media = Media() media.identifier = file media.message = message db.session.commit() media_list.append(media.get_json_compliant_content()) socket.emit('media', { 'media_list': media_list, 'message_id': message.id }) socket.emit('success', {'received': True, 'message_id': message.id}) if not media_list: emit('message', { 'conversation_id': conv.id, 'message': message.get_simple_json_compliant_content(), 'time': message.sent.isoformat(), 'sender': client.get_simple_json_compliant_content(), 'media_list': media_list, 'status': 'done' }, room='conversation_' + str(conv.id), namespace='/') else: emit('message', { 'conversation_id': conv.id, 'message': message.get_simple_json_compliant_content(), 'status': 'pending' }, room='conversation_' + str(conv.id), namespace='/')
def message_send(content, conversation_id, device): try: conv = db.session.query(Conversation).filter( Conversation.id == conversation_id).first() if conv is None: raise e_conversation.ConversationNotFound elif conv.device_access is False or conv.circle.id != device.circle.id: raise e_conversation.UserForbiddenAccess message = Message(content=content["text_message"] if "text_message" in content else "", is_user=False) message.conversation = conv message.device = device media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() if not media_list: emit('message', { 'conversation_id': message.conversation_id, 'message': message.get_simple_json_compliant_content(), 'time': message.sent.isoformat(), 'sender': device.get_simple_json_compliant_content(), 'media_list': media_list, 'status': 'done' }, room='conversation_' + str(message.conversation_id), namespace='/') else: emit('message', { 'conversation_id': message.conversation_id, 'message': message.get_simple_json_compliant_content(), 'status': 'pending' }, room='conversation_' + str(message.conversation_id), namespace='/') info_sender = "[" + conv.name + "] " + device.name + " : " try: messenger_conversation_model_send( 0, conv, info_sender + message.text_content) except Exception: pass try: hangout_conversation_model_send(0, conv, info_sender + message.text_content) except Exception: pass response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id }, "status_code": 200 } except e_conversation.ConversationException as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response