This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 681
/
notifications.py
428 lines (346 loc) · 12.4 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
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
from __future__ import annotations
import datetime
import json
from typing import Optional
# import requests
import requests
from django.conf import settings
from django.db.models import Q
from django.middleware.csrf import get_token
from ninja import Field, Router
from ninja.pagination import paginate
from sentry_sdk import capture_exception
from kuma.documenturls.models import DocumentURL
from kuma.notifications.models import (
DefaultWatch,
Notification,
NotificationData,
UserWatch,
Watch,
)
from kuma.notifications.utils import process_changes
from kuma.settings.common import MAX_NON_SUBSCRIBED
from kuma.users.models import UserProfile
from ..pagination import LimitOffsetPaginatedResponse, LimitOffsetPaginationWithMeta
from ..smarter_schema import Schema
admin_router = Router(tags=["admin"])
notifications_router = Router(tags=["notifications"])
watch_router = Router(tags=["watch"])
limit_offset_paginate_with_meta = paginate(LimitOffsetPaginationWithMeta)
class Ok(Schema):
ok: bool = True
class WatchUpdateResponse(Schema):
ok: bool = True
subscription_limit_reached: bool = False
class NotOk(Schema):
ok: bool = False
error: str
info: dict = None
class NotificationSchema(Schema):
id: int
title: str = Field(..., alias="notification.title")
text: str = Field(..., alias="notification.text")
url: str = Field(..., alias="notification.page_url")
created: datetime.datetime = Field(..., alias="notification.created")
deleted: bool
read: bool
starred: bool
@notifications_router.get(
"/",
response=LimitOffsetPaginatedResponse[NotificationSchema],
url_name="plus.notifications",
)
@limit_offset_paginate_with_meta
def notifications(
request,
starred: bool = None,
unread: bool = None,
filterType: str = None,
q: str = None,
sort: str = None,
**kwargs,
):
qs = request.user.notification_set.select_related("notification")
if starred is not None:
qs = qs.filter(starred=starred)
if unread is not None:
qs = qs.filter(read=not unread)
if filterType:
qs = qs.filter(notification__type=filterType)
if q:
qs = qs.filter(
Q(notification__title__icontains=q) | Q(notification__text__icontains=q)
)
if sort == "title":
order_by = "notification__title"
else:
order_by = "-notification__created"
qs = qs.order_by(order_by, "id")
qs = qs.filter(deleted=False)
return qs
@notifications_router.post("/all/mark-as-read/", response=Ok)
def mark_all_as_read(request):
request.user.notification_set.filter(read=False).update(read=True)
return True
@notifications_router.post("/{int:pk}/mark-as-read/", response=Ok)
def mark_as_read(request, pk: int):
request.user.notification_set.filter(pk=pk, read=False).update(read=True)
return True
@notifications_router.post("/{int:pk}/toggle-starred/", response={200: Ok, 400: str})
def toggle_starred(request, pk: int):
try:
notification = Notification.objects.get(user=request.user, pk=pk)
except Notification.DoesNotExist:
return 400, "no matching notification"
notification.starred = not notification.starred
notification.save()
return 200, True
class StarMany(Schema):
ids: list[int]
@notifications_router.post(
"/star-ids/", response={200: Ok, 400: str}, url_name="notifications_star_ids"
)
def star_many(request, data: StarMany):
request.user.notification_set.filter(deleted=False).filter(pk__in=data.ids).update(
starred=True
)
return 200, True
@notifications_router.post(
"/unstar-ids/", response={200: Ok, 400: str}, url_name="notifications_unstar_ids"
)
def unstar_many(request, data: StarMany):
request.user.notification_set.filter(deleted=False).filter(pk__in=data.ids).update(
starred=False
)
return 200, True
@notifications_router.post(
"/{int:pk}/delete/", response=Ok, url_name="notifications_delete_id"
)
def delete_notification(request, pk: int):
request.user.notification_set.filter(id=pk).update(deleted=True)
return True
@notifications_router.post("/{int:pk}/undo-deletion/", response=Ok)
def undo_deletion(request, pk: int):
request.user.notification_set.filter(id=pk).update(deleted=False)
return True
class DeleteMany(Schema):
ids: list[int]
@notifications_router.post(
"/delete-ids/", response={200: Ok, 400: NotOk}, url_name="notifications_delete_many"
)
def delete_notifications(request, data: DeleteMany):
request.user.notification_set.filter(deleted=False).filter(pk__in=data.ids).update(
deleted=True
)
return 200, True
class WatchSchema(Schema):
title: str
url: str
path: str
@watch_router.get("/watching/", url_name="watching")
def watched(request, q: str = "", url: str = "", limit: int = 20, offset: int = 0):
qs = request.user.userwatch_set.select_related("watch", "user__defaultwatch")
profile: UserProfile = request.auth
hasDefault = None
try:
hasDefault = request.user.defaultwatch.custom_serialize()
except DefaultWatch.DoesNotExist:
pass
if url:
url = DocumentURL.normalize_uri(url)
qs = qs.filter(watch__url=url)
if q:
qs = qs.filter(watch__title__icontains=q)
qs = qs[offset : offset + limit]
response = {}
results = []
# Default settings at top level if exist
if hasDefault:
response["default"] = hasDefault
response["csrfmiddlewaretoken"] = get_token(request)
for item in qs:
res = {}
res["title"] = item.watch.title
res["url"] = item.watch.url
res["path"] = item.watch.path
# No custom notifications just major updates.
if not item.custom:
res["status"] = "major"
else:
res["status"] = "custom"
# Subscribed to custom
if item.custom_default and hasDefault:
# Subscribed to the defaults
res["custom"] = "default"
else:
# Subscribed to fine-grained options
res["custom"] = item.custom_serialize()
results.append(res)
if url != "" and len(results) == 0:
response["status"] = "unwatched"
elif len(results) == 1 and url != "":
response = response | results[0]
else:
response["items"] = results
if not profile.is_subscriber:
response["subscription_limit_reached"] = (
request.user.userwatch_set.count() >= MAX_NON_SUBSCRIBED["notification"]
)
return response
class UpdateWatchCustom(Schema):
compatibility: list[str]
content: bool
class UpdateWatch(Schema):
unwatch: bool = None
title: str = None
path: str = None
custom: UpdateWatchCustom = None
custom_default: bool = None
update_custom_default: bool = False
@watch_router.post(
"/watching/", response={200: WatchUpdateResponse, 400: NotOk, 400: NotOk}
)
def update_watch(request, url: str, data: UpdateWatch):
url = DocumentURL.normalize_uri(url)
profile: UserProfile = request.auth
watched: Optional[UserWatch] = (
request.user.userwatch_set.select_related("watch", "user__defaultwatch")
.filter(watch__url=url)
.first()
)
user = watched.user if watched else request.user
watched_count = request.user.userwatch_set.count()
subscription_limit_reached = watched_count >= MAX_NON_SUBSCRIBED["notification"]
if data.unwatch:
if watched:
watched.delete()
subscription_limit_reached = (watched_count - 1) >= MAX_NON_SUBSCRIBED[
"notification"
]
return 200, {
"subscription_limit_reached": subscription_limit_reached,
"ok": True,
}
title = data.title
if not title:
return 400, {"error": "missing title"}
path = data.path or ""
watched_data = {"custom": data.custom is not None}
if data.custom:
custom_default = bool(data.custom_default)
watched_data["custom_default"] = custom_default
custom_data = {
"content_updates": data.custom.content,
}
custom_data["browser_compatibility"] = sorted(data.custom.compatibility)
if custom_default:
try:
default_watch = user.defaultwatch
if data.update_custom_default:
for key, value in custom_data.items():
setattr(default_watch, key, value)
default_watch.save()
except DefaultWatch.DoesNotExist:
# Always create custom defaults if they are missing.
DefaultWatch.objects.update_or_create(user=user, defaults=custom_data)
watched_data.update(custom_data)
if watched:
watch: Watch = watched.watch
# Update the title / path if they changed.
if title != watch.title or path != watch.path:
watch.title = title
watch.path = path
watch.save()
else:
# Check on creation if allowed.
if (
watched_count >= MAX_NON_SUBSCRIBED["notification"]
and not profile.is_subscriber
):
return 400, {
"error": "max_subscriptions",
"info": {"max_allowed": MAX_NON_SUBSCRIBED["notification"]},
}
watch = Watch.objects.get_or_create(url=url, title=title, path=path)[0]
subscription_limit_reached = (watched_count + 1) >= MAX_NON_SUBSCRIBED[
"notification"
]
user.userwatch_set.update_or_create(watch=watch, defaults=watched_data)
return 200, {"subscription_limit_reached": subscription_limit_reached, "ok": True}
class UnwatchMany(Schema):
unwatch: list[str]
@watch_router.post(
"/unwatch-many/",
response={200: WatchUpdateResponse, 400: NotOk},
url_name="unwatch_many",
)
def unwatch(request, data: UnwatchMany):
request.user.userwatch_set.select_related("watch", "user__watch").filter(
watch__url__in=data.unwatch
).delete()
profile: UserProfile = request.auth
if profile.is_subscriber:
subscription_limit_reached = False
else:
subscription_limit_reached = (
request.user.userwatch_set.count() >= MAX_NON_SUBSCRIBED["notification"]
)
return 200, {"subscription_limit_reached": subscription_limit_reached, "ok": True}
class CreateNotificationSchema(Schema):
raw_url: str = Field(..., alias="page")
title: str
text: str
@admin_router.post("/create/", response={200: Ok, 400: NotOk})
def create(request, body: CreateNotificationSchema):
url = DocumentURL.normalize_uri(body.raw_url)
watchers = Watch.objects.filter(url=url)
if not watchers:
return 400, {"error": "No watchers found"}
notification_data, _ = NotificationData.objects.get_or_create(
text=body.text, title=body.title, type="content"
)
for watcher in watchers:
# considering the possibility of multiple pages existing for the same path
for user in watcher.users.all():
Notification.objects.create(notification=notification_data, user=user)
return True
class UpdateNotificationSchema(Schema):
filename: str
@admin_router.post("/update/", response={200: Ok, 400: NotOk, 401: NotOk})
def update(request, body: UpdateNotificationSchema):
try:
changes = json.loads(
requests.get(settings.NOTIFICATIONS_CHANGES_URL + body.filename).content
)
except Exception as e:
capture_exception(e)
return 400, {"error": f"Error while processing file: {repr(e)}"}
try:
process_changes(changes)
except Exception as e:
capture_exception(e)
return 400, {"ok": False, "error": f"Error while processing file: {repr(e)}"}
return 200, True
class ContentUpdateNotificationSchema(Schema):
raw_url: str = Field(..., alias="page")
pr_url: str = Field(..., alias="pr")
@admin_router.post(
"/update/content/",
response={200: Ok, 400: NotOk, 401: NotOk},
url_name="admin.update_content",
)
def update_content(request, body: ContentUpdateNotificationSchema):
try:
url = DocumentURL.normalize_uri(body.raw_url)
changes = [
{
"event": "content_updated",
"page_url": url,
"pr_url": body.pr_url,
}
]
process_changes(changes)
except Exception as e:
capture_exception(e)
return 400, {"error": f"Error while processing PR: {repr(e)}"}
return 200, True