]>
Commit | Line | Data |
---|---|---|
b49f22d8 | 1 | import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' |
5c6d985f | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
1ca9f7c3 C |
3 | import { |
4 | MAccountVideoRate, | |
5 | MAccountVideoRateAccountUrl, | |
6 | MAccountVideoRateAccountVideo, | |
7 | MAccountVideoRateFormattable | |
d17c7b4e C |
8 | } from '@server/types/models' |
9 | import { AccountVideoRate, VideoRateType } from '@shared/models' | |
6b5f72be | 10 | import { AttributesOnly } from '@shared/typescript-utils' |
b49f22d8 C |
11 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
12 | import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' | |
7d9ba5c0 | 13 | import { ActorModel } from '../actor/actor' |
57e4e1c1 | 14 | import { getSort, throwIfNotValid } from '../utils' |
b49f22d8 C |
15 | import { VideoModel } from '../video/video' |
16 | import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' | |
17 | import { 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 | 44 | export 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: { | |
de94ac86 C |
192 | preferredUsername: accountName, |
193 | serverId: null | |
5c6d985f C |
194 | } |
195 | } | |
196 | ] | |
197 | }, | |
198 | { | |
199 | model: VideoModel.unscoped(), | |
200 | required: true | |
201 | } | |
202 | ] | |
203 | } | |
453e83ea | 204 | if (t) options.transaction = t |
5c6d985f C |
205 | |
206 | return AccountVideoRateModel.findOne(options) | |
207 | } | |
208 | ||
209 | static loadByUrl (url: string, transaction: Transaction) { | |
1735c825 | 210 | const options: FindOptions = { |
5c6d985f C |
211 | where: { |
212 | url | |
213 | } | |
214 | } | |
215 | if (transaction) options.transaction = transaction | |
216 | ||
217 | return AccountVideoRateModel.findOne(options) | |
218 | } | |
219 | ||
8fffe21a C |
220 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { |
221 | const query = { | |
9a4a9b6c C |
222 | offset: start, |
223 | limit: count, | |
8fffe21a C |
224 | where: { |
225 | videoId, | |
226 | type: rateType | |
227 | }, | |
228 | transaction: t, | |
229 | include: [ | |
230 | { | |
231 | attributes: [ 'actorId' ], | |
232 | model: AccountModel.unscoped(), | |
233 | required: true, | |
234 | include: [ | |
235 | { | |
236 | attributes: [ 'url' ], | |
237 | model: ActorModel.unscoped(), | |
238 | required: true | |
239 | } | |
240 | ] | |
241 | } | |
242 | ] | |
243 | } | |
244 | ||
d0800f76 | 245 | return Promise.all([ |
246 | AccountVideoRateModel.count(query), | |
247 | AccountVideoRateModel.findAll<MAccountVideoRateAccountUrl>(query) | |
248 | ]).then(([ total, data ]) => ({ total, data })) | |
8fffe21a | 249 | } |
2ba92871 | 250 | |
1ca9f7c3 | 251 | toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate { |
c100a614 YB |
252 | return { |
253 | video: this.Video.toFormattedJSON(), | |
254 | rating: this.type | |
255 | } | |
256 | } | |
d38b8281 | 257 | } |