]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user-notification.ts
add 'total downloaded' stats from server and peers in player (#3394)
[github/Chocobozzz/PeerTube.git] / server / models / account / user-notification.ts
index 2945bf70953656d3926caac95d45d91fb3c73812..452574dc882faf77eb1c0eab70031c8b2e938ea4 100644 (file)
@@ -88,7 +88,7 @@ function buildAccountInclude (required: boolean, withActor = false) {
       },
 
       {
-        attributes: [ 'id' ],
+        attributes: [ 'id', 'state' ],
         model: AbuseModel.unscoped(),
         required: false,
         include: [
@@ -105,7 +105,7 @@ function buildAccountInclude (required: boolean, withActor = false) {
             include: [
               {
                 attributes: [ 'id', 'originCommentId' ],
-                model: VideoCommentModel,
+                model: VideoCommentModel.unscoped(),
                 required: true,
                 include: [
                   {
@@ -411,6 +411,59 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
     return UserNotificationModel.update({ read: true }, query)
   }
 
+  static removeNotificationsOf (options: { id: number, type: 'account' | 'server', forUserId?: number }) {
+    const id = parseInt(options.id + '', 10)
+
+    function buildAccountWhereQuery (base: string) {
+      const whereSuffix = options.forUserId
+        ? ` AND "userNotification"."userId" = ${options.forUserId}`
+        : ''
+
+      if (options.type === 'account') {
+        return base +
+          ` WHERE "account"."id" = ${id} ${whereSuffix}`
+      }
+
+      return base +
+        ` WHERE "actor"."serverId" = ${id} ${whereSuffix}`
+    }
+
+    const queries = [
+      buildAccountWhereQuery(
+        `SELECT "userNotification"."id" FROM "userNotification" ` +
+        `INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` +
+        `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+      ),
+
+      // Remove notifications from muted accounts that followed ours
+      buildAccountWhereQuery(
+        `SELECT "userNotification"."id" FROM "userNotification" ` +
+        `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
+        `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
+        `INNER JOIN account ON account."actorId" = actor.id `
+      ),
+
+      // Remove notifications from muted accounts that commented something
+      buildAccountWhereQuery(
+        `SELECT "userNotification"."id" FROM "userNotification" ` +
+        `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
+        `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
+        `INNER JOIN account ON account."actorId" = actor.id `
+      ),
+
+      buildAccountWhereQuery(
+        `SELECT "userNotification"."id" FROM "userNotification" ` +
+        `INNER JOIN "videoComment" ON "videoComment".id = "userNotification"."commentId" ` +
+        `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
+        `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+      )
+    ]
+
+    const query = `DELETE FROM "userNotification" WHERE id IN (${queries.join(' UNION ')})`
+
+    return UserNotificationModel.sequelize.query(query)
+  }
+
   toFormattedJSON (this: UserNotificationModelForApi): UserNotification {
     const video = this.Video
       ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) })
@@ -504,6 +557,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
 
     return {
       id: abuse.id,
+      state: abuse.state,
       video: videoAbuse,
       comment: commentAbuse,
       account: accountAbuse