Commits

Konstantine Rybnikov  committed ca1dad7

fixes

  • Participants
  • Parent commits 7c2d7cc

Comments (0)

Files changed (8)

     - read message with replies (ok)
     - reply to message (ok)
     - @username (ok, basic support)
-    - S @username
+    - S @username (ok)
+    - #
     - @username+
     - NICK
 

File openju/bl/message.py

     message.save()
     return message
 
-def get_last_messages(skip=None, limit=10):
-    return Message.objects(reply_to__exists=False)[skip:limit]
+def get_last_messages(skip=None, limit=10, q=lambda x: x):
+    return Message.objects(q(Q(reply_to__exists=False)))[skip:limit]
 
 def get_message(message_id):
     return Message.objects.get(id=message_id)

File openju/bl/user.py

     return User.objects.get(
         nick = nick)
 
+def subscribe_user(user, user_subscribe):
+    """ Subscribe user to updates of user_subscribe """
+    if user_subscribe not in user.friends:
+        user.friends.append(user_subscribe)
+        user.save()
+
 # def set_nickname(jid, nick):
 #     validate_nickname(nick)
 #     with get_mongo_db() as db:

File openju/controllers/message.py

 def last_messages():
     messages = bl.message.get_last_messages()
     
-    # render part
-    ret_val = u''
     if len(messages):
-        ret_val += render_messages_list(reversed(list(messages)))
+        return render_messages_list(reversed(list(messages)))
     else:
-        ret_val = u'There are no messages yet. Go on, post one!'
-    return ret_val
+        return _(u'There are no messages yet. Go on, post one!')
+
+def last_messages_from_friends():
+    messages = g.auth_user.last_friends_messages()
+    
+    if len(messages):
+        return render_messages_list(reversed(list(messages)))
+    else:
+        return _(u'There are no messages from friends yet.')
 
 def post_reply(message_id, body):
     message = bl.message.get_message(message_id)

File openju/controllers/user.py

     user = bl.user.get_user(nick)
     return render_user_info(user)
 
+def subscribe(nick):
+    from openju.views import render_user_nick
+    user_subscribe = bl.user.get_user(nick)
+    g.auth_user = bl.user.subscribe_user(g.auth_user, user_subscribe)
+    return _(u"Successfully subscribed to %(user)s") % dict(
+        user = render_user_nick(user_subscribe))
+
 # def new_nick(nick):
 #     bl.user.set_nickname(g.user_jid, nick)
 #     return u'New nickname is set to: %s' % nick

File openju/message_bus.py

         call_list(
             cmd_register,
             cmd_last_messages,
+            cmd_last_messages_from_friends,
             cmd_read_message,
             cmd_read_message_with_replies,
             cmd_post_reply,
             cmd_user_info,
+            cmd_subscribe,
             cmd_post_message,
             )

File openju/model.py

 class User(Document):
     jid = StringField(max_length=200, required=True, unique=True)
     nick = StringField(max_length=200, required=True, unique=True)
+    friends = ListField(ReferenceField('self'))
+
+    def friends_ids(self):
+        return (x.id for x in self.friends)
     
     def num_overall_messages(self):
         return len(Message.objects(user=self))
         from openju.bl.message import num_today_messages
         return num_today_messages(lambda q: q & Q(user=self))
 
+    def last_friends_messages(self):
+        from openju.bl.message import get_last_messages
+        return get_last_messages(q=lambda q: q & Q(user__in = self.friends))
+
 class Message(Document):
     user = ReferenceField(User, required=True)
     body = StringField(required=True)

File openju/routes.py

 import re
 from openju import g, _
 import openju.controllers as controllers
+from openju.validators import UserAlertError
 
 PATTERNS = {
     'MESSAGE_ID': ur'(?P<message_id>[0-9a-zA-Z]+)',
     m = pattern.match(text)
     if m is None:
         raise UserAlertError(_(
-            u"\nYou are not registered. Pleace, type:\n"
+            u"You are not registered. Pleace, type:\n"
             u"REGISTER %your_nick_name%\nto register"))
     d = m.groupdict()
     return controllers.user.register(d['nick'])
 
 def cmd_last_messages(text):
+    pattern = re.compile(ur'\s*LAST\s*$')
+    m = pattern.match(text)
+    if m is not None:
+        return controllers.message.last_messages()
+
+def cmd_last_messages_from_friends(text):
     pattern = re.compile(ur'\s*#\s*$')
     m = pattern.match(text)
     if m is not None:
-        return controllers.message.last_messages()
+        return controllers.message.last_messages_from_friends()
 
 def cmd_read_message(text):
     pattern = re.compile(ur'^#%(p_message_id)s\s*$' % {
                                               d['message_body'])
 
 def cmd_user_info(text):
-    pattern = re.compile(ur'@%(p_nick)s\s*' % {
+    pattern = re.compile(ur'@%(p_nick)s\s*$' % {
             'p_nick': PATTERNS['NICK'],
             })
     m = pattern.match(text)
     if m is not None:
         d = m.groupdict()
         return controllers.user.info(d['nick'])
-    
+
+def cmd_subscribe(text):
+    pattern = re.compile(ur'S\s+@%(p_nick)s\s*' % {
+            'p_nick': PATTERNS['NICK']
+            })
+    m = pattern.match(text)
+    if m is not None:
+        d = m.groupdict()
+        return controllers.user.subscribe(d['nick'])
+
 # def cmd_new_nick(user_jid, text):
 #     cmd_text = u'NICK'
 #     if text.startswith(cmd_text):