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