2016-06-23 12:17:39 +09:00
|
|
|
|
import Posts from './collection.js'
|
2016-06-23 15:00:58 +09:00
|
|
|
|
import Users from 'meteor/nova:users';
|
2016-06-23 15:16:32 +09:00
|
|
|
|
import Events from "meteor/nova:events";
|
2016-06-15 11:07:10 +09:00
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Post Methods
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-18 11:55:00 +09:00
|
|
|
|
Posts.methods = {};
|
2015-04-29 12:16:14 +09:00
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @summary Insert a post in the database (note: optional post properties not listed here)
|
2015-05-10 13:37:42 +09:00
|
|
|
|
* @param {Object} post - the post being inserted
|
|
|
|
|
* @param {string} post.userId - the id of the user the post belongs to
|
2015-04-29 12:16:14 +09:00
|
|
|
|
* @param {string} post.title - the post's title
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
Posts.methods.new = function (post) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-03-01 14:01:12 +09:00
|
|
|
|
const currentUser = Meteor.users.findOne(post.userId);
|
|
|
|
|
|
|
|
|
|
post = Telescope.callbacks.run("posts.new.sync", post, currentUser);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
|
|
post._id = Posts.insert(post);
|
|
|
|
|
|
2015-06-24 15:38:14 +09:00
|
|
|
|
// note: query for post to get fresh document with collection-hooks effects applied
|
2016-02-24 18:11:53 +09:00
|
|
|
|
Telescope.callbacks.runAsync("posts.new.async", Posts.findOne(post._id));
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
|
|
return post;
|
2015-05-01 18:22:00 +02:00
|
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2015-05-10 13:37:42 +09:00
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @summary Edit a post in the database
|
2015-05-10 13:37:42 +09:00
|
|
|
|
* @param {string} postId – the ID of the post being edited
|
|
|
|
|
* @param {Object} modifier – the modifier object
|
|
|
|
|
* @param {Object} post - the current post object
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
Posts.methods.edit = function (postId, modifier, post) {
|
2015-05-04 10:19:50 +09:00
|
|
|
|
|
|
|
|
|
if (typeof post === "undefined") {
|
|
|
|
|
post = Posts.findOne(postId);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 18:11:53 +09:00
|
|
|
|
modifier = Telescope.callbacks.run("posts.edit.sync", modifier, post);
|
2015-05-04 10:19:50 +09:00
|
|
|
|
|
|
|
|
|
Posts.update(postId, modifier);
|
|
|
|
|
|
2016-02-24 18:11:53 +09:00
|
|
|
|
Telescope.callbacks.runAsync("posts.edit.async", Posts.findOne(postId), post);
|
2015-05-04 10:19:50 +09:00
|
|
|
|
|
|
|
|
|
return Posts.findOne(postId);
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Increase the number of clicks on a post
|
|
|
|
|
* @param {string} postId – the ID of the post being edited
|
|
|
|
|
* @param {string} ip – the IP of the current user
|
|
|
|
|
*/
|
|
|
|
|
Posts.methods.increaseClicks = (postId, ip) => {
|
|
|
|
|
|
|
|
|
|
var clickEvent = {
|
|
|
|
|
name: 'click',
|
|
|
|
|
properties: {
|
|
|
|
|
postId: postId,
|
|
|
|
|
ip: ip
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// make sure this IP hasn't previously clicked on this post
|
|
|
|
|
var existingClickEvent = Events.findOne({name: 'click', 'properties.postId': postId, 'properties.ip': ip});
|
|
|
|
|
|
|
|
|
|
if(!existingClickEvent){
|
|
|
|
|
Events.log(clickEvent);
|
|
|
|
|
Posts.update(postId, { $inc: { clickCount: 1 }});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
|
// ------------------------------------------------------------------------------------------- //
|
|
|
|
|
// ----------------------------------------- Methods ----------------------------------------- //
|
|
|
|
|
// ------------------------------------------------------------------------------------------- //
|
|
|
|
|
|
|
|
|
|
var postViews = [];
|
|
|
|
|
|
|
|
|
|
Meteor.methods({
|
|
|
|
|
|
2015-05-10 13:37:42 +09:00
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @summary Meteor method for submitting a post from the client
|
2016-04-14 09:54:41 +09:00
|
|
|
|
* NOTE: the current user and the post author user might sometimes be two different users!
|
|
|
|
|
* Required properties: title
|
2015-05-10 13:37:42 +09:00
|
|
|
|
* @memberof Posts
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @isMethod true
|
2015-05-10 13:37:42 +09:00
|
|
|
|
* @param {Object} post - the post being inserted
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
'posts.new': function(post){
|
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
Posts.simpleSchema().namedContext("posts.new").validate(post);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-02-24 18:11:53 +09:00
|
|
|
|
post = Telescope.callbacks.run("posts.new.method", post, Meteor.user());
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-04-11 10:23:23 +09:00
|
|
|
|
if (Meteor.isServer && this.connection) {
|
2016-02-21 13:57:02 +09:00
|
|
|
|
post.userIP = this.connection.clientAddress;
|
|
|
|
|
post.userAgent = this.connection.httpHeaders["user-agent"];
|
|
|
|
|
}
|
2015-12-28 11:52:01 -05:00
|
|
|
|
|
2016-02-18 11:55:00 +09:00
|
|
|
|
return Posts.methods.new(post);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
},
|
|
|
|
|
|
2015-05-10 13:37:42 +09:00
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @summary Meteor method for editing a post from the client
|
2015-05-10 13:37:42 +09:00
|
|
|
|
* @memberof Posts
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @isMethod true
|
2015-05-10 13:37:42 +09:00
|
|
|
|
* @param {Object} postId - the id of the post being updated
|
2016-04-14 09:54:41 +09:00
|
|
|
|
* @param {Object} modifier - the update modifier
|
2015-05-10 13:37:42 +09:00
|
|
|
|
*/
|
2016-02-23 11:34:40 +09:00
|
|
|
|
'posts.edit': function (postId, modifier) {
|
2016-04-01 20:57:37 +09:00
|
|
|
|
|
2016-03-31 10:04:41 +09:00
|
|
|
|
Posts.simpleSchema().namedContext("posts.edit").validate(modifier, {modifier: true});
|
2015-07-10 11:05:13 +09:00
|
|
|
|
check(postId, String);
|
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
const post = Posts.findOne(postId);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
modifier = Telescope.callbacks.run("posts.edit.method", modifier, post, Meteor.user());
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-02-18 11:55:00 +09:00
|
|
|
|
return Posts.methods.edit(postId, modifier, post);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Meteor method for approving a post
|
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post to approve
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
'posts.approve': function(postId){
|
2015-07-10 11:40:11 +09:00
|
|
|
|
|
2015-07-28 10:59:04 +09:00
|
|
|
|
check(postId, String);
|
2015-10-03 13:03:39 +09:00
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
const post = Posts.findOne(postId);
|
|
|
|
|
const now = new Date();
|
2015-07-10 11:40:11 +09:00
|
|
|
|
|
2016-07-21 09:53:58 +09:00
|
|
|
|
if (Users.canDo(Meteor.user(), "posts.new.approved")) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
const set = {status: Posts.config.STATUS_APPROVED};
|
2015-10-03 13:03:39 +09:00
|
|
|
|
|
|
|
|
|
if (!post.postedAt) {
|
|
|
|
|
set.postedAt = now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Posts.update(post._id, {$set: set});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-04-01 16:52:02 +09:00
|
|
|
|
Telescope.callbacks.runAsync("posts.approve.async", post);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
} else {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
Messages.flash('You need to be an admin to do that.', "error");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Meteor method for rejecting a post
|
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post to reject
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
'posts.reject': function(postId){
|
2015-07-10 11:40:11 +09:00
|
|
|
|
|
2015-07-28 10:59:04 +09:00
|
|
|
|
check(postId, String);
|
2016-04-14 09:54:41 +09:00
|
|
|
|
|
|
|
|
|
const post = Posts.findOne(postId);
|
2015-07-28 10:59:04 +09:00
|
|
|
|
|
2016-07-21 14:51:58 +09:00
|
|
|
|
if(Users.isAdmin(Meteor.user())){
|
2015-09-06 11:37:48 +09:00
|
|
|
|
|
|
|
|
|
Posts.update(post._id, {$set: {status: Posts.config.STATUS_REJECTED}});
|
|
|
|
|
|
|
|
|
|
Telescope.callbacks.runAsync("postRejectAsync", post);
|
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
|
}else{
|
|
|
|
|
Messages.flash('You need to be an admin to do that.', "error");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Meteor method for increasing the number of views on a post
|
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
'posts.increaseViews': function(postId, sessionId){
|
2015-07-10 11:05:13 +09:00
|
|
|
|
|
|
|
|
|
check(postId, String);
|
2015-08-13 15:46:33 +09:00
|
|
|
|
check(sessionId, Match.Any);
|
2016-02-17 17:46:34 +09:00
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
|
// only let users increment a post's view counter once per session
|
|
|
|
|
var view = {_id: postId, userId: this.userId, sessionId: sessionId};
|
|
|
|
|
|
2016-04-14 09:54:41 +09:00
|
|
|
|
if (_.where(postViews, view).length === 0) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
postViews.push(view);
|
|
|
|
|
Posts.update(postId, { $inc: { viewCount: 1 }});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Meteor method for deleting a post
|
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post
|
|
|
|
|
*/
|
2016-06-23 11:40:35 +09:00
|
|
|
|
'posts.remove': function(postId) {
|
2015-07-10 11:40:11 +09:00
|
|
|
|
|
|
|
|
|
check(postId, String);
|
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
|
// remove post comments
|
|
|
|
|
// if(!this.isSimulation) {
|
|
|
|
|
// Comments.remove({post: postId});
|
|
|
|
|
// }
|
|
|
|
|
// NOTE: actually, keep comments after all
|
|
|
|
|
|
|
|
|
|
var post = Posts.findOne({_id: postId});
|
|
|
|
|
|
2016-07-20 10:25:05 +09:00
|
|
|
|
if (!Meteor.userId() || !Users.canEdit(Meteor.user(), post)){
|
2016-04-14 09:54:41 +09:00
|
|
|
|
throw new Meteor.Error(606, 'You need permission to edit or delete a post');
|
|
|
|
|
}
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
|
|
// decrement post count
|
2015-05-10 13:37:42 +09:00
|
|
|
|
Users.update({_id: post.userId}, {$inc: {"telescope.postCount": -1}});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
|
|
// delete post
|
|
|
|
|
Posts.remove(postId);
|
2015-08-24 09:56:21 +09:00
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
|
Telescope.callbacks.runAsync("posts.remove.async", post);
|
2015-08-24 09:56:21 +09:00
|
|
|
|
|
2015-09-19 10:33:37 +09:00
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
|
|
|
|
* @summary Check for other posts with the same URL
|
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} url - the URL to check
|
|
|
|
|
*/
|
2016-02-18 11:55:00 +09:00
|
|
|
|
'posts.checkForDuplicates': function (url) {
|
2015-09-19 10:33:37 +09:00
|
|
|
|
Posts.checkForSameUrl(url);
|
2016-02-18 12:16:32 +09:00
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
2016-04-14 09:54:41 +09:00
|
|
|
|
* @summary Upvote a post
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post
|
|
|
|
|
*/
|
2016-02-18 12:16:32 +09:00
|
|
|
|
'posts.upvote': function (postId) {
|
|
|
|
|
check(postId, String);
|
|
|
|
|
return Telescope.operateOnItem.call(this, Posts, postId, Meteor.user(), "upvote");
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
2016-04-14 09:54:41 +09:00
|
|
|
|
* @summary Downvote a post
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post
|
|
|
|
|
*/
|
2016-02-18 12:16:32 +09:00
|
|
|
|
'posts.downvote': function (postId) {
|
|
|
|
|
check(postId, String);
|
|
|
|
|
return Telescope.operateOnItem.call(this, Posts, postId, Meteor.user(), "downvote");
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
|
|
|
|
|
/**
|
2016-04-14 09:54:41 +09:00
|
|
|
|
* @summary Cancel an upvote on a post
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post
|
|
|
|
|
*/
|
2016-02-18 12:16:32 +09:00
|
|
|
|
'posts.cancelUpvote': function (postId) {
|
|
|
|
|
check(postId, String);
|
|
|
|
|
return Telescope.operateOnItem.call(this, Posts, postId, Meteor.user(), "cancelUpvote");
|
|
|
|
|
},
|
|
|
|
|
|
2016-04-09 09:41:20 +09:00
|
|
|
|
/**
|
2016-04-14 09:54:41 +09:00
|
|
|
|
* @summary Cancel a downvote on a post
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @memberof Posts
|
|
|
|
|
* @isMethod true
|
|
|
|
|
* @param {String} postId - the id of the post
|
|
|
|
|
*/
|
2016-02-18 12:16:32 +09:00
|
|
|
|
'posts.cancelDownvote': function (postId) {
|
|
|
|
|
check(postId, String);
|
|
|
|
|
return Telescope.operateOnItem.call(this, Posts, postId, Meteor.user(), "cancelDownvote");
|
2015-04-22 07:50:11 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|