Add image URL's (thumb & full)
[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, User
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 from mediagoblin.notifications.task import email_notification_task
54 email_notification_task.apply_async([cn.id, message])
55
56
57 def mark_notification_seen(notification):
58 if notification:
59 notification.seen = True
60 notification.save()
61
62
63 def mark_comment_notification_seen(comment_id, user):
64 notification = CommentNotification.query.filter_by(
65 user_id=user.id,
66 subject_id=comment_id).first()
67
68 _log.debug(u'Marking {0} as seen.'.format(notification))
69
70 mark_notification_seen(notification)
71
72
73 def get_comment_subscription(user_id, media_entry_id):
74 return CommentSubscription.query.filter_by(
75 user_id=user_id,
76 media_entry_id=media_entry_id).first()
77
78 def add_comment_subscription(user, media_entry):
79 '''
80 Create a comment subscription for a User on a MediaEntry.
81
82 Uses the User's wants_comment_notification to set email notifications for
83 the subscription to enabled/disabled.
84 '''
85 cn = get_comment_subscription(user.id, media_entry.id)
86
87 if not cn:
88 cn = CommentSubscription(
89 user_id=user.id,
90 media_entry_id=media_entry.id)
91
92 cn.notify = True
93
94 if not user.wants_comment_notification:
95 cn.send_email = False
96
97 cn.save()
98
99
100 def silence_comment_subscription(user, media_entry):
101 '''
102 Silence a subscription so that the user is never notified in any way about
103 new comments on an entry
104 '''
105 cn = get_comment_subscription(user.id, media_entry.id)
106
107 if cn:
108 cn.notify = False
109 cn.send_email = False
110 cn.save()
111
112
113 def remove_comment_subscription(user, media_entry):
114 cn = get_comment_subscription(user.id, media_entry.id)
115
116 if cn:
117 cn.delete()
118
119
120 NOTIFICATION_FETCH_LIMIT = 100
121
122
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()\
126 .wants_notifications
127
128 # If the user does not want notifications, don't return any
129 if not wants_notifications:
130 return None
131
132 if only_unseen:
133 query = query.filter_by(seen=False)
134
135 notifications = query.limit(
136 NOTIFICATION_FETCH_LIMIT).all()
137
138 return notifications
139
140
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()\
144 .wants_notifications
145
146 if only_unseen:
147 query = query.filter_by(seen=False)
148
149 # If the user doesn't want notifications, don't show any
150 if not wants_notifications:
151 count = None
152 else:
153 count = query.count()
154
155 return count