diff options
author | Chocobozzz <me@florianbigard.com> | 2020-11-18 11:13:01 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-11-18 11:32:57 +0100 |
commit | ea3674d04dd0a67962224073256dc7d4173527a5 (patch) | |
tree | 7a1936b088509b88157e6493c8fb897f06bd29cb /server/models | |
parent | aa9cf3b98d5db85fefce918bb29840d0cdcb84c4 (diff) | |
download | PeerTube-ea3674d04dd0a67962224073256dc7d4173527a5.tar.gz PeerTube-ea3674d04dd0a67962224073256dc7d4173527a5.tar.zst PeerTube-ea3674d04dd0a67962224073256dc7d4173527a5.zip |
Remove notifications of muted accounts/servers
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/user-notification.ts | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index bd89b8973..452574dc8 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts | |||
@@ -105,7 +105,7 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
105 | include: [ | 105 | include: [ |
106 | { | 106 | { |
107 | attributes: [ 'id', 'originCommentId' ], | 107 | attributes: [ 'id', 'originCommentId' ], |
108 | model: VideoCommentModel, | 108 | model: VideoCommentModel.unscoped(), |
109 | required: true, | 109 | required: true, |
110 | include: [ | 110 | include: [ |
111 | { | 111 | { |
@@ -411,6 +411,59 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
411 | return UserNotificationModel.update({ read: true }, query) | 411 | return UserNotificationModel.update({ read: true }, query) |
412 | } | 412 | } |
413 | 413 | ||
414 | static removeNotificationsOf (options: { id: number, type: 'account' | 'server', forUserId?: number }) { | ||
415 | const id = parseInt(options.id + '', 10) | ||
416 | |||
417 | function buildAccountWhereQuery (base: string) { | ||
418 | const whereSuffix = options.forUserId | ||
419 | ? ` AND "userNotification"."userId" = ${options.forUserId}` | ||
420 | : '' | ||
421 | |||
422 | if (options.type === 'account') { | ||
423 | return base + | ||
424 | ` WHERE "account"."id" = ${id} ${whereSuffix}` | ||
425 | } | ||
426 | |||
427 | return base + | ||
428 | ` WHERE "actor"."serverId" = ${id} ${whereSuffix}` | ||
429 | } | ||
430 | |||
431 | const queries = [ | ||
432 | buildAccountWhereQuery( | ||
433 | `SELECT "userNotification"."id" FROM "userNotification" ` + | ||
434 | `INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` + | ||
435 | `INNER JOIN actor ON "actor"."id" = "account"."actorId" ` | ||
436 | ), | ||
437 | |||
438 | // Remove notifications from muted accounts that followed ours | ||
439 | buildAccountWhereQuery( | ||
440 | `SELECT "userNotification"."id" FROM "userNotification" ` + | ||
441 | `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` + | ||
442 | `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` + | ||
443 | `INNER JOIN account ON account."actorId" = actor.id ` | ||
444 | ), | ||
445 | |||
446 | // Remove notifications from muted accounts that commented something | ||
447 | buildAccountWhereQuery( | ||
448 | `SELECT "userNotification"."id" FROM "userNotification" ` + | ||
449 | `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` + | ||
450 | `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` + | ||
451 | `INNER JOIN account ON account."actorId" = actor.id ` | ||
452 | ), | ||
453 | |||
454 | buildAccountWhereQuery( | ||
455 | `SELECT "userNotification"."id" FROM "userNotification" ` + | ||
456 | `INNER JOIN "videoComment" ON "videoComment".id = "userNotification"."commentId" ` + | ||
457 | `INNER JOIN account ON account.id = "videoComment"."accountId" ` + | ||
458 | `INNER JOIN actor ON "actor"."id" = "account"."actorId" ` | ||
459 | ) | ||
460 | ] | ||
461 | |||
462 | const query = `DELETE FROM "userNotification" WHERE id IN (${queries.join(' UNION ')})` | ||
463 | |||
464 | return UserNotificationModel.sequelize.query(query) | ||
465 | } | ||
466 | |||
414 | toFormattedJSON (this: UserNotificationModelForApi): UserNotification { | 467 | toFormattedJSON (this: UserNotificationModelForApi): UserNotification { |
415 | const video = this.Video | 468 | const video = this.Video |
416 | ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) }) | 469 | ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) }) |