Merge commit 'refs/merge-requests/55' of git://gitorious.org/mediagoblin/mediagoblin...
[mediagoblin.git] / mediagoblin / notifications / __init__.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17 import logging
18
19 from mediagoblin.db.models import Notification, \
20 CommentNotification, CommentSubscription
21 from mediagoblin.notifications.task import email_notification_task
22 from mediagoblin.notifications.tools import generate_comment_message
23
24 _log = logging.getLogger(__name__)
25
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()
32
33 for subscription in subscriptions:
34 if not subscription.notify:
35 continue
36
37 if comment.get_author == subscription.user:
38 continue
39
40 cn = CommentNotification(
41 user_id=subscription.user_id,
42 subject_id=comment.id)
43
44 cn.save()
45
46 if subscription.send_email:
47 message = generate_comment_message(
48 subscription.user,
49 comment,
50 media_entry,
51 request)
52
53 email_notification_task.apply_async([cn.id, message])
54
55
56 def mark_notification_seen(notification):
57 if notification:
58 notification.seen = True
59 notification.save()
60
61
62 def mark_comment_notification_seen(comment_id, user):
63 notification = CommentNotification.query.filter_by(
64 user_id=user.id,
65 subject_id=comment_id).first()
66
67 _log.debug('Marking {0} as seen.'.format(notification))
68
69 mark_notification_seen(notification)
70
71
72 def get_comment_subscription(user_id, media_entry_id):
73 return CommentSubscription.query.filter_by(
74 user_id=user_id,
75 media_entry_id=media_entry_id).first()
76
77 def add_comment_subscription(user, media_entry):
78 '''
79 Create a comment subscription for a User on a MediaEntry.
80
81 Uses the User's wants_comment_notification to set email notifications for
82 the subscription to enabled/disabled.
83 '''
84 cn = get_comment_subscription(user.id, media_entry.id)
85
86 if not cn:
87 cn = CommentSubscription(
88 user_id=user.id,
89 media_entry_id=media_entry.id)
90
91 cn.notify = True
92
93 if not user.wants_comment_notification:
94 cn.send_email = False
95
96 cn.save()
97
98
99 def silence_comment_subscription(user, media_entry):
100 '''
101 Silence a subscription so that the user is never notified in any way about
102 new comments on an entry
103 '''
104 cn = get_comment_subscription(user.id, media_entry.id)
105
106 if cn:
107 cn.notify = False
108 cn.send_email = False
109 cn.save()
110
111
112 def remove_comment_subscription(user, media_entry):
113 cn = get_comment_subscription(user.id, media_entry.id)
114
115 if cn:
116 cn.delete()
117
118
119 NOTIFICATION_FETCH_LIMIT = 100
120
121
122 def get_notifications(user_id, only_unseen=True):
123 query = Notification.query.filter_by(user_id=user_id)
124
125 if only_unseen:
126 query = query.filter_by(seen=False)
127
128 notifications = query.limit(
129 NOTIFICATION_FETCH_LIMIT).all()
130
131 return notifications
132
133 def get_notification_count(user_id, only_unseen=True):
134 query = Notification.query.filter_by(user_id=user_id)
135
136 if only_unseen:
137 query = query.filter_by(seen=False)
138
139 count = query.count()
140
141 return count