forked from inventures/hatchjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLike.js
91 lines (83 loc) · 2.89 KB
/
Like.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Hatch.js is a CMS and social website building framework built in Node.js
// Copyright (C) 2013 Inventures Software Ltd
//
// This file is part of Hatch.js
//
// Hatch.js is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation, version 3
//
// Hatch.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Affero General Public License for more details. You should have received a copy of the GNU
// General Public License along with Hatch.js. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Marcus Greenwood, Anatoliy Chakkaev and others
//
module.exports = function (compound, Like) {
var Content = compound.models.Content;
var _ = require('underscore');
// validation
Like.validatesPresenceOf('contentId', 'userId');
/**
* Set the date automatically if we don't have one.
*
* @param {Function} next - callback function
*/
Like.beforeSave = function (next) {
if (!this.createdAt) {
this.createdAt = new Date();
}
next();
};
/**
* After a like is saved, update the parent content item.
*
* @param {Function} next - callback function
*/
Like.afterSave = function (next) {
Content.updateLikes(this.contentId, next);
};
/**
* After a like is deleted, update the parent content item.
*
* @param {Function} next - callback function
*/
Like.afterDestroy = function (next) {
Content.updateLikes(this.contentId, next);
};
/**
* Check whether the specified content is liked by the specified user.
*
* @param {Number} contentId - id of the content
* @param {Number} userId - id of the user
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Like.doesLike = function (contentId, userId, callback) {
Like.findOne({ where: { contentId: contentId, userId: userId }}, function (err, like) {
callback (err, like != null);
});
};
/**
* Get all of the likes for the specified user.
*
* @param {Number} userId - id of the user
* @param {Function} callback - callback function
*/
Like.getLikes = function (userId, callback) {
Like.all({ where: { userId: userId }}, callback);
};
/**
* Get all of the content ids for likes for the specified user.
*
* @param {Number} userId - id of the user
* @param {Function} callback - callback function
*/
Like.getLikeIds = function (userId, callback) {
Like.getLikes(userId, function (err, likes) {
callback(err, !err && _.pluck(likes, 'contentId'));
});
};
};