2019-07-25 04:08:29 -05:00
|
|
|
const _ = require('lodash');
|
2020-05-28 05:57:02 -05:00
|
|
|
const urlUtils = require('../../shared/url-utils');
|
2021-07-01 06:30:23 -05:00
|
|
|
const getContextObject = require('./context-object.js');
|
2021-06-30 08:56:57 -05:00
|
|
|
const settingsCache = require('../../shared/settings-cache');
|
2017-08-03 06:48:39 -05:00
|
|
|
|
|
|
|
function getTwitterImage(data) {
|
2019-07-25 04:08:29 -05:00
|
|
|
const context = data.context ? data.context : null;
|
2019-11-21 08:08:00 -05: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 06:48:39 -05:00
|
|
|
|
|
|
|
if (_.includes(context, 'post') || _.includes(context, 'page') || _.includes(context, 'amp')) {
|
|
|
|
if (contextObject.twitter_image) {
|
2019-11-21 08:08:00 -05:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.twitter_image);
|
2017-08-03 06:48:39 -05:00
|
|
|
} else if (contextObject.feature_image) {
|
2019-11-21 08:08:00 -05:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.feature_image);
|
2021-02-05 00:50:11 -05:00
|
|
|
} else if (settingsCache.get('twitter_image')) {
|
|
|
|
return urlUtils.relativeToAbsolute(settingsCache.get('twitter_image'));
|
|
|
|
} else if (settingsCache.get('cover_image')) {
|
|
|
|
return urlUtils.relativeToAbsolute(settingsCache.get('cover_image'));
|
2017-08-03 06:48:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:08:00 -05:00
|
|
|
if (_.includes(context, 'author') && contextObject.cover_image) {
|
|
|
|
return urlUtils.relativeToAbsolute(contextObject.cover_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.includes(context, 'tag')) {
|
2020-07-10 06:52:48 -05:00
|
|
|
if (contextObject.twitter_image) {
|
|
|
|
return urlUtils.relativeToAbsolute(contextObject.twitter_image);
|
|
|
|
} else if (contextObject.feature_image) {
|
2019-11-21 08:08:00 -05:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.feature_image);
|
|
|
|
} else if (settingsCache.get('cover_image')) {
|
|
|
|
return urlUtils.relativeToAbsolute(settingsCache.get('cover_image'));
|
|
|
|
}
|
2019-07-25 04:08:29 -05:00
|
|
|
}
|
|
|
|
|
2017-08-03 06:48:39 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getTwitterImage;
|