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