Merge pull request 'Use async functions' (#239) from json/teddit:async-await into main

Reviewed-on: https://codeberg.org/teddit/teddit/pulls/239
This commit is contained in:
teddit 2021-09-09 18:17:00 +02:00
commit 4cc8dd7bd5
15 changed files with 1040 additions and 942 deletions

3
.prettierrc Normal file
View File

@ -0,0 +1,3 @@
{
"singleQuote": true
}

42
app.js
View File

@ -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();

View File

@ -1,16 +1,135 @@
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')();
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 post = json[0].data.children[0].data
let post_id = post.name
let comments = json[1].data.children
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;
}
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,
@ -37,52 +156,69 @@ module.exports = function(fetch) {
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) : '')
}
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
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 (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
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
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)
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]
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)
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]
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 = 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,
@ -98,55 +234,62 @@ module.exports = function(fetch) {
selftext_crosspost: unescape(post.crosspost.selftext_html),
poll_data: post.poll_data,
is_crosspost: true,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
}
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)
}
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)
}
source: await downloadAndSave(post.preview.images[0].source.url),
};
}
}
}
if (post.gallery_data) {
obj.gallery = true
obj.gallery_items = []
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
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 }
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),
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)
obj.gallery_items.push(item);
}
}
}
}
}
let comms = []
let comms = [];
for (var i = 0; i < comments.length; i++) {
let comment = comments[i].data
let kind = comments[i].kind
let obj = {}
let comment = comments[i].data;
let kind = comments[i].kind;
let obj = {};
if (kind !== 'more') {
obj = {
@ -165,9 +308,15 @@ module.exports = function(fetch) {
edited: comment.edited,
replies: [],
depth: comment.depth,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''),
controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '')
}
user_flair:
user_preferences.flairs != 'false'
? await formatUserFlair(comment)
: '',
controversiality:
user_preferences.highlight_controversial != 'false'
? comment.controversiality
: '',
};
} else {
obj = {
type: 'load_more',
@ -175,158 +324,79 @@ module.exports = function(fetch) {
id: comment.id,
parent_id: comment.parent_id,
post_id: post.name,
children: []
}
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)
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])
obj.children.push(comment.children[j]);
}
}
comms.push(obj)
comms.push(obj);
}
obj.comments = comms
obj.comments = comms;
resolve(obj)
})()
})
return obj;
}
this.finalizeJsonPost = async (processed_json, post_id, post_url, morechildren_ids, viewing_comment, user_preferences) => {
let comments_html = `<div class="comments">`
let comments = processed_json.comments
let last_known_depth = undefined
async function finalizeJsonPost(
processed_json,
post_id,
post_url,
morechildren_ids,
viewing_comment,
user_preferences
) {
let comments_html = `<div class="comments">`;
let comments = processed_json.comments;
let last_known_depth = undefined;
for (var i = 0; i < comments.length; i++) {
let next_comment = false
let next_comment = false;
if (comments[i + 1]) {
next_comment = comments[i+1]
next_comment = comments[i + 1];
}
if (comments[i].depth != undefined) {
last_known_depth = comments[i].depth
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 += await compilePostCommentsHtml(
comments[i],
next_comment,
post_id,
post_url,
morechildren_ids,
processed_json.author,
viewing_comment,
user_preferences,
last_known_depth
);
}
comments_html += `</div>`
comments_html += `</div>`;
delete processed_json['comments']
let post_data = processed_json
return { post_data: post_data, comments: 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.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
}
}
module.exports = {
processReplies,
processJsonPost,
finalizeJsonPost,
};

View File

@ -1,45 +1,53 @@
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 () => {
const link = require('./components/link');
async function processJsonSubreddit(
json,
from,
subreddit_front,
user_preferences,
saved
) {
if (from === 'redis') {
json = JSON.parse(json)
json = JSON.parse(json);
}
if (json.error) {
resolve({ error: true, error_data: json })
return { error: true, error_data: json };
} else {
if (saved) {
let t = {
data: {
data: {
before: null,
after: null,
children: json
}
}
json = t
children: json,
},
};
json = t;
}
let before = json.data.before
let after = json.data.after
let before = json.data.before;
let after = json.data.after;
let ret = {
info: {
before: before,
after: after
after: after,
},
links: []
}
links: [],
};
let children_len = json.data.children.length
let children_len = json.data.children.length;
for (var i = 0; i < children_len; i++) {
let data = json.data.children[i].data
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
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
/*
// Todo: Remove this once the link component is done
@ -71,13 +79,12 @@ module.exports = function() {
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
} */
let obj = await link.fromJson(data, user_preferences, subreddit_front)
let obj = await link.fromJson(data, user_preferences, subreddit_front);
ret.links.push(obj)
ret.links.push(obj);
}
resolve(ret)
}
})()
})
return ret;
}
}
module.exports = processJsonSubreddit;

