diff --git a/inc/processJsonSubreddit.js b/inc/processJsonSubreddit.js
index cb9f058..8d8f57f 100644
--- a/inc/processJsonSubreddit.js
+++ b/inc/processJsonSubreddit.js
@@ -1,83 +1,90 @@
-module.exports = function() {
- const config = require('../config');
- 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
+const config = require('../config');
+const link = require('./components/link');
- let ret = {
- info: {
- before: before,
- after: after
- },
- links: []
- }
+async function processJsonSubreddit(
+ json,
+ from,
+ subreddit_front,
+ user_preferences,
+ 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 data = json.data.children[i].data
+ let ret = {
+ info: {
+ before: before,
+ after: after,
+ },
+ links: [],
+ };
- if(data.over_18)
- if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
- continue
+ let children_len = json.data.children.length;
- /*
- // Todo: Remove this once the link component is done
- // 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) : '')
- } */
+ for (var i = 0; i < children_len; i++) {
+ let data = json.data.children[i].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)
- }
- resolve(ret)
- }
- })()
- })
+ /*
+ // Todo: Remove this once the link component is done
+ // 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;
diff --git a/inc/teddit_api/handleSubreddit.js b/inc/teddit_api/handleSubreddit.js
index f135940..eea258b 100644
--- a/inc/teddit_api/handleSubreddit.js
+++ b/inc/teddit_api/handleSubreddit.js
@@ -1,114 +1,130 @@
-module.exports = function() {
- 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']
+const processJsonSubreddit = require('../processJsonSubreddit');
- 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)
+module.exports = function () {
+ 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) {
+ 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)) {
- is_self_link = true
- link.url = teddifyUrl(link.url)
+ if (
+ config.valid_media_domains.includes(link.domain) ||
+ valid_reddit_self_domains.includes(link.domain)
+ ) {
+ is_self_link = true;
+ link.url = teddifyUrl(link.url);
}
}
-
- 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}`
+
+ 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}`;
} 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}`
+ 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}`;
}
}
}
}
-
- 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
+
+ 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 = ``
+
+ let enclosure = '';
+ if (thumbnail != '') {
+ let mime = '';
+ let ext = thumbnail.split('.').pop();
+ if (ext === 'png') mime = 'image/png';
+ else mime = 'image/jpeg';
+ enclosure = ``;
}
-
- let append_desc_html = `
[link] [comments]`
-
+
+ let append_desc_html = `
[link] [comments]`;
+
items += `
-
${link.title}
${link.author}
${link.created}
- ${new Date(link.created_utc*1000).toGMTString()}
+ ${new Date(
+ link.created_utc * 1000
+ ).toGMTString()}
${link.domain}
${link.id}
${thumbnail}
${enclosure}
${link.permalink}
${link.url}
-
+
${link.num_comments}
${link.ups}
${link.stickied}
${is_self_link}
- `
+ `;
}
-
- 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}`
+
+ 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}`;
}
-
- let xml_output =
- `
+
+ let xml_output = `
@@ -117,50 +133,57 @@ module.exports = function() {
Subreddit feed for: ${r_subreddit}
${items}
- `
- res.setHeader('Content-Type', 'application/rss+xml')
- return res.end(xml_output)
+ `;
+ res.setHeader('Content-Type', 'application/rss+xml');
+ return res.end(xml_output);
} else {
- res.setHeader('Content-Type', 'application/json')
- if(api_target === 'reddit') {
- return res.end(JSON.stringify(json))
+ res.setHeader('Content-Type', 'application/json');
+ if (api_target === 'reddit') {
+ return res.end(JSON.stringify(json));
} else {
- let processed_json = await processJsonSubreddit(_json, from, null, req.cookies)
-
- let protocol = (config.https_enabled ? '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
+ let processed_json = await processJsonSubreddit(
+ _json,
+ from,
+ null,
+ req.cookies
+ );
- 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)
+ let protocol = config.https_enabled ? '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);
}
}
- if(config.valid_media_domains.includes(link.domain) || valid_reddit_self_domains.includes(link.domain)) {
- is_self_link = true
- link.url = teddifyUrl(link.url)
+ if (
+ config.valid_media_domains.includes(link.domain) ||
+ valid_reddit_self_domains.includes(link.domain)
+ ) {
+ is_self_link = true;
+ link.url = teddifyUrl(link.url);
}
}
-
- 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}`
+
+ 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}`;
}
}
}
-
- return res.end(JSON.stringify(processed_json))
- }
+
+ return res.end(JSON.stringify(processed_json));
+ }
}
- }
-}
+ };
+};
diff --git a/routes/home.js b/routes/home.js
index b0b5636..2997767 100644
--- a/routes/home.js
+++ b/routes/home.js
@@ -2,7 +2,7 @@ const config = require('../config');
const { redis, fetch } = require('../app');
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 processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
diff --git a/routes/save.js b/routes/save.js
index d5bc4cf..e09d23a 100644
--- a/routes/save.js
+++ b/routes/save.js
@@ -2,7 +2,7 @@ const config = require('../config');
const { redis, fetch } = require('../app');
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 processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
diff --git a/routes/subreddit.js b/routes/subreddit.js
index cc6f06a..ed0c97b 100644
--- a/routes/subreddit.js
+++ b/routes/subreddit.js
@@ -8,7 +8,7 @@ const {
} = require('../inc/processJsonPost.js');
const processSubredditAbout = require('../inc/processSubredditAbout.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 processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
diff --git a/routes/user.js b/routes/user.js
index 25eb2b0..bbdc8af 100644
--- a/routes/user.js
+++ b/routes/user.js
@@ -4,7 +4,7 @@ const userRoutes = require('express').Router();
const processJsonUser = require('../inc/processJsonUser.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 processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =