2016-06-07 15:03:00 +09:00
|
|
|
import moment from 'moment';
|
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-07 15:03:00 +09:00
|
|
|
|
2015-06-19 11:52:57 +09:00
|
|
|
//////////////////
|
|
|
|
// Link Helpers //
|
|
|
|
//////////////////
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary 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
|
|
|
|
*/
|
2016-07-11 11:47:36 +09:00
|
|
|
Posts.getLink = function (post, isAbsolute = false, isRedirected = true) {
|
|
|
|
const url = isRedirected ? Telescope.utils.getOutgoingUrl(post.url) : post.url;
|
|
|
|
return !!post.url ? 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
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Depending on the settings, return either a post's URL link (if it has one) or its page URL.
|
2015-08-12 15:22:05 +09:00
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getShareableLink = function (post) {
|
2016-02-16 15:08:30 +09:00
|
|
|
return Telescope.settings.get("outsideLinksPointTo", "link") === "link" ? Posts.getLink(post) : Posts.getPageUrl(post, true);
|
2015-08-12 15:22:05 +09:00
|
|
|
};
|
|
|
|
Posts.helpers({getShareableLink: function () {return Posts.getShareableLink(this);}});
|
|
|
|
|
2015-08-01 12:49:44 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Whether a post's link should open in a new tab or not
|
2015-08-01 12:49:44 +09:00
|
|
|
* @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
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get URL of a post page.
|
2015-06-18 13:04:38 +09:00
|
|
|
* @param {Object} post
|
2015-04-22 07:50:11 +09:00
|
|
|
*/
|
2015-06-19 09:48:59 +09:00
|
|
|
Posts.getPageUrl = function(post, isAbsolute){
|
2016-06-14 10:01:44 +09:00
|
|
|
isAbsolute = typeof isAbsolute === "undefined" ? false : isAbsolute; // default to false
|
2015-06-19 09:48:59 +09:00
|
|
|
var prefix = isAbsolute ? Telescope.utils.getSiteUrl().slice(0,-1) : "";
|
2016-06-14 10:01:44 +09:00
|
|
|
return `${prefix}/posts/${post._id}/${post.slug}`;
|
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-19 11:52:57 +09:00
|
|
|
///////////////////
|
|
|
|
// Other Helpers //
|
|
|
|
///////////////////
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get a post author's name
|
2015-06-19 11:52:57 +09:00
|
|
|
* @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);}});
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get default status for new posts.
|
2015-06-19 11:52:57 +09:00
|
|
|
* @param {Object} user
|
|
|
|
*/
|
|
|
|
Posts.getDefaultStatus = function (user) {
|
|
|
|
var hasAdminRights = typeof user === 'undefined' ? false : Users.is.admin(user);
|
2016-02-16 15:08:30 +09:00
|
|
|
if (hasAdminRights || !Telescope.settings.get('requirePostsApproval', false)) {
|
2015-06-19 11:52:57 +09:00
|
|
|
// if user is admin, or else post approval is not required
|
2016-02-05 15:37:06 +09:00
|
|
|
return Posts.config.STATUS_APPROVED;
|
2015-06-19 11:52:57 +09:00
|
|
|
} else {
|
2016-02-05 15:37:06 +09:00
|
|
|
return Posts.config.STATUS_PENDING;
|
2015-06-19 11:52:57 +09:00
|
|
|
}
|
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-09-06 11:37:48 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Check if a post is approved
|
2015-09-06 11:37:48 +09:00
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.isApproved = function (post) {
|
2016-07-12 17:36:58 +09:00
|
|
|
return post.status === Posts.config.STATUS_APPROVED;
|
2015-09-06 11:37:48 +09:00
|
|
|
};
|
|
|
|
Posts.helpers({isApproved: function () {return Posts.isApproved(this);}});
|
|
|
|
|
2016-07-15 11:15:38 +09:00
|
|
|
/**
|
|
|
|
* @summary Check if a post is pending
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.isPending = function (post) {
|
|
|
|
return post.status === Posts.config.STATUS_PENDING;
|
|
|
|
};
|
|
|
|
Posts.helpers({isPending: function () {return Posts.isPending(this);}});
|
|
|
|
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Check to see if post URL is unique.
|
2015-04-22 07:50:11 +09:00
|
|
|
* We need the current user so we know who to upvote the existing post as.
|
|
|
|
* @param {String} url
|
|
|
|
*/
|
2015-09-19 10:33:37 +09:00
|
|
|
Posts.checkForSameUrl = function (url) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
// 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') {
|
2016-07-04 10:42:50 +09:00
|
|
|
throw new Meteor.Error('603', 'this_link_has_already_been_posted', postWithSameLink._id);
|
2015-04-22 07:50:11 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary When on a post page, return the current post
|
2015-04-22 07:50:11 +09:00
|
|
|
*/
|
|
|
|
Posts.current = function () {
|
2016-06-11 16:37:03 +09:00
|
|
|
return Posts.findOne("foo");
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
2015-07-27 15:15:38 +09:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Check to see if a post is a link to a video
|
2015-07-27 15:15:38 +09:00
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.isVideo = function (post) {
|
|
|
|
return post.media && post.media.type === "video";
|
|
|
|
};
|
|
|
|
Posts.helpers({isVideo: function () {return Posts.isVideo(this);}});
|
2016-02-17 21:57:07 +09:00
|
|
|
|
2016-04-11 10:36:01 +02:00
|
|
|
/**
|
|
|
|
* @summary Get the complete thumbnail url whether it is hosted on Embedly or on an external website, or locally in the app.
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
2016-04-07 14:34:13 +02:00
|
|
|
Posts.getThumbnailUrl = (post) => {
|
|
|
|
const thumbnailUrl = post.thumbnailUrl;
|
|
|
|
if (!!thumbnailUrl) {
|
2016-04-11 10:36:01 +02:00
|
|
|
return thumbnailUrl.indexOf('//') > -1 ? Telescope.utils.addHttp(thumbnailUrl) : Telescope.utils.getSiteUrl().slice(0,-1) + thumbnailUrl;
|
2016-04-07 14:34:13 +02:00
|
|
|
}
|
|
|
|
};
|
2016-06-23 11:40:35 +09:00
|
|
|
Posts.helpers({ getThumbnailUrl() { return Posts.getThumbnailUrl(this); } });
|
|
|
|
|
2016-07-11 11:47:36 +09:00
|
|
|
/**
|
|
|
|
* @summary Get URL for sharing on Twitter.
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getTwitterShareUrl = post => {
|
|
|
|
const via = Telescope.settings.get("twitterAccount", null) ? `&via=${Telescope.settings.get("twitterAccount")}` : "";
|
|
|
|
return `https://twitter.com/intent/tweet?text=${ encodeURIComponent(post.title) }%20${ encodeURIComponent(Posts.getLink(post, true)) }${via}`;
|
|
|
|
};
|
|
|
|
Posts.helpers({ getTwitterShareUrl() { return Posts.getTwitterShareUrl(this); } });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Get URL for sharing on Facebook.
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getFacebookShareUrl = post => {
|
|
|
|
return `https://www.facebook.com/sharer/sharer.php?u=${ encodeURIComponent(Posts.getLink(post, true)) }`;
|
|
|
|
};
|
|
|
|
Posts.helpers({ getFacebookShareUrl() { return Posts.getFacebookShareUrl(this); } });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Get URL for sharing by Email.
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getEmailShareUrl = post => {
|
|
|
|
const subject = `Interesting link: ${post.title}`;
|
|
|
|
const body = `I thought you might find this interesting:
|
|
|
|
|
|
|
|
${post.title}
|
|
|
|
${Posts.getLink(post, true, false)}
|
|
|
|
|
|
|
|
(found via ${Telescope.settings.get("siteUrl")})
|
|
|
|
`;
|
|
|
|
return `mailto:?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
|
|
|
|
};
|
|
|
|
Posts.helpers({ getEmailShareUrl() { return Posts.getEmailShareUrl(this); } });
|
|
|
|
|
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
///////////////////
|
|
|
|
// Users Helpers //
|
|
|
|
///////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Check if a given user can view a specific post
|
|
|
|
* @param {Object} user - can be undefined!
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Users.can.viewPost = function (user, post) {
|
|
|
|
|
|
|
|
if (Users.is.admin(user)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
switch (post.status) {
|
|
|
|
|
|
|
|
case Posts.config.STATUS_APPROVED:
|
|
|
|
return Users.can.view(user);
|
2016-07-12 17:36:58 +09:00
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
case Posts.config.STATUS_REJECTED:
|
|
|
|
case Posts.config.STATUS_SPAM:
|
2016-07-12 17:36:58 +09:00
|
|
|
case Posts.config.STATUS_PENDING:
|
2016-06-23 11:40:35 +09:00
|
|
|
return Users.can.view(user) && Users.is.owner(user, post);
|
2016-07-12 17:36:58 +09:00
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
case Posts.config.STATUS_DELETED:
|
|
|
|
return false;
|
2016-07-12 17:36:58 +09:00
|
|
|
|
2016-06-23 11:40:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Users.helpers({canViewPost: function () {return Users.can.viewPost(this, post);}});
|