]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-video-rate.ts
Update translations
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
b49f22d8 2import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
5c6d985f 3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
1ca9f7c3
C
4import {
5 MAccountVideoRate,
6 MAccountVideoRateAccountUrl,
7 MAccountVideoRateAccountVideo,
8 MAccountVideoRateFormattable
26d6bf65 9} from '@server/types/models/video/video-rate'
b49f22d8
C
10import { AccountVideoRate } from '../../../shared'
11import { VideoRateType } from '../../../shared/models/videos'
12import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
13import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
14import { ActorModel } from '../activitypub/actor'
15import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
16import { VideoModel } from '../video/video'
17import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
18import { AccountModel } from './account'
d38b8281 19
3fd3ab2d
C
20/*
21 Account rates per video.
22*/
23@Table({
24 tableName: 'accountVideoRate',
25 indexes: [
d38b8281 26 {
3fd3ab2d
C
27 fields: [ 'videoId', 'accountId' ],
28 unique: true
8cd72bd3
C
29 },
30 {
31 fields: [ 'videoId' ]
32 },
33 {
34 fields: [ 'accountId' ]
35 },
36 {
37 fields: [ 'videoId', 'type' ]
5c6d985f
C
38 },
39 {
40 fields: [ 'url' ],
41 unique: true
d38b8281 42 }
3fd3ab2d
C
43 ]
44})
b49f22d8 45export class AccountVideoRateModel extends Model {
d38b8281 46
3fd3ab2d 47 @AllowNull(false)
1735c825 48 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
3fd3ab2d 49 type: VideoRateType
e02643f3 50
5c6d985f
C
51 @AllowNull(false)
52 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
53 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
54 url: string
55
3fd3ab2d
C
56 @CreatedAt
57 createdAt: Date
e02643f3 58
3fd3ab2d
C
59 @UpdatedAt
60 updatedAt: Date
d38b8281 61
3fd3ab2d
C
62 @ForeignKey(() => VideoModel)
63 @Column
64 videoId: number
d38b8281 65
3fd3ab2d 66 @BelongsTo(() => VideoModel, {
d38b8281 67 foreignKey: {
d38b8281
C
68 allowNull: false
69 },
70 onDelete: 'CASCADE'
71 })
3fd3ab2d 72 Video: VideoModel
d38b8281 73
3fd3ab2d
C
74 @ForeignKey(() => AccountModel)
75 @Column
76 accountId: number
77
78 @BelongsTo(() => AccountModel, {
d38b8281 79 foreignKey: {
d38b8281
C
80 allowNull: false
81 },
82 onDelete: 'CASCADE'
83 })
3fd3ab2d 84 Account: AccountModel
d38b8281 85
b49f22d8 86 static load (accountId: number, videoId: number, transaction?: Transaction): Promise<MAccountVideoRate> {
1735c825 87 const options: FindOptions = {
3fd3ab2d
C
88 where: {
89 accountId,
90 videoId
91 }
d38b8281 92 }
3fd3ab2d 93 if (transaction) options.transaction = transaction
d38b8281 94
3fd3ab2d
C
95 return AccountVideoRateModel.findOne(options)
96 }
8fffe21a 97
b49f22d8 98 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise<MAccountVideoRate> {
970ceac0
C
99 const options: FindOptions = {
100 where: {
a1587156 101 [Op.or]: [
970ceac0
C
102 {
103 accountId,
104 videoId
105 },
106 {
107 url
108 }
109 ]
110 }
111 }
453e83ea 112 if (t) options.transaction = t
970ceac0
C
113
114 return AccountVideoRateModel.findOne(options)
115 }
116
c100a614 117 static listByAccountForApi (options: {
a1587156
C
118 start: number
119 count: number
120 sort: string
121 type?: string
c100a614
YB
122 accountId: number
123 }) {
1735c825 124 const query: FindOptions = {
c100a614
YB
125 offset: options.start,
126 limit: options.count,
127 order: getSort(options.sort),
128 where: {
129 accountId: options.accountId
130 },
131 include: [
132 {
133 model: VideoModel,
134 required: true,
135 include: [
136 {
a1587156 137 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
c100a614
YB
138 required: true
139 }
140 ]
141 }
142 ]
143 }
144 if (options.type) query.where['type'] = options.type
145
146 return AccountVideoRateModel.findAndCountAll(query)
147 }
148
453e83ea
C
149 static loadLocalAndPopulateVideo (
150 rateType: VideoRateType,
151 accountName: string,
d5d9b6d7 152 videoId: number | string,
453e83ea 153 t?: Transaction
b49f22d8 154 ): Promise<MAccountVideoRateAccountVideo> {
1735c825 155 const options: FindOptions = {
5c6d985f
C
156 where: {
157 videoId,
158 type: rateType
159 },
160 include: [
161 {
162 model: AccountModel.unscoped(),
163 required: true,
164 include: [
165 {
453e83ea 166 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
5c6d985f
C
167 model: ActorModel.unscoped(),
168 required: true,
169 where: {
de94ac86
C
170 preferredUsername: accountName,
171 serverId: null
5c6d985f
C
172 }
173 }
174 ]
175 },
176 {
177 model: VideoModel.unscoped(),
178 required: true
179 }
180 ]
181 }
453e83ea 182 if (t) options.transaction = t
5c6d985f
C
183
184 return AccountVideoRateModel.findOne(options)
185 }
186
187 static loadByUrl (url: string, transaction: Transaction) {
1735c825 188 const options: FindOptions = {
5c6d985f
C
189 where: {
190 url
191 }
192 }
193 if (transaction) options.transaction = transaction
194
195 return AccountVideoRateModel.findOne(options)
196 }
197
8fffe21a
C
198 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
199 const query = {
9a4a9b6c
C
200 offset: start,
201 limit: count,
8fffe21a
C
202 where: {
203 videoId,
204 type: rateType
205 },
206 transaction: t,
207 include: [
208 {
209 attributes: [ 'actorId' ],
210 model: AccountModel.unscoped(),
211 required: true,
212 include: [
213 {
214 attributes: [ 'url' ],
215 model: ActorModel.unscoped(),
216 required: true
217 }
218 ]
219 }
220 ]
221 }
222
453e83ea 223 return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
8fffe21a 224 }
2ba92871
C
225
226 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
227 return AccountVideoRateModel.sequelize.transaction(async t => {
228 const query = {
229 where: {
230 updatedAt: {
231 [Op.lt]: beforeUpdatedAt
232 },
233 videoId,
6b9c966f
C
234 type,
235 accountId: {
236 [Op.notIn]: buildLocalAccountIdsIn()
970ceac0 237 }
6b9c966f 238 },
2ba92871
C
239 transaction: t
240 }
241
b49f22d8 242 await AccountVideoRateModel.destroy(query)
2ba92871 243
b49f22d8
C
244 const field = type === 'like'
245 ? 'likes'
246 : 'dislikes'
247
248 const rawQuery = `UPDATE "video" SET "${field}" = ` +
249 '(' +
250 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' +
251 ') ' +
252 'WHERE "video"."id" = :videoId'
2ba92871 253
b49f22d8
C
254 return AccountVideoRateModel.sequelize.query(rawQuery, {
255 transaction: t,
256 replacements: { videoId, rateType: type },
257 type: QueryTypes.UPDATE
258 })
2ba92871
C
259 })
260 }
c100a614 261
1ca9f7c3 262 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
c100a614
YB
263 return {
264 video: this.Video.toFormattedJSON(),
265 rating: this.type
266 }
267 }
d38b8281 268}