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