]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/user/user-video-rate.ts
Share models between server and client
[github/Chocobozzz/PeerTube.git] / server / models / user / user-video-rate.ts
... / ...
CommitLineData
1/*
2 User rates per video.
3*/
4import { values } from 'lodash'
5import * as Sequelize from 'sequelize'
6
7import { VIDEO_RATE_TYPES } from '../../initializers'
8
9import { addMethodsToModel } from '../utils'
10import {
11 UserVideoRateClass,
12 UserVideoRateInstance,
13 UserVideoRateAttributes,
14
15 UserVideoRateMethods
16} from './user-video-rate-interface'
17
18let UserVideoRate: Sequelize.Model<UserVideoRateInstance, UserVideoRateAttributes>
19let load: UserVideoRateMethods.Load
20
21export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
22 UserVideoRate = sequelize.define<UserVideoRateInstance, UserVideoRateAttributes>('UserVideoRate',
23 {
24 type: {
25 type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
26 allowNull: false
27 }
28 },
29 {
30 indexes: [
31 {
32 fields: [ 'videoId', 'userId', 'type' ],
33 unique: true
34 }
35 ]
36 }
37 )
38
39 const classMethods = [
40 associate,
41
42 load
43 ]
44 addMethodsToModel(UserVideoRate, classMethods)
45
46 return UserVideoRate
47}
48
49// ------------------------------ STATICS ------------------------------
50
51function associate (models) {
52 UserVideoRate.belongsTo(models.Video, {
53 foreignKey: {
54 name: 'videoId',
55 allowNull: false
56 },
57 onDelete: 'CASCADE'
58 })
59
60 UserVideoRate.belongsTo(models.User, {
61 foreignKey: {
62 name: 'userId',
63 allowNull: false
64 },
65 onDelete: 'CASCADE'
66 })
67}
68
69load = function (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: UserVideoRateMethods.LoadCallback) {
70 const options: Sequelize.FindOptions = {
71 where: {
72 userId,
73 videoId
74 }
75 }
76 if (transaction) options.transaction = transaction
77
78 return UserVideoRate.findOne(options).asCallback(callback)
79}