Vulcan/packages/vulcan-posts/lib/helpers.js

172 lines
4.6 KiB
JavaScript
Raw Normal View History

import moment from 'moment';
2016-06-23 12:17:39 +09:00
import Posts from './collection.js';
2017-03-23 16:27:59 +09:00
import Users from 'meteor/vulcan:users';
import { Utils, getSetting } from 'meteor/vulcan:core';
2016-07-19 17:30:59 +09:00
//////////////////
// Link Helpers //
//////////////////
/**
* @summary Return a post's link if it has one, else return its post page URL
* @param {Object} post
*/
2016-07-11 11:47:36 +09:00
Posts.getLink = function (post, isAbsolute = false, isRedirected = true) {
2016-12-12 11:34:28 +09:00
const url = isRedirected ? Utils.getOutgoingUrl(post.url) : post.url;
return !!post.url ? url : Posts.getPageUrl(post, isAbsolute);
};
/**
* @summary 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) {
2016-12-12 15:00:56 +09:00
return getSetting("outsideLinksPointTo", "link") === "link" ? Posts.getLink(post) : Posts.getPageUrl(post, true);
};
/**
* @summary Whether a post's link should open in a new tab or not
* @param {Object} post
*/
Posts.getLinkTarget = function (post) {
return !!post.url ? "_blank" : "";
};
/**
* @summary Get URL of a post page.
2015-06-18 13:04:38 +09:00
* @param {Object} post
*/
Posts.getPageUrl = function(post, isAbsolute = false){
2016-12-12 11:34:28 +09:00
const prefix = isAbsolute ? Utils.getSiteUrl().slice(0,-1) : "";
2016-06-14 10:01:44 +09:00
return `${prefix}/posts/${post._id}/${post.slug}`;
};
///////////////////
// Other Helpers //
///////////////////
/**
* @summary Get a post author's name
* @param {Object} post
*/
Posts.getAuthorName = function (post) {
var user = Users.findOne(post.userId);
if (user) {
return Users.getDisplayName(user);
} else {
return post.author;
}
};
/**
* @summary Get default status for new posts.
* @param {Object} user
*/
Posts.getDefaultStatus = function (user) {
2016-07-21 09:53:58 +09:00
const canPostApproved = typeof user === 'undefined' ? false : Users.canDo(user, "posts.new.approved");
2016-12-12 15:00:56 +09:00
if (!getSetting('requirePostsApproval', false) || canPostApproved) {
2016-07-21 09:53:58 +09:00
// if user can post straight to "approved", or else post approval is not required
return Posts.config.STATUS_APPROVED;
} else {
return Posts.config.STATUS_PENDING;
}
};
/**
* @summary Get status name
* @param {Object} user
*/
Posts.getStatusName = function (post) {
2017-02-16 10:14:25 +01:00
return Utils.findWhere(Posts.statuses, {value: post.status}).label;
};
/**
* @summary Check if a post is approved
* @param {Object} post
*/
Posts.isApproved = function (post) {
2016-07-12 17:36:58 +09:00
return post.status === Posts.config.STATUS_APPROVED;
};
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;
};
/**
* @summary 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
*/
Posts.checkForSameUrl = function (url) {
// 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}});
return !!postWithSameLink;
};
/**
* @summary When on a post page, return the current post
*/
Posts.current = function () {
2016-06-11 16:37:03 +09:00
return Posts.findOne("foo");
};
/**
* @summary 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";
};
/**
* @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-12-12 11:34:28 +09:00
return thumbnailUrl.indexOf('//') > -1 ? Utils.addHttp(thumbnailUrl) : Utils.getSiteUrl().slice(0,-1) + thumbnailUrl;
2016-04-07 14:34:13 +02:00
}
};
2016-06-23 11:40:35 +09:00
2016-07-11 11:47:36 +09:00
/**
* @summary Get URL for sharing on Twitter.
* @param {Object} post
*/
Posts.getTwitterShareUrl = post => {
2016-12-12 15:00:56 +09:00
const via = getSetting("twitterAccount", null) ? `&via=${getSetting("twitterAccount")}` : "";
2016-07-11 11:47:36 +09:00
return `https://twitter.com/intent/tweet?text=${ encodeURIComponent(post.title) }%20${ encodeURIComponent(Posts.getLink(post, true)) }${via}`;
};
/**
* @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)) }`;
};
/**
* @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)}
2016-12-12 15:00:56 +09:00
(found via ${getSetting("siteUrl")})
2016-07-11 11:47:36 +09:00
`;
return `mailto:?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
};