]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/account-video-rate.ts
Try to fix subscriptions inconsistencies
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
index d5c214ecbc69b1a9e82395404b88b5c680642aab..c593595b2b2c22d3c6716bf181345f7e8c6c38a5 100644 (file)
@@ -6,10 +6,17 @@ import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constan
 import { VideoModel } from '../video/video'
 import { AccountModel } from './account'
 import { ActorModel } from '../activitypub/actor'
-import { getSort, throwIfNotValid } from '../utils'
+import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
 import { AccountVideoRate } from '../../../shared'
 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
+import * as Bluebird from 'bluebird'
+import {
+  MAccountVideoRate,
+  MAccountVideoRateAccountUrl,
+  MAccountVideoRateAccountVideo,
+  MAccountVideoRateFormattable
+} from '@server/typings/models/video/video-rate'
 
 /*
   Account rates per video.
@@ -77,7 +84,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
   })
   Account: AccountModel
 
-  static load (accountId: number, videoId: number, transaction?: Transaction) {
+  static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird<MAccountVideoRate> {
     const options: FindOptions = {
       where: {
         accountId,
@@ -89,7 +96,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
     return AccountVideoRateModel.findOne(options)
   }
 
-  static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) {
+  static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird<MAccountVideoRate> {
     const options: FindOptions = {
       where: {
         [ Op.or]: [
@@ -103,7 +110,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
         ]
       }
     }
-    if (transaction) options.transaction = transaction
+    if (t) options.transaction = t
 
     return AccountVideoRateModel.findOne(options)
   }
@@ -140,7 +147,12 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
     return AccountVideoRateModel.findAndCountAll(query)
   }
 
-  static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
+  static loadLocalAndPopulateVideo (
+    rateType: VideoRateType,
+    accountName: string,
+    videoId: number | string,
+    t?: Transaction
+  ): Bluebird<MAccountVideoRateAccountVideo> {
     const options: FindOptions = {
       where: {
         videoId,
@@ -152,7 +164,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
           required: true,
           include: [
             {
-              attributes: [ 'id', 'url', 'preferredUsername' ],
+              attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
               model: ActorModel.unscoped(),
               required: true,
               where: {
@@ -167,7 +179,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
         }
       ]
     }
-    if (transaction) options.transaction = transaction
+    if (t) options.transaction = t
 
     return AccountVideoRateModel.findOne(options)
   }
@@ -208,7 +220,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
       ]
     }
 
-    return AccountVideoRateModel.findAndCountAll(query)
+    return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
   }
 
   static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
@@ -219,25 +231,11 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
             [Op.lt]: beforeUpdatedAt
           },
           videoId,
-          type
-        },
-        include: [
-          {
-            model: AccountModel.unscoped(),
-            required: true,
-            include: [
-              {
-                model: ActorModel.unscoped(),
-                required: true,
-                where: {
-                  serverId: {
-                    [Op.ne]: null
-                  }
-                }
-              }
-            ]
+          type,
+          accountId: {
+            [Op.notIn]: buildLocalAccountIdsIn()
           }
-        ],
+        },
         transaction: t
       }
 
@@ -255,7 +253,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
     })
   }
 
-  toFormattedJSON (): AccountVideoRate {
+  toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
     return {
       video: this.Video.toFormattedJSON(),
       rating: this.type