Beispiel #1
0
 def getMessages(self, other_end=None, count=20, cursor=None):
     if other_end:
         message_pointers = MessagePointer.query(ancestor=MessagePointer.messagePointerKey(self, other_end)).order(
             -MessagePointer.timestamp)
     else:
         message_pointers = MessagePointer.query(ancestor=MessagePointer.messagePointerKey(self)).order(
             -MessagePointer.timestamp)
     if cursor:
         message_pointers, cursor, more = message_pointers.fetch_page(count, start_cursor=Cursor(urlsafe=cursor))
     else:
         message_pointers, cursor, more = message_pointers.fetch_page(count)
     messages = []
     for pointer in message_pointers:
         message = MessageModel.get_by_id(pointer.message_id, parent=MessageModel.messageKey(pointer.sender))
         epoch = datetime.datetime.fromtimestamp(0)
         timestamp = pointer.timestamp - epoch
         senderKey = UserKey.query(ancestor=UserKey.newKey(message.sender)).fetch(1)[0]
         messages.append({JSON_MSG_CONTENTS: message.contents,
                          JSON_MSG_KEY1: message.key1,
                          JSON_MSG_KEY2: message.key2,
                          JSON_MSG_RECEIVER: message.receiver,
                          JSON_MSG_SENDER: message.sender,
                          JSON_MSG_ID: pointer.key.id(),
                          JSON_MSG_TIMESTAMP: timestamp.total_seconds(),
                          JSON_MSG_SIGNATURE: message.signature,
                          JSON_MSG_SENDER_KEY: senderKey.pubkey, })
     if cursor:
         return {JSON_MESSAGES: messages, JSON_MESSAGES_CURSOR: cursor.urlsafe(), JSON_MORE_MESSAGES: more}
     else:
         return {JSON_MESSAGES: messages, JSON_MESSAGES_CURSOR: None, JSON_MORE_MESSAGES: False}
Beispiel #2
0
 def deleteAllMessages(self):
     messagePointers = MessagePointer.query(ancestor=MessagePointer.messagePointerKey(self)).iter()
     for pointer in messagePointers:
         message = MessageModel.get_by_id(pointer.message_id, parent=MessageModel.messageKey(pointer.sender))
         if pointer.sender == self.getId():
             message.pointerSender = False
         else:
             message.pointerReceiver = False
         message.put()
         if not (message.pointerSender or message.pointerReceiver):
             message.key.delete_async()
         pointer.key.delete_async()
Beispiel #3
0
 def post(self):
     if not self.requireParameters(POST_MSG_PEER, POST_MSG_KEY1, POST_MSG_KEY2, POST_MSG_CONTENTS,
                                   POST_MSG_SIGNATURE):
         return
     sender = self.user
     receiver = self.user_model.get_by_auth_id(self.request.POST[POST_MSG_PEER])
     if not receiver:
         self.response.write(RESPONSE_MSG_NO_PEER)
         return
     key1 = self.request.POST[POST_MSG_KEY1]
     key2 = self.request.POST[POST_MSG_KEY2]
     contents = self.request.POST[POST_MSG_CONTENTS]
     signature = self.request.POST[POST_MSG_SIGNATURE]
     message = MessageModel(parent=MessageModel.messageKey(sender.getId()))
     message.create(self.user.auth_ids[0], receiver.auth_ids[0], key1, key2, contents, signature)
     sender.addMessage(message, receiver, sender)
     receiver.addMessage(message, sender, sender)
     self.response.write(RESPONSE_OK)
Beispiel #4
0
 def post(self):
     if not self.requireParameters(POST_MSG_ID, POST_MSG_PEER):
         return
     messagePointerId = long(self.request.POST[POST_MSG_ID])
     other_end = self.user_model.get_by_auth_id(self.request.POST[POST_MSG_PEER])
     if not other_end:
         self.response.write(RESPONSE_MSG_NO_PEER)
         return
     pointer = MessagePointer.get_by_id(messagePointerId,
                                        parent=MessagePointer.messagePointerKey(self.user, other_end))
     if not pointer:
         self.response.write(RESPONSE_MSG_INVALID)
         return
     message = MessageModel.get_by_id(pointer.message_id, parent=MessageModel.messageKey(pointer.sender))
     if pointer.sender == other_end.getId():
         message.pointerReceiver = False
     else:
         message.pointerSender = False
     message.put()
     if not (message.pointerSender or message.pointerReceiver):
         message.key.delete()
     pointer.key.delete()
     self.response.write(RESPONSE_OK)