Skip to content

Commit

Permalink
rref #5367
Browse files Browse the repository at this point in the history
rref #5366

ref #66
ref #65
  • Loading branch information
evrenesat committed Jul 21, 2016
1 parent 5f34aa7 commit bc58f05
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
34 changes: 21 additions & 13 deletions zengine/messaging/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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([
Expand Down
59 changes: 37 additions & 22 deletions zengine/messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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.
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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))

Expand All @@ -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):
Expand Down Expand Up @@ -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, "")
Expand Down

0 comments on commit bc58f05

Please sign in to comment.