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/>.
19 from mediagoblin
.db
.models
import Notification
, \
20 CommentNotification
, CommentSubscription
, User
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
):
28 Send out notifications about a new comment.
30 subscriptions
= CommentSubscription
.query
.filter_by(
31 media_entry_id
=media_entry
.id).all()
33 for subscription
in subscriptions
:
34 if not subscription
.notify
:
37 if comment
.get_author
== subscription
.user
:
40 cn
= CommentNotification(
41 user_id
=subscription
.user_id
,
42 subject_id
=comment
.id)
46 if subscription
.send_email
:
47 message
= generate_comment_message(
53 from mediagoblin
.notifications
.task
import email_notification_task
54 email_notification_task
.apply_async([cn
.id, message
])
57 def mark_notification_seen(notification
):
59 notification
.seen
= True
63 def mark_comment_notification_seen(comment_id
, user
):
64 notification
= CommentNotification
.query
.filter_by(
66 subject_id
=comment_id
).first()
68 _log
.debug(u
'Marking {0} as seen.'.format(notification
))
70 mark_notification_seen(notification
)
73 def get_comment_subscription(user_id
, media_entry_id
):
74 return CommentSubscription
.query
.filter_by(
76 media_entry_id
=media_entry_id
).first()
78 def add_comment_subscription(user
, media_entry
):
80 Create a comment subscription for a User on a MediaEntry.
82 Uses the User's wants_comment_notification to set email notifications for
83 the subscription to enabled/disabled.
85 cn
= get_comment_subscription(user
.id, media_entry
.id)
88 cn
= CommentSubscription(
90 media_entry_id
=media_entry
.id)
94 if not user
.wants_comment_notification
:
100 def silence_comment_subscription(user
, media_entry
):
102 Silence a subscription so that the user is never notified in any way about
103 new comments on an entry
105 cn
= get_comment_subscription(user
.id, media_entry
.id)
109 cn
.send_email
= False
113 def remove_comment_subscription(user
, media_entry
):
114 cn
= get_comment_subscription(user
.id, media_entry
.id)
120 NOTIFICATION_FETCH_LIMIT
= 100
123 def get_notifications(user_id
, only_unseen
=True):
124 query
= Notification
.query
.filter_by(user_id
=user_id
)
125 wants_notifications
= User
.query
.filter_by(id=user_id
).first()\
128 # If the user does not want notifications, don't return any
129 if not wants_notifications
:
133 query
= query
.filter_by(seen
=False)
135 notifications
= query
.limit(
136 NOTIFICATION_FETCH_LIMIT
).all()
141 def get_notification_count(user_id
, only_unseen
=True):
142 query
= Notification
.query
.filter_by(user_id
=user_id
)
143 wants_notifications
= User
.query
.filter_by(id=user_id
).first()\
147 query
= query
.filter_by(seen
=False)
149 # If the user doesn't want notifications, don't show any
150 if not wants_notifications
:
153 count
= query
.count()