mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
creating comments package
This commit is contained in:
parent
08c6e373fc
commit
50da57e059
6 changed files with 366 additions and 0 deletions
|
@ -65,6 +65,7 @@ telescope:posts
|
|||
telescope:lib
|
||||
telescope:users
|
||||
telescope:settings
|
||||
telescope:comments
|
||||
|
||||
# Telescope Packages (Optional)
|
||||
|
||||
|
|
|
@ -112,6 +112,7 @@ standard-app-packages@1.0.5
|
|||
stylus@1.0.7
|
||||
tap:i18n@1.4.1
|
||||
telescope:api@0.1.0
|
||||
telescope:comments@0.1.0
|
||||
telescope:daily@0.1.0
|
||||
telescope:datetimepicker@0.1.0
|
||||
telescope:email@0.3.0
|
||||
|
|
1
packages/telescope-comments/README.md
Normal file
1
packages/telescope-comments/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
Telescope comments package, used internally.
|
286
packages/telescope-comments/lib/comments.js
Normal file
286
packages/telescope-comments/lib/comments.js
Normal file
|
@ -0,0 +1,286 @@
|
|||
commentSchemaObject = {
|
||||
_id: {
|
||||
type: String,
|
||||
optional: true
|
||||
},
|
||||
parentCommentId: {
|
||||
type: String,
|
||||
optional: true,
|
||||
autoform: {
|
||||
editable: true,
|
||||
omit: true
|
||||
}
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
optional: true
|
||||
},
|
||||
postedAt: { // for now, comments are always created and posted at the same time
|
||||
type: Date,
|
||||
optional: true
|
||||
},
|
||||
body: {
|
||||
type: String,
|
||||
autoform: {
|
||||
editable: true
|
||||
}
|
||||
},
|
||||
htmlBody: {
|
||||
type: String,
|
||||
optional: true
|
||||
},
|
||||
baseScore: {
|
||||
type: Number,
|
||||
decimal: true,
|
||||
optional: true
|
||||
},
|
||||
score: {
|
||||
type: Number,
|
||||
decimal: true,
|
||||
optional: true
|
||||
},
|
||||
upvotes: {
|
||||
type: Number,
|
||||
optional: true
|
||||
},
|
||||
upvoters: {
|
||||
type: [String], // XXX
|
||||
optional: true
|
||||
},
|
||||
downvotes: {
|
||||
type: Number,
|
||||
optional: true
|
||||
},
|
||||
downvoters: {
|
||||
type: [String], // XXX
|
||||
optional: true
|
||||
},
|
||||
author: {
|
||||
type: String,
|
||||
optional: true
|
||||
},
|
||||
inactive: {
|
||||
type: Boolean,
|
||||
optional: true
|
||||
},
|
||||
postId: {
|
||||
type: String, // XXX
|
||||
optional: true,
|
||||
autoform: {
|
||||
editable: true,
|
||||
omit: true
|
||||
}
|
||||
},
|
||||
userId: {
|
||||
type: String, // XXX
|
||||
optional: true
|
||||
},
|
||||
isDeleted: {
|
||||
type: Boolean,
|
||||
optional: true
|
||||
}
|
||||
};
|
||||
|
||||
// add any extra properties to commentSchemaObject (provided by packages for example)
|
||||
_.each(addToCommentsSchema, function(item){
|
||||
commentSchemaObject[item.propertyName] = item.propertySchema;
|
||||
});
|
||||
|
||||
Comments = new Meteor.Collection("comments");
|
||||
|
||||
commentSchema = new SimpleSchema(commentSchemaObject);
|
||||
|
||||
Comments.attachSchema(commentSchema);
|
||||
|
||||
Comments.deny({
|
||||
update: function(userId, post, fieldNames) {
|
||||
if(Users.isAdminById(userId))
|
||||
return false;
|
||||
// deny the update if it contains something other than the following fields
|
||||
return (_.without(fieldNames, 'body').length > 0);
|
||||
}
|
||||
});
|
||||
|
||||
Comments.allow({
|
||||
update: Users.can.editById,
|
||||
remove: Users.can.editById
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------------------------------- //
|
||||
// ------------------------------------------ Hooks ------------------------------------------ //
|
||||
// ------------------------------------------------------------------------------------------- //
|
||||
|
||||
Comments.before.insert(function (userId, doc) {
|
||||
// note: only actually sanitizes on the server
|
||||
doc.htmlBody = Telescope.utils.sanitize(marked(doc.body));
|
||||
});
|
||||
|
||||
Comments.before.update(function (userId, doc, fieldNames, modifier, options) {
|
||||
// if body is being modified, update htmlBody too
|
||||
if (Meteor.isServer && modifier.$set && modifier.$set.body) {
|
||||
modifier.$set = modifier.$set || {};
|
||||
modifier.$set.htmlBody = Telescope.utils.sanitize(marked(modifier.$set.body));
|
||||
}
|
||||
});
|
||||
|
||||
commentAfterSubmitMethodCallbacks.push(function (comment) {
|
||||
|
||||
var userId = comment.userId,
|
||||
commentAuthor = Meteor.users.findOne(userId);
|
||||
|
||||
// increment comment count
|
||||
Meteor.users.update({_id: userId}, {
|
||||
$inc: {'commentCount': 1}
|
||||
});
|
||||
|
||||
// update post
|
||||
Posts.update(comment.postId, {
|
||||
$inc: {commentCount: 1},
|
||||
$set: {lastCommentedAt: new Date()},
|
||||
$addToSet: {commenters: userId}
|
||||
});
|
||||
|
||||
// upvote comment
|
||||
upvoteItem(Comments, comment, commentAuthor);
|
||||
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------------------------------- //
|
||||
// -------------------------------------- Submit Comment ------------------------------------- //
|
||||
// ------------------------------------------------------------------------------------------- //
|
||||
|
||||
submitComment = function (comment) {
|
||||
|
||||
var userId = comment.userId; // at this stage, a userId is expected
|
||||
|
||||
// ------------------------------ Checks ------------------------------ //
|
||||
|
||||
// Don't allow empty comments
|
||||
if (!comment.body)
|
||||
throw new Meteor.Error(704,i18n.t('your_comment_is_empty'));
|
||||
|
||||
// ------------------------------ Properties ------------------------------ //
|
||||
|
||||
var defaultProperties = {
|
||||
createdAt: new Date(),
|
||||
postedAt: new Date(),
|
||||
upvotes: 0,
|
||||
downvotes: 0,
|
||||
baseScore: 0,
|
||||
score: 0,
|
||||
author: Users.getDisplayNameById(userId)
|
||||
};
|
||||
|
||||
comment = _.extend(defaultProperties, comment);
|
||||
|
||||
// ------------------------------ Callbacks ------------------------------ //
|
||||
|
||||
// run all post submit server callbacks on comment object successively
|
||||
comment = commentSubmitMethodCallbacks.reduce(function(result, currentFunction) {
|
||||
return currentFunction(result);
|
||||
}, comment);
|
||||
|
||||
// -------------------------------- Insert -------------------------------- //
|
||||
|
||||
comment._id = Comments.insert(comment);
|
||||
|
||||
// --------------------- Server-side Async Callbacks --------------------- //
|
||||
|
||||
// run all post submit server callbacks on comment object successively
|
||||
if (Meteor.isServer) {
|
||||
Meteor.setTimeout(function () { // use setTimeout to avoid holding up client
|
||||
comment = commentAfterSubmitMethodCallbacks.reduce(function(result, currentFunction) {
|
||||
return currentFunction(result);
|
||||
}, comment);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
return comment;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------- //
|
||||
// ----------------------------------------- Methods ----------------------------------------- //
|
||||
// ------------------------------------------------------------------------------------------- //
|
||||
|
||||
Meteor.methods({
|
||||
submitComment: function(comment){
|
||||
|
||||
// required properties:
|
||||
// postId
|
||||
// body
|
||||
|
||||
// optional properties:
|
||||
// parentCommentId
|
||||
|
||||
var user = Meteor.user(),
|
||||
hasAdminRights = Users.isAdmin(user);
|
||||
|
||||
// ------------------------------ Checks ------------------------------ //
|
||||
|
||||
// check that user can comment
|
||||
if (!user || !Users.can.comment(user))
|
||||
throw new Meteor.Error(i18n.t('you_need_to_login_or_be_invited_to_post_new_comments'));
|
||||
|
||||
// ------------------------------ Rate Limiting ------------------------------ //
|
||||
|
||||
if (!hasAdminRights) {
|
||||
|
||||
var timeSinceLastComment = Users.timeSinceLast(user, Comments),
|
||||
commentInterval = Math.abs(parseInt(Settings.get('commentInterval',15)));
|
||||
|
||||
// check that user waits more than 15 seconds between comments
|
||||
if((timeSinceLastComment < commentInterval))
|
||||
throw new Meteor.Error(704, i18n.t('please_wait')+(commentInterval-timeSinceLastComment)+i18n.t('seconds_before_commenting_again'));
|
||||
|
||||
}
|
||||
|
||||
// ------------------------------ Properties ------------------------------ //
|
||||
|
||||
// admin-only properties
|
||||
// userId
|
||||
|
||||
// if user is not admin, clear restricted properties
|
||||
if (!hasAdminRights) {
|
||||
_.keys(comment).forEach(function (propertyName) {
|
||||
var property = commentSchemaObject[propertyName];
|
||||
if (!property || !property.autoform || !property.autoform.editable) {
|
||||
console.log("// Disallowed property detected: "+propertyName+" (nice try!)");
|
||||
delete comment[propertyName]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// if no userId has been set, default to current user id
|
||||
if (!comment.userId) {
|
||||
comment.userId = user._id
|
||||
}
|
||||
|
||||
return submitComment(comment);
|
||||
},
|
||||
removeComment: function(commentId){
|
||||
var comment = Comments.findOne(commentId);
|
||||
if(Users.can.edit(Meteor.user(), comment)){
|
||||
// decrement post comment count and remove user ID from post
|
||||
Posts.update(comment.postId, {
|
||||
$inc: {commentCount: -1},
|
||||
$pull: {commenters: comment.userId}
|
||||
});
|
||||
|
||||
// decrement user comment count and remove comment ID from user
|
||||
Meteor.users.update({_id: comment.userId}, {
|
||||
$inc: {'commentCount': -1}
|
||||
});
|
||||
|
||||
// note: should we also decrease user's comment karma ?
|
||||
// We don't actually delete the comment to avoid losing all child comments.
|
||||
// Instead, we give it a special flag
|
||||
Comments.update({_id: commentId}, {$set: {
|
||||
body: 'Deleted',
|
||||
htmlBody: 'Deleted',
|
||||
isDeleted: true
|
||||
}});
|
||||
}else{
|
||||
Messages.flash("You don't have permission to delete this comment.", "error");
|
||||
}
|
||||
}
|
||||
});
|
46
packages/telescope-comments/lib/server/publications.js
Normal file
46
packages/telescope-comments/lib/server/publications.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
Comments._ensureIndex({"postId": 1});
|
||||
|
||||
// Publish a single comment
|
||||
|
||||
Meteor.publish('singleCommentAndChildren', function(commentId) {
|
||||
if(Users.can.viewById(this.userId)){
|
||||
// publish both current comment and child comments
|
||||
var commentIds = [commentId];
|
||||
var childCommentIds = _.pluck(Comments.find({parentCommentId: commentId}, {fields: {_id: 1}}).fetch(), '_id');
|
||||
commentIds = commentIds.concat(childCommentIds);
|
||||
return Comments.find({_id: {$in: commentIds}}, {sort: {score: -1, postedAt: -1}});
|
||||
}
|
||||
return [];
|
||||
});
|
||||
|
||||
// Publish the post related to the current comment
|
||||
|
||||
Meteor.publish('commentPost', function(commentId) {
|
||||
if(Users.can.viewById(this.userId)){
|
||||
var comment = Comments.findOne(commentId);
|
||||
return Posts.find({_id: comment && comment.postId});
|
||||
}
|
||||
return [];
|
||||
});
|
||||
|
||||
// Publish author of the current comment, and author of the post related to the current comment
|
||||
|
||||
Meteor.publish('commentUsers', function(commentId) {
|
||||
|
||||
var userIds = [];
|
||||
|
||||
if(Users.can.viewById(this.userId)){
|
||||
|
||||
var comment = Comments.findOne(commentId);
|
||||
userIds.push(comment.userId);
|
||||
|
||||
var post = Posts.findOne(comment.postId);
|
||||
userIds.push(post.userId);
|
||||
|
||||
return Meteor.users.find({_id: {$in: userIds}}, {fields: Users.pubsub.privacyOptions});
|
||||
|
||||
}
|
||||
|
||||
return [];
|
||||
|
||||
});
|
31
packages/telescope-comments/package.js
Normal file
31
packages/telescope-comments/package.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
Package.describe({
|
||||
name: "telescope:comments",
|
||||
summary: "Telescope comments package",
|
||||
version: "0.1.0",
|
||||
git: "https://github.com/TelescopeJS/Telescope.git"
|
||||
});
|
||||
|
||||
Package.onUse(function (api) {
|
||||
|
||||
api.versionsFrom(['METEOR@1.0']);
|
||||
|
||||
api.use([
|
||||
'jquery',
|
||||
'underscore',
|
||||
'mongo',
|
||||
'aldeed:simple-schema@1.3.2',
|
||||
'telescope:lib@0.3.0',
|
||||
'telescope:users@0.1.0'
|
||||
]);
|
||||
|
||||
api.add_files([
|
||||
'lib/comments.js',
|
||||
], ['client', 'server']);
|
||||
|
||||
api.add_files([
|
||||
'lib/server/publications.js',
|
||||
], ['server']);
|
||||
|
||||
api.export('Posts');
|
||||
|
||||
});
|
Loading…
Add table
Reference in a new issue