def execute(self, message, message_data): print(message_data) to_return = {'intent:': self.name, 'success': False, 'error': None} if 'conversation_id' in message_data: conversation_id = message_data['conversation_id'] result = Conversation.objects.filter(id=conversation_id) if result.count() != 0: conv = result.first() if message.user in conv.participants.all(): to_return['success'] = True new_message = Message() new_message.content = message_data['content'] new_message.conversation = conv new_message.from_user = message.user new_message.save() unixtime = time.mktime(new_message.date_sent.timetuple()) for p in conv.participants.all(): Group("chat-%s" % p.username).send({ "text": json.dumps({"intent": "receive_message", "conversation_id": conversation_id, "content": message_data['content'], "date_sent": unixtime, "username": new_message.from_user.username}) }) else: to_return['error'] = "You do not have access to this conversation." elif 'username' in message_data: recipient_username = message_data['username'] if recipient_username in get_friends(message.user) and recipient_username != message.user.username: to_return['success'] = True recipient = User.objects.get(username=recipient_username) conv = Conversation() conv.save() conv.participants.add(recipient) conv.participants.add(message.user) new_message = Message() new_message.content = message_data['content'] new_message.conversation = conv new_message.from_user = message.user new_message.save() conv.save() print(new_message.date_sent) unixtime = time.mktime(new_message.date_sent.timetuple()) for p in conv.participants.all(): print(("chat-%s" % p.username)) Group("chat-%s" % p.username).send({ "text": json.dumps({"intent": "receive_message", "conversation_id": conv.id, "content": message_data['content'], "date_sent": unixtime, "username": message.from_user.username}) }) else: to_return['error'] = "You cannot speak with this user as you are not friends with the user." else: to_return['error'] = "You appear to have sent a malformed request." Group("chat-%s" % message.user.username).send({ "text": json.dumps(to_return) })
def receive(self, text_data): text_data_json = json.loads(text_data) content = text_data_json.get('content') room_pk = text_data_json.get('room_id') user_pk = text_data_json.get('user_id') match = re.match(r'^/stock=([A-Z]*)$', content) if match: tasks.query_quote.delay(match.group(1), room_pk) else: room = Room.objects.get(pk=room_pk) user = User.objects.get(pk=user_pk) message = Message() message.content = content message.room = room message.user = user message.save() datetime = message.datetime.isoformat() user_name = user.username async_to_sync(self.channel_layer.group_send)(self.group_name, { 'type': 'send_message', 'datetime': datetime, 'content': content, 'room_pk': room_pk, 'user_pk': user_pk, 'user_name': user_name })
def handle(self, *args, **options): if options['room'] and options['from'] and options['text']: msg = Message() msg.room = options['room'] msg.sender = options['from'] msg.content = options['text'] msg.save()
def new_message_object(self, sender, room_name, message_content): message = Message() if sender is not None: message.sender = sender message.room_name = room_name message.content = message_content message.created_at = now() return message
def reply(request, chat_room_id): chat = get_object_or_404(ChatRoom, pk=chat_room_id) #selected_choice = chat.message_set.get(pk=request.POST['choice']) m = Message() m.content = request.POST['message'] m.datetime = datetime.datetime.now() m.user = request.user chat.message_set.add(m) descstr = "Nowy post w " + chat.name change = Change() change.datetime = datetime.datetime.now() change.description = descstr change.user = request.user change.save() #chat.save() return render(request, 'chats/chat_room.html', {'chat': chat})
def message(request, socket, context, message): try: action = message["action"] session_key = message["session_key"] except KeyError: print "Session key error." return if action == "join": # Try to get the user from the dictionary try: user_entry = connected_users[session_key] except KeyError: print "Key Error." return # If the user is not logged in, reject the joining if not user_entry[1].is_authenticated() or user_entry[1].is_anonymous(): print "User error." return # Updating the current comptoir of the user connected_users[session_key] = (user_entry[0], user_entry[1], user_entry[2], message["cid"]) elif action == "identification": # Identification of the new user with the session key identify(socket, session_key) # Getting user entry in the dictionary user_entry = connected_users[session_key] # Iteration on all users for other_user in connected_users.values(): osock, ouser, ocmptrs, ocid = other_user[0], other_user[1], other_user[2], other_user[3] # Computing the common comptoirs between the new user and each other user common_cid = [c.id for c in set(ocmptrs).intersection(set(user_entry[2]))] # If there are some common comptoirs if len(common_cid) > 0: # We notify both entites that the other is connected # For the new arrivant, the notification is of type "Already connected" user_entry[0].send({"type": "connected", "user": ouser.username, "cmptrs": common_cid}) # For the others, the notification is of type "Just arrived" osock.send({"type": "joined", "user": user_entry[1].username, "cmptrs": common_cid}) elif action == "post": # Get user information from dictionary user = connected_users[session_key][1] # Get cid where to post the message cid = message["cid"] # Get the corresponding comptoir object comptoir = Comptoir.objects.get(id=cid) # If the hash of the comptoir key does not match with the db if (comptoir.key_hash != message["hash"]): # We reject the message socket.send({"type": "error", "error_msg": "Your message was rejected because your key is not valid."}) socket.send({"type": "ack"}) return # If the message is empty, reject it if message["content"] == "": socket.send({"type": "error", "error_msg": "Your message was rejected because it is empty."}) socket.send({"type": "ack"}) return # Try to determine if this is a '/me' message or not try: me_msg = message["me_msg"] except KeyError: me_msg = False # Creation of a new message object msg = Message(owner=user, comptoir=comptoir, content=message["content"], me_message=me_msg) # Saving the message msg.save() # Updating last message on the comptoir comptoir.last_message = msg comptoir.save() # At this point the date of the message is in utc format, so we need to correct it msg_local_date = msg.date.astimezone(timezone_local) # Iteration on each connected user to deliver the new message for other_user in connected_users.values(): osock, ouser, ocmptrs, ocid = other_user[0], other_user[1], other_user[2], other_user[3] # if the user is the one that posted the message if ouser == user: # Send acknoledgement socket.send({"type": "ack"}) # If the currently iterated user is related to the comptoir if comptoir in ocmptrs: # If the comptoir is not the one where the user is connected (and if the user is not the one # that posted the message) if ocid != cid and ouser != user: # We just send an update-badge message osock.send({"type": "update-badge", "cid": message["cid"], "user": user.username, "msgdate": date_to_tooltip(msg_local_date)}) else: # Else we deliver the message osock.send({ "type": "new-message", "cid": message["cid"], "user": user.username, "content": message["content"], "me_msg": me_msg, "msgdate": date_to_tooltip(msg_local_date), "mid": msg.id, }) elif action == "edit-msg": # Get user from dictionary user = connected_users[session_key][1] # Get the socket of the user sock = connected_users[session_key][0] # Get cid where to edit the message cid = message["cid"] # Get the corresponding comptoir object comptoir = Comptoir.objects.get(id=cid) # Get the id of the message to edit mid = message["mid"] # Get the message object msg = Message.objects.get(id=mid) # Check the owner of the message if msg.owner != user: sock.send({"type": "error", "error_msg": "You cannot edit a message that does not belong to you. The new message has not been saved on the server."}) return if msg.content != message["oldmsg"]: sock.send({"type": "error", "error_msg": "The edition has failed. The new message has not been saved on the server."}) return # If the hash of the comptoir key does not match with the db if (comptoir.key_hash != message["hash"]): # We reject the message socket.send({"type": "error", "error_msg": "Edition was rejected because your key is not valid."}) return # Update message content msg.content = message["newmsg"] msg.edited = True # Save modification msg.save() # Propagate the edition to connected users for other_user in connected_users.values(): osock, ouser, ocmptrs, ocid = other_user[0], other_user[1], other_user[2], other_user[3] # If the currently iterated user is related to the comptoir if cid == ocid: # We notify the modification osock.send({"type": "edit-msg", "cid": message["cid"], "mid": mid, "content": message["newmsg"]}) # To be commented elif action == "wizz": user = connected_users[session_key][1] cid = message["cid"] comptoir = Comptoir.objects.get(id=cid) context["cid"] = cid context["username"] = user.username if (comptoir.key_hash != message["hash"]): socket.send({"type": "error", "error_msg": "Your wizz was rejected because your key is not valid."}) else: for other_user in connected_users.values(): osock, ouser, ocmptrs, ocid = other_user[0], other_user[1], other_user[2], other_user[3] if comptoir in ocmptrs: if ocid == cid: osock.send({"type": "wizz", "from": user.username})