aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-03-08 21:35:43 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-03-08 21:35:43 +0100
commitd38b82810638b9f664c9016fac2684454c273a77 (patch)
tree9465c367e5033675309efca4d66790c6fdd5230d /server/models
parent8f9064432122cba0f518a24ac4378357dadec589 (diff)
downloadPeerTube-d38b82810638b9f664c9016fac2684454c273a77.tar.gz
PeerTube-d38b82810638b9f664c9016fac2684454c273a77.tar.zst
PeerTube-d38b82810638b9f664c9016fac2684454c273a77.zip
Add like/dislike system for videos
Diffstat (limited to 'server/models')
-rw-r--r--server/models/request-video-event.js3
-rw-r--r--server/models/user-video-rate.js77
-rw-r--r--server/models/video.js31
3 files changed, 109 insertions, 2 deletions
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) {
83 if (podIds.length === 0) return callback(null, []) 83 if (podIds.length === 0) return callback(null, [])
84 84
85 const query = { 85 const query = {
86 order: [
87 [ 'id', 'ASC' ]
88 ],
86 include: [ 89 include: [
87 { 90 {
88 model: self.sequelize.models.Video, 91 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 @@
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}
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) {
89 min: 0, 89 min: 0,
90 isInt: true 90 isInt: true
91 } 91 }
92 },
93 likes: {
94 type: DataTypes.INTEGER,
95 allowNull: false,
96 defaultValue: 0,
97 validate: {
98 min: 0,
99 isInt: true
100 }
101 },
102 dislikes: {
103 type: DataTypes.INTEGER,
104 allowNull: false,
105 defaultValue: 0,
106 validate: {
107 min: 0,
108 isInt: true
109 }
92 } 110 }
93 }, 111 },
94 { 112 {
@@ -113,6 +131,9 @@ module.exports = function (sequelize, DataTypes) {
113 }, 131 },
114 { 132 {
115 fields: [ 'views' ] 133 fields: [ 'views' ]
134 },
135 {
136 fields: [ 'likes' ]
116 } 137 }
117 ], 138 ],
118 classMethods: { 139 classMethods: {
@@ -349,6 +370,8 @@ function toFormatedJSON () {
349 author: this.Author.name, 370 author: this.Author.name,
350 duration: this.duration, 371 duration: this.duration,
351 views: this.views, 372 views: this.views,
373 likes: this.likes,
374 dislikes: this.dislikes,
352 tags: map(this.Tags, 'name'), 375 tags: map(this.Tags, 'name'),
353 thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()), 376 thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
354 createdAt: this.createdAt, 377 createdAt: this.createdAt,
@@ -381,7 +404,9 @@ function toAddRemoteJSON (callback) {
381 createdAt: self.createdAt, 404 createdAt: self.createdAt,
382 updatedAt: self.updatedAt, 405 updatedAt: self.updatedAt,
383 extname: self.extname, 406 extname: self.extname,
384 views: self.views 407 views: self.views,
408 likes: self.likes,
409 dislikes: self.dislikes
385 } 410 }
386 411
387 return callback(null, remoteVideo) 412 return callback(null, remoteVideo)
@@ -400,7 +425,9 @@ function toUpdateRemoteJSON (callback) {
400 createdAt: this.createdAt, 425 createdAt: this.createdAt,
401 updatedAt: this.updatedAt, 426 updatedAt: this.updatedAt,
402 extname: this.extname, 427 extname: this.extname,
403 views: this.views 428 views: this.views,
429 likes: this.likes,
430 dislikes: this.dislikes
404 } 431 }
405 432
406 return json 433 return json