mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-30 21:50:59 +00:00
Add message reply notification
This commit is contained in:
parent
0477081eb4
commit
0305c3245f
@ -1,7 +1,15 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models.signals import post_save
|
||||||
|
from django.dispatch import receiver
|
||||||
|
|
||||||
|
from seaserv import get_group_members
|
||||||
|
|
||||||
|
from shortcuts import get_first_object_or_none
|
||||||
|
from notifications.models import UserNotification
|
||||||
|
from profile.models import Profile
|
||||||
|
|
||||||
class GroupMessage(models.Model):
|
class GroupMessage(models.Model):
|
||||||
group_id = models.IntegerField(db_index=True)
|
group_id = models.IntegerField(db_index=True)
|
||||||
@ -14,3 +22,45 @@ class MessageReply(models.Model):
|
|||||||
from_email = models.EmailField()
|
from_email = models.EmailField()
|
||||||
message = models.CharField(max_length=150)
|
message = models.CharField(max_length=150)
|
||||||
timestamp = models.DateTimeField(default=datetime.datetime.now)
|
timestamp = models.DateTimeField(default=datetime.datetime.now)
|
||||||
|
|
||||||
|
@receiver(post_save, sender=MessageReply)
|
||||||
|
def msgreply_save_handler(sender, instance, **kwargs):
|
||||||
|
"""
|
||||||
|
Handl sending notification to '@<user>' when reply messages.
|
||||||
|
"""
|
||||||
|
from_email = instance.from_email
|
||||||
|
reply_msg = instance.message
|
||||||
|
group_msg = instance.reply_to
|
||||||
|
to_user = ''
|
||||||
|
|
||||||
|
at_pattern = re.compile(r'(\s|^)(@\w+)', flags=re.U)
|
||||||
|
m = re.match(at_pattern, reply_msg)
|
||||||
|
if m:
|
||||||
|
nickname_or_emailprefix = m.group()[1:]
|
||||||
|
p = get_first_object_or_none(\
|
||||||
|
Profile.objects.filter(nickname=nickname_or_emailprefix))
|
||||||
|
if p:
|
||||||
|
to_user = p.user
|
||||||
|
else:
|
||||||
|
# Match email prefix in group members.
|
||||||
|
group_members = get_group_members(group_msg.group_id)
|
||||||
|
for m in group_members:
|
||||||
|
if m.user_name.startswith(nickname_or_emailprefix):
|
||||||
|
to_user = m.user_name
|
||||||
|
break
|
||||||
|
|
||||||
|
if to_user and to_user != from_email:
|
||||||
|
# Send notification to the user if user replies someone else'
|
||||||
|
# message.
|
||||||
|
try:
|
||||||
|
UserNotification.objects.get(to_user=to_user,
|
||||||
|
msg_type='grpmsg_reply',
|
||||||
|
detail=group_msg.id)
|
||||||
|
except UserNotification.DoesNotExist:
|
||||||
|
n = UserNotification(to_user=to_user,
|
||||||
|
msg_type='grpmsg_reply',
|
||||||
|
detail=group_msg.id)
|
||||||
|
n.save()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.http import HttpResponse, HttpResponseRedirect, Http404
|
from django.http import HttpResponse, HttpResponseRedirect, Http404, \
|
||||||
|
HttpResponseBadRequest
|
||||||
from django.shortcuts import render_to_response, redirect
|
from django.shortcuts import render_to_response, redirect
|
||||||
from django.template import RequestContext
|
from django.template import RequestContext
|
||||||
|
|
||||||
from auth.decorators import login_required
|
from auth.decorators import login_required
|
||||||
from seaserv import ccnet_rpc, ccnet_threaded_rpc, seafserv_threaded_rpc, get_repo, \
|
from seaserv import ccnet_rpc, ccnet_threaded_rpc, seafserv_threaded_rpc, \
|
||||||
get_group_repoids, check_group_staff, get_commits, get_personal_groups, \
|
get_repo, get_group_repoids, check_group_staff, get_commits, \
|
||||||
get_group
|
get_personal_groups, get_group, get_group_members
|
||||||
from pysearpc import SearpcError
|
from pysearpc import SearpcError
|
||||||
|
|
||||||
from models import GroupMessage, MessageReply
|
from models import GroupMessage, MessageReply
|
||||||
@ -153,7 +154,7 @@ def render_group_info(request, group_id, form):
|
|||||||
else:
|
else:
|
||||||
is_staff = False
|
is_staff = False
|
||||||
|
|
||||||
members = ccnet_threaded_rpc.get_group_members(group_id_int)
|
members = get_group_members(group_id_int)
|
||||||
managers = []
|
managers = []
|
||||||
common_members = []
|
common_members = []
|
||||||
for member in members:
|
for member in members:
|
||||||
@ -228,6 +229,8 @@ def render_group_info(request, group_id, form):
|
|||||||
@login_required
|
@login_required
|
||||||
def msg_reply(request, msg_id):
|
def msg_reply(request, msg_id):
|
||||||
"""Show group message replies, and process message reply in ajax"""
|
"""Show group message replies, and process message reply in ajax"""
|
||||||
|
|
||||||
|
content_type = 'application/json; charset=utf-8'
|
||||||
if request.is_ajax():
|
if request.is_ajax():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = MessageReplyForm(request.POST)
|
form = MessageReplyForm(request.POST)
|
||||||
@ -238,7 +241,7 @@ def msg_reply(request, msg_id):
|
|||||||
try:
|
try:
|
||||||
group_msg = GroupMessage.objects.get(id=msg_id)
|
group_msg = GroupMessage.objects.get(id=msg_id)
|
||||||
except GroupMessage.DoesNotExist:
|
except GroupMessage.DoesNotExist:
|
||||||
return HttpResponse(status=400)
|
return HttpResponseBadRequest(content_type=content_type)
|
||||||
|
|
||||||
msg_reply = MessageReply()
|
msg_reply = MessageReply()
|
||||||
msg_reply.reply_to = group_msg
|
msg_reply.reply_to = group_msg
|
||||||
@ -251,10 +254,6 @@ def msg_reply(request, msg_id):
|
|||||||
grpmsg_reply_added.send(sender=MessageReply,
|
grpmsg_reply_added.send(sender=MessageReply,
|
||||||
msg_id=msg_id,
|
msg_id=msg_id,
|
||||||
from_email=request.user.username)
|
from_email=request.user.username)
|
||||||
|
|
||||||
|
|
||||||
content_type = 'application/json; charset=utf-8'
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg = GroupMessage.objects.get(id=msg_id)
|
msg = GroupMessage.objects.get(id=msg_id)
|
||||||
except GroupMessage.DoesNotExist:
|
except GroupMessage.DoesNotExist:
|
||||||
|
12
shortcuts.py
Normal file
12
shortcuts.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
def get_first_object_or_none(queryset):
|
||||||
|
"""
|
||||||
|
A shortcut to obtain the first object of a queryset if it exists or None
|
||||||
|
otherwise.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return queryset[:1][0]
|
||||||
|
except IndexError:
|
||||||
|
return None
|
@ -5,7 +5,8 @@ from service import ccnet_rpc, monitor_rpc, seafserv_rpc, \
|
|||||||
from service import send_command
|
from service import send_command
|
||||||
from service import get_emailusers
|
from service import get_emailusers
|
||||||
from service import get_org_groups, get_personal_groups, get_group_repoids, \
|
from service import get_org_groups, get_personal_groups, get_group_repoids, \
|
||||||
check_group_staff, remove_group_user, get_group, get_org_id_by_group
|
check_group_staff, remove_group_user, get_group, get_org_id_by_group, \
|
||||||
|
get_group_members
|
||||||
from service import get_repos, get_repo, get_commits, get_branches, \
|
from service import get_repos, get_repo, get_commits, get_branches, \
|
||||||
get_org_repos, is_repo_owner, create_org_repo
|
get_org_repos, is_repo_owner, create_org_repo
|
||||||
from service import get_binding_peerids, is_valid_filename
|
from service import get_binding_peerids, is_valid_filename
|
||||||
|
@ -139,6 +139,14 @@ def get_personal_groups(email):
|
|||||||
|
|
||||||
return personal_groups
|
return personal_groups
|
||||||
|
|
||||||
|
def get_group_members(group_id):
|
||||||
|
group_id_int = int(group_id)
|
||||||
|
try:
|
||||||
|
members = ccnet_threaded_rpc.get_group_members(group_id_int)
|
||||||
|
except SearpcError:
|
||||||
|
members = []
|
||||||
|
return members
|
||||||
|
|
||||||
def get_org_id_by_group(group_id):
|
def get_org_id_by_group(group_id):
|
||||||
try:
|
try:
|
||||||
org_id = ccnet_threaded_rpc.get_org_id_by_group(group_id)
|
org_id = ccnet_threaded_rpc.get_org_id_by_group(group_id)
|
||||||
|
Loading…
Reference in New Issue
Block a user