Example #1
0
    def create(cls, group_id, msg_type):
        group = Group.get_by_id(group_id)
        participant_key = None
        participant_id = request.cookies.get("participant_id")

        if participant_id:
            participant_key = Participant.get_by_id(long(participant_id)).key

        message = request.form.get("message", u"", type=unicode)
        reference_id = request.form.get("reference", u"", type=unicode)

        chat = Chat(
            group_key=group.key,
            participant_key=participant_key,
            type=msg_type,
            message=message
        )

        #set reference if exist
        if reference_id:
            reference = Chat.get_by_id(long(reference_id))
            if reference is not None:
                chat.reference = reference.key.id()

        chat.put()

        # send same group members (include myself)
        cls.__broadcast(group_id, chat)

        # message is send by channel, so you don't need return
        return ""
Example #2
0
    def get(self):
        search_term = self.get_argument('name', None)
        if search_term:
            chats = Chat.objects(name__icontains=search_term, users__not__size=0)
        else:
            chats = Chat.objects(users__not__size=0)
        chats = chats.order_by('-users')

        rtn = [ch.to_representation() for ch in chats]
        self.write(json.dumps({'results': rtn}))
Example #3
0
    def post(self):
        try:
            data = json.loads(self.request.body)
            body = data['body']
            name = data['chat_id']
            username = data['username']
            chat = Chat.objects(slug_name=name).get()
            user = User.objects(username=username).get()
            self.set_status(201)

            message = Message(
                from_user=user,
                chat=chat,
                created_at=datetime.now(),
                body=body,
                is_private=self.is_private
            )

            users = [user for user in chat.users]
            if len(users) > 1:
                to_user = users[random.randrange(0, len(users))]
                while to_user.id == user.id:
                    to_user = users[random.randrange(0, len(users))]
                message.to_user = to_user

            message.save()

            self.write(message.to_representation())

        except (Chat.DoesNotExist, User.DoesNotExist):
            self.clear()
            self.set_status(404)
            self.finish({'error': 'not found'})
Example #4
0
 def get(self, name):
     try:
         chat = Chat.objects(slug_name=name).get()
         self.write(json.dumps(chat.to_representation()))
     except Chat.DoesNotExist:
         self.clear()
         self.set_status(404)
         self.finish({'error': 'not found'})
Example #5
0
def clean():
    for message in Message.objects():
        message.delete()

    for user in User.objects():
        user.delete()

    for chat in Chat.objects():
        chat.delete()
Example #6
0
    def update(cls, group_id, update_func):
        chat_id = request.form.get("id", u"", type=unicode)
        if chat_id:
            chat = Chat.get_by_id(long(chat_id))
            if chat is not None:
                update_func(chat)
                chat.put()
                cls.__broadcast(group_id, chat)

        return ""
Example #7
0
def do_reload(chats):
    clean()

    dummy_users_list = dummy_users()

    for chat in chats:
        messages = chat.pop('messages')
        ch = Chat(**chat)
        ch.save()
        ch.update(add_to_set__users=dummy_users_list)

        for i, message in enumerate(messages):
            m = Message(
                from_user=dummy_users_list[i % 2],
                chat=ch,
                created_at=datetime.now(),
                body=message,
            )
            m.save()
Example #8
0
    def post(self, name):
        try:
            data = json.loads(self.request.body)
            username = data['username']
            user = User.objects(username=username).get()
            chat = Chat.objects(slug_name=name).get()
            chat.update(add_to_set__users=[user])
            self.write({'info': 'User joined'})

        except (User.DoesNotExist, Chat.DoesNotExist):
            self.clear()
            self.set_status(404)
            self.finish({'error': 'not found'})
Example #9
0
 def delete(self, name):
     self.set_status(204)
     try:
         chat = Chat.objects(slug_name=name).get()
         data = json.loads(self.request.body)
         username = data['username']
         user = User.objects(username=username).get()
         chat.update(pull__users=user)
         chat.save()
     except (Chat.DoesNotExist, User.DoesNotExist):
         self.clear()
         self.set_status(404)
         self.finish({'error': 'not found'})
Example #10
0
 def get(self, name):
     try:
         chat = Chat.objects(slug_name=name).get()
         last_date = self.get_argument('last_date', None)
         if last_date:
             last_date = datetime.strptime(last_date, '%Y-%m-%dT%H:%M:%S.%f')
             messages = Message.objects(chat=chat, is_private=self.is_private, created_at__gt=last_date).order_by('created_at')
         else:
             messages = Message.objects(chat=chat, is_private=self.is_private).order_by('-created_at').limit(10)
             messages = [message for message in messages]
             messages.reverse()
         self.write({'results': [message.to_representation() for message in messages]})
     except Chat.DoesNotExist:
         self.clear()
         self.set_status(404)
         self.finish({'error': 'not found'})
Example #11
0
 def find(cls, group_id):
     group = Group.get_by_id(group_id)
     chats = Chat.query(Chat.group_key == group.key).order(-Chat.created_at).fetch(100)
     return jsonify(chats=list(map(lambda c: c.to_dict(), chats)))