diff options
Diffstat (limited to 'mediagoblin/notifications')
-rw-r--r-- | mediagoblin/notifications/__init__.py | 43 | ||||
-rw-r--r-- | mediagoblin/notifications/task.py | 6 | ||||
-rw-r--r-- | mediagoblin/notifications/tools.py | 31 | ||||
-rw-r--r-- | mediagoblin/notifications/views.py | 22 |
4 files changed, 69 insertions, 33 deletions
diff --git a/mediagoblin/notifications/__init__.py b/mediagoblin/notifications/__init__.py index b6f9f478..d554de2d 100644 --- a/mediagoblin/notifications/__init__.py +++ b/mediagoblin/notifications/__init__.py @@ -16,8 +16,8 @@ import logging -from mediagoblin.db.models import Notification, \ - CommentNotification, CommentSubscription, User +from mediagoblin.db.models import Notification, CommentSubscription, User, \ + Comment, GenericModelReference from mediagoblin.notifications.task import email_notification_task from mediagoblin.notifications.tools import generate_comment_message @@ -27,20 +27,29 @@ def trigger_notification(comment, media_entry, request): ''' Send out notifications about a new comment. ''' + # Verify we have the Comment object and not any other type e.g. TextComment + if not isinstance(comment, Comment): + raise ValueError("Must provide Comment to trigger_notification") + + # Get the associated object associated to the Comment wrapper. + comment_object = comment.comment() + subscriptions = CommentSubscription.query.filter_by( media_entry_id=media_entry.id).all() for subscription in subscriptions: + # Check the user wants to be notified, if not, skip. if not subscription.notify: continue - if comment.get_author == subscription.user: + # If the subscriber is the current actor, don't bother. + if comment_object.get_actor == subscription.user: continue - cn = CommentNotification( + cn = Notification( user_id=subscription.user_id, - subject_id=comment.id) - + ) + cn.obj = comment cn.save() if subscription.send_email: @@ -61,11 +70,27 @@ def mark_notification_seen(notification): def mark_comment_notification_seen(comment_id, user): - notification = CommentNotification.query.filter_by( + comment = Comment.query.get(comment_id) + + # If there is no comment, there is no notification + if comment == None: + return + + comment_gmr = GenericModelReference.query.filter_by( + obj_pk=comment.id, + model_type=comment.__tablename__ + ).first() + + # If there is no GMR, there is no notification + if comment_gmr == None: + return + + notification = Notification.query.filter_by( user_id=user.id, - subject_id=comment_id).first() + object_id=comment_gmr.id + ).first() - _log.debug('Marking {0} as seen.'.format(notification)) + _log.debug(u'Marking {0} as seen.'.format(notification)) mark_notification_seen(notification) diff --git a/mediagoblin/notifications/task.py b/mediagoblin/notifications/task.py index 52573b57..652b78e2 100644 --- a/mediagoblin/notifications/task.py +++ b/mediagoblin/notifications/task.py @@ -20,7 +20,7 @@ from celery import registry from celery.task import Task from mediagoblin.tools.mail import send_email -from mediagoblin.db.models import CommentNotification +from mediagoblin.db.models import Notification _log = logging.getLogger(__name__) @@ -34,8 +34,8 @@ class EmailNotificationTask(Task): the web server. ''' def run(self, notification_id, message): - cn = CommentNotification.query.filter_by(id=notification_id).first() - _log.info('Sending notification email about {0}'.format(cn)) + cn = Notification.query.filter_by(id=notification_id).first() + _log.info(u'Sending notification email about {0}'.format(cn)) return send_email( message['from'], diff --git a/mediagoblin/notifications/tools.py b/mediagoblin/notifications/tools.py index 25432780..b251f0aa 100644 --- a/mediagoblin/notifications/tools.py +++ b/mediagoblin/notifications/tools.py @@ -18,32 +18,38 @@ from mediagoblin.tools.template import render_template from mediagoblin.tools.translate import pass_to_ugettext as _ from mediagoblin import mg_globals -def generate_comment_message(user, comment, media, request): +def generate_comment_message(user, comment, commentee, request): """ Sends comment email to user when a comment is made on their media. Args: - user: the user object to whom the email is sent - - comment: the comment object referencing user's media - - media: the media object the comment is about + - comment: the comment wrapper object + - commentee: the object the comment is on - request: the request """ + # Get the comment object associated to the wrapper + comment_object = comment.comment() + + # Get the URL to the comment comment_url = request.urlgen( - 'mediagoblin.user_pages.media_home.view_comment', - comment=comment.id, - user=media.get_uploader.username, - media=media.slug_or_id, - qualified=True) + '#comment' + "mediagoblin.user_pages.media_home.view_comment", + comment=comment.id, + user=commentee.get_actor.username, + media=commentee.slug_or_id, + qualified=True) + "#comment" - comment_author = comment.get_author.username + comment_author = comment.comment().get_actor.username rendered_email = render_template( request, 'mediagoblin/user_pages/comment_email.txt', {'username': user.username, 'comment_author': comment_author, - 'comment_content': comment.content, - 'comment_url': comment_url}) + 'comment_content': comment_object.content, + 'comment_url': comment_url + } + ) return { 'from': mg_globals.app_config['email_sender_address'], @@ -52,4 +58,5 @@ def generate_comment_message(user, comment, media, request): comment_author=comment_author, instance_title=mg_globals.app_config['html_title']) \ + _('commented on your post'), - 'body': rendered_email} + 'body': rendered_email + } diff --git a/mediagoblin/notifications/views.py b/mediagoblin/notifications/views.py index cfe66b2e..30ecec77 100644 --- a/mediagoblin/notifications/views.py +++ b/mediagoblin/notifications/views.py @@ -30,10 +30,10 @@ def subscribe_comments(request, media): add_comment_subscription(request.user, media) - messages.add_message(request, - messages.SUCCESS, - _('Subscribed to comments on %s!') - % media.title) + messages.add_message( + request, + messages.SUCCESS, + _('Subscribed to comments on %s!') % media.title) return redirect(request, location=media.url_for_self(request.urlgen)) @@ -43,10 +43,11 @@ def subscribe_comments(request, media): def silence_comments(request, media): silence_comment_subscription(request.user, media) - messages.add_message(request, - messages.SUCCESS, - _('You will not receive notifications for comments on' - ' %s.') % media.title) + messages.add_message( + request, + messages.SUCCESS, + _('You will not receive notifications for comments on %s.') % + media.title) return redirect(request, location=media.url_for_self(request.urlgen)) @@ -57,7 +58,10 @@ def mark_all_comment_notifications_seen(request): Marks all comment notifications seen. """ for comment in get_notifications(request.user.id): - mark_comment_notification_seen(comment.subject_id, request.user) + mark_comment_notification_seen( + comment.id, + request.user + ) if request.GET.get('next'): return redirect(request, location=request.GET.get('next')) |