convert processJsonSubreddit to async function

This commit is contained in:
json 2021-09-08 12:03:35 +01:00
parent 153a50e60a
commit 6ff892767c
6 changed files with 225 additions and 195 deletions

View File

@ -1,83 +1,90 @@
module.exports = function() { const config = require('../config');
const config = require('../config'); const link = require('./components/link');
const link = require('./components/link')
this.processJsonSubreddit = (json, from, subreddit_front, user_preferences, saved) => {
return new Promise(resolve => {
(async () => {
if(from === 'redis') {
json = JSON.parse(json)
}
if(json.error) {
resolve({ error: true, error_data: json })
} else {
if(saved) {
let t = {
data: {
before: null,
after: null,
children: json
}
}
json = t
}
let before = json.data.before
let after = json.data.after
let ret = { async function processJsonSubreddit(
info: { json,
before: before, from,
after: after subreddit_front,
}, user_preferences,
links: [] saved
} ) {
if (from === 'redis') {
json = JSON.parse(json);
}
if (json.error) {
return { error: true, error_data: json };
} else {
if (saved) {
let t = {
data: {
before: null,
after: null,
children: json,
},
};
json = t;
}
let children_len = json.data.children.length let before = json.data.before;
let after = json.data.after;
for(var i = 0; i < children_len; i++) { let ret = {
let data = json.data.children[i].data info: {
before: before,
after: after,
},
links: [],
};
if(data.over_18) let children_len = json.data.children.length;
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
/* for (var i = 0; i < children_len; i++) {
// Todo: Remove this once the link component is done let data = json.data.children[i].data;
// but keep it for now in case we need it later
let obj = {
author: data.author,
created: data.created_utc,
domain: data.domain,
id: data.id,
images: images,
is_video: data.is_video,
link_flair_text: data.link_flair_text,
locked: data.locked,
media: data.media,
selftext_html: data.selftext_html,
num_comments: data.num_comments,
over_18: data.over_18,
permalink: data.permalink,
score: data.score,
subreddit: data.subreddit,
title: data.title,
ups: data.ups,
upvote_ratio: data.upvote_ratio,
url: replaceDomains(data.url, user_preferences),
stickied: data.stickied,
is_self_link: is_self_link,
subreddit_front: subreddit_front,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
} */
let obj = await link.fromJson(data, user_preferences, subreddit_front) if (data.over_18)
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
ret.links.push(obj) /*
} // Todo: Remove this once the link component is done
resolve(ret) // but keep it for now in case we need it later
} let obj = {
})() author: data.author,
}) created: data.created_utc,
domain: data.domain,
id: data.id,
images: images,
is_video: data.is_video,
link_flair_text: data.link_flair_text,
locked: data.locked,
media: data.media,
selftext_html: data.selftext_html,
num_comments: data.num_comments,
over_18: data.over_18,
permalink: data.permalink,
score: data.score,
subreddit: data.subreddit,
title: data.title,
ups: data.ups,
upvote_ratio: data.upvote_ratio,
url: replaceDomains(data.url, user_preferences),
stickied: data.stickied,
is_self_link: is_self_link,
subreddit_front: subreddit_front,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
} */
let obj = await link.fromJson(data, user_preferences, subreddit_front);
ret.links.push(obj);
}
return ret;
} }
} }
module.exports = processJsonSubreddit;

View File

@ -1,114 +1,130 @@
module.exports = function() { const processJsonSubreddit = require('../processJsonSubreddit');
const config = require('../../config')
this.handleTedditApiSubreddit = async (json, req, res, from, api_type, api_target, subreddit) => {
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))
}
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 ? '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) { module.exports = function () {
let tld = link.domain.split('self.') const config = require('../../config');
if(tld.length > 1) { this.handleTedditApiSubreddit = async (
if(!tld[1].includes('.')) { json,
is_self_link = true req,
link.url = teddifyUrl(link.url) res,
from,
api_type,
api_target,
subreddit
) => {
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));
}
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 ? '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);
} }
} }
if(config.valid_media_domains.includes(link.domain) || valid_reddit_self_domains.includes(link.domain)) { if (
is_self_link = true config.valid_media_domains.includes(link.domain) ||
link.url = teddifyUrl(link.url) valid_reddit_self_domains.includes(link.domain)
) {
is_self_link = true;
link.url = teddifyUrl(link.url);
} }
} }
if(link.preview && link.thumbnail !== 'self') { if (link.preview && link.thumbnail !== 'self') {
if(!link.url.startsWith('/r/') && isGif(link.url)) { if (!link.url.startsWith('/r/') && isGif(link.url)) {
let s = await downloadAndSave(link.thumbnail, 'thumb_') let s = await downloadAndSave(link.thumbnail, 'thumb_');
thumbnail = `${protocol}://${config.domain}${s}` thumbnail = `${protocol}://${config.domain}${s}`;
} else { } else {
if(link.preview.images[0].resolutions[0]) { if (link.preview.images[0].resolutions[0]) {
let s = await downloadAndSave(link.preview.images[0].resolutions[0].url, 'thumb_') let s = await downloadAndSave(
thumbnail = `${protocol}://${config.domain}${s}` link.preview.images[0].resolutions[0].url,
if(!isGif(link.url) && !link.post_hint.includes(':video')) { 'thumb_'
s = await downloadAndSave(link.preview.images[0].source.url) );
post_image = `${protocol}://${config.domain}${s}` 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}`;
} }
} }
} }
} }
link.permalink = `${protocol}://${config.domain}${link.permalink}` link.permalink = `${protocol}://${config.domain}${link.permalink}`;
if(is_self_link) if (is_self_link) link.url = link.permalink;
link.url = link.permalink
if (req.query.hasOwnProperty('full_thumbs')) {
if(req.query.hasOwnProperty('full_thumbs')) { if (!post_image) post_image = thumbnail;
if(!post_image)
post_image = thumbnail thumbnail = post_image;
thumbnail = post_image
} }
let enclosure = '' let enclosure = '';
if(thumbnail != '') { if (thumbnail != '') {
let mime = '' let mime = '';
let ext = thumbnail.split('.').pop() let ext = thumbnail.split('.').pop();
if(ext === 'png') if (ext === 'png') mime = 'image/png';
mime = 'image/png' else mime = 'image/jpeg';
else enclosure = `<enclosure length="0" type="${mime}" url="${thumbnail}" />`;
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>` let append_desc_html = `<br/><a href="${link.url}">[link]</a> <a href="${link.permalink}">[comments]</a>`;
items += ` items += `
<item> <item>
<title>${link.title}</title> <title>${link.title}</title>
<author>${link.author}</author> <author>${link.author}</author>
<created>${link.created}</created> <created>${link.created}</created>
<pubDate>${new Date(link.created_utc*1000).toGMTString()}</pubDate> <pubDate>${new Date(
link.created_utc * 1000
).toGMTString()}</pubDate>
<domain>${link.domain}</domain> <domain>${link.domain}</domain>
<id>${link.id}</id> <id>${link.id}</id>
<thumbnail>${thumbnail}</thumbnail> <thumbnail>${thumbnail}</thumbnail>
${enclosure} ${enclosure}
<link>${link.permalink}</link> <link>${link.permalink}</link>
<url>${link.url}</url> <url>${link.url}</url>
<description><![CDATA[${unescape(link.selftext_html)}${append_desc_html}]]></description> <description><![CDATA[${unescape(
link.selftext_html
)}${append_desc_html}]]></description>
<num_comments>${link.num_comments}</num_comments> <num_comments>${link.num_comments}</num_comments>
<ups>${link.ups}</ups> <ups>${link.ups}</ups>
<stickied>${link.stickied}</stickied> <stickied>${link.stickied}</stickied>
<is_self_link>${is_self_link}</is_self_link> <is_self_link>${is_self_link}</is_self_link>
</item> </item>
` `;
} }
let r_subreddit = '/r/' + subreddit let r_subreddit = '/r/' + subreddit;
let title = r_subreddit let title = r_subreddit;
let link = `${protocol}://${config.domain}${r_subreddit}` let link = `${protocol}://${config.domain}${r_subreddit}`;
if(subreddit === '/') { if (subreddit === '/') {
r_subreddit = 'frontpage' r_subreddit = 'frontpage';
title = 'teddit frontpage' title = 'teddit frontpage';
link = `${protocol}://${config.domain}` link = `${protocol}://${config.domain}`;
} }
let xml_output = let xml_output = `<?xml version="1.0" encoding="UTF-8"?>
`<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel> <channel>
<atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" /> <atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" />
@ -117,50 +133,57 @@ module.exports = function() {
<description>Subreddit feed for: ${r_subreddit}</description> <description>Subreddit feed for: ${r_subreddit}</description>
${items} ${items}
</channel> </channel>
</rss>` </rss>`;
res.setHeader('Content-Type', 'application/rss+xml') res.setHeader('Content-Type', 'application/rss+xml');
return res.end(xml_output) return res.end(xml_output);
} else { } else {
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json');
if(api_target === 'reddit') { if (api_target === 'reddit') {
return res.end(JSON.stringify(json)) return res.end(JSON.stringify(json));
} else { } else {
let processed_json = await processJsonSubreddit(_json, from, null, req.cookies) let processed_json = await processJsonSubreddit(
_json,
let protocol = (config.https_enabled ? 'https' : 'http') from,
for(var i = 0; i < processed_json.links.length; i++) { null,
let link = processed_json.links[i] req.cookies
let valid_reddit_self_domains = ['reddit.com'] );
let is_self_link = false
if(link.domain) { let protocol = config.https_enabled ? 'https' : 'http';
let tld = link.domain.split('self.') for (var i = 0; i < processed_json.links.length; i++) {
if(tld.length > 1) { let link = processed_json.links[i];
if(!tld[1].includes('.')) { let valid_reddit_self_domains = ['reddit.com'];
is_self_link = true let is_self_link = false;
link.url = teddifyUrl(link.url)
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);
} }
} }
if(config.valid_media_domains.includes(link.domain) || valid_reddit_self_domains.includes(link.domain)) { if (
is_self_link = true config.valid_media_domains.includes(link.domain) ||
link.url = teddifyUrl(link.url) valid_reddit_self_domains.includes(link.domain)
) {
is_self_link = true;
link.url = teddifyUrl(link.url);
} }
} }
link.permalink = `${protocol}://${config.domain}${link.permalink}` link.permalink = `${protocol}://${config.domain}${link.permalink}`;
if(is_self_link) if (is_self_link) link.url = link.permalink;
link.url = link.permalink
if (link.images) {
if(link.images) { if (link.images.thumb !== 'self') {
if(link.images.thumb !== 'self') { link.images.thumb = `${protocol}://${config.domain}${link.images.thumb}`;
link.images.thumb = `${protocol}://${config.domain}${link.images.thumb}`
} }
} }
} }
return res.end(JSON.stringify(processed_json)) return res.end(JSON.stringify(processed_json));
} }
} }
} };
} };

View File

@ -2,7 +2,7 @@ const config = require('../config');
const { redis, fetch } = require('../app'); const { redis, fetch } = require('../app');
const homeRoute = require('express').Router(); const homeRoute = require('express').Router();
const processSubreddit = require('../inc/processJsonSubreddit.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore = const processSubredditsExplore =

View File

@ -2,7 +2,7 @@ const config = require('../config');
const { redis, fetch } = require('../app'); const { redis, fetch } = require('../app');
const saveRoutes = require('express').Router(); const saveRoutes = require('express').Router();
const processSubreddit = require('../inc/processJsonSubreddit.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore = const processSubredditsExplore =

View File

@ -8,7 +8,7 @@ const {
} = require('../inc/processJsonPost.js'); } = require('../inc/processJsonPost.js');
const processSubredditAbout = require('../inc/processSubredditAbout.js'); const processSubredditAbout = require('../inc/processSubredditAbout.js');
const processSearchResults = require('../inc/processSearchResults.js'); const processSearchResults = require('../inc/processSearchResults.js');
const processSubreddit = require('../inc/processJsonSubreddit.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore = const processSubredditsExplore =

View File

@ -4,7 +4,7 @@ const userRoutes = require('express').Router();
const processJsonUser = require('../inc/processJsonUser.js'); const processJsonUser = require('../inc/processJsonUser.js');
const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')();
const processSubreddit = require('../inc/processJsonSubreddit.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore = const processSubredditsExplore =