2015-06-19 11:52:57 +09:00
|
|
|
///////////////////////////
|
|
|
|
// Notifications Helpers //
|
|
|
|
///////////////////////////
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
2015-06-19 12:00:47 +09:00
|
|
|
* Grab common post properties (for email notifications, only used on server).
|
2015-04-22 07:50:11 +09:00
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getProperties = function (post) {
|
|
|
|
var postAuthor = Meteor.users.findOne(post.userId);
|
|
|
|
var p = {
|
2015-06-19 11:52:57 +09:00
|
|
|
postAuthorName : post.getAuthorName(),
|
2015-04-22 07:50:11 +09:00
|
|
|
postTitle : Telescope.utils.cleanUp(post.title),
|
2015-06-19 12:00:47 +09:00
|
|
|
profileUrl: Users.getProfileUrl(postAuthor, true),
|
2015-06-19 11:52:57 +09:00
|
|
|
postUrl: post.getPageUrl(true),
|
2015-04-22 07:50:11 +09:00
|
|
|
thumbnailUrl: post.thumbnailUrl,
|
2015-06-19 11:52:57 +09:00
|
|
|
linkUrl: !!post.url ? Telescope.utils.getOutgoingUrl(post.url) : post.getPageUrl(true)
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
if(post.url)
|
|
|
|
p.url = post.url;
|
|
|
|
|
|
|
|
if(post.htmlBody)
|
|
|
|
p.htmlBody = post.htmlBody;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
};
|
|
|
|
|
2015-06-19 11:52:57 +09:00
|
|
|
//////////////////
|
|
|
|
// Link Helpers //
|
|
|
|
//////////////////
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
2015-06-18 13:04:38 +09:00
|
|
|
* Return a post's link if it has one, else return its post page URL
|
2015-04-22 07:50:11 +09:00
|
|
|
* @param {Object} post
|
|
|
|
*/
|
2015-06-19 09:48:59 +09:00
|
|
|
Posts.getLink = function (post, isAbsolute) {
|
2015-06-19 11:52:57 +09:00
|
|
|
return !!post.url ? Telescope.utils.getOutgoingUrl(post.url) : this.getPageUrl(post, isAbsolute);
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
2015-06-19 11:52:57 +09:00
|
|
|
Posts.helpers({getLink: function (isAbsolute) {return Posts.getLink(this, isAbsolute);}});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
2015-06-18 13:04:38 +09:00
|
|
|
* Get URL of a post page.
|
|
|
|
* @param {Object} post
|
2015-04-22 07:50:11 +09:00
|
|
|
*/
|
2015-06-19 09:48:59 +09:00
|
|
|
Posts.getPageUrl = function(post, isAbsolute){
|
|
|
|
var isAbsolute = typeof isAbsolute === "undefined" ? false : isAbsolute; // default to false
|
|
|
|
var prefix = isAbsolute ? Telescope.utils.getSiteUrl().slice(0,-1) : "";
|
|
|
|
return prefix + Router.path("post_page", post);
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
2015-06-19 11:52:57 +09:00
|
|
|
Posts.helpers({getPageUrl: function (isAbsolute) {return Posts.getPageUrl(this, isAbsolute);}});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
2015-06-18 13:04:38 +09:00
|
|
|
* Get post edit page URL.
|
|
|
|
* @param {String} id
|
2015-04-22 07:50:11 +09:00
|
|
|
*/
|
2015-06-19 09:48:59 +09:00
|
|
|
Posts.getEditUrl = function(post, isAbsolute){
|
|
|
|
var isAbsolute = typeof isAbsolute === "undefined" ? false : isAbsolute; // default to false
|
|
|
|
var prefix = isAbsolute ? Telescope.utils.getSiteUrl().slice(0,-1) : "";
|
|
|
|
return prefix + Router.path("post_edit", post);
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
2015-06-19 11:52:57 +09:00
|
|
|
Posts.helpers({getEditUrl: function (isAbsolute) {return Posts.getEditUrl(this, isAbsolute);}});
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Other Helpers //
|
|
|
|
///////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a post author's name
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getAuthorName = function (post) {
|
|
|
|
var user = Meteor.users.findOne(post.userId);
|
|
|
|
if (user) {
|
2015-06-19 12:09:03 +09:00
|
|
|
return user.getDisplayName();
|
2015-06-19 11:52:57 +09:00
|
|
|
} else {
|
|
|
|
return post.author;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Posts.helpers({getAuthorName: function () {return Posts.getAuthorName(this);}});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default status for new posts.
|
|
|
|
* @param {Object} user
|
|
|
|
*/
|
|
|
|
Posts.getDefaultStatus = function (user) {
|
|
|
|
var hasAdminRights = typeof user === 'undefined' ? false : Users.is.admin(user);
|
|
|
|
if (hasAdminRights || !Settings.get('requirePostsApproval', false)) {
|
|
|
|
// if user is admin, or else post approval is not required
|
|
|
|
return Posts.config.STATUS_APPROVED
|
|
|
|
} else {
|
|
|
|
// else
|
|
|
|
return Posts.config.STATUS_PENDING
|
|
|
|
}
|
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if post URL is unique.
|
|
|
|
* We need the current user so we know who to upvote the existing post as.
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} currentUser
|
|
|
|
*/
|
|
|
|
Posts.checkForSameUrl = function (url, currentUser) {
|
|
|
|
|
|
|
|
// check that there are no previous posts with the same link in the past 6 months
|
|
|
|
var sixMonthsAgo = moment().subtract(6, 'months').toDate();
|
|
|
|
var postWithSameLink = Posts.findOne({url: url, postedAt: {$gte: sixMonthsAgo}});
|
|
|
|
|
|
|
|
if(typeof postWithSameLink !== 'undefined'){
|
2015-04-23 11:11:07 +09:00
|
|
|
Telescope.upvoteItem(Posts, postWithSameLink, currentUser);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
// note: error.details returns undefined on the client, so add post ID to reason
|
|
|
|
throw new Meteor.Error('603', i18n.t('this_link_has_already_been_posted') + '|' + postWithSameLink._id, postWithSameLink._id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When on a post page, return the current post
|
|
|
|
*/
|
|
|
|
Posts.current = function () {
|
|
|
|
return Posts.findOne(Router.current().data()._id);
|
|
|
|
};
|