2019-07-25 12:08:29 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const getContextObject = require('./context_object.js');
|
|
|
|
const urlUtils = require('../../server/lib/url-utils');
|
|
|
|
const settingsCache = require('../../server/services/settings/cache');
|
2017-08-03 14:48:39 +03:00
|
|
|
|
|
|
|
function getOgImage(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('og_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.og_image) {
|
2019-11-21 16:08:00 +03:00
|
|
|
return urlUtils.relativeToAbsolute(contextObject.og_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')) {
|
|
|
|
if (contextObject.feature_image) {
|
|
|
|
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 = getOgImage;
|