]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-video-rate.ts
Refractor activity pub lib/helpers
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
CommitLineData
d38b8281 1/*
e4f97bab 2 Account rates per video.
d38b8281 3*/
65fcc311 4import { values } from 'lodash'
e02643f3 5import * as Sequelize from 'sequelize'
d38b8281 6
74889a71 7import { VIDEO_RATE_TYPES } from '../../initializers'
d38b8281 8
74889a71 9import { addMethodsToModel } from '../utils'
e02643f3 10import {
e4f97bab
C
11 AccountVideoRateInstance,
12 AccountVideoRateAttributes,
d38b8281 13
e4f97bab
C
14 AccountVideoRateMethods
15} from './account-video-rate-interface'
e02643f3 16
e4f97bab
C
17let AccountVideoRate: Sequelize.Model<AccountVideoRateInstance, AccountVideoRateAttributes>
18let load: AccountVideoRateMethods.Load
e02643f3 19
127944aa 20export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
e4f97bab 21 AccountVideoRate = sequelize.define<AccountVideoRateInstance, AccountVideoRateAttributes>('AccountVideoRate',
d38b8281
C
22 {
23 type: {
65fcc311 24 type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
d38b8281
C
25 allowNull: false
26 }
27 },
28 {
29 indexes: [
30 {
e4f97bab 31 fields: [ 'videoId', 'accountId', 'type' ],
d38b8281
C
32 unique: true
33 }
e02643f3 34 ]
d38b8281
C
35 }
36 )
37
e02643f3
C
38 const classMethods = [
39 associate,
40
41 load
42 ]
e4f97bab 43 addMethodsToModel(AccountVideoRate, classMethods)
e02643f3 44
e4f97bab 45 return AccountVideoRate
d38b8281
C
46}
47
48// ------------------------------ STATICS ------------------------------
49
50function associate (models) {
e4f97bab 51 AccountVideoRate.belongsTo(models.Video, {
d38b8281
C
52 foreignKey: {
53 name: 'videoId',
54 allowNull: false
55 },
56 onDelete: 'CASCADE'
57 })
58
e4f97bab 59 AccountVideoRate.belongsTo(models.Account, {
d38b8281 60 foreignKey: {
e4f97bab 61 name: 'accountId',
d38b8281
C
62 allowNull: false
63 },
64 onDelete: 'CASCADE'
65 })
66}
67
e4f97bab
C
68load = function (accountId: number, videoId: number, transaction: Sequelize.Transaction) {
69 const options: Sequelize.FindOptions<AccountVideoRateAttributes> = {
d38b8281 70 where: {
e4f97bab 71 accountId,
d38b8281
C
72 videoId
73 }
74 }
d38b8281
C
75 if (transaction) options.transaction = transaction
76
e4f97bab 77 return AccountVideoRate.findOne(options)
d38b8281 78}