2019-07-25 12:08:29 +03:00
|
|
|
const _ = require('lodash');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../shared/url-utils');
|
2019-07-25 12:08:29 +03:00
|
|
|
const getContextObject = require('./context_object.js');
|
|
|
|
const settingsCache = require('../../server/services/settings/cache');
|
2017-08-03 14:48:39 +03:00
|
|
|
|
|
|
|
function getTwitterImage(data) {
|
2019-07-25 12:08:29 +03:00
|
|
|
const context = data.context ? data.context : null;
|
2019-11-21 16:08:00 +03:00
|
|
|
const contextObject = getContextObject(data, context, false);
|
|
|
|
|
|
|
|
if (_.includes(context, 'home')) {
|
|
|
|
const imgUrl = settingsCache.get('twitter_image') || settingsCache.get('cover_image');
|
|
|
|
return (imgUrl && urlUtils.relativeToAbsolute(imgUrl)) || null;
|
|
|
|
}
|
2017-08-03 14:48:39 +03:00
|
|
|
|
|
|
|
if (_.includes(context, 'post') || _.includes(context, 'page') || _.includes(context, 'amp')) {
|
|
|
|
if (contextObject.twitter_image) {
|
2019-11-21 16:08:00 +03:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.twitter_image);
|
2017-08-03 14:48:39 +03:00
|
|
|
} else if (contextObject.feature_image) {
|
2019-11-21 16:08:00 +03:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.feature_image);
|
2017-08-03 14:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:08:00 +03:00
|
|
|
if (_.includes(context, 'author') && contextObject.cover_image) {
|
|
|
|
return urlUtils.relativeToAbsolute(contextObject.cover_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.includes(context, 'tag')) {
|
2020-07-10 14:52:48 +03:00
|
|
|
if (contextObject.twitter_image) {
|
|
|
|
return urlUtils.relativeToAbsolute(contextObject.twitter_image);
|
|
|
|
} else if (contextObject.feature_image) {
|
2019-11-21 16:08:00 +03:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.feature_image);
|
|
|
|
} else if (settingsCache.get('cover_image')) {
|
|
|
|
return urlUtils.relativeToAbsolute(settingsCache.get('cover_image'));
|
|
|
|
}
|
2019-07-25 12:08:29 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 14:48:39 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getTwitterImage;
|