]>
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 | |
8cd72bd3 C |
20 | }, |
21 | { | |
22 | fields: [ 'videoId' ] | |
23 | }, | |
24 | { | |
25 | fields: [ 'accountId' ] | |
26 | }, | |
27 | { | |
28 | fields: [ 'videoId', 'type' ] | |
d38b8281 | 29 | } |
3fd3ab2d C |
30 | ] |
31 | }) | |
32 | export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |
d38b8281 | 33 | |
3fd3ab2d C |
34 | @AllowNull(false) |
35 | @Column(DataType.ENUM(values(VIDEO_RATE_TYPES))) | |
36 | type: VideoRateType | |
e02643f3 | 37 | |
3fd3ab2d C |
38 | @CreatedAt |
39 | createdAt: Date | |
e02643f3 | 40 | |
3fd3ab2d C |
41 | @UpdatedAt |
42 | updatedAt: Date | |
d38b8281 | 43 | |
3fd3ab2d C |
44 | @ForeignKey(() => VideoModel) |
45 | @Column | |
46 | videoId: number | |
d38b8281 | 47 | |
3fd3ab2d | 48 | @BelongsTo(() => VideoModel, { |
d38b8281 | 49 | foreignKey: { |
d38b8281 C |
50 | allowNull: false |
51 | }, | |
52 | onDelete: 'CASCADE' | |
53 | }) | |
3fd3ab2d | 54 | Video: VideoModel |
d38b8281 | 55 | |
3fd3ab2d C |
56 | @ForeignKey(() => AccountModel) |
57 | @Column | |
58 | accountId: number | |
59 | ||
60 | @BelongsTo(() => AccountModel, { | |
d38b8281 | 61 | foreignKey: { |
d38b8281 C |
62 | allowNull: false |
63 | }, | |
64 | onDelete: 'CASCADE' | |
65 | }) | |
3fd3ab2d | 66 | Account: AccountModel |
d38b8281 | 67 | |
3fd3ab2d C |
68 | static load (accountId: number, videoId: number, transaction: Transaction) { |
69 | const options: IFindOptions<AccountVideoRateModel> = { | |
70 | where: { | |
71 | accountId, | |
72 | videoId | |
73 | } | |
d38b8281 | 74 | } |
3fd3ab2d | 75 | if (transaction) options.transaction = transaction |
d38b8281 | 76 | |
3fd3ab2d C |
77 | return AccountVideoRateModel.findOne(options) |
78 | } | |
8fffe21a C |
79 | |
80 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { | |
81 | const query = { | |
9a4a9b6c C |
82 | offset: start, |
83 | limit: count, | |
8fffe21a C |
84 | where: { |
85 | videoId, | |
86 | type: rateType | |
87 | }, | |
88 | transaction: t, | |
89 | include: [ | |
90 | { | |
91 | attributes: [ 'actorId' ], | |
92 | model: AccountModel.unscoped(), | |
93 | required: true, | |
94 | include: [ | |
95 | { | |
96 | attributes: [ 'url' ], | |
97 | model: ActorModel.unscoped(), | |
98 | required: true | |
99 | } | |
100 | ] | |
101 | } | |
102 | ] | |
103 | } | |
104 | ||
105 | return AccountVideoRateModel.findAndCountAll(query) | |
106 | } | |
d38b8281 | 107 | } |