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 # Verify we have the Comment object and not any other type e.g. TextComment
31 if not isinstance(comment
, Comment
):
32 raise ValueError("Must provide Comment to trigger_notification")
34 # Get the associated object associated to the Comment wrapper.
35 comment_object
= comment
.comment()
37 subscriptions
= CommentSubscription
.query
.filter_by(
38 media_entry_id
=media_entry
.id).all()
40 for subscription
in subscriptions
:
41 # Check the user wants to be notified, if not, skip.
42 if not subscription
.notify
:
45 # If the subscriber is the current actor, don't bother.
46 if comment_object
.get_actor
== subscription
.user
:
50 user_id
=subscription
.user_id
,
55 if subscription
.send_email
:
56 message
= generate_comment_message(
62 from mediagoblin
.notifications
.task
import email_notification_task
63 email_notification_task
.apply_async([cn
.id, message
])
66 def mark_notification_seen(notification
):
68 notification
.seen
= True
72 def mark_comment_notification_seen(comment_id
, user
):
73 comment
= Comment
.query
.get(comment_id
)
75 # If there is no comment, there is no notification
79 comment_gmr
= GenericModelReference
.query
.filter_by(
81 model_type
=comment
.__tablename
__
84 # If there is no GMR, there is no notification
85 if comment_gmr
== None:
88 notification
= Notification
.query
.filter_by(
90 object_id
=comment_gmr
.id
93 _log
.debug(u
'Marking {0} as seen.'.format(notification
))
95 mark_notification_seen(notification
)
98 def get_comment_subscription(user_id
, media_entry_id
):
99 return CommentSubscription
.query
.filter_by(
101 media_entry_id
=media_entry_id
).first()
103 def add_comment_subscription(user
, media_entry
):
105 Create a comment subscription for a User on a MediaEntry.
107 Uses the User's wants_comment_notification to set email notifications for
108 the subscription to enabled/disabled.
110 cn
= get_comment_subscription(user
.id, media_entry
.id)
113 cn
= CommentSubscription(
115 media_entry_id
=media_entry
.id)
119 if not user
.wants_comment_notification
:
120 cn
.send_email
= False
125 def silence_comment_subscription(user
, media_entry
):
127 Silence a subscription so that the user is never notified in any way about
128 new comments on an entry
130 cn
= get_comment_subscription(user
.id, media_entry
.id)
134 cn
.send_email
= False
138 def remove_comment_subscription(user
, media_entry
):
139 cn
= get_comment_subscription(user
.id, media_entry
.id)
145 NOTIFICATION_FETCH_LIMIT
= 100
148 def get_notifications(user_id
, only_unseen
=True):
149 query
= Notification
.query
.filter_by(user_id
=user_id
)
150 wants_notifications
= User
.query
.filter_by(id=user_id
).first()\
153 # If the user does not want notifications, don't return any
154 if not wants_notifications
:
158 query
= query
.filter_by(seen
=False)
160 notifications
= query
.limit(
161 NOTIFICATION_FETCH_LIMIT
).all()
166 def get_notification_count(user_id
, only_unseen
=True):
167 query
= Notification
.query
.filter_by(user_id
=user_id
)
168 wants_notifications
= User
.query
.filter_by(id=user_id
).first()\
172 query
= query
.filter_by(seen
=False)
174 # If the user doesn't want notifications, don't show any
175 if not wants_notifications
:
178 count
= query
.count()