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