1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-14 22:33:17 +00:00

[api2]Change according to the new design for new replies

This commit is contained in:
poetwang
2014-03-19 23:42:31 +08:00
parent 6a4f2666f8
commit 1e74e371cf
3 changed files with 70 additions and 17 deletions

View File

@@ -130,14 +130,25 @@ def get_groups(email):
group_json.append(group)
return group_json, replynum
def get_msg_group_id(msg_id):
try:
msg = GroupMessage.objects.get(id=msg_id)
except GroupMessage.DoesNotExist:
return None
return msg.group_id
def get_group_and_contacts(email):
group_json = []
contacts_json = []
replies_json = []
gmsgnums = {}
umsgnums = {}
replies = {}
gmsgnum = umsgnum = replynum = 0
contacts = [c.contact_email for c in Contact.objects.filter(user_email=email)]
joined_groups = get_personal_groups_by_user(email)
gmsgnums = umsgnums = {}
notes = UserNotification.objects.get_user_notifications(email, seen=False)
for n in notes:
@@ -148,12 +159,25 @@ def get_group_and_contacts(email):
continue
gmsgnums[gid] = gmsgnums.get(gid, 0) + 1
elif n.is_grpmsg_reply():
d = n.grpmsg_reply_detail_to_dict()
msg_id = d['msg_id']
if replies.get(msg_id, None):
replies[msg_id] = replies[msg_id] + 1
else:
replies[msg_id] = 1
d['mtime'] = n.timestamp
d['name'] = email2nickname(d['reply_from'])
d['group_id'] = get_msg_group_id(msg_id)
replies_json.append(d)
replynum = replynum + 1
elif n.is_user_message():
if n.detail not in contacts:
contacts.append(n.detail)
umsgnums[n.detail] = umsgnums.get(n.detail, 0) + 1
for r in replies_json:
r['msgnum'] = replies[r['msg_id']]
for g in joined_groups:
msg = GroupMessage.objects.filter(group_id=g.id).order_by('-timestamp')[:1]
mtime = 0
@@ -186,7 +210,7 @@ def get_group_and_contacts(email):
umsgnum = umsgnum + umsgnums.get(contact, 0)
contacts_json.append(c)
contacts_json.sort(key=lambda x: x["mtime"], reverse=True)
return contacts_json, umsgnum, group_json, replynum, gmsgnum
return contacts_json, umsgnum, group_json, gmsgnum, replies_json, replynum
def prepare_events(event_groups):
for g in event_groups:
@@ -259,12 +283,17 @@ def get_group_msgs(groupid, page, username):
return group_msgs
def get_timetamp(msgtimestamp):
timestamp = int(time.mktime(msgtimestamp.timetuple()))
return timestamp
def group_msg_to_json(msg, get_all_replies):
ret = {
'from_email' : msg.from_email,
'nickname' : email2nickname(msg.from_email),
'time' : msg.timestamp,
'timestamp' : get_timetamp(msg.timestamp),
'msg' : msg.message,
'msgid' : msg.id,
}
try:
@@ -291,8 +320,10 @@ def group_msg_to_json(msg, get_all_replies):
for reply in msg.replies:
r = {
'from_email' : reply.from_email,
'time' : reply.timestamp,
'nickname' : email2nickname(reply.from_email),
'timestamp' : get_timetamp(reply.timestamp),
'msg' : reply.message,
'msgid' : reply.id,
}
replies.append(r)
@@ -309,7 +340,7 @@ def get_group_msgs_json(groupid, page, username):
try:
group_msgs = paginator.page(page)
except (EmptyPage, InvalidPage):
return None
return None, -1
if group_msgs.has_next():
next_page = group_msgs.next_page_number()
@@ -317,7 +348,7 @@ def get_group_msgs_json(groupid, page, username):
next_page = -1
group_msgs.object_list = list(group_msgs.object_list)
msgs = [ group_msg_to_json(msg, False) for msg in group_msgs.object_list ]
msgs = [ group_msg_to_json(msg, True) for msg in group_msgs.object_list ]
return msgs, next_page
def get_group_message_json(group_id, msg_id, get_all_replies):
@@ -339,7 +370,7 @@ def get_person_msgs(to_email, page, username):
try:
person_msgs = paginator.page(page)
except (EmptyPage, InvalidPage):
person_msgs = paginator.page(paginator.num_pages)
return None
# Force evaluate queryset to fix some database error for mysql.
person_msgs.object_list = list(person_msgs.object_list)

View File

