forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.py
249 lines (226 loc) · 11.9 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import string
import random
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api.labs import taskqueue
from v2ex.babel import Member
from v2ex.babel import Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply
from v2ex.babel import Note
from v2ex.babel import Notification
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.l10n import *
from v2ex.babel.ext.cookies import Cookies
from v2ex.babel.handlers import BaseHandler
import config
template.register_template_library('v2ex.templatetags.filters')
class NotificationsHandler(BaseHandler):
def get(self):
if self.member:
if self.member.private_token is None:
self.member.private_token = hashlib.sha256(str(self.member.num) + ';' + config.site_key).hexdigest()
self.member.put()
notifications = memcache.get('nn::' + self.member.username_lower)
if notifications is None:
q = db.GqlQuery("SELECT * FROM Notification WHERE for_member_num = :1 ORDER BY num DESC LIMIT 20", self.member.num)
notifications = []
i = 0
for n in q:
if i == 0:
if self.member.notification_position != n.num:
self.member.notification_position = n.num
self.member.put()
if n.type == 'reply':
n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 里回复了你'
notifications.append(n)
if n.type == 'mention_reply':
n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在回复 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 时提到了你'
notifications.append(n)
if n.type == 'mention_topic':
n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在创建主题 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 时提到了你'
notifications.append(n)
i = i + 1
self.member.notifications = 0
self.member.put()
memcache.set('Member_' + str(self.member.num), self.member, 86400)
memcache.set('nn::' + self.member.username_lower, notifications, 360)
self.values['notifications'] = notifications
self.set_title(u'提醒系统')
self.finalize(template_name='notifications', mobile_optimized=True)
else:
self.redirect('/signin')
class NotificationsCheckHandler(BaseHandler):
def post(self, member_key):
member = db.get(db.Key(member_key))
if member:
if member.notification_position is None:
member.notification_position = 0
q = db.GqlQuery("SELECT __key__ FROM Notification WHERE for_member_num = :1 AND num > :2 ORDER BY num DESC", member.num, member.notification_position)
count = q.count()
if count > 0:
member.notifications = count
member.put()
memcache.delete('nn::' + member.username_lower)
memcache.set('Member_' + str(member.num), member, 86400)
# For mentions in reply content
class NotificationsReplyHandler(BaseHandler):
def post(self, reply_key):
reply = db.get(db.Key(reply_key))
topic = GetKindByNum('Topic', reply.topic_num)
ms = re.findall('(@[a-zA-Z0-9\_]+\.?)\s?', reply.content)
unique = []
for m in ms:
if m.lower() not in unique:
unique.append(m.lower())
keys = []
if (len(unique) > 0):
for m in unique:
m_id = re.findall('@([a-zA-Z0-9\_]+\.?)', m)
if (len(m_id) > 0):
if (m_id[0].endswith('.') != True):
member_username = m_id[0]
member = GetMemberByUsername(member_username)
if member:
if (member.key() != topic.member.key()) and (member.key() != reply.member.key()) and (member.key() not in keys):
q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.max')
if (q.count() == 1):
counter = q[0]
counter.value = counter.value + 1
else:
counter = Counter()
counter.name = 'notification.max'
counter.value = 1
q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.total')
if (q2.count() == 1):
counter2 = q2[0]
counter2.value = counter2.value + 1
else:
counter2 = Counter()
counter2.name = 'notification.total'
counter2.value = 1
notification = Notification(parent=member)
notification.num = counter.value
notification.type = 'mention_reply'
notification.payload = reply.content
notification.label1 = topic.title
notification.link1 = '/t/' + str(topic.num) + '#reply' + str(topic.replies)
notification.member = reply.member
notification.for_member_num = member.num
keys.append(str(member.key()))
counter.put()
counter2.put()
notification.put()
for k in keys:
taskqueue.add(url='/notifications/check/' + k)
# For mentions in topic title and content
class NotificationsTopicHandler(BaseHandler):
def post(self, topic_key):
topic = db.get(db.Key(topic_key))
combined = topic.title + " " + topic.content
ms = re.findall('(@[a-zA-Z0-9\_]+\.?)\s?', combined)
unique = []
for m in ms:
if m.lower() not in unique:
unique.append(m.lower())
keys = []
if (len(unique) > 0):
for m in unique:
m_id = re.findall('@([a-zA-Z0-9\_]+\.?)', m)
if (len(m_id) > 0):
if (m_id[0].endswith('.') != True):
member_username = m_id[0]
member = GetMemberByUsername(member_username)
if member:
if member.key() != topic.member.key() and member.key() not in keys:
q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.max')
if (q.count() == 1):
counter = q[0]
counter.value = counter.value + 1
else:
counter = Counter()
counter.name = 'notification.max'
counter.value = 1
q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'notification.total')
if (q2.count() == 1):
counter2 = q2[0]
counter2.value = counter2.value + 1
else:
counter2 = Counter()
counter2.name = 'notification.total'
counter2.value = 1
notification = Notification(parent=member)
notification.num = counter.value
notification.type = 'mention_topic'
notification.payload = topic.content
notification.label1 = topic.title
notification.link1 = '/t/' + str(topic.num) + '#reply' + str(topic.replies)
notification.member = topic.member
notification.for_member_num = member.num
keys.append(str(member.key()))
counter.put()
counter2.put()
notification.put()
for k in keys:
taskqueue.add(url='/notifications/check/' + k)
class NotificationsFeedHandler(BaseHandler):
def head(self, private_token):
pass
def get(self, private_token):
n = memcache.get('n_' + private_token)
if n is not None:
self.values['notification'] = n
self.response.headers['Content-type'] = 'application/xml;charset=UTF-8'
self.values['member'] = self.member
self.finalize(template_name='notifications', template_root='feed', template_type='xml')
else:
q = db.GqlQuery("SELECT * FROM Member WHERE private_token = :1", private_token)
count = q.count()
if count > 0:
member = q[0]
q = db.GqlQuery("SELECT * FROM Notification WHERE for_member_num = :1 ORDER BY num DESC LIMIT 20", member.num)
notifications = []
i = 0
for n in q:
if n.type == 'reply':
n.title = u'' + n.member.username + u' 在 ' + self.escape(n.label1) + u' 里回复了你'
n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 里回复了你'
notifications.append(n)
if n.type == 'mention_reply':
n.title = u'' + n.member.username + u' 在回复 ' + self.escape(n.label1) + u' 时提到了你'
n.text = u'<a href="/member/' + n.member.username + u'"><strong>' + n.member.username + u'</strong></a> 在回复 <a href="' + n.link1 + '">' + self.escape(n.label1) + u'</a> 时提到了你'
notifications.append(n)
i = i + 1
self.values['notifications'] = notifications
memcache.set('n_' + private_token, notifications, 600)
self.response.headers['Content-type'] = 'application/xml;charset=UTF-8'
self.values['site'] = GetSite()
self.values['member'] = member
self.finalize(template_name='notifications', template_root='feed', template_type='xml')
def main():
application = webapp.WSGIApplication([
('/notifications/?', NotificationsHandler),
('/notifications/check/(.+)', NotificationsCheckHandler),
('/notifications/reply/(.+)', NotificationsReplyHandler),
('/notifications/topic/(.+)', NotificationsTopicHandler),
('/n/([a-z0-9]+).xml', NotificationsFeedHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()