]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-share.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / video-share.ts
index b7f5f3fa3c54fa4da41bef2a0bd035a1b7cfdaf3..ad95dec6e72787340deace90ac26adf2f82012cd 100644 (file)
@@ -1,10 +1,11 @@
-import { literal, Op, Transaction } from 'sequelize'
+import { literal, Op, QueryTypes, Transaction } from 'sequelize'
 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
+import { AttributesOnly } from '@shared/typescript-utils'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
 import { MActorDefault } from '../../types/models'
 import { MVideoShareActor, MVideoShareFull } from '../../types/models/video'
-import { ActorModel } from '../activitypub/actor'
+import { ActorModel } from '../actor/actor'
 import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
 
@@ -50,7 +51,7 @@ enum ScopeNames {
     }
   ]
 })
-export class VideoShareModel extends Model {
+export class VideoShareModel extends Model<Partial<AttributesOnly<VideoShareModel>>> {
 
   @AllowNull(false)
   @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@@ -182,7 +183,21 @@ export class VideoShareModel extends Model {
       transaction: t
     }
 
-    return VideoShareModel.findAndCountAll(query)
+    return Promise.all([
+      VideoShareModel.count(query),
+      VideoShareModel.findAll(query)
+    ]).then(([ total, data ]) => ({ total, data }))
+  }
+
+  static listRemoteShareUrlsOfLocalVideos () {
+    const query = `SELECT "videoShare".url FROM "videoShare" ` +
+      `INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` +
+      `INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE`
+
+    return VideoShareModel.sequelize.query<{ url: string }>(query, {
+      type: QueryTypes.SELECT,
+      raw: true
+    }).then(rows => rows.map(r => r.url))
   }
 
   static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {