From 7f347d13f728ea0a4f578a784052f5cab629d6d4 Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 13:11:57 +0100 Subject: [PATCH 01/15] add prettierrc --- .prettierrc | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..92cde39 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "singleQuote": true +} \ No newline at end of file From d1ef359435921bba4e38bc256f06d7008e279b8c Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 13:19:28 +0100 Subject: [PATCH 02/15] remove unused processJsonUser function --- routes/home.js | 1 - routes/save.js | 1 - routes/subreddit.js | 1 - 3 files changed, 3 deletions(-) diff --git a/routes/home.js b/routes/home.js index ceb5800..40d78fe 100644 --- a/routes/home.js +++ b/routes/home.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const homeRoute = require('express').Router(); -const processUser = require('../inc/processJsonUser.js')(); const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); diff --git a/routes/save.js b/routes/save.js index 6a8cdbf..b43ee77 100644 --- a/routes/save.js +++ b/routes/save.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const saveRoutes = require('express').Router(); -const processUser = require('../inc/processJsonUser.js')(); const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); diff --git a/routes/subreddit.js b/routes/subreddit.js index a8830dc..2a357af 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch, RedditAPI } = require('../app'); const subredditRoutes = require('express').Router(); -const processUser = require('../inc/processJsonUser.js')(); const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); From f3cc837a01e11652a279e1d60b1bfb0e37a4f67c Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 13:27:29 +0100 Subject: [PATCH 03/15] convert processJsonUser to async function --- inc/processJsonUser.js | 197 ++++++++++++++++++----------------- inc/teddit_api/handleUser.js | 173 ++++++++++++++++-------------- routes/user.js | 2 +- 3 files changed, 200 insertions(+), 172 deletions(-) diff --git a/inc/processJsonUser.js b/inc/processJsonUser.js index 83c8fce..86dc084 100644 --- a/inc/processJsonUser.js +++ b/inc/processJsonUser.js @@ -1,97 +1,106 @@ -module.exports = function() { - const config = require('../config'); - const link = require('./components/link') - this.processJsonUser = function(json, parsed, after, before, user_preferences, kind, post_type) { - return new Promise(resolve => { - (async () => { - if(!parsed) { - json = JSON.parse(json) - } +const config = require('../config'); +const link = require('./components/link'); - let about = json.about.data - let posts = [] - let view_more_posts = false - let posts_limit = 25 - let user_front = false - - if(json.overview.data.children.length > posts_limit) { - view_more_posts = true - } else { - posts_limit = json.overview.data.children.length - } - - if(!after && !before) { - user_front = true - } - - if(json.overview.data.children) { - if(json.overview.data.children[posts_limit - 1]) { - after = json.overview.data.children[posts_limit - 1].data.name - } - if(json.overview.data.children[0]) { - before = json.overview.data.children[0].data.name - } - } - - for(var i = 0; i < posts_limit; i++) { - let post = json.overview.data.children[i].data - let thumbnail = 'self' - let type = json.overview.data.children[i].kind - let obj - - let post_id = post.permalink.split('/').slice(-2)[0] + '/' - let url = post.permalink.replace(post_id, '') - - if(type !== kind && kind) - continue - - if(post.over_18) - if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') - continue - - if(type === 't3') { - obj = await link.fromJson(post, user_preferences) - obj.type = 't3' - } - if(type === 't1') { - obj = { - type: type, - subreddit: post.subreddit, - title: post.title, - created: post.created_utc, - subreddit_name_prefixed: post.subreddit_name_prefixed, - ups: post.ups, - url: replaceDomains(url, user_preferences), - edited: post.edited, - body_html: unescape(post.body_html), - num_comments: post.num_comments, - over_18: post.over_18, - permalink: post.permalink, - link_author: post.link_author, - link_title: post.link_title, - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') - } - } - posts.push(obj) - } - - let obj = { - username: about.name, - icon_img: await downloadAndSave(about.icon_img, "icon_"), - created: about.created_utc, - verified: about.verified, - link_karma: about.link_karma, - comment_karma: about.comment_karma, - view_more_posts: view_more_posts, - user_front: user_front, - post_type: post_type, - before: before, - after: after, - posts: posts - } - - resolve(obj) - })() - }) +async function processJsonUser( + json, + parsed, + after, + before, + user_preferences, + kind, + post_type +) { + if (!parsed) { + json = JSON.parse(json); } + + let about = json.about.data; + let posts = []; + let view_more_posts = false; + let posts_limit = 25; + let user_front = false; + + if (json.overview.data.children.length > posts_limit) { + view_more_posts = true; + } else { + posts_limit = json.overview.data.children.length; + } + + if (!after && !before) { + user_front = true; + } + + if (json.overview.data.children) { + if (json.overview.data.children[posts_limit - 1]) { + after = json.overview.data.children[posts_limit - 1].data.name; + } + if (json.overview.data.children[0]) { + before = json.overview.data.children[0].data.name; + } + } + + for (var i = 0; i < posts_limit; i++) { + let post = json.overview.data.children[i].data; + let thumbnail = 'self'; + let type = json.overview.data.children[i].kind; + let obj; + + let post_id = post.permalink.split('/').slice(-2)[0] + '/'; + let url = post.permalink.replace(post_id, ''); + + if (type !== kind && kind) continue; + + if (post.over_18) + if ( + (config.nsfw_enabled === false && + user_preferences.nsfw_enabled != 'true') || + user_preferences.nsfw_enabled === 'false' + ) + continue; + + if (type === 't3') { + obj = await link.fromJson(post, user_preferences); + obj.type = 't3'; + } + if (type === 't1') { + obj = { + type: type, + subreddit: post.subreddit, + title: post.title, + created: post.created_utc, + subreddit_name_prefixed: post.subreddit_name_prefixed, + ups: post.ups, + url: replaceDomains(url, user_preferences), + edited: post.edited, + body_html: unescape(post.body_html), + num_comments: post.num_comments, + over_18: post.over_18, + permalink: post.permalink, + link_author: post.link_author, + link_title: post.link_title, + user_flair: + user_preferences.flairs != 'false' ? await formatUserFlair(post) : '', + }; + } + posts.push(obj); + } + + let obj = { + username: about.name, + icon_img: await downloadAndSave(about.icon_img, 'icon_'), + created: about.created_utc, + verified: about.verified, + link_karma: about.link_karma, + comment_karma: about.comment_karma, + view_more_posts: view_more_posts, + user_front: user_front, + post_type: post_type, + before: before, + after: after, + posts: posts, + }; + + return obj; } + +module.exports = processJsonUser; diff --git a/inc/teddit_api/handleUser.js b/inc/teddit_api/handleUser.js index 3efde60..f07c2bb 100644 --- a/inc/teddit_api/handleUser.js +++ b/inc/teddit_api/handleUser.js @@ -1,74 +1,86 @@ -module.exports = function() { - const config = require('../../config') - this.handleTedditApiUser = async (json, req, res, from, api_type, api_target, user) => { - 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)) +const processJsonUser = require('../processJsonUser'); + +module.exports = function () { + const config = require('../../config'); + this.handleTedditApiUser = async ( + json, + req, + res, + from, + api_type, + api_target, + user + ) => { + 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 - user') - let _json = json // Keep the original json - if(from === 'redis') - json = JSON.parse(json) - - let protocol = (config.https_enabled ? 'https' : 'http') - let link = `${protocol}://${config.domain}/user/${user}` - - if(api_type === 'rss') { - let items = '' - let posts_limit = 25 - - if(json.overview.data.children.length <= posts_limit) { - posts_limit = json.overview.data.children.length + + console.log('Teddit API request - user'); + let _json = json; // Keep the original json + if (from === 'redis') json = JSON.parse(json); + + let protocol = config.https_enabled ? 'https' : 'http'; + let link = `${protocol}://${config.domain}/user/${user}`; + + if (api_type === 'rss') { + let items = ''; + let posts_limit = 25; + + if (json.overview.data.children.length <= posts_limit) { + posts_limit = json.overview.data.children.length; } - - for(var i = 0; i < posts_limit; i++) { - let post = json.overview.data.children[i].data - let post_id = post.permalink.split('/').slice(-2)[0] + '/' - let url = post.permalink.replace(post_id, '') - let permalink = `${protocol}://${config.domain}${post.permalink}` - let comments_url = `${protocol}://${config.domain}${url}` - let kind = json.overview.data.children[i].kind - - let t1_elements = '' - let t3_elements = '' - if(kind === 't1') { - let append_desc_html = `
[link] [comments]` + + for (var i = 0; i < posts_limit; i++) { + let post = json.overview.data.children[i].data; + let post_id = post.permalink.split('/').slice(-2)[0] + '/'; + let url = post.permalink.replace(post_id, ''); + let permalink = `${protocol}://${config.domain}${post.permalink}`; + let comments_url = `${protocol}://${config.domain}${url}`; + let kind = json.overview.data.children[i].kind; + + let t1_elements = ''; + let t3_elements = ''; + if (kind === 't1') { + let append_desc_html = `
[link] [comments]`; t1_elements = ` - + ${comments_url} - ` + `; } - if(kind === 't3') { - let s = await downloadAndSave(post.thumbnail, 'thumb_') - let thumbnail = '' - let enclosure = '' - if(s !== 'self' && s != '') { - let img = `${protocol}://${config.domain}${s}` - thumbnail = `${img}` - - let mime = '' - let ext = s.split('.').pop() - if(ext === 'png') - mime = 'image/png' - else - mime = 'image/jpeg' - enclosure = `` + if (kind === 't3') { + let s = await downloadAndSave(post.thumbnail, 'thumb_'); + let thumbnail = ''; + let enclosure = ''; + if (s !== 'self' && s != '') { + let img = `${protocol}://${config.domain}${s}`; + thumbnail = `${img}`; + + let mime = ''; + let ext = s.split('.').pop(); + if (ext === 'png') mime = 'image/png'; + else mime = 'image/jpeg'; + enclosure = ``; } - let append_desc_html = `submitted by r/${post.subreddit}` - append_desc_html += `
[comments]` + let append_desc_html = `submitted by r/${post.subreddit}`; + append_desc_html += `
[comments]`; t3_elements = ` - + ${thumbnail} ${enclosure} - ` + `; } - - let title = post.title - if(!post.title) - title = post.link_title - + + let title = post.title; + if (!post.title) title = post.link_title; + items += ` ${title} @@ -76,7 +88,9 @@ module.exports = function() { ${kind} ${post.subreddit} ${post.created_utc} - ${new Date(post.created_utc*1000).toGMTString()} + ${new Date( + post.created_utc * 1000 + ).toGMTString()} ${post.ups} ${permalink} ${post.edited} @@ -85,11 +99,10 @@ module.exports = function() { ${t1_elements} ${t3_elements} - ` + `; } - - let xml_output = - ` + + let xml_output = ` @@ -97,17 +110,23 @@ module.exports = function() { ${link} ${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 processJsonUser(json, true, null, null, req.cookies) - return res.end(JSON.stringify(processed_json)) - } + let processed_json = await processJsonUser( + json, + true, + null, + null, + req.cookies + ); + return res.end(JSON.stringify(processed_json)); + } } - } -} + }; +}; diff --git a/routes/user.js b/routes/user.js index 4abd6cf..e57889a 100644 --- a/routes/user.js +++ b/routes/user.js @@ -2,7 +2,7 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const userRoutes = require('express').Router(); -const processUser = require('../inc/processJsonUser.js')(); +const processJsonUser = require('../inc/processJsonUser.js'); const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); From 93f70533aaa0562c503aea8f4726e72674d869c9 Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 13:42:45 +0100 Subject: [PATCH 04/15] remove unused processJsonPost functions --- routes/home.js | 1 - routes/save.js | 1 - routes/user.js | 1 - 3 files changed, 3 deletions(-) diff --git a/routes/home.js b/routes/home.js index 40d78fe..d23e841 100644 --- a/routes/home.js +++ b/routes/home.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const homeRoute = require('express').Router(); -const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); diff --git a/routes/save.js b/routes/save.js index b43ee77..a2c4441 100644 --- a/routes/save.js +++ b/routes/save.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const saveRoutes = require('express').Router(); -const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); diff --git a/routes/user.js b/routes/user.js index e57889a..e21b1c0 100644 --- a/routes/user.js +++ b/routes/user.js @@ -3,7 +3,6 @@ const { redis, fetch } = require('../app'); const userRoutes = require('express').Router(); const processJsonUser = require('../inc/processJsonUser.js'); -const processPost = require('../inc/processJsonPost.js')(); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); From d95642cf80bb0f632ce9e555a2aead2ea7a266b0 Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 13:44:34 +0100 Subject: [PATCH 05/15] convert processJsonPost to async functions --- inc/processJsonPost.js | 666 +++++++++++++++++++++++------------------ routes/subreddit.js | 5 +- 2 files changed, 372 insertions(+), 299 deletions(-) diff --git a/inc/processJsonPost.js b/inc/processJsonPost.js index ef16664..754b589 100644 --- a/inc/processJsonPost.js +++ b/inc/processJsonPost.js @@ -1,155 +1,58 @@ -module.exports = function(fetch) { - var compilePostComments = require('./compilePostComments.js')(); - var procPostMedia = require('./processPostMedia.js')(); - this.processJsonPost = (json, parsed, user_preferences) => { - return new Promise(resolve => { - (async () => { - if(!parsed) { - json = JSON.parse(json) - } +const compilePostComments = require('./compilePostComments.js')(); +const procPostMedia = require('./processPostMedia.js')(); - let post = json[0].data.children[0].data - let post_id = post.name - let comments = json[1].data.children +async function processReplies(data, post_id, depth, user_preferences) { + let return_replies = []; + for (var i = 0; i < data.length; i++) { + let kind = data[i].kind; + let reply = data[i].data; + let obj = {}; + if (kind !== 'more') { + obj = { + author: reply.author, + body_html: reply.body_html, + parent_id: reply.parent_id, + created: reply.created_utc, + edited: reply.edited, + score: reply.score, + ups: reply.ups, + id: reply.id, + permalink: teddifyUrl(reply.permalink), + stickied: reply.stickied, + distinguished: reply.distinguished, + score_hidden: reply.score_hidden, + edited: reply.edited, + replies: [], + depth: depth, + user_flair: + user_preferences.flairs != 'false' + ? await formatUserFlair(reply) + : '', + controversiality: + user_preferences.highlight_controversial != 'false' + ? reply.controversiality + : '', + }; + } else { + obj = { + type: 'load_more', + count: reply.count, + id: reply.id, + parent_id: reply.parent_id, + post_id: post_id, + children: [], + depth: depth, + }; + } - let obj = { - author: post.author, - created: post.created_utc, - edited: post.edited, - is_video: post.is_video, - locked: post.locked, - link_flair_text: post.link_flair_text, - name: post_id, - num_comments: post.num_comments, - over_18: post.over_18, - permalink: teddifyUrl(post.permalink), - title: post.title, - url: teddifyUrl(post.url, user_preferences), - ups: post.ups, - id: post.id, - domain: post.domain, - contest_mode: post.contest_mode, - upvote_ratio: post.upvote_ratio, - comments: null, - has_media: false, - media: null, - images: null, - crosspost: false, - selftext: unescape(post.selftext_html), - poll_data: post.poll_data, - link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''), - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') - } + if (reply.replies && kind !== 'more') { + if (reply.replies.data.children.length) { + for (var j = 0; j < reply.replies.data.children.length; j++) { + let comment = reply.replies.data.children[j].data; + let objct = {}; - let valid_embed_video_domains = ['gfycat.com'] - let has_gif = false - let gif_to_mp4 = null - let reddit_video = null - let embed_video = false - - if(post.media) - if(valid_embed_video_domains.includes(post.media.type)) - embed_video = true - - if(post.preview && !embed_video) { - if(post.preview.reddit_video_preview) { - if(post.preview.reddit_video_preview.is_gif) { - has_gif = true - gif_url = post.preview.reddit_video_preview.fallback_url - } else { - let file_ext = getFileExtension(post.preview.reddit_video_preview.fallback_url) - if(file_ext === 'mp4')  { - post.media = true - reddit_video = post.preview.reddit_video_preview - } - } - } - if(post.preview.images) { - if(post.preview.images[0].source) { - let file_ext = getFileExtension(post.preview.images[0].source.url) - if(file_ext === 'gif') { - has_gif = true - let resolutions = post.preview.images[0].variants.mp4.resolutions - gif_to_mp4 = resolutions[resolutions.length - 1] - } - } - } - } - - obj = await processPostMedia(obj, post, post.media, has_gif, reddit_video, gif_to_mp4) - - if(post.crosspost_parent_list) { - post.crosspost = post.crosspost_parent_list[0] - } - if(post.crosspost) { - obj = await processPostMedia(obj, post.crosspost, post.crosspost.media, has_gif, reddit_video, gif_to_mp4) - obj.crosspost = { - author: post.crosspost.author, - created: post.crosspost.created_utc, - subreddit: post.crosspost.subreddit, - title: post.crosspost.title, - name: post.crosspost.name, - num_comments: post.crosspost.num_comments, - over_18: post.crosspost.over_18, - id: post.crosspost.id, - permalink: teddifyUrl(post.crosspost.permalink), - ups: post.crosspost.ups, - selftext: unescape(post.selftext_html), - selftext_crosspost: unescape(post.crosspost.selftext_html), - poll_data: post.poll_data, - is_crosspost: true, - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') - } - } - - if(post.preview && !obj.has_media) { - obj.images = { - source: await downloadAndSave(post.preview.images[0].source.url) - } - } - - if(obj.media) { - if(obj.media.source === 'external') { - if(post.preview) { - obj.images = { - source: await downloadAndSave(post.preview.images[0].source.url) - } - } - } - } - - if(post.gallery_data) { - obj.gallery = true - obj.gallery_items = [] - for(var i = 0; i < post.gallery_data.items.length; i++) { - let id = post.gallery_data.items[i].media_id - if(post.media_metadata[id]) { - if(post.media_metadata[id].p) { - if(post.media_metadata[id].p[0]) { - let item = { source: null, thumbnail: null, large: null } - if(post.media_metadata[id].s && post.media_metadata[id].p[0].u) { - item = { - type: post.media_metadata[id].e, - source: await downloadAndSave(post.media_metadata[id].s.u), - thumbnail: await downloadAndSave(post.media_metadata[id].p[0].u), - large: await downloadAndSave(post.media_metadata[id].p[post.media_metadata[id].p.length - 1].u), - } - } - obj.gallery_items.push(item) - } - } - } - } - } - - let comms = [] - for(var i = 0; i < comments.length; i++) { - let comment = comments[i].data - let kind = comments[i].kind - let obj = {} - - if(kind !== 'more') { - obj = { + if (comment.author && comment.body_html) { + objct = { author: comment.author, body_html: comment.body_html, parent_id: comment.parent_id, @@ -159,174 +62,341 @@ module.exports = function(fetch) { ups: comment.ups, id: comment.id, permalink: teddifyUrl(comment.permalink), - stickied: comment.stickied, - distinguished: comment.distinguished, score_hidden: comment.score_hidden, - edited: comment.edited, + distinguished: comment.distinguished, + distinguished: comment.edited, replies: [], - depth: comment.depth, - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''), - controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '') - } + depth: depth + 1, + user_flair: + user_preferences.flairs != 'false' + ? await formatUserFlair(comment) + : '', + controversiality: + user_preferences.highlight_controversial != 'false' + ? comment.controversiality + : '', + }; } else { - obj = { + objct = { type: 'load_more', count: comment.count, id: comment.id, parent_id: comment.parent_id, - post_id: post.name, - children: [] - } - } - - if(comment.replies && kind !== 'more') { - if(comment.replies.data) { - if(comment.replies.data.children.length > 0) { - obj.replies = await processReplies(comment.replies.data.children, post_id, 1, user_preferences) + post_id: post_id, + children: [], + depth: depth + 1, + }; + if (comment.children) { + for (var k = 0; k < comment.children.length; k++) { + objct.children.push(comment.children[k]); } } } - if(comment.children) { - for(var j = 0; j < comment.children.length; j++) { - obj.children.push(comment.children[j]) + if (comment.replies) { + if (comment.replies.data) { + if (comment.replies.data.children.length > 0) { + objct.replies = await processReplies( + comment.replies.data.children, + post_id, + depth, + user_preferences + ); + } } } - comms.push(obj) + obj.replies.push(objct); } - - obj.comments = comms - - resolve(obj) - })() - }) - } - - this.finalizeJsonPost = async (processed_json, post_id, post_url, morechildren_ids, viewing_comment, user_preferences) => { - let comments_html = `
` - let comments = processed_json.comments - let last_known_depth = undefined - for(var i = 0; i < comments.length; i++) { - let next_comment = false - if(comments[i+1]) { - next_comment = comments[i+1] } - if(comments[i].depth != undefined) { - last_known_depth = comments[i].depth - } - - comments_html += await compilePostCommentsHtml(comments[i], next_comment, post_id, post_url, morechildren_ids, processed_json.author, viewing_comment, user_preferences, last_known_depth) } - comments_html += `
` - - delete processed_json['comments'] - let post_data = processed_json - return { post_data: post_data, comments: comments_html } - } - - this.processReplies = async (data, post_id, depth, user_preferences) => { - let return_replies = [] - for(var i = 0; i < data.length; i++) { - let kind = data[i].kind - let reply = data[i].data - let obj = {} - if(kind !== 'more') { - obj = { - author: reply.author, - body_html: reply.body_html, - parent_id: reply.parent_id, - created: reply.created_utc, - edited: reply.edited, - score: reply.score, - ups: reply.ups, - id: reply.id, - permalink: teddifyUrl(reply.permalink), - stickied: reply.stickied, - distinguished: reply.distinguished, - score_hidden: reply.score_hidden, - edited: reply.edited, - replies: [], - depth: depth, - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(reply) : ''), - controversiality: (user_preferences.highlight_controversial != 'false' ? reply.controversiality : '') - } - } else { - obj = { - type: 'load_more', - count: reply.count, - id: reply.id, - parent_id: reply.parent_id, - post_id: post_id, - children: [], - depth: depth - } + if (reply.children) { + for (var j = 0; j < reply.children.length; j++) { + obj.children.push(reply.children[j]); } - - if(reply.replies && kind !== 'more') { - if(reply.replies.data.children.length) { - for(var j = 0; j < reply.replies.data.children.length; j++) { - let comment = reply.replies.data.children[j].data - let objct = {} - - if(comment.author && comment.body_html) { - objct = { - author: comment.author, - body_html: comment.body_html, - parent_id: comment.parent_id, - created: comment.created_utc, - edited: comment.edited, - score: comment.score, - ups: comment.ups, - id: comment.id, - permalink: teddifyUrl(comment.permalink), - score_hidden: comment.score_hidden, - distinguished: comment.distinguished, - distinguished: comment.edited, - replies: [], - depth: depth + 1, - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''), - controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '') - } - } else { - objct = { - type: 'load_more', - count: comment.count, - id: comment.id, - parent_id: comment.parent_id, - post_id: post_id, - children: [], - depth: depth + 1 - } - if(comment.children) { - for(var k = 0; k < comment.children.length; k++) { - objct.children.push(comment.children[k]) - } - } - } - - if(comment.replies) { - if(comment.replies.data) { - if(comment.replies.data.children.length > 0) { - objct.replies = await processReplies(comment.replies.data.children, post_id, depth, user_preferences) - } - } - } - - obj.replies.push(objct) - } - } - } - - if(reply.children) { - for(var j = 0; j < reply.children.length; j++) { - obj.children.push(reply.children[j]) - } - } - - return_replies.push(obj) } - return return_replies + + return_replies.push(obj); } + return return_replies; } + +async function processJsonPost(json, parsed, user_preferences) { + if (!parsed) { + json = JSON.parse(json); + } + + let post = json[0].data.children[0].data; + let post_id = post.name; + let comments = json[1].data.children; + + let obj = { + author: post.author, + created: post.created_utc, + edited: post.edited, + is_video: post.is_video, + locked: post.locked, + link_flair_text: post.link_flair_text, + name: post_id, + num_comments: post.num_comments, + over_18: post.over_18, + permalink: teddifyUrl(post.permalink), + title: post.title, + url: teddifyUrl(post.url, user_preferences), + ups: post.ups, + id: post.id, + domain: post.domain, + contest_mode: post.contest_mode, + upvote_ratio: post.upvote_ratio, + comments: null, + has_media: false, + media: null, + images: null, + crosspost: false, + selftext: unescape(post.selftext_html), + poll_data: post.poll_data, + link_flair: + user_preferences.flairs != 'false' ? await formatLinkFlair(post) : '', + user_flair: + user_preferences.flairs != 'false' ? await formatUserFlair(post) : '', + }; + + let valid_embed_video_domains = ['gfycat.com']; + let has_gif = false; + let gif_to_mp4 = null; + let reddit_video = null; + let embed_video = false; + + if (post.media) + if (valid_embed_video_domains.includes(post.media.type)) embed_video = true; + + if (post.preview && !embed_video) { + if (post.preview.reddit_video_preview) { + if (post.preview.reddit_video_preview.is_gif) { + has_gif = true; + gif_url = post.preview.reddit_video_preview.fallback_url; + } else { + let file_ext = getFileExtension( + post.preview.reddit_video_preview.fallback_url + ); + if (file_ext === 'mp4') { + post.media = true; + reddit_video = post.preview.reddit_video_preview; + } + } + } + if (post.preview.images) { + if (post.preview.images[0].source) { + let file_ext = getFileExtension(post.preview.images[0].source.url); + if (file_ext === 'gif') { + has_gif = true; + let resolutions = post.preview.images[0].variants.mp4.resolutions; + gif_to_mp4 = resolutions[resolutions.length - 1]; + } + } + } + } + + obj = await processPostMedia( + obj, + post, + post.media, + has_gif, + reddit_video, + gif_to_mp4 + ); + + if (post.crosspost_parent_list) { + post.crosspost = post.crosspost_parent_list[0]; + } + if (post.crosspost) { + obj = await processPostMedia( + obj, + post.crosspost, + post.crosspost.media, + has_gif, + reddit_video, + gif_to_mp4 + ); + obj.crosspost = { + author: post.crosspost.author, + created: post.crosspost.created_utc, + subreddit: post.crosspost.subreddit, + title: post.crosspost.title, + name: post.crosspost.name, + num_comments: post.crosspost.num_comments, + over_18: post.crosspost.over_18, + id: post.crosspost.id, + permalink: teddifyUrl(post.crosspost.permalink), + ups: post.crosspost.ups, + selftext: unescape(post.selftext_html), + selftext_crosspost: unescape(post.crosspost.selftext_html), + poll_data: post.poll_data, + is_crosspost: true, + user_flair: + user_preferences.flairs != 'false' ? await formatUserFlair(post) : '', + }; + } + + if (post.preview && !obj.has_media) { + obj.images = { + source: await downloadAndSave(post.preview.images[0].source.url), + }; + } + + if (obj.media) { + if (obj.media.source === 'external') { + if (post.preview) { + obj.images = { + source: await downloadAndSave(post.preview.images[0].source.url), + }; + } + } + } + + if (post.gallery_data) { + obj.gallery = true; + obj.gallery_items = []; + for (var i = 0; i < post.gallery_data.items.length; i++) { + let id = post.gallery_data.items[i].media_id; + if (post.media_metadata[id]) { + if (post.media_metadata[id].p) { + if (post.media_metadata[id].p[0]) { + let item = { source: null, thumbnail: null, large: null }; + if (post.media_metadata[id].s && post.media_metadata[id].p[0].u) { + item = { + type: post.media_metadata[id].e, + source: await downloadAndSave(post.media_metadata[id].s.u), + thumbnail: await downloadAndSave( + post.media_metadata[id].p[0].u + ), + large: await downloadAndSave( + post.media_metadata[id].p[ + post.media_metadata[id].p.length - 1 + ].u + ), + }; + } + obj.gallery_items.push(item); + } + } + } + } + } + + let comms = []; + for (var i = 0; i < comments.length; i++) { + let comment = comments[i].data; + let kind = comments[i].kind; + let obj = {}; + + if (kind !== 'more') { + obj = { + author: comment.author, + body_html: comment.body_html, + parent_id: comment.parent_id, + created: comment.created_utc, + edited: comment.edited, + score: comment.score, + ups: comment.ups, + id: comment.id, + permalink: teddifyUrl(comment.permalink), + stickied: comment.stickied, + distinguished: comment.distinguished, + score_hidden: comment.score_hidden, + edited: comment.edited, + replies: [], + depth: comment.depth, + user_flair: + user_preferences.flairs != 'false' + ? await formatUserFlair(comment) + : '', + controversiality: + user_preferences.highlight_controversial != 'false' + ? comment.controversiality + : '', + }; + } else { + obj = { + type: 'load_more', + count: comment.count, + id: comment.id, + parent_id: comment.parent_id, + post_id: post.name, + children: [], + }; + } + + if (comment.replies && kind !== 'more') { + if (comment.replies.data) { + if (comment.replies.data.children.length > 0) { + obj.replies = await processReplies( + comment.replies.data.children, + post_id, + 1, + user_preferences + ); + } + } + } + + if (comment.children) { + for (var j = 0; j < comment.children.length; j++) { + obj.children.push(comment.children[j]); + } + } + + comms.push(obj); + } + + obj.comments = comms; + + return obj; +} + +async function finalizeJsonPost( + processed_json, + post_id, + post_url, + morechildren_ids, + viewing_comment, + user_preferences +) { + let comments_html = `
`; + let comments = processed_json.comments; + let last_known_depth = undefined; + for (var i = 0; i < comments.length; i++) { + let next_comment = false; + if (comments[i + 1]) { + next_comment = comments[i + 1]; + } + if (comments[i].depth != undefined) { + last_known_depth = comments[i].depth; + } + + comments_html += await compilePostCommentsHtml( + comments[i], + next_comment, + post_id, + post_url, + morechildren_ids, + processed_json.author, + viewing_comment, + user_preferences, + last_known_depth + ); + } + + comments_html += `
`; + + delete processed_json['comments']; + let post_data = processed_json; + return { post_data: post_data, comments: comments_html }; +} + +module.exports = { + processReplies, + processJsonPost, + finalizeJsonPost, +}; diff --git a/routes/subreddit.js b/routes/subreddit.js index 2a357af..e56676c 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -2,7 +2,10 @@ const config = require('../config'); const { redis, fetch, RedditAPI } = require('../app'); const subredditRoutes = require('express').Router(); -const processPost = require('../inc/processJsonPost.js')(); +const { + processJsonPost, + finalizeJsonPost, +} = require('../inc/processJsonPost.js'); const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); From 5f5388da6f9ef85c87694cf2e76c6a77384d9e6c Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 14:09:53 +0100 Subject: [PATCH 06/15] remove unused processSubredditAbout function --- routes/home.js | 1 - routes/save.js | 1 - routes/user.js | 1 - 3 files changed, 3 deletions(-) diff --git a/routes/home.js b/routes/home.js index d23e841..5cc64c4 100644 --- a/routes/home.js +++ b/routes/home.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const homeRoute = require('express').Router(); -const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); diff --git a/routes/save.js b/routes/save.js index a2c4441..402444c 100644 --- a/routes/save.js +++ b/routes/save.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const saveRoutes = require('express').Router(); -const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); diff --git a/routes/user.js b/routes/user.js index e21b1c0..7ea8bf8 100644 --- a/routes/user.js +++ b/routes/user.js @@ -3,7 +3,6 @@ const { redis, fetch } = require('../app'); const userRoutes = require('express').Router(); const processJsonUser = require('../inc/processJsonUser.js'); -const processAbout = require('../inc/processSubredditAbout.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); From 1c7a3a8f5ae0e77b823094a8ab1aee6da4f79fa7 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 09:27:56 +0100 Subject: [PATCH 07/15] separate redis from app.js --- app.js | 42 +----------------------------------------- inc/redis.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 41 deletions(-) create mode 100644 inc/redis.js diff --git a/app.js b/app.js index 7f21d05..2aaefbb 100644 --- a/app.js +++ b/app.js @@ -12,41 +12,7 @@ const pug = require('pug'); const compression = require('compression'); const express = require('express'); const cookieParser = require('cookie-parser'); -const r = require('redis'); - -const redis = (() => { - if (!config.redis_enabled) { - // Stub Redis if disabled - return { - get: (_, callback) => callback(null, null), - setex: (_, _1, _2, callback) => callback(null), - on: () => {}, - }; - } - - const redisOptions = { - host: '127.0.0.1', - port: 6379, - }; - - if (config.redis_db) { - redisOptions.db = config.redis_db; - } - - if (config.redis_host) { - redisOptions.host = config.redis_host; - } - - if (config.redis_port && config.redis_port > 0) { - redisOptions.port = config.redis_port; - } - - if (config.redis_password) { - redisOptions.password = config.redis_password; - } - - return r.createClient(redisOptions); -})(); +const { redis } = require('./inc/redis'); const nodeFetch = require('node-fetch'); const fetch = config.http_proxy @@ -162,12 +128,6 @@ if (config.redirect_http_to_https) { }); } -redis.on('error', (error) => { - if (error) { - console.error(`Redis error: ${error}`); - } -}); - const cacheControl = require('./cacheControl.js'); cacheControl.removeCacheFiles(); diff --git a/inc/redis.js b/inc/redis.js new file mode 100644 index 0000000..0918d9d --- /dev/null +++ b/inc/redis.js @@ -0,0 +1,51 @@ +const config = require('../config'); +const { promisify } = require('util'); +const r = require('redis'); + +const redisOptions = { + host: '127.0.0.1', + port: 6379, +}; + +if (config.redis_db) { + redisOptions.db = config.redis_db; +} + +if (config.redis_host) { + redisOptions.host = config.redis_host; +} + +if (config.redis_port && config.redis_port > 0) { + redisOptions.port = config.redis_port; +} + +if (config.redis_password) { + redisOptions.password = config.redis_password; +} + +// Stub Redis if disabled +const stub = { + get: (_, callback) => callback(null, null), + setex: (_, _1, _2, callback) => callback(null), + on: () => {}, +}; + +const redisDisabled = !config.redis_enabled; + +const redis = redisDisabled ? stub : r.createClient(redisOptions); + +const redisAsync = { + get: promisify(redis.get).bind(redis), + setex: promisify(redis.setex).bind(redis), +}; + +redis.on('error', (error) => { + if (error) { + console.error(`Redis error: ${error}`); + } +}); + +module.exports = { + redis, + redisAsync, +}; From aae3e00ab11bd5e46732c3d801ea00c307775c66 Mon Sep 17 00:00:00 2001 From: json Date: Tue, 7 Sep 2021 14:24:10 +0100 Subject: [PATCH 08/15] convert processSubredditAbout to async function --- inc/processSubredditAbout.js | 213 +++++++++++++++++++---------------- routes/subreddit.js | 2 +- 2 files changed, 114 insertions(+), 101 deletions(-) diff --git a/inc/processSubredditAbout.js b/inc/processSubredditAbout.js index af13604..f6505af 100644 --- a/inc/processSubredditAbout.js +++ b/inc/processSubredditAbout.js @@ -1,106 +1,119 @@ -module.exports = function() { - const config = require('../config') - this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => { - return new Promise(resolve => { - (async () => { - if(subreddit && !subreddit.includes('+') && subreddit !== 'all') { - function returnRelevantKeys(json) { - return { - title: json.data.title, - public_description_html: json.data.public_description_html, - active_user_count: json.data.active_user_count, - subscribers: json.data.subscribers, - created_utc: json.data.created_utc, - over18: json.data.over18, - description_html: json.data.description_html, - moderators: json.moderators - } - } - - let key = `${subreddit}:sidebar` - redis.get(key, (error, json) => { - if(error) { - console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error) - resolve(null) - } - if(json) { - json = JSON.parse(json) - resolve(returnRelevantKeys(json)) - } else { - let url = `https://reddit.com/r/${subreddit}/about.json` - if(config.use_reddit_oauth) { - url = `https://oauth.reddit.com/r/${subreddit}/about` - } - fetch(encodeURI(url), redditApiGETHeaders()) - .then(result => { - if(result.status === 200) { - result.json() - .then(json => { - json.moderators = [] - redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { - if(error) { - console.error('Error setting the sidebar key to redis.', error) - return res.render('index', { json: null, user_preferences: req.cookies }) - } else { - console.log('Fetched the sidebar from reddit API.') - let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json` - if(config.use_reddit_oauth) { - moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators` - } - resolve(returnRelevantKeys(json)) - /* - * The following code is commented out because Reddit doesn't - * anymore support fetching moderators for subreddits - * when not logged in. - * This might change in the future though. - * https://codeberg.org/teddit/teddit/issues/207 - */ - - /* - fetch(encodeURI(moderators_url), redditApiGETHeaders()) - .then(mod_result => { - if(mod_result.status === 200) { - mod_result.json() - .then(mod_json => { - json.moderators = mod_json - redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { - if(error) { - console.error('Error setting the sidebar with moderators key to redis.', error) - return res.render('index', { json: null, user_preferences: req.cookies }) - } else { - console.log('Fetched the moderators from reddit API.') - resolve(returnRelevantKeys(json)) - } - }) - }) - } else { - console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} – ${mod_result.statusText}`) - console.error(config.reddit_api_error_text) - resolve(returnRelevantKeys(json)) - } - }).catch(error => { - console.error('Error fetching moderators.', error) - resolve(returnRelevantKeys(json)) - }) - */ +const config = require('../config'); + +async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) { + if (subreddit && !subreddit.includes('+') && subreddit !== 'all') { + function returnRelevantKeys(json) { + return { + title: json.data.title, + public_description_html: json.data.public_description_html, + active_user_count: json.data.active_user_count, + subscribers: json.data.subscribers, + created_utc: json.data.created_utc, + over18: json.data.over18, + description_html: json.data.description_html, + moderators: json.moderators, + }; + } + + let key = `${subreddit}:sidebar`; + redis.get(key, (error, json) => { + if (error) { + console.error( + `Error getting the ${subreddit}:sidebar key from redis.`, + error + ); + return null; + } + if (json) { + json = JSON.parse(json); + return returnRelevantKeys(json); + } else { + let url = `https://reddit.com/r/${subreddit}/about.json`; + if (config.use_reddit_oauth) { + url = `https://oauth.reddit.com/r/${subreddit}/about`; + } + fetch(encodeURI(url), redditApiGETHeaders()) + .then((result) => { + if (result.status === 200) { + result.json().then((json) => { + json.moderators = []; + redis.setex( + key, + config.setexs.sidebar, + JSON.stringify(json), + (error) => { + if (error) { + console.error( + 'Error setting the sidebar key to redis.', + error + ); + return res.render('index', { + json: null, + user_preferences: req.cookies, + }); + } else { + console.log('Fetched the sidebar from reddit API.'); + let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`; + if (config.use_reddit_oauth) { + moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`; } - }) + return returnRelevantKeys(json); + /* + * The following code is commented out because Reddit doesn't + * anymore support fetching moderators for subreddits + * when not logged in. + * This might change in the future though. + * https://codeberg.org/teddit/teddit/issues/207 + */ + + /* + fetch(encodeURI(moderators_url), redditApiGETHeaders()) + .then(mod_result => { + if(mod_result.status === 200) { + mod_result.json() + .then(mod_json => { + json.moderators = mod_json + redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { + if(error) { + console.error('Error setting the sidebar with moderators key to redis.', error) + return res.render('index', { json: null, user_preferences: req.cookies }) + } else { + console.log('Fetched the moderators from reddit API.') + return(returnRelevantKeys(json)) + } + }) + }) + } else { + console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} – ${mod_result.statusText}`) + console.error(config.reddit_api_error_text) + return(returnRelevantKeys(json)) + } + }).catch(error => { + console.error('Error fetching moderators.', error) + return(returnRelevantKeys(json)) }) - } else { - console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`) - console.error(config.reddit_api_error_text) - resolve(null) - } - }).catch(error => { - console.error('Error fetching the sidebar.', error) - resolve(null) - }) + */ + } + } + ); + }); + } else { + console.error( + `Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}` + ); + console.error(config.reddit_api_error_text); + return null; } }) - } else { - resolve(null) - } - })() - }) + .catch((error) => { + console.error('Error fetching the sidebar.', error); + return null; + }); + } + }); + } else { + return null; } } + +module.exports = processSubredditAbout; diff --git a/routes/subreddit.js b/routes/subreddit.js index e56676c..4a1ea21 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -6,7 +6,7 @@ const { processJsonPost, finalizeJsonPost, } = require('../inc/processJsonPost.js'); -const processAbout = require('../inc/processSubredditAbout.js')(); +const processSubredditAbout = require('../inc/processSubredditAbout.js'); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); From 711f2450c783c16a61c127df86c4b82c63c0b7cd Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 09:32:26 +0100 Subject: [PATCH 09/15] change callback to await in processSubredditAbout --- inc/processSubredditAbout.js | 156 ++++++++++++----------------------- 1 file changed, 52 insertions(+), 104 deletions(-) diff --git a/inc/processSubredditAbout.js b/inc/processSubredditAbout.js index f6505af..0800f14 100644 --- a/inc/processSubredditAbout.js +++ b/inc/processSubredditAbout.js @@ -1,116 +1,64 @@ const config = require('../config'); +const { redisAsync } = require('./redis'); + +function returnRelevantKeys(json) { + return { + title: json.data.title, + public_description_html: json.data.public_description_html, + active_user_count: json.data.active_user_count, + subscribers: json.data.subscribers, + created_utc: json.data.created_utc, + over18: json.data.over18, + description_html: json.data.description_html, + moderators: json.moderators, + }; +} async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) { if (subreddit && !subreddit.includes('+') && subreddit !== 'all') { - function returnRelevantKeys(json) { - return { - title: json.data.title, - public_description_html: json.data.public_description_html, - active_user_count: json.data.active_user_count, - subscribers: json.data.subscribers, - created_utc: json.data.created_utc, - over18: json.data.over18, - description_html: json.data.description_html, - moderators: json.moderators, - }; - } + const key = `${subreddit}:sidebar`; - let key = `${subreddit}:sidebar`; - redis.get(key, (error, json) => { - if (error) { - console.error( - `Error getting the ${subreddit}:sidebar key from redis.`, - error + try { + const cached = await redisAsync.get(key); + + if (cached !== null) { + return returnRelevantKeys(JSON.parse(cached)); + } + + let url = `https://reddit.com/r/${subreddit}/about.json`; + + if (config.use_reddit_oauth) { + url = `https://oauth.reddit.com/r/${subreddit}/about`; + } + + const subredditAboutRequest = await fetch(url, redditApiGETHeaders()); + + if (subredditAboutRequest.ok) { + let response = await subredditAboutRequest.json(); + response.moderators = []; + + await redisAsync.setex( + key, + config.setexs.sidebar, + JSON.stringify(response) ); + + console.log(`Fetched sidebar for ${subreddit} from reddit API`); + + return returnRelevantKeys(response); + } else { + console.error( + `Something went wrong while fetching data from reddit API: + ${subredditAboutRequest.status} – ${subredditAboutRequest.statusText}` + ); + console.error(config.reddit_api_error_text); return null; } - if (json) { - json = JSON.parse(json); - return returnRelevantKeys(json); - } else { - let url = `https://reddit.com/r/${subreddit}/about.json`; - if (config.use_reddit_oauth) { - url = `https://oauth.reddit.com/r/${subreddit}/about`; - } - fetch(encodeURI(url), redditApiGETHeaders()) - .then((result) => { - if (result.status === 200) { - result.json().then((json) => { - json.moderators = []; - redis.setex( - key, - config.setexs.sidebar, - JSON.stringify(json), - (error) => { - if (error) { - console.error( - 'Error setting the sidebar key to redis.', - error - ); - return res.render('index', { - json: null, - user_preferences: req.cookies, - }); - } else { - console.log('Fetched the sidebar from reddit API.'); - let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`; - if (config.use_reddit_oauth) { - moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`; - } - return returnRelevantKeys(json); - /* - * The following code is commented out because Reddit doesn't - * anymore support fetching moderators for subreddits - * when not logged in. - * This might change in the future though. - * https://codeberg.org/teddit/teddit/issues/207 - */ + } catch (error) { + console.error('Error fetching the sidebar: ', error); - /* - fetch(encodeURI(moderators_url), redditApiGETHeaders()) - .then(mod_result => { - if(mod_result.status === 200) { - mod_result.json() - .then(mod_json => { - json.moderators = mod_json - redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { - if(error) { - console.error('Error setting the sidebar with moderators key to redis.', error) - return res.render('index', { json: null, user_preferences: req.cookies }) - } else { - console.log('Fetched the moderators from reddit API.') - return(returnRelevantKeys(json)) - } - }) - }) - } else { - console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} – ${mod_result.statusText}`) - console.error(config.reddit_api_error_text) - return(returnRelevantKeys(json)) - } - }).catch(error => { - console.error('Error fetching moderators.', error) - return(returnRelevantKeys(json)) - }) - */ - } - } - ); - }); - } else { - console.error( - `Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}` - ); - console.error(config.reddit_api_error_text); - return null; - } - }) - .catch((error) => { - console.error('Error fetching the sidebar.', error); - return null; - }); - } - }); + return null; + } } else { return null; } From 9bc3ff8d34f092899ed8c0c60faf0c13b3fb4463 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 10:01:05 +0100 Subject: [PATCH 10/15] remove unused handleTedditApiUser function --- routes/home.js | 1 - routes/save.js | 1 - routes/subreddit.js | 1 - 3 files changed, 3 deletions(-) diff --git a/routes/home.js b/routes/home.js index 5cc64c4..a8f1d52 100644 --- a/routes/home.js +++ b/routes/home.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const homeRoute = require('express').Router(); -const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); diff --git a/routes/save.js b/routes/save.js index 402444c..f068351 100644 --- a/routes/save.js +++ b/routes/save.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const saveRoutes = require('express').Router(); -const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); diff --git a/routes/subreddit.js b/routes/subreddit.js index 4a1ea21..fa4d19f 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -7,7 +7,6 @@ const { finalizeJsonPost, } = require('../inc/processJsonPost.js'); const processSubredditAbout = require('../inc/processSubredditAbout.js'); -const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); From 0bf3e36c56cb8dce890c29ba5c322e76ff7209ae Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 11:46:42 +0100 Subject: [PATCH 11/15] remove unused processSearchResults function --- routes/home.js | 1 - routes/save.js | 1 - routes/user.js | 1 - 3 files changed, 3 deletions(-) diff --git a/routes/home.js b/routes/home.js index a8f1d52..b0b5636 100644 --- a/routes/home.js +++ b/routes/home.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const homeRoute = require('express').Router(); -const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); diff --git a/routes/save.js b/routes/save.js index f068351..d5bc4cf 100644 --- a/routes/save.js +++ b/routes/save.js @@ -2,7 +2,6 @@ const config = require('../config'); const { redis, fetch } = require('../app'); const saveRoutes = require('express').Router(); -const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); diff --git a/routes/user.js b/routes/user.js index 7ea8bf8..25eb2b0 100644 --- a/routes/user.js +++ b/routes/user.js @@ -4,7 +4,6 @@ const userRoutes = require('express').Router(); const processJsonUser = require('../inc/processJsonUser.js'); const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); -const processSearches = require('../inc/processSearchResults.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); From 153a50e60afa663576f6995bd1c7c0c92c011747 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 11:47:25 +0100 Subject: [PATCH 12/15] convert processSearchResults to async function --- inc/processSearchResults.js | 126 +++++++++++++++++++----------------- routes/subreddit.js | 2 +- 2 files changed, 68 insertions(+), 60 deletions(-) diff --git a/inc/processSearchResults.js b/inc/processSearchResults.js index aa97b9d..c968d59 100644 --- a/inc/processSearchResults.js +++ b/inc/processSearchResults.js @@ -1,62 +1,70 @@ -module.exports = function() { - const config = require('../config'); - const link = require('./components/link') - this.processSearchResults = (json, parsed, after, before, user_preferences) => { - return new Promise(resolve => { - (async () => { - if(!parsed) { - json = JSON.parse(json) - } - let posts = [] - let search_firstpage = false - let before = json.data.before - let after = json.data.after - - if(!after && !before) { - search_firstpage = true - } - - let suggested_subreddits = false - if(json.suggested_subreddits) { - if(json.suggested_subreddits.data) { - if(json.suggested_subreddits.data.children.length > 0) { - suggested_subreddits = json.suggested_subreddits.data.children - } - } - } - - if(json.data.children) { - let view_more_posts = false - let posts_limit = 25 +const config = require('../config'); +const link = require('./components/link'); - if(json.data.children.length > posts_limit) { - view_more_posts = true - } else { - posts_limit = json.data.children.length - } - - for(var i = 0; i < posts_limit; i++) { - let post = json.data.children[i].data - - if(post.over_18) - if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') - continue - - let obj = await link.fromJson(post, user_preferences) - posts.push(obj) - } - } - - let obj = { - search_firstpage: search_firstpage, - before: before, - after: after, - posts: posts, - suggested_subreddits: suggested_subreddits, - } - - resolve(obj) - })() - }) +async function processSearchResults( + json, + parsed, + after, + before, + user_preferences +) { + if (!parsed) { + json = JSON.parse(json); } + let posts = []; + let search_firstpage = false; + + before = json.data.before; + after = json.data.after; + + if (!after && !before) { + search_firstpage = true; + } + + let suggested_subreddits = false; + if (json.suggested_subreddits) { + if (json.suggested_subreddits.data) { + if (json.suggested_subreddits.data.children.length > 0) { + suggested_subreddits = json.suggested_subreddits.data.children; + } + } + } + + if (json.data.children) { + let view_more_posts = false; + let posts_limit = 25; + + if (json.data.children.length > posts_limit) { + view_more_posts = true; + } else { + posts_limit = json.data.children.length; + } + + for (var i = 0; i < posts_limit; i++) { + let post = json.data.children[i].data; + + if (post.over_18) + if ( + (config.nsfw_enabled === false && + user_preferences.nsfw_enabled != 'true') || + user_preferences.nsfw_enabled === 'false' + ) + continue; + + let obj = await link.fromJson(post, user_preferences); + posts.push(obj); + } + } + + let obj = { + search_firstpage: search_firstpage, + before: before, + after: after, + posts: posts, + suggested_subreddits: suggested_subreddits, + }; + + return obj; } + +module.exports = processSearchResults; diff --git a/routes/subreddit.js b/routes/subreddit.js index fa4d19f..cc6f06a 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -7,7 +7,7 @@ const { finalizeJsonPost, } = require('../inc/processJsonPost.js'); const processSubredditAbout = require('../inc/processSubredditAbout.js'); -const processSearches = require('../inc/processSearchResults.js')(); +const processSearchResults = require('../inc/processSearchResults.js'); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); From 6ff892767c03812ff9e7e480b8ef5f82cff55f5d Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 12:03:35 +0100 Subject: [PATCH 13/15] convert processJsonSubreddit to async function --- inc/processJsonSubreddit.js | 155 +++++++++--------- inc/teddit_api/handleSubreddit.js | 257 ++++++++++++++++-------------- routes/home.js | 2 +- routes/save.js | 2 +- routes/subreddit.js | 2 +- routes/user.js | 2 +- 6 files changed, 225 insertions(+), 195 deletions(-) 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 = From 6ced924bc891e4a598b6e0dbdbc9523689a35459 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 12:25:41 +0100 Subject: [PATCH 14/15] remove unused processSubredditsExplore function --- routes/home.js | 2 -- routes/save.js | 2 -- routes/user.js | 2 -- 3 files changed, 6 deletions(-) diff --git a/routes/home.js b/routes/home.js index 2997767..b68ba45 100644 --- a/routes/home.js +++ b/routes/home.js @@ -5,8 +5,6 @@ const homeRoute = require('express').Router(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js'); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); -const processSubredditsExplore = - require('../inc/processSubredditsExplore.js')(); homeRoute.get('/:sort?', async (req, res, next) => { let past = req.query.t; diff --git a/routes/save.js b/routes/save.js index e09d23a..9479700 100644 --- a/routes/save.js +++ b/routes/save.js @@ -5,8 +5,6 @@ const saveRoutes = require('express').Router(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js'); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); -const processSubredditsExplore = - require('../inc/processSubredditsExplore.js')(); saveRoutes.get('/saved', (req, res, next) => { let saved = req.cookies.saved; diff --git a/routes/user.js b/routes/user.js index bbdc8af..23b2150 100644 --- a/routes/user.js +++ b/routes/user.js @@ -7,8 +7,6 @@ const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js'); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); -const processSubredditsExplore = - require('../inc/processSubredditsExplore.js')(); userRoutes.get('/user/:user/:kind?', (req, res, next) => { let kind = ''; From e1df2ea8ae4b96a4056036059e263967e80b9975 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 12:28:45 +0100 Subject: [PATCH 15/15] convert processJsonSubredditsExplore to async function --- inc/processSubredditsExplore.js | 100 +++++++++++++++++--------------- routes/subreddit.js | 3 +- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/inc/processSubredditsExplore.js b/inc/processSubredditsExplore.js index ab3f931..66ed2f9 100644 --- a/inc/processSubredditsExplore.js +++ b/inc/processSubredditsExplore.js @@ -1,52 +1,58 @@ -module.exports = function() { - const config = require('../config'); - this.processJsonSubredditsExplore = (json, from, subreddit_front, user_preferences) => { - return new Promise(resolve => { - (async () => { - if(from === 'redis') { - json = JSON.parse(json) - } - if(json.error) { - resolve({ error: true, error_data: json }) - } else { - let before = json.data.before - let after = json.data.after +const config = require('../config'); - let ret = { - info: { - before: before, - after: after - }, - links: [] - } +async function processJsonSubredditsExplore( + json, + from, + subreddit_front, + user_preferences +) { + if (from === 'redis') { + json = JSON.parse(json); + } + if (json.error) { + return { error: true, error_data: json }; + } else { + let before = json.data.before; + let after = json.data.after; - let children_len = json.data.children.length + let ret = { + info: { + before: before, + after: after, + }, + links: [], + }; - for(var i = 0; i < children_len; i++) { - let data = json.data.children[i].data - - if(data.over_18) - if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') - continue - - let obj = { - created: data.created_utc, - id: data.id, - over_18: data.over_18, - display_name: data.display_name, - display_name_prefixed: data.display_name_prefixed, - public_description: data.public_description, - url: replaceDomains(data.url, user_preferences), - subscribers: data.subscribers, - over_18: data.over18, - title: data.title, - subreddit_front: subreddit_front, - } - ret.links.push(obj) - } - resolve(ret) - } - })() - }) + let children_len = json.data.children.length; + + for (var i = 0; i < children_len; i++) { + let data = json.data.children[i].data; + + if (data.over_18) + if ( + (config.nsfw_enabled === false && + user_preferences.nsfw_enabled != 'true') || + user_preferences.nsfw_enabled === 'false' + ) + continue; + + let obj = { + created: data.created_utc, + id: data.id, + over_18: data.over_18, + display_name: data.display_name, + display_name_prefixed: data.display_name_prefixed, + public_description: data.public_description, + url: replaceDomains(data.url, user_preferences), + subscribers: data.subscribers, + over_18: data.over18, + title: data.title, + subreddit_front: subreddit_front, + }; + ret.links.push(obj); + } + return ret; } } + +module.exports = processJsonSubredditsExplore; diff --git a/routes/subreddit.js b/routes/subreddit.js index ed0c97b..e6efad2 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -11,8 +11,7 @@ const processSearchResults = require('../inc/processSearchResults.js'); const processJsonSubreddit = require('../inc/processJsonSubreddit.js'); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); -const processSubredditsExplore = - require('../inc/processSubredditsExplore.js')(); +const processJsonSubredditsExplore = require('../inc/processSubredditsExplore.js'); subredditRoutes.get('/r/:subreddit/search', (req, res, next) => { let subreddit = req.params.subreddit;