2017-03-23 16:27:59 +09:00
import Posts from "meteor/vulcan:posts" ;
import { addCallback , getSetting } from 'meteor/vulcan:core' ;
2016-06-23 11:40:35 +09:00
2016-12-06 15:51:59 +09:00
function getEmbedlyData ( url ) {
2015-04-22 07:50:11 +09:00
var extractBase = 'http://api.embed.ly/1/extract' ;
2016-12-12 15:00:56 +09:00
var embedlyKey = getSetting ( 'embedlyKey' ) ;
2017-03-27 12:02:54 -04:00
// 200 x 200 is the minimum size accepted by facebook
2016-12-12 15:00:56 +09:00
var thumbnailWidth = getSetting ( 'thumbnailWidth' , 200 ) ;
2017-03-24 16:23:58 -04:00
var thumbnailHeight = getSetting ( 'thumbnailHeight' , 200 ) ;
2015-04-22 07:50:11 +09:00
if ( ! embedlyKey ) {
// fail silently to still let the post be submitted as usual
2016-11-26 02:46:55 +08:00
console . log ( "Couldn't find an Embedly API key! Please add it to your Telescope settings or remove the Embedly module." ) ; // eslint-disable-line
2015-04-22 07:50:11 +09:00
return null ;
}
try {
var result = Meteor . http . get ( extractBase , {
params : {
key : embedlyKey ,
url : url ,
image _width : thumbnailWidth ,
image _height : thumbnailHeight ,
image _method : 'crop'
}
} ) ;
if ( ! ! result . data . images && ! ! result . data . images . length ) // there may not always be an image
2017-03-26 19:37:55 -04:00
result . data . thumbnailUrl = result . data . images [ 0 ] . url // add thumbnailUrl as its own property and leave "http" in, Facebook scraper won't accept it as valid otherwise
2015-04-22 07:50:11 +09:00
2015-08-24 12:26:20 +09:00
if ( result . data . authors && result . data . authors . length > 0 ) {
result . data . sourceName = result . data . authors [ 0 ] . name ;
result . data . sourceUrl = result . data . authors [ 0 ] . url ;
}
var embedlyData = _ . pick ( result . data , 'title' , 'media' , 'description' , 'thumbnailUrl' , 'sourceName' , 'sourceUrl' ) ;
return embedlyData ;
2015-04-22 07:50:11 +09:00
} catch ( error ) {
2016-11-26 02:46:55 +08:00
console . log ( error ) ; // eslint-disable-line
2015-04-22 07:50:11 +09:00
// the first 13 characters of the Embedly errors are "failed [400] ", so remove them and parse the rest
var errorObject = JSON . parse ( error . message . substring ( 13 ) ) ;
2017-02-02 15:15:51 +01:00
throw new Error ( errorObject . error _code , errorObject . error _message ) ;
2015-04-22 07:50:11 +09:00
}
}
2015-08-24 12:26:20 +09:00
// For security reason, we make the media property non-modifiable by the client and
// we use a separate server-side API call to set it (and the thumbnail object if it hasn't already been set)
2015-04-22 07:50:11 +09:00
// Async variant that directly modifies the post object with update()
2015-05-04 12:32:00 +09:00
function addMediaAfterSubmit ( post ) {
2015-04-22 07:50:11 +09:00
var set = { } ;
if ( post . url ) {
var data = getEmbedlyData ( post . url ) ;
if ( ! ! data ) {
// only add a thumbnailUrl if there isn't one already
if ( ! post . thumbnailUrl && ! ! data . thumbnailUrl ) {
set . thumbnailUrl = data . thumbnailUrl ;
}
// add media if necessary
if ( ! ! data . media . html ) {
set . media = data . media ;
}
2015-08-24 12:26:20 +09:00
// add source name & url if they exist
if ( ! ! data . sourceName && ! ! data . sourceUrl ) {
set . sourceName = data . sourceName ;
set . sourceUrl = data . sourceUrl ;
}
2015-04-22 07:50:11 +09:00
}
// make sure set object is not empty (Embedly call could have failed)
if ( ! _ . isEmpty ( set ) ) {
Posts . update ( post . _id , { $set : set } ) ;
}
}
2015-08-24 12:26:20 +09:00
}
2016-12-13 11:40:24 +09:00
addCallback ( "posts.new.async" , addMediaAfterSubmit ) ;
2015-04-22 07:50:11 +09:00
2015-05-04 10:24:39 +09:00
function updateMediaOnEdit ( modifier , post ) {
var newUrl = modifier . $set . url ;
if ( newUrl && newUrl !== post . url ) {
var data = getEmbedlyData ( newUrl ) ;
2015-08-24 12:26:20 +09:00
if ( ! ! data ) {
if ( ! ! data . media . html ) {
2017-01-23 15:50:55 +01:00
if ( modifier . $unset . media ) {
delete modifier . $unset . media
}
2015-08-24 12:26:20 +09:00
modifier . $set . media = data . media ;
}
// add source name & url if they exist
if ( ! ! data . sourceName && ! ! data . sourceUrl ) {
modifier . $set . sourceName = data . sourceName ;
modifier . $set . sourceUrl = data . sourceUrl ;
}
2015-05-04 10:19:50 +09:00
}
2015-04-22 07:50:11 +09:00
}
2015-05-04 10:24:39 +09:00
return modifier ;
2015-04-22 07:50:11 +09:00
}
2016-12-13 11:40:24 +09:00
addCallback ( "posts.edit.sync" , updateMediaOnEdit ) ;
2015-04-22 07:50:11 +09:00
2015-10-01 16:13:54 +09:00
var regenerateThumbnail = function ( post ) {
delete post . thumbnailUrl ;
delete post . media ;
delete post . sourceName ;
delete post . sourceUrl ;
addMediaAfterSubmit ( post ) ;
} ;
2015-04-22 07:50:11 +09:00
2016-12-06 15:51:59 +09:00
export { getEmbedlyData , addMediaAfterSubmit , updateMediaOnEdit , regenerateThumbnail }