]>
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/video/video-rate' | |
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' | |
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 { | |
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 query: FindOptions = { | |
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 | { | |
137 | model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), | |
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 | ||
149 | static loadLocalAndPopulateVideo ( | |
150 | rateType: VideoRateType, | |
151 | accountName: string, | |
152 | videoId: number | string, | |
153 | t?: Transaction | |
154 | ): Promise<MAccountVideoRateAccountVideo> { | |
155 | const options: FindOptions = { | |
156 | where: { | |
157 | videoId, | |
158 | type: rateType | |
159 | }, | |
160 | include: [ | |
161 | { | |
162 | model: AccountModel.unscoped(), | |
163 | required: true, | |
164 | include: [ | |
165 | { | |
166 | attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ], | |
167 | model: ActorModel.unscoped(), | |
168 | required: true, | |
169 | where: { | |
170 | preferredUsername: accountName, | |
171 | serverId: null | |
172 | } | |
173 | } | |
174 | ] | |
175 | }, | |
176 | { | |
177 | model: VideoModel.unscoped(), | |
178 | required: true | |
179 | } | |
180 | ] | |
181 | } | |
182 | if (t) options.transaction = t | |
183 | ||
184 | return AccountVideoRateModel.findOne(options) | |
185 | } | |
186 | ||
187 | static loadByUrl (url: string, transaction: Transaction) { | |
188 | const options: FindOptions = { | |
189 | where: { | |
190 | url | |
191 | } | |
192 | } | |
193 | if (transaction) options.transaction = transaction | |
194 | ||
195 | return AccountVideoRateModel.findOne(options) | |
196 | } | |
197 | ||
198 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { | |
199 | const query = { | |
200 | offset: start, | |
201 | limit: count, | |
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 | ||
223 | return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query) | |
224 | } | |
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, | |
234 | type, | |
235 | accountId: { | |
236 | [Op.notIn]: buildLocalAccountIdsIn() | |
237 | } | |
238 | }, | |
239 | transaction: t | |
240 | } | |
241 | ||
242 | await AccountVideoRateModel.destroy(query) | |
243 | ||
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' | |
253 | ||
254 | return AccountVideoRateModel.sequelize.query(rawQuery, { | |
255 | transaction: t, | |
256 | replacements: { videoId, rateType: type }, | |
257 | type: QueryTypes.UPDATE | |
258 | }) | |
259 | }) | |
260 | } | |
261 | ||
262 | toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate { | |
263 | return { | |
264 | video: this.Video.toFormattedJSON(), | |
265 | rating: this.type | |
266 | } | |
267 | } | |
268 | } |