Commits

Konstantine Rybnikov committed 27bd64b

implemented read message cmd

Comments (0)

Files changed (5)

+=====
+TODO
+=====
+
+#. Implement commands::
+    - read message
+    - NICK
+    - reply to message
+    - reply to reply
+
+#. Write tests.
 
 from openju.model import *
 
+def post_message(user, body):
+    message = (
+        Message(
+            user=user,
+            body=body))
+    message.save()
+    return message
+
+def get_last_messages(skip=None, limit=10):
+    return Message.objects(reply_to__exists=False)[skip:limit]
+
+def get_message(message_id):
+    """
+    Raises ``MultipleObjectsReturned`` or ``DocumentName.MultipleObjectsReturned``
+    or ``DoesNotExist`` or ``DocumentName.DoesNotExist`` errors. Need to catch
+    them in controller.
+    """
+    return Message.objects.get(id=message_id)
+
 # from openju import g
 
 # def get_next_hrid():
 #         val = last_reply_no[0].get('r_no') or 0
 #         return val + 1
 
-def post_message(user, body):
-    message = (
-        Message(
-            user=user,
-            body=body))
-    message.save()
-    return message
-    
     # next_hrid = get_next_hrid()
     # with get_mongo_db() as db:
     #     db.messages.insert(dict(
 #             raise PostNotExistError(u'Message does not exist')
 #         return message
 
-def get_last_messages(skip=None, limit=10):
-    return Message.objects(reply_to__exists=False)[skip:limit]
-    
     # with get_mongo_db() as db:
     #     messages = (
     #         db.messages

openju/controllers/message.py

 #         r_no=r_no)
 #     return u'Reply posted. #%s/%s' % (hrid, r_no)
 
-# def read(hrid):
-#     message = bl.message.one_from_hrid(hrid)
-#     user_nick = bl.user.get_nick_by_jid(message['jid'])
+def read(message_id):
+    message = bl.message.get_message(message_id)
     
-#     ret_msg = (
-#         u"""\n@%(user)s:\n"""
-#         u"""%(body)s\n"""
-#         u"""#%(hrid)s posted at %(dt)s""") % dict(
-#         user = user_nick,
-#         body = message['message'],
-#         hrid = hrid,
-#         dt = message['dt']
-#         )
-#     return ret_msg
+    ret_msg = (
+        u"""\n@%(user)s:\n"""
+        u"""%(body)s\n"""
+        u"""#%(m_id)s posted at %(dt)s""") % dict(
+        user = message.user.nick,
+        body = message.body,
+        m_id = message.id,
+        dt = pretty_date(message.dt)
+        )
+    return ret_msg
 
 def last_messages():
     messages = bl.message.get_last_messages()

openju/message_bus.py

         if m is not None:
             return controllers.message.last_messages()
     
+    def cmd_read_message(text):
+        pattern = re.compile(ur'^#(?P<message_hrid>[0-9a-zA-Z]+)\s*$')
+        m = pattern.match(text)
+        if m is not None:
+            d = m.groupdict()
+            return controllers.message.read(d['message_hrid'])
+    
     # def cmd_read_message(user_jid, text):
     #     # pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<more>\+)?\s*')
     #     pattern = re.compile(ur'^#(?P<message_hrid>[0-9]+)\s*$')
         call_list(
             cmd_register,
             cmd_last_messages,
-            # cmd_read_message,
+            cmd_read_message,
             # cmd_new_nick,
             # cmd_post_reply,
             cmd_post_message,
 class User(Document):
     jid = StringField(max_length=200, required=True, unique=True)
     nick = StringField(max_length=200, required=True, unique=True)
-    
+
 class Message(Document):
     user = ReferenceField(User, required=True)
     body = StringField(required=True)