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:
@@ -130,14 +130,25 @@ def get_groups(email):
|
|||||||
group_json.append(group)
|
group_json.append(group)
|
||||||
return group_json, replynum
|
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):
|
def get_group_and_contacts(email):
|
||||||
group_json = []
|
group_json = []
|
||||||
contacts_json = []
|
contacts_json = []
|
||||||
|
replies_json = []
|
||||||
|
gmsgnums = {}
|
||||||
|
umsgnums = {}
|
||||||
|
replies = {}
|
||||||
gmsgnum = umsgnum = replynum = 0
|
gmsgnum = umsgnum = replynum = 0
|
||||||
|
|
||||||
contacts = [c.contact_email for c in Contact.objects.filter(user_email=email)]
|
contacts = [c.contact_email for c in Contact.objects.filter(user_email=email)]
|
||||||
joined_groups = get_personal_groups_by_user(email)
|
joined_groups = get_personal_groups_by_user(email)
|
||||||
gmsgnums = umsgnums = {}
|
|
||||||
|
|
||||||
notes = UserNotification.objects.get_user_notifications(email, seen=False)
|
notes = UserNotification.objects.get_user_notifications(email, seen=False)
|
||||||
for n in notes:
|
for n in notes:
|
||||||
@@ -148,12 +159,25 @@ def get_group_and_contacts(email):
|
|||||||
continue
|
continue
|
||||||
gmsgnums[gid] = gmsgnums.get(gid, 0) + 1
|
gmsgnums[gid] = gmsgnums.get(gid, 0) + 1
|
||||||
elif n.is_grpmsg_reply():
|
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
|
replynum = replynum + 1
|
||||||
elif n.is_user_message():
|
elif n.is_user_message():
|
||||||
if n.detail not in contacts:
|
if n.detail not in contacts:
|
||||||
contacts.append(n.detail)
|
contacts.append(n.detail)
|
||||||
umsgnums[n.detail] = umsgnums.get(n.detail, 0) + 1
|
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:
|
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
|
||||||
@@ -186,7 +210,7 @@ def get_group_and_contacts(email):
|
|||||||
umsgnum = umsgnum + umsgnums.get(contact, 0)
|
umsgnum = umsgnum + umsgnums.get(contact, 0)
|
||||||
contacts_json.append(c)
|
contacts_json.append(c)
|
||||||
contacts_json.sort(key=lambda x: x["mtime"], reverse=True)
|
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):
|
def prepare_events(event_groups):
|
||||||
for g in event_groups:
|
for g in event_groups:
|
||||||
@@ -259,12 +283,17 @@ def get_group_msgs(groupid, page, username):
|
|||||||
|
|
||||||
return group_msgs
|
return group_msgs
|
||||||
|
|
||||||
|
def get_timetamp(msgtimestamp):
|
||||||
|
timestamp = int(time.mktime(msgtimestamp.timetuple()))
|
||||||
|
return timestamp
|
||||||
|
|
||||||
def group_msg_to_json(msg, get_all_replies):
|
def group_msg_to_json(msg, get_all_replies):
|
||||||
ret = {
|
ret = {
|
||||||
'from_email' : msg.from_email,
|
'from_email' : msg.from_email,
|
||||||
'nickname' : email2nickname(msg.from_email),
|
'nickname' : email2nickname(msg.from_email),
|
||||||
'time' : msg.timestamp,
|
'timestamp' : get_timetamp(msg.timestamp),
|
||||||
'msg' : msg.message,
|
'msg' : msg.message,
|
||||||
|
'msgid' : msg.id,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -291,8 +320,10 @@ def group_msg_to_json(msg, get_all_replies):
|
|||||||
for reply in msg.replies:
|
for reply in msg.replies:
|
||||||
r = {
|
r = {
|
||||||
'from_email' : reply.from_email,
|
'from_email' : reply.from_email,
|
||||||
'time' : reply.timestamp,
|
'nickname' : email2nickname(reply.from_email),
|
||||||
|
'timestamp' : get_timetamp(reply.timestamp),
|
||||||
'msg' : reply.message,
|
'msg' : reply.message,
|
||||||
|
'msgid' : reply.id,
|
||||||
}
|
}
|
||||||
replies.append(r)
|
replies.append(r)
|
||||||
|
|
||||||
@@ -309,7 +340,7 @@ def get_group_msgs_json(groupid, page, username):
|
|||||||
try:
|
try:
|
||||||
group_msgs = paginator.page(page)
|
group_msgs = paginator.page(page)
|
||||||
except (EmptyPage, InvalidPage):
|
except (EmptyPage, InvalidPage):
|
||||||
return None
|
return None, -1
|
||||||
|
|
||||||
if group_msgs.has_next():
|
if group_msgs.has_next():
|
||||||
next_page = group_msgs.next_page_number()
|
next_page = group_msgs.next_page_number()
|
||||||
@@ -317,7 +348,7 @@ def get_group_msgs_json(groupid, page, username):
|
|||||||
next_page = -1
|
next_page = -1
|
||||||
|
|
||||||
group_msgs.object_list = list(group_msgs.object_list)
|
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
|
return msgs, next_page
|
||||||
|
|
||||||
def get_group_message_json(group_id, msg_id, get_all_replies):
|
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:
|
try:
|
||||||
person_msgs = paginator.page(page)
|
person_msgs = paginator.page(page)
|
||||||
except (EmptyPage, InvalidPage):
|
except (EmptyPage, InvalidPage):
|
||||||
person_msgs = paginator.page(paginator.num_pages)
|
return None
|
||||||
|
|
||||||
# Force evaluate queryset to fix some database error for mysql.
|
# Force evaluate queryset to fix some database error for mysql.
|
||||||
person_msgs.object_list = list(person_msgs.object_list)
|
person_msgs.object_list = list(person_msgs.object_list)
|
||||||
|
@@ -31,7 +31,7 @@ from serializers import AuthTokenSerializer, AccountSerializer
|
|||||||
from utils import is_repo_writable, is_repo_accessible, calculate_repo_info, \
|
from utils import is_repo_writable, is_repo_accessible, calculate_repo_info, \
|
||||||
api_error, get_file_size, prepare_starred_files, \
|
api_error, get_file_size, prepare_starred_files, \
|
||||||
get_groups, get_group_and_contacts, prepare_events, \
|
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
|
get_group_message_json, get_group_msgs, get_group_msgs_json
|
||||||
from seahub.base.accounts import User
|
from seahub.base.accounts import User
|
||||||
from seahub.base.models import FileDiscuss, UserStarredFiles, \
|
from seahub.base.models import FileDiscuss, UserStarredFiles, \
|
||||||
@@ -252,6 +252,7 @@ class AccountInfo(APIView):
|
|||||||
email = request.user.username
|
email = request.user.username
|
||||||
info['email'] = email
|
info['email'] = email
|
||||||
info['total'] = get_user_quota(email)
|
info['total'] = get_user_quota(email)
|
||||||
|
info['nickname'] = email2nickname(email)
|
||||||
|
|
||||||
if CALC_SHARE_USAGE:
|
if CALC_SHARE_USAGE:
|
||||||
my_usage = get_user_quota_usage(email)
|
my_usage = get_user_quota_usage(email)
|
||||||
@@ -1995,10 +1996,11 @@ class GroupAndContacts(APIView):
|
|||||||
throttle_classes = (UserRateThrottle, )
|
throttle_classes = (UserRateThrottle, )
|
||||||
|
|
||||||
def get(self, request, format=None):
|
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 = {
|
res = {
|
||||||
"groups": group_json,
|
"groups": group_json,
|
||||||
"contacts": contacts,
|
"contacts": contacts,
|
||||||
|
"newreplies":replies,
|
||||||
"replynum": replynum,
|
"replynum": replynum,
|
||||||
"umsgnum" : umsgnum,
|
"umsgnum" : umsgnum,
|
||||||
"gmsgnum" : gmsgnum,
|
"gmsgnum" : gmsgnum,
|
||||||
@@ -2088,7 +2090,8 @@ class GroupMsgsView(APIView):
|
|||||||
username = request.user.username
|
username = request.user.username
|
||||||
page = get_page_index (request, 1)
|
page = get_page_index (request, 1)
|
||||||
msgs, next_page = get_group_msgs_json(group.id, page, username)
|
msgs, next_page = get_group_msgs_json(group.id, page, username)
|
||||||
|
if not msgs:
|
||||||
|
msgs = []
|
||||||
# remove user notifications
|
# remove user notifications
|
||||||
UserNotification.objects.seen_group_msg_notices(username, group.id)
|
UserNotification.objects.seen_group_msg_notices(username, group.id)
|
||||||
ret = {
|
ret = {
|
||||||
@@ -2137,6 +2140,8 @@ class GroupMsgView(APIView):
|
|||||||
msg = get_group_message_json(group.id, msg_id, True)
|
msg = get_group_message_json(group.id, msg_id, True)
|
||||||
if not msg:
|
if not msg:
|
||||||
return api_error(status.HTTP_404_NOT_FOUND, 'Messageg not found.')
|
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)
|
return Response(msg)
|
||||||
|
|
||||||
@api_group_check
|
@api_group_check
|
||||||
@@ -2177,6 +2182,12 @@ class UserMsgsView(APIView):
|
|||||||
page = get_page_index(request, 1)
|
page = get_page_index(request, 1)
|
||||||
|
|
||||||
person_msgs = get_person_msgs(to_email, page, username)
|
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
|
next_page = -1
|
||||||
if person_msgs.has_next():
|
if person_msgs.has_next():
|
||||||
next_page = person_msgs.next_page_number()
|
next_page = person_msgs.next_page_number()
|
||||||
@@ -2191,10 +2202,11 @@ class UserMsgsView(APIView):
|
|||||||
})
|
})
|
||||||
m = {
|
m = {
|
||||||
'from_email' : msg.from_email,
|
'from_email' : msg.from_email,
|
||||||
'nick' : email2nickname(msg.from_email),
|
'nickname' : email2nickname(msg.from_email),
|
||||||
'time' : msg.timestamp,
|
'timestamp' : get_timetamp(msg.timestamp),
|
||||||
'msg' : msg.message,
|
'msg' : msg.message,
|
||||||
'attachments' : atts,
|
'attachments' : atts,
|
||||||
|
'msgid' : msg.message_id,
|
||||||
}
|
}
|
||||||
msgs.append(m)
|
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']
|
grpmsg_reply_list = [ n.grpmsg_reply_detail_to_dict().get('msg_id') for n in notes if n.msg_type == 'grpmsg_reply']
|
||||||
group_msgs = []
|
group_msgs = []
|
||||||
for msg_id in grpmsg_reply_list:
|
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:
|
if msg:
|
||||||
group_msgs.append(msg)
|
group_msgs.append(msg)
|
||||||
|
|
||||||
|
@@ -196,7 +196,7 @@ class UserNotificationManager(models.Manager):
|
|||||||
qs = qs.filter(seen=seen)
|
qs = qs.filter(seen=seen)
|
||||||
return qs
|
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.
|
"""Mark all group message replies of a user as seen.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -204,9 +204,19 @@ class UserNotificationManager(models.Manager):
|
|||||||
- `to_user`:
|
- `to_user`:
|
||||||
- `msg_id`:
|
- `msg_id`:
|
||||||
"""
|
"""
|
||||||
super(UserNotificationManager, self).filter(
|
if not msg_id:
|
||||||
to_user=to_user, msg_type=MSG_TYPE_GRPMSG_REPLY,
|
super(UserNotificationManager, self).filter(
|
||||||
seen=False).update(seen=True)
|
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):
|
def remove_group_msg_reply_notice(self, to_user):
|
||||||
"""Mark all group message replies of a user as seen.
|
"""Mark all group message replies of a user as seen.
|
||||||
|
Reference in New Issue
Block a user