]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account-video-rate.ts
Automatically remove bad followings
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
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
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 loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) {
93 const options: FindOptions = {
94 where: {
95 [ Op.or]: [
96 {
97 accountId,
98 videoId
99 },
100 {
101 url
102 }
103 ]
104 }
105 }
106 if (transaction) options.transaction = transaction
107
108 return AccountVideoRateModel.findOne(options)
109 }
110
111 static listByAccountForApi (options: {
112 start: number,
113 count: number,
114 sort: string,
115 type?: string,
116 accountId: number
117 }) {
118 const query: FindOptions = {
119 offset: options.start,
120 limit: options.count,
121 order: getSort(options.sort),
122 where: {
123 accountId: options.accountId
124 },
125 include: [
126 {
127 model: VideoModel,
128 required: true,
129 include: [
130 {
131 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
132 required: true
133 }
134 ]
135 }
136 ]
137 }
138 if (options.type) query.where['type'] = options.type
139
140 return AccountVideoRateModel.findAndCountAll(query)
141 }
142
143 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
144 const options: FindOptions = {
145 where: {
146 videoId,
147 type: rateType
148 },
149 include: [
150 {
151 model: AccountModel.unscoped(),
152 required: true,
153 include: [
154 {
155 attributes: [ 'id', 'url', 'preferredUsername' ],
156 model: ActorModel.unscoped(),
157 required: true,
158 where: {
159 preferredUsername: accountName
160 }
161 }
162 ]
163 },
164 {
165 model: VideoModel.unscoped(),
166 required: true
167 }
168 ]
169 }
170 if (transaction) options.transaction = transaction
171
172 return AccountVideoRateModel.findOne(options)
173 }
174
175 static loadByUrl (url: string, transaction: Transaction) {
176 const options: FindOptions = {
177 where: {
178 url
179 }
180 }
181 if (transaction) options.transaction = transaction
182
183 return AccountVideoRateModel.findOne(options)
184 }
185
186 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
187 const query = {
188 offset: start,
189 limit: count,
190 where: {
191 videoId,
192 type: rateType
193 },
194 transaction: t,
195 include: [
196 {
197 attributes: [ 'actorId' ],
198 model: AccountModel.unscoped(),
199 required: true,
200 include: [
201 {
202 attributes: [ 'url' ],
203 model: ActorModel.unscoped(),
204 required: true
205 }
206 ]
207 }
208 ]
209 }
210
211 return AccountVideoRateModel.findAndCountAll(query)
212 }
213
214 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
215 return AccountVideoRateModel.sequelize.transaction(async t => {
216 const query = {
217 where: {
218 updatedAt: {
219 [Op.lt]: beforeUpdatedAt
220 },
221 videoId,
222 type,
223 accountId: {
224 [Op.notIn]: buildLocalAccountIdsIn()
225 }
226 },
227 transaction: t
228 }
229
230 const deleted = await AccountVideoRateModel.destroy(query)
231
232 const options = {
233 transaction: t,
234 where: {
235 id: videoId
236 }
237 }
238
239 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
240 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
241 })
242 }
243
244 toFormattedJSON (): AccountVideoRate {
245 return {
246 video: this.Video.toFormattedJSON(),
247 rating: this.type
248 }
249 }
250 }