-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsubscription.py
510 lines (435 loc) · 16.6 KB
/
subscription.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# -*- coding: utf-8 -*-
import json
import re
from trac.admin import IAdminCommandProvider
from trac.attachment import Attachment, IAttachmentChangeListener
from trac.core import Component, implements
from trac.util.html import html as tag
from trac.versioncontrol import (
RepositoryManager, NoSuchChangeset, IRepositoryChangeListener)
from trac.web.api import HTTPNotFound, IRequestHandler, ITemplateStreamFilter
from genshi.filters import Transformer
from code_comments.api import ICodeCommentChangeListener
from code_comments.comments import Comments
class Subscription(object):
"""
Representation of a code comment subscription.
"""
id = 0
user = ''
type = ''
path = ''
rev = ''
repos = ''
notify = True
def __init__(self, env, data=None):
if isinstance(data, dict):
self.__dict__ = data
self.env = env
def __str__(self):
"""
Returns a user friendly string representation.
"""
template = "{0} for {1} {2}"
if self.type == "changeset":
_identifier = self.rev
elif self.type == "browser":
_identifier = "{0} @ {1}".format(self.path, self.rev)
else:
_identifier = self.path
return template.format(self.user, self.type, _identifier)
@classmethod
def select(cls, env, args={}, notify=None):
"""
Retrieve existing subscription(s).
"""
select = u'SELECT * FROM code_comments_subscriptions'
if notify:
args['notify'] = bool(notify)
if len(args) > 0:
select += ' WHERE '
criteria = []
for key, value in args.iteritems():
template = u'{0}={1}'
if isinstance(value, basestring):
template = u'{0}=\'{1}\''
if (isinstance(value, tuple) or isinstance(value, list)):
template = u'{0} IN (\'{1}\')'
value = u'\',\''.join(value)
if isinstance(value, bool):
value = int(value)
criteria.append(template.format(key, value))
select += u' AND '.join(criteria)
for row in env.db_query(select):
yield cls._from_row(env, row)
def insert(self):
"""
Insert a new subscription. Returns bool to indicate success.
"""
if self.id > 0:
# Already has an id, don't insert
return False
else:
with self.env.db_transaction as db:
cursor = db.cursor()
cursor.execute("""
INSERT INTO code_comments_subscriptions
(user, type, path, repos, rev, notify)
VALUES (%s, %s, %s, %s, %s, %s)
""", (self.user, self.type, self.path, self.repos,
self.rev, self.notify))
self.id = db.get_last_id(cursor, 'code_comments_subscriptions')
return True
def update(self):
"""
Update an existing subscription. Returns bool to indicate success.
"""
if self.id == 0:
# Doesn't have a valid id, don't update
return False
else:
try:
self.env.db_transaction("""
UPDATE code_comments_subscriptions
SET user=%s, type=%s, path=%s, repos=%s, rev=%s,
notify=%s WHERE id=%s
""", (self.user, self.type, self.path, self.repos,
self.rev, self.notify, self.id))
except self.env.db_exc.IntegrityError:
self.env.log.warning("Subscription update failed.")
return False
return True
def delete(self):
"""
Delete an existing subscription.
"""
if self.id > 0:
self.env.db_transaction("""
DELETE FROM code_comments_subscriptions WHERE id=%s
""", (self.id,))
@classmethod
def _from_row(cls, env, row):
"""
Creates a subscription from a list (representing a database row).
"""
try:
subscription = cls(env)
subscription.id = int(row[0])
subscription.user = row[1]
subscription.type = row[2]
subscription.path = row[3]
subscription.repos = row[4]
subscription.rev = row[5]
subscription.notify = bool(row[6])
return subscription
except IndexError:
# Invalid row
return None
@classmethod
def from_dict(cls, env, dict_, create=True):
"""
Retrieves or (optionally) creates a subscription from a dict.
"""
subscription = None
# Look for existing subscriptions
args = {
'user': dict_['user'],
'type': dict_['type'],
'path': dict_['path'],
'repos': dict_['repos'],
'rev': dict_['rev'],
}
subscriptions = cls.select(env, args)
# Only return the first one
for _subscription in subscriptions:
if subscription is None:
subscription = _subscription
env.log.info('Subscription found: [%d] %s',
subscription.id, subscription)
else:
# The unique constraint on the table should prevent this ever
# occurring
env.log.warning('Multiple subscriptions found: [%d] %s',
subscription.id, subscription)
# (Optionally) create a new subscription if we didn't find one
if subscription is None:
subscription = cls(env, dict_)
if create:
subscription.insert()
env.log.info('Subscription created: [%d] %s',
subscription.id, subscription)
return subscription
@classmethod
def from_attachment(cls, env, attachment, user=None, notify=True):
"""
Creates a subscription from an Attachment object.
"""
_path = u"/{0}/{1}/{2}".format(attachment.parent_realm,
attachment.parent_id,
attachment.filename)
sub = {
'user': user or attachment.author,
'type': 'attachment',
'path': _path,
'repos': '',
'rev': '',
'notify': notify,
}
return cls.from_dict(env, sub)
@classmethod
def from_changeset(cls, env, changeset, user=None, notify=True):
"""
Creates a subscription from a Changeset object.
"""
sub = {
'user': user or changeset.author,
'type': 'changeset',
'path': '',
'repos': changeset.repos.reponame,
'rev': changeset.rev,
'notify': notify,
}
return cls.from_dict(env, sub)
@classmethod
def from_comment(cls, env, comment, user=None, notify=True):
"""
Creates a subscription from a Comment object.
"""
sub = {
'user': user or comment.author,
'type': comment.type,
'notify': notify,
}
# Munge attachments
if comment.type == 'attachment':
sub['path'] = comment.path.split(':')[1]
sub['repos'] = ''
sub['rev'] = ''
# Munge changesets and browser
if comment.type in ('changeset', 'browser'):
rm = RepositoryManager(env)
reponame, repos, path = rm.get_repository_by_path(comment.path)
if comment.type == 'browser':
sub['path'] = path
else:
sub['path'] = ''
sub['repos'] = reponame or '(default)'
try:
_cs = repos.get_changeset(comment.revision)
except NoSuchChangeset:
# Invalid changeset
return None
else:
sub['rev'] = _cs.rev
return cls.from_dict(env, sub)
@classmethod
def for_attachment(cls, env, attachment, path=None, notify=None):
"""
Returns all subscriptions for an attachment. The path can be
overridden.
"""
path_template = "/{0}/{1}/{2}"
_path = path or path_template.format(attachment.parent_realm,
attachment.parent_id,
attachment.filename)
args = {
'type': 'attachment',
'path': _path,
}
return cls.select(env, args, notify)
@classmethod
def for_changeset(cls, env, changeset, notify=None):
"""
Returns all subscriptions for an changeset.
"""
args = {
'type': 'changeset',
'repos': changeset.repos.reponame,
'rev': changeset.rev,
}
return cls.select(env, args, notify)
@classmethod
def for_comment(cls, env, comment, notify=None):
"""
Return all subscriptions for a comment.
"""
args = {}
if comment.type == 'attachment':
args['type'] = comment.type
args['path'] = comment.path.split(':')[1]
if comment.type == 'changeset':
args['type'] = comment.type
args['rev'] = str(comment.revision)
if comment.type == 'browser':
rm = RepositoryManager(env)
reponame, _, path = rm.get_repository_by_path(comment.path)
args['type'] = ('browser', 'changeset')
args['path'] = (path, '')
args['repos'] = reponame
args['rev'] = (str(comment.revision), '')
return cls.select(env, args, notify)
@classmethod
def for_request(cls, env, req, create=False):
"""
Return a **single** subscription for a HTTP request.
"""
rm = RepositoryManager(env)
dict_ = {
'user': req.authname,
'type': req.args.get('realm'),
'path': '',
'rev': '',
'repos': '',
}
path = req.args.get('path') or ''
if dict_['type'] == 'attachment':
dict_['path'] = path
if dict_['type'] == 'changeset':
parts = [p for p in path.split('/') if p]
dict_['rev'] = parts[0]
dict_['repos'] = parts[1]
if dict_['type'] == 'browser':
reponame, repos, path = rm.get_repository_by_path(path)
dict_['path'] = '/' if len(path) == 0 else path
dict_['rev'] = req.args.get('rev') or ''
dict_['repos'] = reponame
return cls.from_dict(env, dict_, create=create)
class SubscriptionJSONEncoder(json.JSONEncoder):
"""
JSON Encoder for a Subscription object.
"""
def default(self, o):
data = o.__dict__.copy()
del data['env']
return data
class SubscriptionAdmin(Component):
"""
trac-admin command provider for subscription administration.
"""
implements(IAdminCommandProvider)
# IAdminCommandProvider methods
def get_admin_commands(self):
yield ('subscription seed', '',
"""Seeds subscriptions for existing attachments, changesets,
and comments.
""",
None, self._do_seed)
def _do_seed(self):
# Create a subscription for all existing attachments
for row in self.env.db_query("""
SELECT DISTINCT type, id FROM attachment
"""):
for attachment in Attachment.select(self.env, row[0], row[1]):
Subscription.from_attachment(self.env, attachment)
# Create a subscription for all existing revisions
rm = RepositoryManager(self.env)
repos = rm.get_real_repositories()
for repo in repos:
_rev = repo.get_oldest_rev()
while _rev:
try:
_cs = repo.get_changeset(_rev)
Subscription.from_changeset(self.env, _cs)
except NoSuchChangeset:
pass
_rev = repo.next_rev(_rev)
# Create a subscription for all existing comments
comments = Comments(None, self.env).all()
for comment in comments:
Subscription.from_comment(self.env, comment)
class SubscriptionListeners(Component):
"""
Automatically creates subscriptions for attachments, changesets, and
comments.
"""
implements(IAttachmentChangeListener, IRepositoryChangeListener,
ICodeCommentChangeListener)
# IAttachmentChangeListener methods
def attachment_added(self, attachment):
Subscription.from_attachment(self.env, attachment)
def attachment_deleted(self, attachment):
for subscription in Subscription.for_attachment(self.env, attachment):
subscription.delete()
def attachment_reparented(self, attachment, old_parent_realm,
old_parent_id):
path_template = "/{0}/{1}/{2}"
old_path = path_template.format(old_parent_realm,
old_parent_id,
attachment.filename)
new_path = path_template.format(attachment.parent_realm,
attachment.parent_id,
attachment.filename)
for subscription in Subscription.for_attachment(self.env, attachment,
old_path):
subscription.path = new_path
subscription.update()
# IRepositoryChangeListener methods
def changeset_added(self, repos, changeset):
Subscription.from_changeset(self.env, changeset)
def changeset_modified(self, repos, changeset, old_changeset):
if changeset.author != old_changeset.author:
# Create a new author subscription
Subscription.from_changeset(self.env, changeset)
# ICodeCommentChangeListener methods
def comment_created(self, comment):
Subscription.from_comment(self.env, comment)
class SubscriptionModule(Component):
implements(IRequestHandler, ITemplateStreamFilter)
# IRequestHandler methods
def match_request(self, req):
match = re.match(r'\/subscription\/(\w+)(\/?.*)$', req.path_info)
if match:
if match.group(1):
req.args['realm'] = match.group(1)
if match.group(2):
req.args['path'] = match.group(2)
return True
def process_request(self, req):
if req.method == 'POST':
return self._do_POST(req)
elif req.method == 'PUT':
return self._do_PUT(req)
return self._do_GET(req)
# ITemplateStreamFilter methods
def filter_stream(self, req, method, filename, stream, data):
if re.match(r'^/(changeset|browser|attachment/ticket/\d+/.?).*',
req.path_info):
filter = Transformer('//h1')
button = self._subscription_button(req.path_info,
req.args.get('rev'))
stream |= filter.before(button)
return stream
# Internal methods
def _do_GET(self, req):
subscription = Subscription.for_request(self.env, req)
if subscription is None:
req.send('', 'application/json', 204)
req.send(json.dumps(subscription, cls=SubscriptionJSONEncoder),
'application/json')
def _do_POST(self, req):
content = req.read()
data = json.loads(content)
subscription = Subscription.from_dict(self.env, data, create=True)
status = 201
req.send(json.dumps(subscription, cls=SubscriptionJSONEncoder),
'application/json', status)
def _do_PUT(self, req):
content = req.read()
if len(content) > 0:
data = json.loads(content)
subscription = Subscription.from_dict(self.env, data, create=True)
subscription.notify = data['notify']
subscription.update()
req.send(json.dumps(subscription, cls=SubscriptionJSONEncoder),
'application/json')
def _subscription_button(self, path, rev):
"""
Generates a (disabled) button to connect JavaScript to.
"""
return tag.button(
'Subscribe', id_='subscribe', disabled=True,
title=('Code comment subscriptions require JavaScript '
'to be enabled'),
data_base_url=self.env.project_url or self.env.abs_href(),
data_path=path,
data_rev=rev)