View File

@ -1,57 +1,66 @@
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 () => {
const link = require('./components/link');
async function processJsonUser(
json,
parsed,
after,
before,
user_preferences,
kind,
post_type
) {
if (!parsed) {
json = JSON.parse(json)
json = JSON.parse(json);
}
let about = json.about.data
let posts = []
let view_more_posts = false
let posts_limit = 25
let user_front = false
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
view_more_posts = true;
} else {
posts_limit = json.overview.data.children.length
posts_limit = json.overview.data.children.length;
}
if (!after && !before) {
user_front = true
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
after = json.overview.data.children[posts_limit - 1].data.name;
}
if (json.overview.data.children[0]) {
before = json.overview.data.children[0].data.name
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 = 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, '')
let post_id = post.permalink.split('/').slice(-2)[0] + '/';
let url = post.permalink.replace(post_id, '');
if(type !== kind && kind)
continue
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 (
(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'
obj = await link.fromJson(post, user_preferences);
obj.type = 't3';
}
if (type === 't1') {
obj = {
@ -69,15 +78,16 @@ module.exports = function() {
permalink: post.permalink,
link_author: post.link_author,
link_title: post.link_title,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
user_flair:
user_preferences.flairs != 'false' ? await formatUserFlair(post) : '',
};
}
}
posts.push(obj)
posts.push(obj);
}
let obj = {
username: about.name,
icon_img: await downloadAndSave(about.icon_img, "icon_"),
icon_img: await downloadAndSave(about.icon_img, 'icon_'),
created: about.created_utc,
verified: about.verified,
link_karma: about.link_karma,
@ -87,11 +97,10 @@ module.exports = function() {
post_type: post_type,
before: before,
after: after,
posts: posts
posts: posts,
};
return obj;
}
resolve(obj)
})()
})
}
}
module.exports = processJsonUser;

View File

@ -1,49 +1,58 @@
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 () => {
const link = require('./components/link');
async function processSearchResults(
json,
parsed,
after,
before,
user_preferences
) {
if (!parsed) {
json = JSON.parse(json)
json = JSON.parse(json);
}
let posts = []
let search_firstpage = false
let before = json.data.before
let after = json.data.after
let posts = [];
let search_firstpage = false;
before = json.data.before;
after = json.data.after;
if (!after && !before) {
search_firstpage = true
search_firstpage = true;
}
let suggested_subreddits = false
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
suggested_subreddits = json.suggested_subreddits.data.children;
}
}
}
if (json.data.children) {
let view_more_posts = false
let posts_limit = 25
let view_more_posts = false;
let posts_limit = 25;
if (json.data.children.length > posts_limit) {
view_more_posts = true
view_more_posts = true;
} else {
posts_limit = json.data.children.length
posts_limit = json.data.children.length;
}
for (var i = 0; i < posts_limit; i++) {
let post = json.data.children[i].data
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
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 = await link.fromJson(post, user_preferences);
posts.push(obj);
}
}
@ -53,10 +62,9 @@ module.exports = function() {
after: after,
posts: posts,
suggested_subreddits: suggested_subreddits,
};
return obj;
}
resolve(obj)
})()
})
}
}
module.exports = processSearchResults;

View File

