Exemplo n.º 1
0
def _sendMessage(sender, recipient, content, application=False):
    success = False
    if content:
        # Message is valid, check if there is a conversation already started between 2 users
        conversationID = None
        conversations1 = models.Conversation.objects.filter(user1=sender,
                                                            user2=recipient)
        conversations2 = models.Conversation.objects.filter(user1=recipient,
                                                            user2=sender)
        if conversations1 or conversations2:
            if conversations1 and len(conversations1) == 1:
                conversationID = conversations1[0].conversationID
            elif conversations2 and len(conversations2) == 1:
                conversationID = conversations2[0].conversationID
        if not conversationID:
            conversationID = helpers.createUniqueID(
                destDatabase=models.Conversation, idKey="conversationID")
            conversation = models.Conversation(conversationID=conversationID,
                                               user1=sender,
                                               user2=recipient)
            conversation.save()

        if conversationID:
            messageID = helpers.createUniqueID(destDatabase=models.Message,
                                               idKey="messageID")
            message = models.Message(messageID=messageID,
                                     conversationID=conversationID,
                                     sender=sender,
                                     recipient=recipient,
                                     content=content,
                                     applicationMessage=application)
            message.save()
            success = True
        return success
Exemplo n.º 2
0
def create_room():
    room_name = request.json.get('room_name')
    room = models.Conversation(conversation_name=room_name)
    print(room)
    for user_id in request.json.get('user_ids'):
        user = models.User.query.filter(models.User.id == user_id).first()
        room.users_in_this_conversation.append(user)
        print(user)
    db.session.add(room)
    db.session.commit()
    response = {'conversation_name': room_name, 'conversation_id': room.conversation_id}
    return "User registered succesfully"
Exemplo n.º 3
0
    def _parse_request_body(self, text):
        ''' Parses the contents of a POST body as JSON.
        Returns :class: `Conversation <Conversastion>` object 

        :param text: the contents of the request, as a string.
        '''

        data = json.loads(text)
        participants = data['to']
        participants.append(str(data['sender']))
        conversation = models.Conversation(subject=data['subject'],
                                           participants=participants)
        message = models.Message(sender=data['sender'], text=data['text'])
        return conversation, message
Exemplo n.º 4
0
def get_for_user(user_id):
    cursor = mysql.conn.cursor()

    # Select conversation_ids and last_msg_ids
    query = "SELECT conversation_id, last_msg_id FROM messenger.conversations_users WHERE user_id = %s ORDER BY last_msg_id DESC"
    cursor.execute(query, (user_id,))
    rows1 = cursor.fetchall()

    if len(rows1) == 0:
        return []

    conversation_ids = [conv_id for conv_id, _ in rows1]
    conversation_ids_str = ','.join([str(conv_id) for conv_id, _ in rows1])
    consersation_last_msg_ids = {conv_id: msg_id for conv_id, msg_id in rows1}
    msg_ids_str = ','.join(str(msg_id) for _, msg_id in rows1)

    # Select conversations data
    query = "SELECT id, title FROM messenger.conversations WHERE id IN (" + conversation_ids_str + ")"
    cursor.execute(query)
    rows2 = cursor.fetchall()

    # Select users in this conversations
    query = "SELECT conversation_id, user_id FROM messenger.conversations_users WHERE conversation_id IN (" + conversation_ids_str + ")"
    cursor.execute(query)
    rows3 = cursor.fetchall()

    conversations_users = collections.defaultdict(set)
    for conversation_id, user_id in rows3:
        conversations_users[conversation_id].add(user_id)

    # Select messages data
    query = "SELECT id, user_id, body, type, date, updated FROM messenger.messages WHERE id IN (" + msg_ids_str + ")"
    cursor.execute(query)
    rows4 = cursor.fetchall()

    messages = {id: deserialize_message(id, user_id, type, body, date, updated) for
                id, user_id, body, type, date, updated in rows4}

    conversations = {}
    for id, title in rows2:
        user_ids = conversations_users.get(id) or []
        last_msg = messages[consersation_last_msg_ids[id]]
        conversations[id] = models.Conversation(id, title, user_ids, last_msg)

    return [conversations[conv_id] for conv_id in conversation_ids]
Exemplo n.º 5
0
    def post_conversation(self):
        ''' Used to add messages to a conversation '''
        subject = self.request.get('subject')
        if not self._validate_missing_field(subject, 'subject'):
            return

        sender = utils.parseaddr(self.request.get('sender'))[1]
        if not self._validate_missing_field(sender, 'sender'):
            return

        recipient = utils.parseaddr(self.request.get('recipient'))[1]
        if not self._validate_missing_field(recipient, 'recipient'):
            return

        participants = [recipient, sender]
        conversation = models.Conversation(subject=subject,
                                           participants=participants)
        conversation.put()

        msg = models.Message(sender=sender,
                             text=self.request.get('text'),
                             conversation=conversation)
        notifications = msg.set_notifications()
        msg.put()

        # send mail only to the recipient
        mail.send_mail(sender='{0} <{1}>'.format(
            'YunoJuno notifications',
            '{0}@conversations-app.appspotmail.com'.format(
                conversation.key().id())),
                       to=recipient,
                       reply_to='{0}@conversations-app.appspotmail.com'.format(
                           conversation.key().id()),
                       subject=conversation.subject,
                       body=msg.text)

        self.response.set_status(201, 'Created')
        self.response.out.write(json.dumps(conversation.to_json()))
        return