From d38b82810638b9f664c9016fac2684454c273a77 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 8 Mar 2017 21:35:43 +0100 Subject: Add like/dislike system for videos --- server/models/request-video-event.js | 3 ++ server/models/user-video-rate.js | 77 ++++++++++++++++++++++++++++++++++++ server/models/video.js | 31 ++++++++++++++- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 server/models/user-video-rate.js (limited to 'server/models') diff --git a/server/models/request-video-event.js b/server/models/request-video-event.js index ef3ebcb3a..9ebeaec90 100644 --- a/server/models/request-video-event.js +++ b/server/models/request-video-event.js @@ -83,6 +83,9 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { if (podIds.length === 0) return callback(null, []) const query = { + order: [ + [ 'id', 'ASC' ] + ], include: [ { model: self.sequelize.models.Video, diff --git a/server/models/user-video-rate.js b/server/models/user-video-rate.js new file mode 100644 index 000000000..84007d70c --- /dev/null +++ b/server/models/user-video-rate.js @@ -0,0 +1,77 @@ +'use strict' + +/* + User rates per video. + +*/ + +const values = require('lodash/values') + +const constants = require('../initializers/constants') + +// --------------------------------------------------------------------------- + +module.exports = function (sequelize, DataTypes) { + const UserVideoRate = sequelize.define('UserVideoRate', + { + type: { + type: DataTypes.ENUM(values(constants.VIDEO_RATE_TYPES)), + allowNull: false + } + }, + { + indexes: [ + { + fields: [ 'videoId', 'userId', 'type' ], + unique: true + } + ], + classMethods: { + associate, + + load + } + } + ) + + return UserVideoRate +} + +// ------------------------------ STATICS ------------------------------ + +function associate (models) { + this.belongsTo(models.Video, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'CASCADE' + }) + + this.belongsTo(models.User, { + foreignKey: { + name: 'userId', + allowNull: false + }, + onDelete: 'CASCADE' + }) +} + +function load (userId, videoId, transaction, callback) { + if (!callback) { + callback = transaction + transaction = null + } + + const query = { + where: { + userId, + videoId + } + } + + const options = {} + if (transaction) options.transaction = transaction + + return this.findOne(query, options).asCallback(callback) +} diff --git a/server/models/video.js b/server/models/video.js index fb46aca86..182555c85 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -89,6 +89,24 @@ module.exports = function (sequelize, DataTypes) { min: 0, isInt: true } + }, + likes: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0, + validate: { + min: 0, + isInt: true + } + }, + dislikes: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0, + validate: { + min: 0, + isInt: true + } } }, { @@ -113,6 +131,9 @@ module.exports = function (sequelize, DataTypes) { }, { fields: [ 'views' ] + }, + { + fields: [ 'likes' ] } ], classMethods: { @@ -349,6 +370,8 @@ function toFormatedJSON () { author: this.Author.name, duration: this.duration, views: this.views, + likes: this.likes, + dislikes: this.dislikes, tags: map(this.Tags, 'name'), thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()), createdAt: this.createdAt, @@ -381,7 +404,9 @@ function toAddRemoteJSON (callback) { createdAt: self.createdAt, updatedAt: self.updatedAt, extname: self.extname, - views: self.views + views: self.views, + likes: self.likes, + dislikes: self.dislikes } return callback(null, remoteVideo) @@ -400,7 +425,9 @@ function toUpdateRemoteJSON (callback) { createdAt: this.createdAt, updatedAt: this.updatedAt, extname: this.extname, - views: this.views + views: this.views, + likes: this.likes, + dislikes: this.dislikes } return json -- cgit v1.2.3