@ -1,9 +1,6 @@
module.exports = function() {
const config = require('../config')
this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => {
return new Promise(resolve => {
(async () => {
if(subreddit && !subreddit.includes('+') && subreddit !== 'all') {
const config = require('../config');
const { redisAsync } = require('./redis');
function returnRelevantKeys(json) {
return {
title: json.data.title,
@ -13,94 +10,58 @@ module.exports = function() {
created_utc: json.data.created_utc,
over18: json.data.over18,
description_html: json.data.description_html,
moderators: json.moderators
}
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)
async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) {
if (subreddit && !subreddit.includes('+') && subreddit !== 'all') {
const key = `${subreddit}:sidebar`;
try {
const cached = await redisAsync.get(key);
if (cached !== null) {
return returnRelevantKeys(JSON.parse(cached));
}
if(json) {
json = JSON.parse(json)
resolve(returnRelevantKeys(json))
} else {
let url = `https://reddit.com/r/${subreddit}/about.json`
let url = `https://reddit.com/r/${subreddit}/about.json`;
if (config.use_reddit_oauth) {
url = `https://oauth.reddit.com/r/${subreddit}/about`
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 })
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.log('Fetched the moderators from reddit API.')
resolve(returnRelevantKeys(json))
console.error(
`Something went wrong while fetching data from reddit API:
${subredditAboutRequest.status} ${subredditAboutRequest.statusText}`
);
console.error(config.reddit_api_error_text);
return null;
}
} catch (error) {
console.error('Error fetching the sidebar: ', error);
return null;
}
})
})
} 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))
})
*/
}
})
})
} 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 {
resolve(null)
}
})()
})
return null;
}
}
module.exports = processSubredditAbout;

View File

@ -1,33 +1,40 @@
module.exports = function() {
const config = require('../config');
this.processJsonSubredditsExplore = (json, from, subreddit_front, user_preferences) => {
return new Promise(resolve => {
(async () => {
async function processJsonSubredditsExplore(
json,
from,
subreddit_front,
user_preferences
) {
if (from === 'redis') {
json = JSON.parse(json)
json = JSON.parse(json);
}
if (json.error) {
resolve({ error: true, error_data: json })
return { error: true, error_data: json };
} else {
let before = json.data.before
let after = json.data.after
let before = json.data.before;
let after = json.data.after;
let ret = {
info: {
before: before,
after: after
after: after,
},
links: []
}
links: [],
};
let children_len = json.data.children.length
let children_len = json.data.children.length;
for (var i = 0; i < children_len; i++) {
let data = json.data.children[i].data
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
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
let obj = {
created: data.created_utc,
@ -41,12 +48,11 @@ module.exports = function() {
over_18: data.over18,
title: data.title,
subreddit_front: subreddit_front,
};
ret.links.push(obj);
}
ret.links.push(obj)
}
resolve(ret)
}
})()
})
return ret;
}
}
module.exports = processJsonSubredditsExplore;

51
inc/redis.js Normal file
View File

@ -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,
};

View File

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

View File

