diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-03-08 21:35:43 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-03-08 21:35:43 +0100 |
commit | d38b82810638b9f664c9016fac2684454c273a77 (patch) | |
tree | 9465c367e5033675309efca4d66790c6fdd5230d /server/models/user-video-rate.js | |
parent | 8f9064432122cba0f518a24ac4378357dadec589 (diff) | |
download | PeerTube-d38b82810638b9f664c9016fac2684454c273a77.tar.gz PeerTube-d38b82810638b9f664c9016fac2684454c273a77.tar.zst PeerTube-d38b82810638b9f664c9016fac2684454c273a77.zip |
Add like/dislike system for videos
Diffstat (limited to 'server/models/user-video-rate.js')
-rw-r--r-- | server/models/user-video-rate.js | 77 |
1 files changed, 77 insertions, 0 deletions
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 | |||
8 | const values = require('lodash/values') | ||
9 | |||
10 | const constants = require('../initializers/constants') | ||
11 | |||
12 | // --------------------------------------------------------------------------- | ||
13 | |||
14 | module.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 | |||
42 | function 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 | |||
60 | function 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 | } | ||