]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
3fd3ab2d C |
2 | import { Transaction } from 'sequelize' |
3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
4 | import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' | |
5 | import { VideoRateType } from '../../../shared/models/videos' | |
74889a71 | 6 | import { VIDEO_RATE_TYPES } from '../../initializers' |
3fd3ab2d C |
7 | import { VideoModel } from '../video/video' |
8 | import { AccountModel } from './account' | |
8fffe21a | 9 | import { ActorModel } from '../activitypub/actor' |
d38b8281 | 10 | |
3fd3ab2d C |
11 | /* |
12 | Account rates per video. | |
13 | */ | |
14 | @Table({ | |
15 | tableName: 'accountVideoRate', | |
16 | indexes: [ | |
d38b8281 | 17 | { |
3fd3ab2d C |
18 | fields: [ 'videoId', 'accountId' ], |
19 | unique: true | |
d38b8281 | 20 | } |
3fd3ab2d C |
21 | ] |
22 | }) | |
23 | export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |
d38b8281 | 24 | |
3fd3ab2d C |
25 | @AllowNull(false) |
26 | @Column(DataType.ENUM(values(VIDEO_RATE_TYPES))) | |
27 | type: VideoRateType | |
e02643f3 | 28 | |
3fd3ab2d C |
29 | @CreatedAt |
30 | createdAt: Date | |
e02643f3 | 31 | |
3fd3ab2d C |
32 | @UpdatedAt |
33 | updatedAt: Date | |
d38b8281 | 34 | |
3fd3ab2d C |
35 | @ForeignKey(() => VideoModel) |
36 | @Column | |
37 | videoId: number | |
d38b8281 | 38 | |
3fd3ab2d | 39 | @BelongsTo(() => VideoModel, { |
d38b8281 | 40 | foreignKey: { |
d38b8281 C |
41 | allowNull: false |
42 | }, | |
43 | onDelete: 'CASCADE' | |
44 | }) | |
3fd3ab2d | 45 | Video: VideoModel |
d38b8281 | 46 | |
3fd3ab2d C |
47 | @ForeignKey(() => AccountModel) |
48 | @Column | |
49 | accountId: number | |
50 | ||
51 | @BelongsTo(() => AccountModel, { | |
d38b8281 | 52 | foreignKey: { |
d38b8281 C |
53 | allowNull: false |
54 | }, | |
55 | onDelete: 'CASCADE' | |
56 | }) | |
3fd3ab2d | 57 | Account: AccountModel |
d38b8281 | 58 | |
3fd3ab2d C |
59 | static load (accountId: number, videoId: number, transaction: Transaction) { |
60 | const options: IFindOptions<AccountVideoRateModel> = { | |
61 | where: { | |
62 | accountId, | |
63 | videoId | |
64 | } | |
d38b8281 | 65 | } |
3fd3ab2d | 66 | if (transaction) options.transaction = transaction |
d38b8281 | 67 | |
3fd3ab2d C |
68 | return AccountVideoRateModel.findOne(options) |
69 | } | |
8fffe21a C |
70 | |
71 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { | |
72 | const query = { | |
73 | start, | |
74 | count, | |
75 | where: { | |
76 | videoId, | |
77 | type: rateType | |
78 | }, | |
79 | transaction: t, | |
80 | include: [ | |
81 | { | |
82 | attributes: [ 'actorId' ], | |
83 | model: AccountModel.unscoped(), | |
84 | required: true, | |
85 | include: [ | |
86 | { | |
87 | attributes: [ 'url' ], | |
88 | model: ActorModel.unscoped(), | |
89 | required: true | |
90 | } | |
91 | ] | |
92 | } | |
93 | ] | |
94 | } | |
95 | ||
96 | return AccountVideoRateModel.findAndCountAll(query) | |
97 | } | |
d38b8281 | 98 | } |