@ -1,73 +1,85 @@
const processJsonUser = require('../processJsonUser');
module.exports = function () {
const config = require('../../config')
this.handleTedditApiUser = async (json, req, res, from, api_type, api_target, user) => {
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))
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)
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}`
let protocol = config.https_enabled ? 'https' : 'http';
let link = `${protocol}://${config.domain}/user/${user}`;
if (api_type === 'rss') {
let items = ''
let posts_limit = 25
let items = '';
let posts_limit = 25;
if (json.overview.data.children.length <= posts_limit) {
posts_limit = json.overview.data.children.length
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 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 = ''
let t1_elements = '';
let t3_elements = '';
if (kind === 't1') {
let append_desc_html = `<br/><a href="${permalink}">[link]</a> <a href="${comments_url}">[comments]</a>`
let append_desc_html = `<br/><a href="${permalink}">[link]</a> <a href="${comments_url}">[comments]</a>`;
t1_elements = `
<description><![CDATA[${unescape(post.body_html)}${append_desc_html}]]></description>
<description><![CDATA[${unescape(
post.body_html
)}${append_desc_html}]]></description>
<url>${comments_url}</url>
`
`;
}
if (kind === 't3') {
let s = await downloadAndSave(post.thumbnail, 'thumb_')
let thumbnail = ''
let enclosure = ''
let s = await downloadAndSave(post.thumbnail, 'thumb_');
let thumbnail = '';
let enclosure = '';
if (s !== 'self' && s != '') {
let img = `${protocol}://${config.domain}${s}`
thumbnail = `<thumbnail>${img}</thumbnail>`
let img = `${protocol}://${config.domain}${s}`;
thumbnail = `<thumbnail>${img}</thumbnail>`;
let mime = ''
let ext = s.split('.').pop()
if(ext === 'png')
mime = 'image/png'
else
mime = 'image/jpeg'
enclosure = `<enclosure length="0" type="${mime}" url="${img}" />`
let mime = '';
let ext = s.split('.').pop();
if (ext === 'png') mime = 'image/png';
else mime = 'image/jpeg';
enclosure = `<enclosure length="0" type="${mime}" url="${img}" />`;
}
let append_desc_html = `submitted by <a href="${protocol}://${config.domain}/u/${user}>/u/${user}</a> to <a href="${protocol}://${config.domain}/r/${post.subreddit}/">r/${post.subreddit}</a>`
append_desc_html += `<br/><a href="${permalink}">[comments]</a>`
let append_desc_html = `submitted by <a href="${protocol}://${config.domain}/u/${user}>/u/${user}</a> to <a href="${protocol}://${config.domain}/r/${post.subreddit}/">r/${post.subreddit}</a>`;
append_desc_html += `<br/><a href="${permalink}">[comments]</a>`;
t3_elements = `
<description><![CDATA[${unescape(post.selftext_html)}${append_desc_html}]]></description>
<description><![CDATA[${unescape(
post.selftext_html
)}${append_desc_html}]]></description>
${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 += `
<item>
@ -76,7 +88,9 @@ module.exports = function() {
<kind>${kind}</kind>
<subreddit>${post.subreddit}</subreddit>
<created>${post.created_utc}</created>
<pubDate>${new Date(post.created_utc*1000).toGMTString()}</pubDate>
<pubDate>${new Date(
post.created_utc * 1000
).toGMTString()}</pubDate>
<ups>${post.ups}</ups>
<link>${permalink}</link>
<edited>${post.edited}</edited>
@ -85,11 +99,10 @@ module.exports = function() {
${t1_elements}
${t3_elements}
</item>
`
`;
}
let xml_output =
`<?xml version="1.0" encoding="UTF-8"?>
let xml_output = `<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" />
@ -97,17 +110,23 @@ module.exports = function() {
<link>${link}</link>
${items}
</channel>
</rss>`
res.setHeader('Content-Type', 'application/rss+xml')
return res.end(xml_output)
</rss>`;
res.setHeader('Content-Type', 'application/rss+xml');
return res.end(xml_output);
} else {
res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-Type', 'application/json');
if (api_target === 'reddit') {
return res.end(JSON.stringify(json))
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));
}
}
};
};

View File

@ -2,16 +2,9 @@ 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')();
const processSearches = 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 =
require('../inc/processSubredditsExplore.js')();
homeRoute.get('/:sort?', async (req, res, next) => {
let past = req.query.t;

View File

@ -2,16 +2,9 @@ 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')();
const processSearches = 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 =
require('../inc/processSubredditsExplore.js')();
saveRoutes.get('/saved', (req, res, next) => {
let saved = req.cookies.saved;

View File

@ -2,16 +2,16 @@ 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')();
const processSearches = require('../inc/processSearchResults.js')();
const processSubreddit = require('../inc/processJsonSubreddit.js')();
const {
processJsonPost,
finalizeJsonPost,
} = require('../inc/processJsonPost.js');
const processSubredditAbout = require('../inc/processSubredditAbout.js');
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;

View File

@ -2,16 +2,11 @@ const config = require('../config');
const { redis, fetch } = require('../app');
const userRoutes = require('express').Router();
const processUser = require('../inc/processJsonUser.js')();
const processPost = require('../inc/processJsonPost.js')();
const processAbout = require('../inc/processSubredditAbout.js')();
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 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 = '';