-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathComment.js
127 lines (114 loc) · 3.7 KB
/
Comment.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// 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, Comment) {
var Content = compound.models.Content;
// validation
Comment.validatesPresenceOf('contentId', 'authorId', 'text');
/**
* Get whether this comment has been flagged.
*
* @return {Boolean}
*/
Comment.getter.hasFlag = function () {
return this.flags.length > 0;
};
/**
* Set the date automatically if we don't have one.
*
* @param {Function} next - callback function
*/
Content.beforeSave = function (next) {
if (!this.createdAt) {
this.createdAt = new Date();
}
next();
};
/**
* After a comment is saved, update the parent content item.
*
* @param {Function} next - callback function
*/
Comment.afterSave = function (next) {
Content.updateComments(this.contentId, next);
};
/**
* After a comment is deleted, update the parent content item.
*
* @param {Function} next - callback function
*/
Comment.afterDestroy = function (next) {
Content.updateComments(this.contentId, next);
};
/**
* Like or unlike a comment.
*
* @param {User} user - user performing the like
* @param {Function} callback - callback function
*/
Comment.prototype.like = function (user, callback) {
if (this.likes.find(user.id, 'userId')) {
this.likes.remove(this.likes.find(user.id, 'userId'));
} else {
this.likes.push({
userId: user.id,
username: user.username,
createdAt: new Date()
});
}
this.save(callback);
};
/**
* Flag or unflag a comment.
*
* @param {User} user - user performing the flag
* @param {Function} callback - callback function
*/
Comment.prototype.flag = function (user, callback) {
if (this.flags.find(user.id, 'userId')) {
this.flags.remove(this.flags.find(user.id, 'userId'));
} else {
this.flags.push({
userId: user.id,
username: user.username,
createdAt: new Date()
});
}
this.save(callback);
};
/**
* Clear all of the flags from this comment.
* @param {Function} callback - callback function
*/
Comment.prototype.clearFlags = function (callback) {
this.flags.items = [];
this.save(callback);
};
/**
* Delete this post and blacklist the author from the group it was posted to.
*
* @param {Function} callback - call back function
*/
Content.prototype.destroyAndBan = function (callback) {
var post = this;
User.find(this.authorId, function (err, user) {
user.setMembershipState(post.groupId, 'blacklisted', function (err, user) {
post.destroy(callback);
});
})
};
};