]>
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 { getSort, throwIfNotValid } from '../utils' | |
10 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
11 | import { AccountVideoRate } from '../../../shared' | |
12 | import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from '../video/video-channel' | |
13 | ||
14 | /* | |
15 | Account rates per video. | |
16 | */ | |
17 | @Table({ | |
18 | tableName: 'accountVideoRate', | |
19 | indexes: [ | |
20 | { | |
21 | fields: [ 'videoId', 'accountId' ], | |
22 | unique: true | |
23 | }, | |
24 | { | |
25 | fields: [ 'videoId' ] | |
26 | }, | |
27 | { | |
28 | fields: [ 'accountId' ] | |
29 | }, | |
30 | { | |
31 | fields: [ 'videoId', 'type' ] | |
32 | }, | |
33 | { | |
34 | fields: [ 'url' ], | |
35 | unique: true | |
36 | } | |
37 | ] | |
38 | }) | |
39 | export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |
40 | ||
41 | @AllowNull(false) | |
42 | @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES))) | |
43 | type: VideoRateType | |
44 | ||
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 | ||
50 | @CreatedAt | |
51 | createdAt: Date | |
52 | ||
53 | @UpdatedAt | |
54 | updatedAt: Date | |
55 | ||
56 | @ForeignKey(() => VideoModel) | |
57 | @Column | |
58 | videoId: number | |
59 | ||
60 | @BelongsTo(() => VideoModel, { | |
61 | foreignKey: { | |
62 | allowNull: false | |
63 | }, | |
64 | onDelete: 'CASCADE' | |
65 | }) | |
66 | Video: VideoModel | |
67 | ||
68 | @ForeignKey(() => AccountModel) | |
69 | @Column | |
70 | accountId: number | |
71 | ||
72 | @BelongsTo(() => AccountModel, { | |
73 | foreignKey: { | |
74 | allowNull: false | |
75 | }, | |
76 | onDelete: 'CASCADE' | |
77 | }) | |
78 | Account: AccountModel | |
79 | ||
80 | static load (accountId: number, videoId: number, transaction?: Transaction) { | |
81 | const options: FindOptions = { | |
82 | where: { | |
83 | accountId, | |
84 | videoId | |
85 | } | |
86 | } | |
87 | if (transaction) options.transaction = transaction | |
88 | ||
89 | return AccountVideoRateModel.findOne(options) | |
90 | } | |
91 | ||
92 | static listByAccountForApi (options: { | |
93 | start: number, | |
94 | count: number, | |
95 | sort: string, | |
96 | type?: string, | |
97 | accountId: number | |
98 | }) { | |
99 | const query: FindOptions = { | |
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 | ||
124 | static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) { | |
125 | const options: FindOptions = { | |
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) { | |
157 | const options: FindOptions = { | |
158 | where: { | |
159 | url | |
160 | } | |
161 | } | |
162 | if (transaction) options.transaction = transaction | |
163 | ||
164 | return AccountVideoRateModel.findOne(options) | |
165 | } | |
166 | ||
167 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { | |
168 | const query = { | |
169 | offset: start, | |
170 | limit: count, | |
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 | } | |
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 | } | |
221 | ||
222 | toFormattedJSON (): AccountVideoRate { | |
223 | return { | |
224 | video: this.Video.toFormattedJSON(), | |
225 | rating: this.type | |
226 | } | |
227 | } | |
228 | } |