@@ -31,7 +31,7 @@ from serializers import AuthTokenSerializer, AccountSerializer
from utils import is_repo_writable, is_repo_accessible, calculate_repo_info, \
api_error, get_file_size, prepare_starred_files, \
get_groups, get_group_and_contacts, prepare_events, \
get_person_msgs, api_group_check, get_email, \
get_person_msgs, api_group_check, get_email, get_timetamp, \
get_group_message_json, get_group_msgs, get_group_msgs_json
from seahub.base.accounts import User
from seahub.base.models import FileDiscuss, UserStarredFiles, \
@@ -252,6 +252,7 @@ class AccountInfo(APIView):
email = request.user.username
info['email'] = email
info['total'] = get_user_quota(email)
info['nickname'] = email2nickname(email)
if CALC_SHARE_USAGE:
my_usage = get_user_quota_usage(email)
@@ -1995,10 +1996,11 @@ class GroupAndContacts(APIView):
throttle_classes = (UserRateThrottle, )
def get(self, request, format=None):
contacts, umsgnum, group_json, replynum, gmsgnum = get_group_and_contacts(request.user.username)
contacts, umsgnum, group_json, gmsgnum, replies, replynum = get_group_and_contacts(request.user.username)
res = {
"groups": group_json,
"contacts": contacts,
"newreplies":replies,
"replynum": replynum,
"umsgnum" : umsgnum,
"gmsgnum" : gmsgnum,
@@ -2088,7 +2090,8 @@ class GroupMsgsView(APIView):
username = request.user.username
page = get_page_index (request, 1)
msgs, next_page = get_group_msgs_json(group.id, page, username)
if not msgs:
msgs = []
# remove user notifications
UserNotification.objects.seen_group_msg_notices(username, group.id)
ret = {
@@ -2137,6 +2140,8 @@ class GroupMsgView(APIView):
msg = get_group_message_json(group.id, msg_id, True)
if not msg:
return api_error(status.HTTP_404_NOT_FOUND, 'Messageg not found.')
UserNotification.objects.seen_group_msg_reply_notice(request.user.username, msg_id)
return Response(msg)
@api_group_check
@@ -2177,6 +2182,12 @@ class UserMsgsView(APIView):
page = get_page_index(request, 1)
person_msgs = get_person_msgs(to_email, page, username)
if not person_msgs:
Response({
'to_email' : to_email,
'next_page' : next_page,
'msgs' : [],})
next_page = -1
if person_msgs.has_next():
next_page = person_msgs.next_page_number()
@@ -2191,10 +2202,11 @@ class UserMsgsView(APIView):
})
m = {
'from_email' : msg.from_email,
'nick' : email2nickname(msg.from_email),
'time' : msg.timestamp,
'nickname' : email2nickname(msg.from_email),
'timestamp' : get_timetamp(msg.timestamp),
'msg' : msg.message,
'attachments' : atts,
'msgid' : msg.message_id,
}
msgs.append(m)
@@ -2229,7 +2241,7 @@ class NewRepliesView(APIView):
grpmsg_reply_list = [ n.grpmsg_reply_detail_to_dict().get('msg_id') for n in notes if n.msg_type == 'grpmsg_reply']
group_msgs = []
for msg_id in grpmsg_reply_list:
msg = get_group_message_json (None, msg_id, False)
msg = get_group_message_json (None, msg_id, True)
if msg:
group_msgs.append(msg)

View File

@@ -196,7 +196,7 @@ class UserNotificationManager(models.Manager):
qs = qs.filter(seen=seen)
return qs
def seen_group_msg_reply_notice(self, to_user):
def seen_group_msg_reply_notice(self, to_user, msg_id=None):
"""Mark all group message replies of a user as seen.
Arguments:
@@ -204,9 +204,19 @@ class UserNotificationManager(models.Manager):
- `to_user`:
- `msg_id`:
"""
super(UserNotificationManager, self).filter(
to_user=to_user, msg_type=MSG_TYPE_GRPMSG_REPLY,
seen=False).update(seen=True)
if not msg_id:
super(UserNotificationManager, self).filter(
to_user=to_user, msg_type=MSG_TYPE_GRPMSG_REPLY,
seen=False).update(seen=True)
else:
notifs = super(UserNotificationManager, self).filter(
to_user=to_user, msg_type=MSG_TYPE_GRPMSG_REPLY,
seen=False)
for n in notifs:
d = n.grpmsg_reply_detail_to_dict()
if msg_id == d['msg_id']:
n.seen = True
n.save()
def remove_group_msg_reply_notice(self, to_user):
"""Mark all group message replies of a user as seen.