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-08-12 15:22:05 +09:00
|
|
|
/**
|
|
|
|
* Depending on the settings, return either a post's URL link (if it has one) or its page URL.
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getShareableLink = function (post) {
|
|
|
|
return Settings.get("outsideLinksPointTo", "link") === "link" ? Posts.getLink(post) : Posts.getPageUrl(post, true);
|
|
|
|
};
|
|
|
|
Posts.helpers({getShareableLink: function () {return Posts.getShareableLink(this);}});
|
|
|
|
|
2015-08-01 12:49:44 +09:00
|
|
|
/**
|
|
|
|
* Whether a post's link should open in a new tab or not
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getLinkTarget = function (post) {
|
|
|
|
return !!post.url ? "_blank" : "";
|
|
|
|
};
|
|
|
|
Posts.helpers({getLinkTarget: function () {return Posts.getLinkTarget(this);}});
|
|
|
|
|
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
|
|
|
|
2015-09-06 11:37:48 +09:00
|
|
|
/**
|
|
|
|
* Check if a post is approved
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.isApproved = function (post) {
|
|
|
|
return post.status === Posts.config.STATUS_APPROVED;
|
|
|
|
};
|
|
|
|
Posts.helpers({isApproved: function () {return Posts.isApproved(this);}});
|
|
|
|
|
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}});
|
|
|
|
|
2015-09-06 11:37:48 +09:00
|
|
|
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 () {
|
2015-07-23 12:00:40 +09:00
|
|
|
return Posts.findOne(Router.current().data().post._id);
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
2015-07-27 15:15:38 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if a post is a link to a video
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.isVideo = function (post) {
|
|
|
|
return post.media && post.media.type === "video";
|
|
|
|
};
|
|
|
|
Posts.helpers({isVideo: function () {return Posts.isVideo(this);}});
|