1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-16 15:19:06 +00:00

[api2] Add last message of contacts/groups

This commit is contained in:
poetwang
2014-04-18 01:01:09 +08:00
parent 29056488ae
commit 7b8294d467

View File

@@ -139,6 +139,19 @@ def get_msg_group_id(msg_id):
return msg.group_id return msg.group_id
def get_msg_group_id_and_last_reply(msg_id):
lastreply = None
try:
msg = GroupMessage.objects.get(id=msg_id)
except GroupMessage.DoesNotExist:
return None, none
replies = MessageReply.objects.filter(reply_to=msg).order_by('-timestamp')[:1]
if len(replies) >= 1:
lastreply = replies[0].message
return msg.group_id, lastreply
def get_group_and_contacts(email): def get_group_and_contacts(email):
group_json = [] group_json = []
contacts_json = [] contacts_json = []
@@ -168,7 +181,7 @@ def get_group_and_contacts(email):
replies[msg_id] = 1 replies[msg_id] = 1
d['mtime'] = get_timestamp(n.timestamp) d['mtime'] = get_timestamp(n.timestamp)
d['name'] = email2nickname(d['reply_from']) d['name'] = email2nickname(d['reply_from'])
d['group_id'] = get_msg_group_id(msg_id) d['group_id'], d['lastmsg'] = get_msg_group_id_and_last_reply(msg_id)
replies_json.append(d) replies_json.append(d)
replynum = replynum + 1 replynum = replynum + 1
elif n.is_user_message(): elif n.is_user_message():
@@ -182,14 +195,17 @@ def get_group_and_contacts(email):
for g in joined_groups: for g in joined_groups:
msg = GroupMessage.objects.filter(group_id=g.id).order_by('-timestamp')[:1] msg = GroupMessage.objects.filter(group_id=g.id).order_by('-timestamp')[:1]
mtime = 0 mtime = 0
lastmsg = None
if len(msg) >= 1: if len(msg) >= 1:
mtime = get_timestamp(msg[0].timestamp) mtime = get_timestamp(msg[0].timestamp)
lastmsg = msg[0].message
group = { group = {
"id":g.id, "id":g.id,
"name":g.group_name, "name":g.group_name,
"creator":g.creator_name, "creator":g.creator_name,
"ctime":g.timestamp, "ctime":g.timestamp,
"mtime":mtime, "mtime":mtime,
"lastmsg":lastmsg,
"msgnum":gmsgnums.get(g.id, 0), "msgnum":gmsgnums.get(g.id, 0),
} }
@@ -200,13 +216,16 @@ def get_group_and_contacts(email):
msg = UserMessage.objects.get_messages_between_users( msg = UserMessage.objects.get_messages_between_users(
contact, email).order_by('-timestamp')[:1] contact, email).order_by('-timestamp')[:1]
mtime = 0 mtime = 0
lastmsg = None
if len(msg) >= 1: if len(msg) >= 1:
mtime = get_timestamp(msg[0].timestamp) mtime = get_timestamp(msg[0].timestamp)
lastmsg = msg[0].message
c = { c = {
'email' : contact, 'email' : contact,
'name' : email2nickname(contact), 'name' : email2nickname(contact),
"msgnum" : umsgnums.get(contact, 0),
"mtime" : mtime, "mtime" : mtime,
"lastmsg":lastmsg,
"msgnum" : umsgnums.get(contact, 0),
} }
umsgnum = umsgnum + umsgnums.get(contact, 0) umsgnum = umsgnum + umsgnums.get(contact, 0)
contacts_json.append(c) contacts_json.append(c)