From 70b3279a10b16a619fc237e9b678c9ecbedb7db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evren=20Esat=20=C3=96zkan?= Date: Tue, 28 Jun 2016 11:15:21 +0300 Subject: [PATCH] rref #5367 rref #5366 ref zetaops/zengine#66 ref zetaops/zengine#65 --- zengine/messaging/model.py | 28 ++++++++++++++++++------ zengine/messaging/views.py | 44 +++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/zengine/messaging/model.py b/zengine/messaging/model.py index 1bb36a99..dd750293 100644 --- a/zengine/messaging/model.py +++ b/zengine/messaging/model.py @@ -88,12 +88,13 @@ def get_or_create_direct_channel(cls, initiator, receiver): Subscriber(channel=channel, user=receiver).save() return channel - def add_message(self, body, title=None, sender=None, url=None, typ=2, receiver=None): + @classmethod + def add_message(self, channel_key, body, title=None, sender=None, url=None, typ=2, receiver=None): mq_channel = self._connect_mq() mq_msg = json.dumps(dict(sender=sender, body=body, msg_title=title, url=url, typ=typ)) - mq_channel.basic_publish(exchange=self.code_name, body=mq_msg) + mq_channel.basic_publish(exchange=channel_key, body=mq_msg) return Message(sender=sender, body=body, msg_title=title, url=url, - typ=typ, channel=self, receiver=receiver).save() + typ=typ, channel_id=channel_key, receiver=receiver).save() def get_last_messages(self): # TODO: Refactor this with RabbitMQ Last Cached Messages exchange @@ -117,11 +118,15 @@ def pre_creation(self): if not self.code_name: if self.name: self.code_name = to_safe_str(self.name) + self.key = self.code_name return if self.owner and self.is_private: self.code_name = "prv_%s" % to_safe_str(self.owner.key) + self.key = self.code_name return raise IntegrityError('Non-private and non-direct channels should have a "name".') + else: + self.key = self.code_name def post_creation(self): self.create_exchange() @@ -138,6 +143,7 @@ class Subscriber(Model): inform_me = field.Boolean("Inform when I'm mentioned", default=True) visible = field.Boolean("Show under user's channel list", default=True) can_leave = field.Boolean("Membership is not obligatory", default=True) + last_seen = field.DateTime("Last seen time") # status = field.Integer("Status", choices=SUBSCRIPTION_STATUS) @@ -161,6 +167,10 @@ def create_exchange(self): channel = self._connect_mq() channel.exchange_declare(exchange=self.user.key, exchange_type='fanout', durable=True) + @classmethod + def mark_seen(cls, key, datetime_str): + cls.objects.filter(key=key).update(last_seen=datetime_str) + def bind_to_channel(self): """ Binds (subscribes) users private exchange to channel exchange @@ -197,13 +207,18 @@ def __unicode__(self): class Message(Model): """ - Permission model + Message model + + Notes: + Never use directly for creating new messages! Use these methods: + - Channel objects's **add_message()** method. + - User object's **set_message()** method. (which uses channel.add_message) """ channel = Channel() sender = UserModel(reverse_name='sent_messages') receiver = UserModel(reverse_name='received_messages') - typ = field.Integer("Type", choices=MSG_TYPES) - status = field.Integer("Status", choices=MESSAGE_STATUS) + typ = field.Integer("Type", choices=MSG_TYPES, default=1) + status = field.Integer("Status", choices=MESSAGE_STATUS, default=1) msg_title = field.String("Title") body = field.String("Body") url = field.String("URL") @@ -226,6 +241,7 @@ def serialize_for(self, user): return { 'content': self.body, 'type': self.typ, + 'time': self.updated_at, 'attachments': [attachment.serialize() for attachment in self.attachment_set], 'title': self.msg_title, 'sender_name': self.sender.full_name, diff --git a/zengine/messaging/views.py b/zengine/messaging/views.py index 07bbfb25..7d520304 100644 --- a/zengine/messaging/views.py +++ b/zengine/messaging/views.py @@ -24,7 +24,7 @@ def create_message(current): { 'view':'_zops_create_message', 'message': { - 'channel': "code_name of the channel", + 'channel': key, # of channel", 'receiver': key, " of receiver. Should be set only for direct messages", 'title': "Title of the message. Can be blank.", 'body': "Message body.", @@ -84,6 +84,7 @@ def show_channel(current): 'last_messages': [ {'content': string, 'title': string, + 'time': datetime, 'channel_key': key, 'sender_name': string, 'sender_key': key, @@ -107,6 +108,47 @@ def show_channel(current): for msg in ch.get_last_messages()] } +def last_seen_channel(current): + """ + Initial display of channel content. + Returns channel description, members, no of members, last 20 messages etc. + + + .. code-block:: python + + # request: + { + 'view':'_zops_last_seen_msg', + 'channel_key': key, + 'msg_key': key, + 'msg_date': datetime, + } + + # response: + { + 'channel_key': key, + 'description': string, + 'no_of_members': int, + 'member_list': [ + {'name': string, + 'is_online': bool, + 'avatar_url': string, + }], + 'last_messages': [ + {'content': string, + 'title': string, + 'channel_key': key, + 'sender_name': string, + 'sender_key': key, + 'type': int, + 'key': key, + 'actions':[('name_string', 'cmd_string'),] + } + ] + } + """ + current.input['seen_channel'] + def mark_offline_user(current): current.user.is_online(False)