]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/account-video-rate.ts
Add /accounts/:username/ratings endpoint (#1756)
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
index 18762f0c55901be4bcaeb623a820bc4ee1d9147d..f462df4b33d78e6f498f01111862fd5acac10349 100644 (file)
@@ -1,5 +1,5 @@
 import { values } from 'lodash'
-import { Transaction } from 'sequelize'
+import { Transaction, Op } from 'sequelize'
 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
 import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
 import { VideoRateType } from '../../../shared/models/videos'
@@ -7,8 +7,10 @@ import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers'
 import { VideoModel } from '../video/video'
 import { AccountModel } from './account'
 import { ActorModel } from '../activitypub/actor'
-import { throwIfNotValid } from '../utils'
+import { throwIfNotValid, getSort } from '../utils'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
+import { AccountVideoRate } from '../../../shared'
+import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from '../video/video-channel'
 
 /*
   Account rates per video.
@@ -88,6 +90,38 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
     return AccountVideoRateModel.findOne(options)
   }
 
+  static listByAccountForApi (options: {
+    start: number,
+    count: number,
+    sort: string,
+    type?: string,
+    accountId: number
+  }) {
+    const query: IFindOptions<AccountVideoRateModel> = {
+      offset: options.start,
+      limit: options.count,
+      order: getSort(options.sort),
+      where: {
+        accountId: options.accountId
+      },
+      include: [
+        {
+          model: VideoModel,
+          required: true,
+          include: [
+            {
+              model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, true] }),
+              required: true
+            }
+          ]
+        }
+      ]
+    }
+    if (options.type) query.where['type'] = options.type
+
+    return AccountVideoRateModel.findAndCountAll(query)
+  }
+
   static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
     const options: IFindOptions<AccountVideoRateModel> = {
       where: {
@@ -158,4 +192,38 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
 
     return AccountVideoRateModel.findAndCountAll(query)
   }
+
+  static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
+    return AccountVideoRateModel.sequelize.transaction(async t => {
+      const query = {
+        where: {
+          updatedAt: {
+            [Op.lt]: beforeUpdatedAt
+          },
+          videoId,
+          type
+        },
+        transaction: t
+      }
+
+      const deleted = await AccountVideoRateModel.destroy(query)
+
+      const options = {
+        transaction: t,
+        where: {
+          id: videoId
+        }
+      }
+
+      if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
+      else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
+    })
+  }
+
+  toFormattedJSON (): AccountVideoRate {
+    return {
+      video: this.Video.toFormattedJSON(),
+      rating: this.type
+    }
+  }
 }