]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user-notification.ts
Add import finished and video published notifs
[github/Chocobozzz/PeerTube.git] / server / models / account / user-notification.ts
index e22f0d57f661767296704fabe439baf4f98c664c..25124437418e4f4ddbe52bc56a199861ecee9c65 100644 (file)
@@ -1,4 +1,17 @@
-import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
+import {
+  AllowNull,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  Default,
+  ForeignKey,
+  IFindOptions,
+  Is,
+  Model,
+  Scopes,
+  Table,
+  UpdatedAt
+} from 'sequelize-typescript'
 import { UserNotification, UserNotificationType } from '../../../shared'
 import { getSort, throwIfNotValid } from '../utils'
 import { isBooleanValid } from '../../helpers/custom-validators/misc'
@@ -11,66 +24,68 @@ import { VideoChannelModel } from '../video/video-channel'
 import { AccountModel } from './account'
 import { VideoAbuseModel } from '../video/video-abuse'
 import { VideoBlacklistModel } from '../video/video-blacklist'
+import { VideoImportModel } from '../video/video-import'
 
 enum ScopeNames {
   WITH_ALL = 'WITH_ALL'
 }
 
+function buildVideoInclude (required: boolean) {
+  return {
+    attributes: [ 'id', 'uuid', 'name' ],
+    model: () => VideoModel.unscoped(),
+    required
+  }
+}
+
+function buildChannelInclude () {
+  return {
+    required: true,
+    attributes: [ 'id', 'name' ],
+    model: () => VideoChannelModel.unscoped()
+  }
+}
+
+function buildAccountInclude () {
+  return {
+    required: true,
+    attributes: [ 'id', 'name' ],
+    model: () => AccountModel.unscoped()
+  }
+}
+
 @Scopes({
   [ScopeNames.WITH_ALL]: {
     include: [
+      Object.assign(buildVideoInclude(false), {
+        include: [ buildChannelInclude() ]
+      }),
       {
-        attributes: [ 'id', 'uuid', 'name' ],
-        model: () => VideoModel.unscoped(),
-        required: false,
-        include: [
-          {
-            required: true,
-            attributes: [ 'id', 'name' ],
-            model: () => VideoChannelModel.unscoped()
-          }
-        ]
-      },
-      {
-        attributes: [ 'id' ],
+        attributes: [ 'id', 'originCommentId' ],
         model: () => VideoCommentModel.unscoped(),
         required: false,
         include: [
-          {
-            required: true,
-            attributes: [ 'id', 'name' ],
-            model: () => AccountModel.unscoped()
-          },
-          {
-            required: true,
-            attributes: [ 'id', 'uuid', 'name' ],
-            model: () => VideoModel.unscoped()
-          }
+          buildAccountInclude(),
+          buildVideoInclude(true)
         ]
       },
       {
         attributes: [ 'id' ],
         model: () => VideoAbuseModel.unscoped(),
         required: false,
-        include: [
-          {
-            required: true,
-            attributes: [ 'id', 'uuid', 'name' ],
-            model: () => VideoModel.unscoped()
-          }
-        ]
+        include: [ buildVideoInclude(true) ]
       },
       {
         attributes: [ 'id' ],
         model: () => VideoBlacklistModel.unscoped(),
         required: false,
-        include: [
-          {
-            required: true,
-            attributes: [ 'id', 'uuid', 'name' ],
-            model: () => VideoModel.unscoped()
-          }
-        ]
+        include: [ buildVideoInclude(true) ]
+      },
+      {
+        attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ],
+        model: () => VideoImportModel.unscoped(),
+        required: false,
+        include: [ buildVideoInclude(false) ]
       }
     ]
   }
@@ -166,8 +181,20 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
   })
   VideoBlacklist: VideoBlacklistModel
 
-  static listForApi (userId: number, start: number, count: number, sort: string) {
-    const query = {
+  @ForeignKey(() => VideoImportModel)
+  @Column
+  videoImportId: number
+
+  @BelongsTo(() => VideoImportModel, {
+    foreignKey: {
+      allowNull: true
+    },
+    onDelete: 'cascade'
+  })
+  VideoImport: VideoImportModel
+
+  static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) {
+    const query: IFindOptions<UserNotificationModel> = {
       offset: start,
       limit: count,
       order: getSort(sort),
@@ -176,6 +203,8 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
       }
     }
 
+    if (unread !== undefined) query.where['read'] = !unread
+
     return UserNotificationModel.scope(ScopeNames.WITH_ALL)
                                 .findAndCountAll(query)
                                 .then(({ rows, count }) => {
@@ -200,45 +229,39 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
   }
 
   toFormattedJSON (): UserNotification {
-    const video = this.Video ? {
-      id: this.Video.id,
-      uuid: this.Video.uuid,
-      name: this.Video.name,
+    const video = this.Video ? Object.assign(this.formatVideo(this.Video), {
       channel: {
         id: this.Video.VideoChannel.id,
         displayName: this.Video.VideoChannel.getDisplayName()
       }
+    }) : undefined
+
+    const videoImport = this.VideoImport ? {
+      id: this.VideoImport.id,
+      video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined,
+      torrentName: this.VideoImport.torrentName,
+      magnetUri: this.VideoImport.magnetUri,
+      targetUrl: this.VideoImport.targetUrl
     } : undefined
 
     const comment = this.Comment ? {
       id: this.Comment.id,
+      threadId: this.Comment.getThreadId(),
       account: {
         id: this.Comment.Account.id,
         displayName: this.Comment.Account.getDisplayName()
       },
-      video: {
-        id: this.Comment.Video.id,
-        uuid: this.Comment.Video.uuid,
-        name: this.Comment.Video.name
-      }
+      video: this.formatVideo(this.Comment.Video)
     } : undefined
 
     const videoAbuse = this.VideoAbuse ? {
       id: this.VideoAbuse.id,
-      video: {
-        id: this.VideoAbuse.Video.id,
-        uuid: this.VideoAbuse.Video.uuid,
-        name: this.VideoAbuse.Video.name
-      }
+      video: this.formatVideo(this.VideoAbuse.Video)
     } : undefined
 
     const videoBlacklist = this.VideoBlacklist ? {
       id: this.VideoBlacklist.id,
-      video: {
-        id: this.VideoBlacklist.Video.id,
-        uuid: this.VideoBlacklist.Video.uuid,
-        name: this.VideoBlacklist.Video.name
-      }
+      video: this.formatVideo(this.VideoBlacklist.Video)
     } : undefined
 
     return {
@@ -246,6 +269,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
       type: this.type,
       read: this.read,
       video,
+      videoImport,
       comment,
       videoAbuse,
       videoBlacklist,
@@ -253,4 +277,12 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
       updatedAt: this.updatedAt.toISOString()
     }
   }
+
+  private formatVideo (video: VideoModel) {
+    return {
+      id: video.id,
+      uuid: video.uuid,
+      name: video.name
+    }
+  }
 }