]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/account-video-rate.ts
Add trending sort tests
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
index e969e4a436b78c9c519baec342c1f0a9165bfb9e..c99e32012b55e14854a94f856efe0ec35aaf14bd 100644 (file)
@@ -6,6 +6,7 @@ import { VideoRateType } from '../../../shared/models/videos'
 import { VIDEO_RATE_TYPES } from '../../initializers'
 import { VideoModel } from '../video/video'
 import { AccountModel } from './account'
+import { ActorModel } from '../activitypub/actor'
 
 /*
   Account rates per video.
@@ -16,6 +17,15 @@ import { AccountModel } from './account'
     {
       fields: [ 'videoId', 'accountId' ],
       unique: true
+    },
+    {
+      fields: [ 'videoId' ]
+    },
+    {
+      fields: [ 'accountId' ]
+    },
+    {
+      fields: [ 'videoId', 'type' ]
     }
   ]
 })
@@ -66,4 +76,32 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
 
     return AccountVideoRateModel.findOne(options)
   }
+
+  static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
+    const query = {
+      offset: start,
+      limit: count,
+      where: {
+        videoId,
+        type: rateType
+      },
+      transaction: t,
+      include: [
+        {
+          attributes: [ 'actorId' ],
+          model: AccountModel.unscoped(),
+          required: true,
+          include: [
+            {
+              attributes: [ 'url' ],
+              model: ActorModel.unscoped(),
+              required: true
+            }
+          ]
+        }
+      ]
+    }
+
+    return AccountVideoRateModel.findAndCountAll(query)
+  }
 }