Commits

Konstantine Rybnikov committed 2d5bcb9

replies work

Comments (0)

Files changed (7)

 
 #. Implement commands::
     - read message (ok)
-    - read message with replies
+    - read message with replies (ok)
     - reply to message
-    - reply to reply
     - NICK
+    - @username
+    - S @username
+    - @username+
 
 #. Write tests.
+#. Lor-talks-bot
+#. Make routes.py better (simpler then a bunch of functions)
+#. In routes.py rewrite PATTERNS for @user and #message_id
+#. Think about ^ in beginning of strings in routes.py
+#. Catch ``MultipleObjectsReturned`` and ``DoesNotExist`` and say "message #msg not found"

openju/bl/message.py

     return Message.objects(reply_to__exists=False)[skip:limit]
 
 def get_message(message_id):
-    """
-    Raises ``MultipleObjectsReturned`` or ``DoesNotExist`` errors.
-    Need to catch them in controller.
-    """
     return Message.objects.get(id=message_id)
 
 def get_replies(message_id):
-    """
-    Raises ``MultipleObjectsReturned`` or ``DoesNotExist`` errors.
-    Need to catch them in controller.
-    """
     return Message.objects(reply_to=message_id)
 
-# def post_reply(jid, hrid, message, r_no=None):
-#     # hrid = int(hrid)
-#     # if r_no is not None:
-#     #     r_no = int(r_no)
-#     with get_mongo_db() as db:
-#         # check is post exist and get id
-#         post = (
-#             db.messages
-#             .find(
-#                 dict(
-#                     hrid = hrid,
-#                     r_no = r_no),
-#                 [],
-#                 )
-#             .limit(1))
-#         if post.count() == 0:
-#             raise PostNotExistError(u'Message does not exist')
-#         post_id = post[0]['_id']
-#         next_r_no = get_next_hrid_r_no(hrid)
-#         db.messages.insert(dict(
-#             jid = jid,
-#             message = message,
-#             hrid = hrid,
-#             r_to = post_id,
-#             r_no = next_r_no,
-#             dt = datetime.now()))
-#         return next_r_no
+def post_reply(message, user, body):
+    """ Returns newly created message """
+    reply = Message(
+        user=user,
+        body=body,
+        reply_to=message,
+        )
+    reply.save()
+    return reply

openju/controllers/message.py

 # -*- coding: utf-8 -*-
 
 from openju import bl, g, _
-from openju.util import pretty_date
-from openju.views import render_message, render_messages_list, render_message_and_replies
+from openju.views import \
+    render_message, render_messages_list, \
+    render_message_and_replies, render_message_id, \
+    render_reply
 
 def post(text):
     message = bl.message.post_message(g.auth_user, text)
     return _(u'Message #%s posted') % message.id
 
-# def post_reply(hrid, message, r_no=None):
-#     r_no = bl.message.post_reply(
-#         g.user_jid,
-#         hrid,
-#         message,
-#         r_no=r_no)
-#     return u'Reply posted. #%s/%s' % (hrid, r_no)
-
 def read(message_id):
     message = bl.message.get_message(message_id)
-    
-    return render_message(message)
+    if message.is_reply():
+        return render_reply(message)
+    else:
+        return render_message(message)
 
 def read_more(message_id):
     message = bl.message.get_message(message_id)
     else:
         ret_val = u'There are no messages yet. Go on, post one!'
     return ret_val
+
+def post_reply(message_id, body):
+    message = bl.message.get_message(message_id)
+    new_message = message.post_reply(g.auth_user, body)
+    return u"Message %(message_id)s posted." % {
+        'message_id': render_message_id(new_message),
+        }

openju/message_bus.py

             cmd_last_messages,
             cmd_read_message,
             cmd_read_message_with_replies,
+            cmd_post_reply,
             # cmd_new_nick,
             # cmd_post_reply,
             cmd_post_message,
     meta = {
         'ordering': ['-dt']
     }
-
+    
     def get_replies(self):
         from openju.bl.message import get_replies
         return get_replies(self.id)
+    
+    def post_reply(self, user, reply_body):
+        from openju.bl.message import post_reply
+        return post_reply(self, user, reply_body)
+
+    def is_reply(self):
+        return bool(self.reply_to)
         d = m.groupdict()
         return controllers.message.read_more(d['message_id'])
 
+def cmd_post_reply(text):
+    pattern = re.compile(ur'#%(p_message_id)s\s(?P<message_body>.+)' % {
+            'p_message_id': PATTERNS['MESSAGE_ID']
+            })
+    m = pattern.match(text)
+    if m is not None:
+        d = m.groupdict()
+        return controllers.message.post_reply(d['message_id'],
+                                              d['message_body'])
+
 # def cmd_new_nick(user_jid, text):
 #     cmd_text = u'NICK'
 #     if text.startswith(cmd_text):
 
 def render_reply(message):
     return (
-        u"""%(user)s replied to %(user_reply)s:\n"""
+        u"""%(user)s replied to %(user_reply)s (%(user_reply_msgid)s):\n"""
         u"""%(quote)s\n"""
         u"""%(body)s\n"""
         u"""%(m_id)s posted at %(dt)s""") % dict(
         user = render_user(message.user),
         user_reply = render_user(message.reply_to.user),
+        user_reply_msgid = render_message_id(message.reply_to),
         quote = render_quote(message.reply_to),
         body = message.body,
         m_id = render_message_id(message),