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
, 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
):
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_actor
== subscription
.user
:
41 user_id
=subscription
.user_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 comment
= Comment
.query
.get(comment_id
).comment()
65 comment_gmr
= GenericModelReference
.query
.filter_by(
67 model_type
=comment
.__tablename
__
69 notification
= Notification
.query
.filter_by(
71 object_id
=comment_gmr
.id
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(
82 media_entry_id
=media_entry_id
).first()
84 def add_comment_subscription(user
, media_entry
):
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.
91 cn
= get_comment_subscription(user
.id, media_entry
.id)
94 cn
= CommentSubscription(
96 media_entry_id
=media_entry
.id)
100 if not user
.wants_comment_notification
:
101 cn
.send_email
= False
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)
115 cn
.send_email
= False
119 def remove_comment_subscription(user
, media_entry
):
120 cn
= get_comment_subscription(user
.id, media_entry
.id)
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()\
134 # If the user does not want notifications, don't return any
135 if not wants_notifications
:
139 query
= query
.filter_by(seen
=False)
141 notifications
= query
.limit(
142 NOTIFICATION_FETCH_LIMIT
).all()
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()\
153 query
= query
.filter_by(seen
=False)
155 # If the user doesn't want notifications, don't show any
156 if not wants_notifications
:
159 count
= query
.count()