Better file name for torrent
[oweals/peertube.git] / server / models / account / account-video-rate.ts
1 import { values } from 'lodash'
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'
6 import { VIDEO_RATE_TYPES } from '../../initializers'
7 import { VideoModel } from '../video/video'
8 import { AccountModel } from './account'
9
10 /*
11   Account rates per video.
12 */
13 @Table({
14   tableName: 'accountVideoRate',
15   indexes: [
16     {
17       fields: [ 'videoId', 'accountId' ],
18       unique: true
19     }
20   ]
21 })
22 export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
23
24   @AllowNull(false)
25   @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
26   type: VideoRateType
27
28   @CreatedAt
29   createdAt: Date
30
31   @UpdatedAt
32   updatedAt: Date
33
34   @ForeignKey(() => VideoModel)
35   @Column
36   videoId: number
37
38   @BelongsTo(() => VideoModel, {
39     foreignKey: {
40       allowNull: false
41     },
42     onDelete: 'CASCADE'
43   })
44   Video: VideoModel
45
46   @ForeignKey(() => AccountModel)
47   @Column
48   accountId: number
49
50   @BelongsTo(() => AccountModel, {
51     foreignKey: {
52       allowNull: false
53     },
54     onDelete: 'CASCADE'
55   })
56   Account: AccountModel
57
58   static load (accountId: number, videoId: number, transaction: Transaction) {
59     const options: IFindOptions<AccountVideoRateModel> = {
60       where: {
61         accountId,
62         videoId
63       }
64     }
65     if (transaction) options.transaction = transaction
66
67     return AccountVideoRateModel.findOne(options)
68   }
69 }