]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
b49f22d8 | 2 | import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' |
5c6d985f | 3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
1ca9f7c3 C |
4 | import { |
5 | MAccountVideoRate, | |
6 | MAccountVideoRateAccountUrl, | |
7 | MAccountVideoRateAccountVideo, | |
8 | MAccountVideoRateFormattable | |
26d6bf65 | 9 | } from '@server/types/models/video/video-rate' |
b49f22d8 C |
10 | import { AccountVideoRate } from '../../../shared' |
11 | import { VideoRateType } from '../../../shared/models/videos' | |
12 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
13 | import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' | |
14 | import { ActorModel } from '../activitypub/actor' | |
15 | import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' | |
16 | import { VideoModel } from '../video/video' | |
17 | import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' | |
18 | import { 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 | 45 | export 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 | ||
74d249bc C |
149 | static listRemoteRateUrlsOfLocalVideos () { |
150 | const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` + | |
151 | `INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` + | |
152 | `INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` + | |
153 | `INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE` | |
154 | ||
155 | return AccountVideoRateModel.sequelize.query<{ url: string }>(query, { | |
156 | type: QueryTypes.SELECT, | |
157 | raw: true | |
158 | }).then(rows => rows.map(r => r.url)) | |
159 | } | |
160 | ||
453e83ea C |
161 | static loadLocalAndPopulateVideo ( |
162 | rateType: VideoRateType, | |
163 | accountName: string, | |
74d249bc | 164 | videoId: number, |
453e83ea | 165 | t?: Transaction |
b49f22d8 | 166 | ): Promise<MAccountVideoRateAccountVideo> { |
1735c825 | 167 | const options: FindOptions = { |
5c6d985f C |
168 | where: { |
169 | videoId, | |
170 | type: rateType | |
171 | }, | |
172 | include: [ | |
173 | { | |
174 | model: AccountModel.unscoped(), | |
175 | required: true, | |
176 | include: [ | |
177 | { | |
453e83ea | 178 | attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ], |
5c6d985f C |
179 | model: ActorModel.unscoped(), |
180 | required: true, | |
181 | where: { | |
de94ac86 C |
182 | preferredUsername: accountName, |
183 | serverId: null | |
5c6d985f C |
184 | } |
185 | } | |
186 | ] | |
187 | }, | |
188 | { | |
189 | model: VideoModel.unscoped(), | |
190 | required: true | |
191 | } | |
192 | ] | |
193 | } | |
453e83ea | 194 | if (t) options.transaction = t |
5c6d985f C |
195 | |
196 | return AccountVideoRateModel.findOne(options) | |
197 | } | |
198 | ||
199 | static loadByUrl (url: string, transaction: Transaction) { | |
1735c825 | 200 | const options: FindOptions = { |
5c6d985f C |
201 | where: { |
202 | url | |
203 | } | |
204 | } | |
205 | if (transaction) options.transaction = transaction | |
206 | ||
207 | return AccountVideoRateModel.findOne(options) | |
208 | } | |
209 | ||
8fffe21a C |
210 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { |
211 | const query = { | |
9a4a9b6c C |
212 | offset: start, |
213 | limit: count, | |
8fffe21a C |
214 | where: { |
215 | videoId, | |
216 | type: rateType | |
217 | }, | |
218 | transaction: t, | |
219 | include: [ | |
220 | { | |
221 | attributes: [ 'actorId' ], | |
222 | model: AccountModel.unscoped(), | |
223 | required: true, | |
224 | include: [ | |
225 | { | |
226 | attributes: [ 'url' ], | |
227 | model: ActorModel.unscoped(), | |
228 | required: true | |
229 | } | |
230 | ] | |
231 | } | |
232 | ] | |
233 | } | |
234 | ||
453e83ea | 235 | return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query) |
8fffe21a | 236 | } |
2ba92871 C |
237 | |
238 | static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { | |
239 | return AccountVideoRateModel.sequelize.transaction(async t => { | |
240 | const query = { | |
241 | where: { | |
242 | updatedAt: { | |
243 | [Op.lt]: beforeUpdatedAt | |
244 | }, | |
245 | videoId, | |
6b9c966f C |
246 | type, |
247 | accountId: { | |
248 | [Op.notIn]: buildLocalAccountIdsIn() | |
970ceac0 | 249 | } |
6b9c966f | 250 | }, |
2ba92871 C |
251 | transaction: t |
252 | } | |
253 | ||
b49f22d8 | 254 | await AccountVideoRateModel.destroy(query) |
2ba92871 | 255 | |
74d249bc | 256 | return VideoModel.updateRatesOf(videoId, type, t) |
2ba92871 C |
257 | }) |
258 | } | |
c100a614 | 259 | |
1ca9f7c3 | 260 | toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate { |
c100a614 YB |
261 | return { |
262 | video: this.Video.toFormattedJSON(), | |
263 | rating: this.type | |
264 | } | |
265 | } | |
d38b8281 | 266 | } |