Commits

Konstantine Rybnikov  committed 427e630

ok. Becoming too complex. I'm scared

  • Participants
  • Parent commits 0ec2278

Comments (0)

Files changed (6)

File openju/bl/message.py

     return next_hrid
 
 def post_reply(jid, hrid, message, r_no=None):
-    hrid = int(hrid)
-    if r_no is not None:
-        r_no = int(r_no)
+    # 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 = (
             r_to = post_id,
             r_no = next_r_no))
         return next_r_no
+
+def get_more(hrid, r_no, more, skip=None, limit=10):
+    pass
+
+# TODO: rewrite this because of similarity of bl.user.get_users_for_messages
+def get_replytos_for_messages(messages):
+    """ Returns map message_id -> message, reply to which it was given """
+    ids = [x.r_id for x in messages if x.r_id is not None]
+    with get_mongo_db() as db:
+        replytos = db.messages.find(dict(
+            _id = {
+                '$in': ids
+                }))
+        replyto_map = dict((x._id, x) for x in replytos)
+        message__replyto_map = dict((x._id, replyto_map[x.r_id]) for x in messages)
+        return message__replyto_map
+    

File openju/bl/user.py

             { '_id': jid,
               'nick': nick },
             upsert=True)
+
+def get_users_for_messages(messages):
+    """ Returns map message_id -> user """
+    jids = [x.jid for x in messages]
+    with get_mongo_db() as db:
+        users = db.users.find(dict(
+            _id = {
+                '$in': jids
+                }))
+        user_map = dict((x._id, x) for x in users)
+        message__user_map = dict((x._id, user_map[x.jid]) for x in messages)
+        return message__user_map
+    

File openju/controllers/message.py

         message,
         r_no=r_no)
     return u'Reply posted. #%s/%s' % (hrid, r_no)
+
+def read_more(hrid, r_no, more):
+    messages = bl.message.get_more(hrid, r_no, more)
+    message__user_map = bl.user.get_users_for_messages(messages)
+    message__replies_map = bl.message.get_replytos_for_messages(messages)
+    # view part goes here
+    ret_val = u""
+    for message in messages:
+        nick = message__user_map[message.jid]['nick']
+        # was this a reply?
+        replito = message__replies_map.get(message._id)
+        if replito is not None:
+            reply_to = u"> %(orig)s" % dict(orig=replito.message)
+            header = u"%(user)s reply to %(reply_to)s:\n" % dict(
+                reply_to = reply_to
+                )
+        else:
+            header = u"%(user)s writes:\n"
+        id_ = message['hrid'] + (('/' + message.get('r_no')) or '')
+        ret_val += (
+            u"%(header)s"
+            u"%(body)s\n\n"
+            u"%(id_)") % dict(
+            header = header,
+            user = nick,
+            body = message.message,
+            id_ = id_
+            )

File openju/messagecb.py

 from openju import g
 import re
 
-from openju.util import UserAlertError
+from openju.util import none_or_int
+from openju.validators import UserAlertError
 import openju.controllers as controllers
 
 def error_handler(f):
 def message_cb(conn, mess):
     """ kind a routing thing """
     
+    def cmd_read_message(user_jid, text):
+        pattern = re.compile(ur'#(?<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?<more>\+)?\s*')
+        m = pattern.match(text)
+        if m is not None:
+            d = m.groupdict()
+            d['r_no'] = none_or_int(d['r_no'])
+            d['more'] = True if d['more'] is not None else False
+            return controllers.message.read_more(d['hrid'], d['r_no'], d['more'])
+    
     def cmd_new_nick(user_jid, text):
         cmd_text = u'NICK'
         if text.startswith(cmd_text):
             return controllers.user.new_nick(nick)
     
     def cmd_post_reply(user_jid, text):
-        pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))? (?P<reply>.*)')
+        pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<reply>\s.*)')
         m = pattern.match(text)
         if m is not None:
             d = m.groupdict()
+            d['r_no'] = none_or_int(d['r_no'])
+            if d['r_no'] is not None: d['r_no'] = int(d['r_no'])
             return controllers.message.post_reply(
                 int(d['message_hrid']),
                 d['reply'],
         g.user_jid = user_jid
         
         call_list(
+            cmd_read_message,
             cmd_new_nick,
             cmd_post_reply,
             cmd_post_message,

File openju/util.py

 def get_mongo_db():
     return mongo_db_wrapper()
 
-class UserAlertError(Exception): pass
+def none_or_int(val):
+    return None if val is None else int(val)

File openju/validators.py

 # -*- coding: utf-8 -*-
 
 import re
-from openju.util import UserAlertError
 
+class UserAlertError(Exception): pass
 class ValidNicknameError(UserAlertError): pass
 class PostNotExistError(UserAlertError): pass