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