Commits

Konstantine Rybnikov committed 1e6c54e

#123 works

Comments (0)

Files changed (4)

openju/bl/message.py

                 )
             .limit(1))
         if post.count() == 0:
-            raise PostNotExistError(u'Post does not exists')
+            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(
             r_no = next_r_no))
         return next_r_no
 
-def get_more(hrid, r_no, more, skip=None, limit=10):
-    pass
+def one_from_hrid(hrid):
+    """ Get one message by it's hrid. """
+    with get_mongo_db() as db:
+        message = db.messages.find_one(dict(
+            hrid = hrid,
+            r_id = { '$exists': False }))
+        if message is None:
+            raise PostNotExistError(u'Message does not exist')
+        return message
 
-# 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
+# # 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
     

openju/bl/user.py

               'nick': nick },
             upsert=True)
 
-def get_users_for_messages(messages):
-    """ Returns map message_id -> user """
-    jids = [x.jid for x in messages]
+def one_from_jid(jid):
     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
+        user = db.users.find_one(dict(_id = jid))
+
+def get_nick_by_jid(jid):
+    with get_mongo_db() as db:
+        user = db.users.find_one(dict(_id = jid))
+        return jid if user is None else user.nick
+
+# 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
     

openju/controllers/message.py

         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_
-            )
+def read(hrid):
+    message = bl.message.one_from_hrid(hrid)
+    user_nick = bl.user.get_nick_by_jid(message['jid'])
+    
+    ret_msg = (
+        u"""\n%(user)s:\n"""
+        u"""%(body)s\n"""
+        u"""#%(hrid)s""") % dict(
+        user = user_nick,
+        body = message['message'],
+        hrid = hrid
+        )
+    return ret_msg

openju/messagecb.py

     """ 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*')
+        # 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*$')
         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'])
+            # d['r_no'] = none_or_int(d['r_no'])
+            return controllers.message.read(int(d['message_hrid']))
     
     def cmd_new_nick(user_jid, text):
         cmd_text = u'NICK'