]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user-video-rate.ts
First typescript iteration
[github/Chocobozzz/PeerTube.git] / server / models / user-video-rate.ts
1 /*
2 User rates per video.
3
4 */
5 import { values } from 'lodash'
6
7 import { VIDEO_RATE_TYPES } from '../initializers'
8
9 // ---------------------------------------------------------------------------
10
11 module.exports = function (sequelize, DataTypes) {
12 const UserVideoRate = sequelize.define('UserVideoRate',
13 {
14 type: {
15 type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
16 allowNull: false
17 }
18 },
19 {
20 indexes: [
21 {
22 fields: [ 'videoId', 'userId', 'type' ],
23 unique: true
24 }
25 ],
26 classMethods: {
27 associate,
28
29 load
30 }
31 }
32 )
33
34 return UserVideoRate
35 }
36
37 // ------------------------------ STATICS ------------------------------
38
39 function associate (models) {
40 this.belongsTo(models.Video, {
41 foreignKey: {
42 name: 'videoId',
43 allowNull: false
44 },
45 onDelete: 'CASCADE'
46 })
47
48 this.belongsTo(models.User, {
49 foreignKey: {
50 name: 'userId',
51 allowNull: false
52 },
53 onDelete: 'CASCADE'
54 })
55 }
56
57 function load (userId, videoId, transaction, callback) {
58 if (!callback) {
59 callback = transaction
60 transaction = null
61 }
62
63 const query = {
64 where: {
65 userId,
66 videoId
67 }
68 }
69
70 const options: any = {}
71 if (transaction) options.transaction = transaction
72
73 return this.findOne(query, options).asCallback(callback)
74 }