From bc58f0521a0d95a0445f2502b65c0a8abc86fcf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evren=20Esat=20=C3=96zkan?= Date: Thu, 21 Jul 2016 19:57:31 +0300 Subject: [PATCH] rref #5367 rref #5366 ref zetaops/zengine#66 ref zetaops/zengine#65 --- zengine/messaging/model.py | 34 +++++++++++++--------- zengine/messaging/views.py | 59 ++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 35 deletions(-) diff --git a/zengine/messaging/model.py b/zengine/messaging/model.py index c3ef6844..750a2fde 100644 --- a/zengine/messaging/model.py +++ b/zengine/messaging/model.py @@ -13,6 +13,7 @@ from pyoko import Model, field, ListNode from pyoko.conf import settings +from pyoko.db.adapter.db_riak import BlockSave from pyoko.exceptions import IntegrityError from pyoko.fields import DATE_TIME_FORMAT from pyoko.lib.utils import get_object_from_path @@ -91,15 +92,22 @@ def get_or_create_direct_channel(cls, initiator_key, receiver_key): channel = existing[0] else: channel_name = '%s_%s' % (initiator_key, receiver_key) - channel = cls(is_direct=True, code_name=channel_name, typ=10).save() - Subscriber.objects.get_or_create(channel=channel, - user_id=initiator_key, - name=receiver_name) - Subscriber.objects.get_or_create(channel=channel, - user_id=receiver_key, - name=UserModel.objects.get(initiator_key).full_name) + channel = cls(is_direct=True, code_name=channel_name, typ=10).blocking_save() + with BlockSave(Subscriber): + Subscriber.objects.get_or_create(channel=channel, + user_id=initiator_key, + name=receiver_name) + Subscriber.objects.get_or_create(channel=channel, + user_id=receiver_key, + name=UserModel.objects.get(initiator_key).full_name) return channel, receiver_name + def get_avatar(self, user): + if self.typ == 10: + return self.subscriber_set.objects.exclude(user=user)[0].user.get_avatar_url() + else: + return None + @classmethod def add_message(cls, channel_key, body, title=None, sender=None, url=None, typ=2, receiver=None): @@ -112,6 +120,9 @@ def add_message(cls, channel_key, body, title=None, sender=None, url=None, typ=2 body=json.dumps(msg_object.serialize())) return msg_object.save() + def get_subscription_for_user(self, user_id): + return self.subscriber_set.objects.get(user_id=user_id) + def get_last_messages(self): # TODO: Try to refactor this with https://github.com/rabbitmq/rabbitmq-recent-history-exchange return self.message_set.objects.filter().set_params(sort="timestamp asc")[:20] @@ -132,11 +143,6 @@ def create_exchange(self): exchange_type='fanout', durable=True) - def subscribe_owner(self): - sbs, new = Subscriber.objects.get_or_create(user=self.owner, - channel=self, - can_manage=True, - can_leave=False) def pre_creation(self): if not self.code_name: @@ -183,6 +189,7 @@ class Subscriber(Model): def __unicode__(self): return "%s subscription of %s" % (self.name, self.user) + @classmethod def _connect_mq(cls): if cls.mq_connection is None or cls.mq_connection.is_closed: @@ -191,7 +198,8 @@ def _connect_mq(cls): def get_actions(self): actions = [ - ('Pin', '_zops_pin_channel') + ('Pin', '_zops_pin_channel'), + # ('Mute', '_zops_mute_channel'), ] if self.channel.owner == self.user or self.can_manage: actions.extend([ diff --git a/zengine/messaging/views.py b/zengine/messaging/views.py index fca75f07..b9348f03 100644 --- a/zengine/messaging/views.py +++ b/zengine/messaging/views.py @@ -7,6 +7,7 @@ # This file is licensed under the GNU General Public License v3 # (GPLv3). See LICENSE.txt for details. from pyoko.conf import settings +from pyoko.db.adapter.db_riak import BlockSave from pyoko.exceptions import ObjectDoesNotExist from pyoko.lib.utils import get_object_from_path from zengine.lib.exceptions import HTTPError @@ -111,8 +112,9 @@ def create_message(current): """ msg = current.input['message'] - msg_obj = Channel.add_message(msg['channel'], body=msg['body'], typ=msg['type'], sender=current.user, - title=msg['title'], receiver=msg['receiver'] or None) + msg_obj = Channel.add_message(msg['channel'], body=msg['body'], typ=msg['type'], + sender=current.user, + title=msg['title'], receiver=msg['receiver'] or None) current.output = { 'msg_key': msg_obj.key, 'status': 'OK', @@ -125,7 +127,7 @@ def create_message(current): file=atch['content'], description=atch['description'], typ=typ).save() -def show_channel(current): +def show_channel(current, waited=False): """ Initial display of channel content. Returns channel description, members, no of members, last 20 messages etc. @@ -155,8 +157,12 @@ def show_channel(current): } """ ch = Channel(current).objects.get(current.input['channel_key']) + sbs = ch.get_subscription_for_user(current.user_id) current.output = {'channel_key': current.input['channel_key'], 'description': ch.description, + 'name': sbs.name, + 'actions': sbs.get_actions(), + 'avatar_url': ch.get_avatar(current.user), 'no_of_members': len(ch.subscriber_set), 'member_list': [{'name': sb.user.full_name, 'is_online': sb.user.is_online(), @@ -295,15 +301,17 @@ def create_channel(current): description=current.input['description'], owner=current.user, typ=15).save() - sbs, new = Subscriber.objects.get_or_create(user=channel.owner, - channel=channel, - can_manage=True, - can_leave=False) - current.output = { - 'channel_key': channel.key, - 'status': 'OK', + with BlockSave(Subscriber): + Subscriber.objects.get_or_create(user=channel.owner, + channel=channel, + can_manage=True, + can_leave=False) + current.input['channel_key'] = channel.key + show_channel(current) + current.output.update({ + 'status': 'Created', 'code': 201 - } + }) def add_members(current): @@ -417,9 +425,14 @@ def search_user(current): 'status': 'OK', 'code': 201 } - for user in UserModel(current).objects.search_on(*settings.MESSAGING_USER_SEARCH_FIELDS, - contains=current.input['query']): - current.output['results'].append((user.full_name, user.key, user.get_avatar_url())) + qs = UserModel(current).objects.exclude(key=current.user_id).search_on( + *settings.MESSAGING_USER_SEARCH_FIELDS, + contains=current.input['query']) + # FIXME: somehow exclude(key=current.user_id) not working with search_on() + + for user in qs: + if user.key != current.user_id: + current.output['results'].append((user.full_name, user.key, user.get_avatar_url())) def search_unit(current): @@ -446,7 +459,7 @@ def search_unit(current): 'status': 'OK', 'code': 201 } - for user in UnitModel(current).objects.search_on(settings.MESSAGING_UNIT_SEARCH_FIELDS, + for user in UnitModel(current).objects.search_on(*settings.MESSAGING_UNIT_SEARCH_FIELDS, contains=current.input['query']): current.output['results'].append((user.name, user.key)) @@ -472,13 +485,14 @@ def create_direct_channel(current): 'name': string, # name of subscribed channel } """ - channel, sub_name = Channel.get_or_create_direct_channel(current.user_id, current.input['user_key']) - current.output = { - 'channel_key': channel.key, - 'name': sub_name, - 'status': 'OK', + channel, sub_name = Channel.get_or_create_direct_channel(current.user_id, + current.input['user_key']) + current.input['channel_key'] = channel.key + show_channel(current) + current.output.update({ + 'status': 'Created', 'code': 201 - } + }) def find_message(current): @@ -605,7 +619,8 @@ def pin_channel(current): """ try: Subscriber(current).objects.filter(user_id=current.user_id, - channel_id=current.input['channel_key']).update(pinned=True) + channel_id=current.input['channel_key']).update( + pinned=True) current.output = {'status': 'OK', 'code': 200} except ObjectDoesNotExist: raise HTTPError(404, "")