teddit-reddit-frontend-alte.../inc/teddit_api/handleSubreddit.js

195 lines
6.5 KiB
JavaScript
Raw Normal View History

const processJsonSubreddit = require('../processJsonSubreddit');
module.exports = function () {
const config = require('../../config');
this.handleTedditApiSubreddit = async (
json,
req,
res,
from,
api_type,
api_target,
2021-12-24 15:37:46 +01:00
subreddit,
mode
) => {
if (!config.api_enabled) {
res.setHeader('Content-Type', 'application/json');
let msg = {
info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.',
};
return res.end(JSON.stringify(msg));
2020-12-19 20:52:22 +01:00
}
console.log('Teddit API request - subreddit');
let _json = json; // Keep the original json
if (from === 'redis') json = JSON.parse(json);
if (api_type === 'rss') {
let protocol = config.https_enabled || config.api_force_https ? 'https' : 'http';
let items = '';
for (var i = 0; i < json.data.children.length; i++) {
let link = json.data.children[i].data;
let thumbnail = '';
let post_image = '';
let is_self_link = false;
let valid_reddit_self_domains = ['reddit.com'];
if (link.domain) {
let tld = link.domain.split('self.');
if (tld.length > 1) {
if (!tld[1].includes('.')) {
is_self_link = true;
link.url = teddifyUrl(link.url);
2020-12-19 20:52:22 +01:00
}
}
if (
config.valid_media_domains.includes(link.domain) ||
valid_reddit_self_domains.includes(link.domain)
) {
is_self_link = true;
link.url = teddifyUrl(link.url);
2020-12-19 20:52:22 +01:00
}
}
if (link.preview && link.thumbnail !== 'self') {
if (!link.url.startsWith('/r/') && isGif(link.url)) {
let s = await downloadAndSave(link.thumbnail, 'thumb_');
thumbnail = `${protocol}://${config.domain}${s}`;
2020-12-19 20:52:22 +01:00
} else {
if (link.preview.images[0].resolutions[0]) {
let s = await downloadAndSave(
link.preview.images[0].resolutions[0].url,
'thumb_'
);
thumbnail = `${protocol}://${config.domain}${s}`;
if (!isGif(link.url) && !link.post_hint.includes(':video')) {
s = await downloadAndSave(link.preview.images[0].source.url);
post_image = `${protocol}://${config.domain}${s}`;
}
2020-12-19 20:52:22 +01:00
}
}
}
link.permalink = `${protocol}://${config.domain}${link.permalink}`;
if (is_self_link) link.url = link.permalink;
if (req.query.hasOwnProperty('full_thumbs')) {
if (!post_image) post_image = thumbnail;
thumbnail = post_image;
}
let enclosure = '';
if (thumbnail != '') {
let mime = '';
let ext = thumbnail.split('.').pop();
if (ext === 'png') mime = 'image/png';
else mime = 'image/jpeg';
enclosure = `<enclosure length="0" type="${mime}" url="${thumbnail}" />`;
}
let append_desc_html = `<br/><a href="${link.url}">[link]</a> <a href="${link.permalink}">[comments]</a>`;
2020-12-19 20:52:22 +01:00
items += `
<item>
<title>${link.title}</title>
<author>${link.author}</author>
<created>${link.created}</created>
<pubDate>${new Date(
link.created_utc * 1000
).toGMTString()}</pubDate>
2020-12-19 20:52:22 +01:00
<domain>${link.domain}</domain>
<id>${link.id}</id>
<thumbnail>${thumbnail}</thumbnail>
${enclosure}
2020-12-22 16:23:27 +01:00
<link>${link.permalink}</link>
2020-12-19 20:52:22 +01:00
<url>${link.url}</url>
<description><![CDATA[${unescape(
link.selftext_html
)}${append_desc_html}]]></description>
2020-12-19 20:52:22 +01:00
<num_comments>${link.num_comments}</num_comments>
<ups>${link.ups}</ups>
<stickied>${link.stickied}</stickied>
<is_self_link>${is_self_link}</is_self_link>
</item>
`;
2020-12-19 20:52:22 +01:00
}
let r_subreddit = '/r/' + subreddit;
let title = r_subreddit;
let link = `${protocol}://${config.domain}${r_subreddit}`;
if (subreddit === '/') {
r_subreddit = 'frontpage';
title = 'teddit frontpage';
link = `${protocol}://${config.domain}`;
2020-12-19 20:52:22 +01:00
}
let xml_output = `<?xml version="1.0" encoding="UTF-8"?>
2020-12-19 20:52:22 +01:00
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" />
<title>${title}</title>
<link>${link}</link>
<description>Subreddit feed for: ${r_subreddit}</description>
${items}
</channel>
</rss>`;
res.setHeader('Content-Type', 'application/rss+xml');
return res.end(xml_output);
2020-12-19 20:52:22 +01:00
} else {
res.setHeader('Content-Type', 'application/json');
if (api_target === 'reddit') {
return res.end(JSON.stringify(json));
2020-12-19 20:52:22 +01:00
} else {
let processed_json = await processJsonSubreddit(
_json,
from,
null,
req.cookies
);
let protocol = config.https_enabled || config.api_force_https ? 'https' : 'http';
for (var i = 0; i < processed_json.links.length; i++) {
let link = processed_json.links[i];
let valid_reddit_self_domains = ['reddit.com'];
let is_self_link = false;
if (link.domain) {
let tld = link.domain.split('self.');
if (tld.length > 1) {
if (!tld[1].includes('.')) {
is_self_link = true;
link.url = teddifyUrl(link.url);
2020-12-19 20:52:22 +01:00
}
}
if (
config.valid_media_domains.includes(link.domain) ||
valid_reddit_self_domains.includes(link.domain)
) {
is_self_link = true;
link.url = teddifyUrl(link.url);
2020-12-19 20:52:22 +01:00
}
}
link.permalink = `${protocol}://${config.domain}${link.permalink}`;
if (is_self_link) link.url = link.permalink;
if (link.images) {
if (link.images.thumb !== 'self') {
link.images.thumb = `${protocol}://${config.domain}${link.images.thumb}`;
2020-12-19 20:52:22 +01:00
}
}
2021-12-28 22:58:34 +01:00
if (mode === 'light') {
link.selftext_html = null;
}
2021-12-24 15:37:46 +01:00
}
2021-12-28 22:58:34 +01:00
return res.end(JSON.stringify(processed_json));
}
2020-12-19 20:52:22 +01:00
}
};
};