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