Fix #5391 - Alembic migrations would only work for SQLite
[larjonas-mediagoblin.git] / mediagoblin / notifications / __init__.py
blob8690aae5a71134671829a16f121ca30d065d1e83
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import logging
19 from mediagoblin.db.models import Notification, CommentSubscription, User, \
20 Comment, GenericModelReference
21 from mediagoblin.notifications.task import email_notification_task
22 from mediagoblin.notifications.tools import generate_comment_message
24 _log = logging.getLogger(__name__)
26 def trigger_notification(comment, media_entry, request):
27 '''
28 Send out notifications about a new comment.
29 '''
30 subscriptions = CommentSubscription.query.filter_by(
31 media_entry_id=media_entry.id).all()
33 for subscription in subscriptions:
34 if not subscription.notify:
35 continue
37 if comment.get_actor == subscription.user:
38 continue
40 cn = Notification(
41 user_id=subscription.user_id,
43 cn.obj = comment
44 cn.save()
46 if subscription.send_email:
47 message = generate_comment_message(
48 subscription.user,
49 comment,
50 media_entry,
51 request)
53 from mediagoblin.notifications.task import email_notification_task
54 email_notification_task.apply_async([cn.id, message])
57 def mark_notification_seen(notification):
58 if notification:
59 notification.seen = True
60 notification.save()
63 def mark_comment_notification_seen(comment_id, user):
64 comment = Comment.query.get(comment_id).comment()
65 comment_gmr = GenericModelReference.query.filter_by(
66 obj_pk=comment.id,
67 model_type=comment.__tablename__
68 ).first()
69 notification = Notification.query.filter_by(
70 user_id=user.id,
71 object_id=comment_gmr.id
72 ).first()
74 _log.debug(u'Marking {0} as seen.'.format(notification))
76 mark_notification_seen(notification)
79 def get_comment_subscription(user_id, media_entry_id):
80 return CommentSubscription.query.filter_by(
81 user_id=user_id,
82 media_entry_id=media_entry_id).first()
84 def add_comment_subscription(user, media_entry):
85 '''
86 Create a comment subscription for a User on a MediaEntry.
88 Uses the User's wants_comment_notification to set email notifications for
89 the subscription to enabled/disabled.
90 '''
91 cn = get_comment_subscription(user.id, media_entry.id)
93 if not cn:
94 cn = CommentSubscription(
95 user_id=user.id,
96 media_entry_id=media_entry.id)
98 cn.notify = True
100 if not user.wants_comment_notification:
101 cn.send_email = False
103 cn.save()
106 def silence_comment_subscription(user, media_entry):
108 Silence a subscription so that the user is never notified in any way about
109 new comments on an entry
111 cn = get_comment_subscription(user.id, media_entry.id)
113 if cn:
114 cn.notify = False
115 cn.send_email = False
116 cn.save()
119 def remove_comment_subscription(user, media_entry):
120 cn = get_comment_subscription(user.id, media_entry.id)
122 if cn:
123 cn.delete()
126 NOTIFICATION_FETCH_LIMIT = 100
129 def get_notifications(user_id, only_unseen=True):
130 query = Notification.query.filter_by(user_id=user_id)
131 wants_notifications = User.query.filter_by(id=user_id).first()\
132 .wants_notifications
134 # If the user does not want notifications, don't return any
135 if not wants_notifications:
136 return None
138 if only_unseen:
139 query = query.filter_by(seen=False)
141 notifications = query.limit(
142 NOTIFICATION_FETCH_LIMIT).all()
144 return notifications
147 def get_notification_count(user_id, only_unseen=True):
148 query = Notification.query.filter_by(user_id=user_id)
149 wants_notifications = User.query.filter_by(id=user_id).first()\
150 .wants_notifications
152 if only_unseen:
153 query = query.filter_by(seen=False)
155 # If the user doesn't want notifications, don't show any
156 if not wants_notifications:
157 count = None
158 else:
159 count = query.count()
161 return count