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 Jun 28, 2016
1 parent 4040086 commit 70b3279
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
28 changes: 22 additions & 6 deletions zengine/messaging/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand Down
44 changes: 43 additions & 1 deletion zengine/messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Expand Down

0 comments on commit 70b3279

Please sign in to comment.