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