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 compression = require('compression');
const express = require('express'); const express = require('express');
const cookieParser = require('cookie-parser'); const cookieParser = require('cookie-parser');
const r = require('redis'); const { redis } = require('./inc/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 nodeFetch = require('node-fetch'); const nodeFetch = require('node-fetch');
const fetch = config.http_proxy 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'); const cacheControl = require('./cacheControl.js');
cacheControl.removeCacheFiles(); cacheControl.removeCacheFiles();

View File

@ -1,155 +1,58 @@
module.exports = function(fetch) { const compilePostComments = require('./compilePostComments.js')();
var compilePostComments = require('./compilePostComments.js')(); const procPostMedia = require('./processPostMedia.js')();
var procPostMedia = require('./processPostMedia.js')();
this.processJsonPost = (json, parsed, user_preferences) => {
return new Promise(resolve => {
(async () => {
if(!parsed) {
json = JSON.parse(json)
}
let post = json[0].data.children[0].data async function processReplies(data, post_id, depth, user_preferences) {
let post_id = post.name let return_replies = [];
let comments = json[1].data.children 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 = { if (reply.replies && kind !== 'more') {
author: post.author, if (reply.replies.data.children.length) {
created: post.created_utc, for (var j = 0; j < reply.replies.data.children.length; j++) {
edited: post.edited, let comment = reply.replies.data.children[j].data;
is_video: post.is_video, let objct = {};
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'] if (comment.author && comment.body_html) {
let has_gif = false objct = {
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, author: comment.author,
body_html: comment.body_html, body_html: comment.body_html,
parent_id: comment.parent_id, parent_id: comment.parent_id,
@ -159,174 +62,341 @@ module.exports = function(fetch) {
ups: comment.ups, ups: comment.ups,
id: comment.id, id: comment.id,
permalink: teddifyUrl(comment.permalink), permalink: teddifyUrl(comment.permalink),
stickied: comment.stickied,
distinguished: comment.distinguished,
score_hidden: comment.score_hidden, score_hidden: comment.score_hidden,
edited: comment.edited, distinguished: comment.distinguished,
distinguished: comment.edited,
replies: [], replies: [],
depth: comment.depth, depth: depth + 1,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''), user_flair:
controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '') user_preferences.flairs != 'false'
} ? await formatUserFlair(comment)
: '',
controversiality:
user_preferences.highlight_controversial != 'false'
? comment.controversiality
: '',
};
} else { } else {
obj = { objct = {
type: 'load_more', type: 'load_more',
count: comment.count, count: comment.count,
id: comment.id, id: comment.id,
parent_id: comment.parent_id, parent_id: comment.parent_id,
post_id: post.name, post_id: post_id,
children: [] children: [],
} depth: depth + 1,
} };
if (comment.children) {
if(comment.replies && kind !== 'more') { for (var k = 0; k < comment.children.length; k++) {
if(comment.replies.data) { objct.children.push(comment.children[k]);
if(comment.replies.data.children.length > 0) {
obj.replies = await processReplies(comment.replies.data.children, post_id, 1, user_preferences)
} }
} }
} }
if(comment.children) { if (comment.replies) {
for(var j = 0; j < comment.children.length; j++) { if (comment.replies.data) {
obj.children.push(comment.children[j]) 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 = `<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
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 += `</div>` if (reply.children) {
for (var j = 0; j < reply.children.length; j++) {
delete processed_json['comments'] obj.children.push(reply.children[j]);
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
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 = `<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;
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 += `</div>`;
delete processed_json['comments'];
let post_data = processed_json;
return { post_data: post_data, comments: comments_html };
}
module.exports = {
processReplies,
processJsonPost,
finalizeJsonPost,
};

View File

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

View File

@ -1,97 +1,106 @@
module.exports = function() { const config = require('../config');
const config = require('../config'); const link = require('./components/link');
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)
}
let about = json.about.data async function processJsonUser(
let posts = [] json,
let view_more_posts = false parsed,
let posts_limit = 25 after,
let user_front = false before,
user_preferences,
if(json.overview.data.children.length > posts_limit) { kind,
view_more_posts = true post_type
} else { ) {
posts_limit = json.overview.data.children.length if (!parsed) {
} json = JSON.parse(json);
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)
})()
})
} }
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;

View File

@ -1,62 +1,70 @@
module.exports = function() { const config = require('../config');
const config = require('../config'); const link = require('./components/link');
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
if(json.data.children.length > posts_limit) { async function processSearchResults(
view_more_posts = true json,
} else { parsed,
posts_limit = json.data.children.length after,
} before,
user_preferences
for(var i = 0; i < posts_limit; i++) { ) {
let post = json.data.children[i].data if (!parsed) {
json = JSON.parse(json);
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)
})()
})
} }
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;

View File

@ -1,106 +1,67 @@
module.exports = function() { const config = require('../config');
const config = require('../config') const { redisAsync } = require('./redis');
this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => {
return new Promise(resolve => { function returnRelevantKeys(json) {
(async () => { return {
if(subreddit && !subreddit.includes('+') && subreddit !== 'all') { title: json.data.title,
function returnRelevantKeys(json) { public_description_html: json.data.public_description_html,
return { active_user_count: json.data.active_user_count,
title: json.data.title, subscribers: json.data.subscribers,
public_description_html: json.data.public_description_html, created_utc: json.data.created_utc,
active_user_count: json.data.active_user_count, over18: json.data.over18,
subscribers: json.data.subscribers, description_html: json.data.description_html,
created_utc: json.data.created_utc, moderators: json.moderators,
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') {
const key = `${subreddit}:sidebar`;
let key = `${subreddit}:sidebar`
redis.get(key, (error, json) => { try {
if(error) { const cached = await redisAsync.get(key);
console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error)
resolve(null) if (cached !== null) {
} return returnRelevantKeys(JSON.parse(cached));
if(json) { }
json = JSON.parse(json)
resolve(returnRelevantKeys(json)) let url = `https://reddit.com/r/${subreddit}/about.json`;
} else {
let url = `https://reddit.com/r/${subreddit}/about.json` if (config.use_reddit_oauth) {
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()) const subredditAboutRequest = await fetch(url, redditApiGETHeaders());
.then(result => {
if(result.status === 200) { if (subredditAboutRequest.ok) {
result.json() let response = await subredditAboutRequest.json();
.then(json => { response.moderators = [];
json.moderators = []
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { await redisAsync.setex(
if(error) { key,
console.error('Error setting the sidebar key to redis.', error) config.setexs.sidebar,
return res.render('index', { json: null, user_preferences: req.cookies }) JSON.stringify(response)
} else { );
console.log('Fetched the sidebar from reddit API.')
let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json` console.log(`Fetched sidebar for ${subreddit} from reddit API`);
if(config.use_reddit_oauth) {
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators` return returnRelevantKeys(response);
} } else {
resolve(returnRelevantKeys(json)) console.error(
/* `Something went wrong while fetching data from reddit API:
* The following code is commented out because Reddit doesn't ${subredditAboutRequest.status} ${subredditAboutRequest.statusText}`
* anymore support fetching moderators for subreddits );
* when not logged in. console.error(config.reddit_api_error_text);
* This might change in the future though. return null;
* https://codeberg.org/teddit/teddit/issues/207 }
*/ } catch (error) {
console.error('Error fetching the sidebar: ', error);
/*
fetch(encodeURI(moderators_url), redditApiGETHeaders()) return null;
.then(mod_result => { }
if(mod_result.status === 200) { } else {
mod_result.json() return null;
.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))
})
*/
}
})
})
} 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)
}
})()
})
} }
} }
module.exports = processSubredditAbout;

View File

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

View File

@ -1,74 +1,86 @@
module.exports = function() { const processJsonUser = require('../processJsonUser');
const config = require('../../config')
this.handleTedditApiUser = async (json, req, res, from, api_type, api_target, user) => { module.exports = function () {
if(!config.api_enabled) { const config = require('../../config');
res.setHeader('Content-Type', 'application/json') this.handleTedditApiUser = async (
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.' } json,
return res.end(JSON.stringify(msg)) 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') console.log('Teddit API request - user');
let _json = json // Keep the original json let _json = json; // Keep the original json
if(from === 'redis') if (from === 'redis') json = JSON.parse(json);
json = JSON.parse(json)
let protocol = config.https_enabled ? 'https' : 'http';
let protocol = (config.https_enabled ? 'https' : 'http') let link = `${protocol}://${config.domain}/user/${user}`;
let link = `${protocol}://${config.domain}/user/${user}`
if (api_type === 'rss') {
if(api_type === 'rss') { let items = '';
let items = '' let posts_limit = 25;
let posts_limit = 25
if (json.overview.data.children.length <= posts_limit) {
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++) { for (var i = 0; i < posts_limit; i++) {
let post = json.overview.data.children[i].data let post = json.overview.data.children[i].data;
let post_id = post.permalink.split('/').slice(-2)[0] + '/' let post_id = post.permalink.split('/').slice(-2)[0] + '/';
let url = post.permalink.replace(post_id, '') let url = post.permalink.replace(post_id, '');
let permalink = `${protocol}://${config.domain}${post.permalink}` let permalink = `${protocol}://${config.domain}${post.permalink}`;
let comments_url = `${protocol}://${config.domain}${url}` let comments_url = `${protocol}://${config.domain}${url}`;
let kind = json.overview.data.children[i].kind let kind = json.overview.data.children[i].kind;
let t1_elements = '' let t1_elements = '';
let t3_elements = '' let t3_elements = '';
if(kind === 't1') { 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 = ` 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> <url>${comments_url}</url>
` `;
} }
if(kind === 't3') { if (kind === 't3') {
let s = await downloadAndSave(post.thumbnail, 'thumb_') let s = await downloadAndSave(post.thumbnail, 'thumb_');
let thumbnail = '' let thumbnail = '';
let enclosure = '' let enclosure = '';
if(s !== 'self' && s != '') { if (s !== 'self' && s != '') {
let img = `${protocol}://${config.domain}${s}` let img = `${protocol}://${config.domain}${s}`;
thumbnail = `<thumbnail>${img}</thumbnail>` thumbnail = `<thumbnail>${img}</thumbnail>`;
let mime = '' let mime = '';
let ext = s.split('.').pop() let ext = s.split('.').pop();
if(ext === 'png') if (ext === 'png') mime = 'image/png';
mime = 'image/png' else mime = 'image/jpeg';
else enclosure = `<enclosure length="0" type="${mime}" url="${img}" />`;
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>` 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>` append_desc_html += `<br/><a href="${permalink}">[comments]</a>`;
t3_elements = ` t3_elements = `
<description><![CDATA[${unescape(post.selftext_html)}${append_desc_html}]]></description> <description><![CDATA[${unescape(
post.selftext_html
)}${append_desc_html}]]></description>
${thumbnail} ${thumbnail}
${enclosure} ${enclosure}
` `;
} }
let title = post.title let title = post.title;
if(!post.title) if (!post.title) title = post.link_title;
title = post.link_title
items += ` items += `
<item> <item>
<title>${title}</title> <title>${title}</title>
@ -76,7 +88,9 @@ module.exports = function() {
<kind>${kind}</kind> <kind>${kind}</kind>
<subreddit>${post.subreddit}</subreddit> <subreddit>${post.subreddit}</subreddit>
<created>${post.created_utc}</created> <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> <ups>${post.ups}</ups>
<link>${permalink}</link> <link>${permalink}</link>
<edited>${post.edited}</edited> <edited>${post.edited}</edited>
@ -85,11 +99,10 @@ module.exports = function() {
${t1_elements} ${t1_elements}
${t3_elements} ${t3_elements}
</item> </item>
` `;
} }
let xml_output = let xml_output = `<?xml version="1.0" encoding="UTF-8"?>
`<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel> <channel>
<atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" /> <atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" />
@ -97,17 +110,23 @@ module.exports = function() {
<link>${link}</link> <link>${link}</link>
${items} ${items}
</channel> </channel>
</rss>` </rss>`;
res.setHeader('Content-Type', 'application/rss+xml') res.setHeader('Content-Type', 'application/rss+xml');
return res.end(xml_output) return res.end(xml_output);
} else { } else {
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json');
if(api_target === 'reddit') { if (api_target === 'reddit') {
return res.end(JSON.stringify(json)) return res.end(JSON.stringify(json));
} else { } else {
let processed_json = await processJsonUser(json, true, null, null, req.cookies) let processed_json = await processJsonUser(
return res.end(JSON.stringify(processed_json)) 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 { redis, fetch } = require('../app');
const homeRoute = require('express').Router(); const homeRoute = require('express').Router();
const processUser = require('../inc/processJsonUser.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.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 tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
require('../inc/processSubredditsExplore.js')();
homeRoute.get('/:sort?', async (req, res, next) => { homeRoute.get('/:sort?', async (req, res, next) => {
let past = req.query.t; let past = req.query.t;

View File

@ -2,16 +2,9 @@ const config = require('../config');
const { redis, fetch } = require('../app'); const { redis, fetch } = require('../app');
const saveRoutes = require('express').Router(); const saveRoutes = require('express').Router();
const processUser = require('../inc/processJsonUser.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.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 tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
require('../inc/processSubredditsExplore.js')();
saveRoutes.get('/saved', (req, res, next) => { saveRoutes.get('/saved', (req, res, next) => {
let saved = req.cookies.saved; let saved = req.cookies.saved;

View File

@ -2,16 +2,16 @@ const config = require('../config');
const { redis, fetch, RedditAPI } = require('../app'); const { redis, fetch, RedditAPI } = require('../app');
const subredditRoutes = require('express').Router(); const subredditRoutes = require('express').Router();
const processUser = require('../inc/processJsonUser.js')(); const {
const processPost = require('../inc/processJsonPost.js')(); processJsonPost,
const processAbout = require('../inc/processSubredditAbout.js')(); finalizeJsonPost,
const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); } = require('../inc/processJsonPost.js');
const processSearches = require('../inc/processSearchResults.js')(); const processSubredditAbout = require('../inc/processSubredditAbout.js');
const processSubreddit = require('../inc/processJsonSubreddit.js')(); const processSearchResults = require('../inc/processSearchResults.js');
const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore = const processJsonSubredditsExplore = require('../inc/processSubredditsExplore.js');
require('../inc/processSubredditsExplore.js')();
subredditRoutes.get('/r/:subreddit/search', (req, res, next) => { subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
let subreddit = req.params.subreddit; let subreddit = req.params.subreddit;

View File

@ -2,16 +2,11 @@ const config = require('../config');
const { redis, fetch } = require('../app'); const { redis, fetch } = require('../app');
const userRoutes = require('express').Router(); 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')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')();
const processSearches = require('../inc/processSearchResults.js')(); const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const processSubreddit = require('../inc/processJsonSubreddit.js')();
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
require('../inc/processSubredditsExplore.js')();
userRoutes.get('/user/:user/:kind?', (req, res, next) => { userRoutes.get('/user/:user/:kind?', (req, res, next) => {
let kind = ''; let kind = '';