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,16 +1,135 @@
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) => { async function processReplies(data, post_id, depth, user_preferences) {
return new Promise(resolve => { let return_replies = [];
(async () => { for (var i = 0; i < data.length; i++) {
if(!parsed) { let kind = data[i].kind;
json = JSON.parse(json) 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 if (reply.replies && kind !== 'more') {
let post_id = post.name if (reply.replies.data.children.length) {
let comments = json[1].data.children 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 = { let obj = {
author: post.author, author: post.author,
@ -37,52 +156,69 @@ module.exports = function(fetch) {
crosspost: false, crosspost: false,
selftext: unescape(post.selftext_html), selftext: unescape(post.selftext_html),
poll_data: post.poll_data, poll_data: post.poll_data,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''), link_flair:
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') user_preferences.flairs != 'false' ? await formatLinkFlair(post) : '',
} user_flair:
user_preferences.flairs != 'false' ? await formatUserFlair(post) : '',
};
let valid_embed_video_domains = ['gfycat.com'] let valid_embed_video_domains = ['gfycat.com'];
let has_gif = false let has_gif = false;
let gif_to_mp4 = null let gif_to_mp4 = null;
let reddit_video = null let reddit_video = null;
let embed_video = false let embed_video = false;
if(post.media) if (post.media)
if(valid_embed_video_domains.includes(post.media.type)) if (valid_embed_video_domains.includes(post.media.type)) embed_video = true;
embed_video = true
if(post.preview && !embed_video) { if (post.preview && !embed_video) {
if(post.preview.reddit_video_preview) { if (post.preview.reddit_video_preview) {
if(post.preview.reddit_video_preview.is_gif) { if (post.preview.reddit_video_preview.is_gif) {
has_gif = true has_gif = true;
gif_url = post.preview.reddit_video_preview.fallback_url gif_url = post.preview.reddit_video_preview.fallback_url;
} else { } else {
let file_ext = getFileExtension(post.preview.reddit_video_preview.fallback_url) let file_ext = getFileExtension(
if(file_ext === 'mp4')  { post.preview.reddit_video_preview.fallback_url
post.media = true );
reddit_video = post.preview.reddit_video_preview if (file_ext === 'mp4') {
post.media = true;
reddit_video = post.preview.reddit_video_preview;
} }
} }
} }
if(post.preview.images) { if (post.preview.images) {
if(post.preview.images[0].source) { 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') { if (file_ext === 'gif') {
has_gif = true has_gif = true;
let resolutions = post.preview.images[0].variants.mp4.resolutions let resolutions = post.preview.images[0].variants.mp4.resolutions;
gif_to_mp4 = resolutions[resolutions.length - 1] 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) { if (post.crosspost_parent_list) {
post.crosspost = post.crosspost_parent_list[0] post.crosspost = post.crosspost_parent_list[0];
} }
if(post.crosspost) { 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 = { obj.crosspost = {
author: post.crosspost.author, author: post.crosspost.author,
created: post.crosspost.created_utc, created: post.crosspost.created_utc,
@ -98,57 +234,64 @@ module.exports = function(fetch) {
selftext_crosspost: unescape(post.crosspost.selftext_html), selftext_crosspost: unescape(post.crosspost.selftext_html),
poll_data: post.poll_data, poll_data: post.poll_data,
is_crosspost: true, 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) { if (post.preview && !obj.has_media) {
obj.images = { 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) {
if(obj.media.source === 'external') { if (obj.media.source === 'external') {
if(post.preview) { if (post.preview) {
obj.images = { obj.images = {
source: await downloadAndSave(post.preview.images[0].source.url) source: await downloadAndSave(post.preview.images[0].source.url),
} };
} }
} }
} }
if(post.gallery_data) { if (post.gallery_data) {
obj.gallery = true obj.gallery = true;
obj.gallery_items = [] obj.gallery_items = [];
for(var i = 0; i < post.gallery_data.items.length; i++) { 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]) {
if(post.media_metadata[id].p) { if (post.media_metadata[id].p) {
if(post.media_metadata[id].p[0]) { 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) { if (post.media_metadata[id].s && post.media_metadata[id].p[0].u) {
item = { item = {
type: post.media_metadata[id].e, type: post.media_metadata[id].e,
source: await downloadAndSave(post.media_metadata[id].s.u), source: await downloadAndSave(post.media_metadata[id].s.u),
thumbnail: await downloadAndSave(post.media_metadata[id].p[0].u), thumbnail: await downloadAndSave(
large: await downloadAndSave(post.media_metadata[id].p[post.media_metadata[id].p.length - 1].u), 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++) { for (var i = 0; i < comments.length; i++) {
let comment = comments[i].data let comment = comments[i].data;
let kind = comments[i].kind let kind = comments[i].kind;
let obj = {} let obj = {};
if(kind !== 'more') { if (kind !== 'more') {
obj = { obj = {
author: comment.author, author: comment.author,
body_html: comment.body_html, body_html: comment.body_html,
@ -165,9 +308,15 @@ module.exports = function(fetch) {
edited: comment.edited, edited: comment.edited,
replies: [], replies: [],
depth: comment.depth, depth: comment.depth,
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 = { obj = {
type: 'load_more', type: 'load_more',
@ -175,158 +324,79 @@ module.exports = function(fetch) {
id: comment.id, id: comment.id,
parent_id: comment.parent_id, parent_id: comment.parent_id,
post_id: post.name, 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
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>`
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: [], children: [],
depth: depth };
}
} }
if(reply.replies && kind !== 'more') { if (comment.replies && kind !== 'more') {
if(reply.replies.data.children.length) { if (comment.replies.data) {
for(var j = 0; j < reply.replies.data.children.length; j++) { if (comment.replies.data.children.length > 0) {
let comment = reply.replies.data.children[j].data obj.replies = await processReplies(
let objct = {} comment.replies.data.children,
post_id,
if(comment.author && comment.body_html) { 1,
objct = { user_preferences
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.children) {
if(comment.replies.data) { for (var j = 0; j < comment.children.length; j++) {
if(comment.replies.data.children.length > 0) { obj.children.push(comment.children[j]);
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) comms.push(obj);
}
return return_replies
} }
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,45 +1,53 @@
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) => { async function processJsonSubreddit(
return new Promise(resolve => { json,
(async () => { from,
if(from === 'redis') { subreddit_front,
json = JSON.parse(json) user_preferences,
saved
) {
if (from === 'redis') {
json = JSON.parse(json);
} }
if(json.error) { if (json.error) {
resolve({ error: true, error_data: json }) return { error: true, error_data: json };
} else { } else {
if(saved) { if (saved) {
let t = { let t = {
data: { data: {
before: null, before: null,
after: null, after: null,
children: json children: json,
} },
} };
json = t json = t;
} }
let before = json.data.before let before = json.data.before;
let after = json.data.after let after = json.data.after;
let ret = { let ret = {
info: { info: {
before: before, 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++) { 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 (data.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') if (
continue (config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
/* /*
// Todo: Remove this once the link component is done // 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) : '') 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,59 +1,68 @@
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) { async function processJsonUser(
return new Promise(resolve => { json,
(async () => { parsed,
if(!parsed) { after,
json = JSON.parse(json) before,
user_preferences,
kind,
post_type
) {
if (!parsed) {
json = JSON.parse(json);
} }
let about = json.about.data let about = json.about.data;
let posts = [] let posts = [];
let view_more_posts = false let view_more_posts = false;
let posts_limit = 25 let posts_limit = 25;
let user_front = false let user_front = false;
if(json.overview.data.children.length > posts_limit) { if (json.overview.data.children.length > posts_limit) {
view_more_posts = true view_more_posts = true;
} else { } else {
posts_limit = json.overview.data.children.length posts_limit = json.overview.data.children.length;
} }
if(!after && !before) { if (!after && !before) {
user_front = true user_front = true;
} }
if(json.overview.data.children) { if (json.overview.data.children) {
if(json.overview.data.children[posts_limit - 1]) { 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]) { 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++) { 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 thumbnail = 'self' let thumbnail = 'self';
let type = json.overview.data.children[i].kind let type = json.overview.data.children[i].kind;
let obj let obj;
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, '');
if(type !== kind && kind) if (type !== kind && kind) continue;
continue
if(post.over_18) if (post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') if (
continue (config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
if(type === 't3') { if (type === 't3') {
obj = await link.fromJson(post, user_preferences) obj = await link.fromJson(post, user_preferences);
obj.type = 't3' obj.type = 't3';
} }
if(type === 't1') { if (type === 't1') {
obj = { obj = {
type: type, type: type,
subreddit: post.subreddit, subreddit: post.subreddit,
@ -69,15 +78,16 @@ module.exports = function() {
permalink: post.permalink, permalink: post.permalink,
link_author: post.link_author, link_author: post.link_author,
link_title: post.link_title, 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 = { let obj = {
username: about.name, username: about.name,
icon_img: await downloadAndSave(about.icon_img, "icon_"), icon_img: await downloadAndSave(about.icon_img, 'icon_'),
created: about.created_utc, created: about.created_utc,
verified: about.verified, verified: about.verified,
link_karma: about.link_karma, link_karma: about.link_karma,
@ -87,11 +97,10 @@ module.exports = function() {
post_type: post_type, post_type: post_type,
before: before, before: before,
after: after, after: after,
posts: posts posts: posts,
} };
resolve(obj) return obj;
})()
})
}
} }
module.exports = processJsonUser;

View File

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

View File

@ -1,10 +1,7 @@
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 () => {
if(subreddit && !subreddit.includes('+') && subreddit !== 'all') {
function returnRelevantKeys(json) {
return { return {
title: json.data.title, title: json.data.title,
public_description_html: json.data.public_description_html, public_description_html: json.data.public_description_html,
@ -13,94 +10,58 @@ module.exports = function() {
created_utc: json.data.created_utc, created_utc: json.data.created_utc,
over18: json.data.over18, over18: json.data.over18,
description_html: json.data.description_html, description_html: json.data.description_html,
moderators: json.moderators moderators: json.moderators,
} };
}
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));
} }
let key = `${subreddit}:sidebar` let url = `https://reddit.com/r/${subreddit}/about.json`;
redis.get(key, (error, json) => {
if(error) {
console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error)
resolve(null)
}
if(json) {
json = JSON.parse(json)
resolve(returnRelevantKeys(json))
} else {
let url = `https://reddit.com/r/${subreddit}/about.json`
if(config.use_reddit_oauth) {
url = `https://oauth.reddit.com/r/${subreddit}/about`
}
fetch(encodeURI(url), redditApiGETHeaders())
.then(result => {
if(result.status === 200) {
result.json()
.then(json => {
json.moderators = []
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
if(error) {
console.error('Error setting the sidebar key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched the sidebar from reddit API.')
let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`
if(config.use_reddit_oauth) {
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`
}
resolve(returnRelevantKeys(json))
/*
* The following code is commented out because Reddit doesn't
* anymore support fetching moderators for subreddits
* when not logged in.
* This might change in the future though.
* https://codeberg.org/teddit/teddit/issues/207
*/
/* if (config.use_reddit_oauth) {
fetch(encodeURI(moderators_url), redditApiGETHeaders()) url = `https://oauth.reddit.com/r/${subreddit}/about`;
.then(mod_result => { }
if(mod_result.status === 200) {
mod_result.json() const subredditAboutRequest = await fetch(url, redditApiGETHeaders());
.then(mod_json => {
json.moderators = mod_json if (subredditAboutRequest.ok) {
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { let response = await subredditAboutRequest.json();
if(error) { response.moderators = [];
console.error('Error setting the sidebar with moderators key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies }) await redisAsync.setex(
key,
config.setexs.sidebar,
JSON.stringify(response)
);
console.log(`Fetched sidebar for ${subreddit} from reddit API`);
return returnRelevantKeys(response);
} else { } else {
console.log('Fetched the moderators from reddit API.') console.error(
resolve(returnRelevantKeys(json)) `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 { } else {
console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} ${mod_result.statusText}`) return null;
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,33 +1,40 @@
module.exports = function() { const config = require('../config');
const config = require('../config');
this.processJsonSubredditsExplore = (json, from, subreddit_front, user_preferences) => { async function processJsonSubredditsExplore(
return new Promise(resolve => { json,
(async () => { from,
if(from === 'redis') { subreddit_front,
json = JSON.parse(json) user_preferences
) {
if (from === 'redis') {
json = JSON.parse(json);
} }
if(json.error) { if (json.error) {
resolve({ error: true, error_data: json }) return { error: true, error_data: json };
} else { } else {
let before = json.data.before let before = json.data.before;
let after = json.data.after let after = json.data.after;
let ret = { let ret = {
info: { info: {
before: before, 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++) { 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 (data.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') if (
continue (config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
let obj = { let obj = {
created: data.created_utc, created: data.created_utc,
@ -41,12 +48,11 @@ module.exports = function() {
over_18: data.over18, over_18: data.over18,
title: data.title, title: data.title,
subreddit_front: subreddit_front, subreddit_front: subreddit_front,
};
ret.links.push(obj);
} }
ret.links.push(obj) return ret;
}
resolve(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) => { module.exports = function () {
if(!config.api_enabled) { const config = require('../../config');
res.setHeader('Content-Type', 'application/json') this.handleTedditApiSubreddit = 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,
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') console.log('Teddit API request - subreddit');
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)
if(api_type === 'rss') { if (api_type === 'rss') {
let protocol = (config.https_enabled ? 'https' : 'http') let protocol = config.https_enabled ? 'https' : 'http';
let items = '' let items = '';
for(var i = 0; i < json.data.children.length; i++) { for (var i = 0; i < json.data.children.length; i++) {
let link = json.data.children[i].data let link = json.data.children[i].data;
let thumbnail = '' let thumbnail = '';
let post_image = '' let post_image = '';
let is_self_link = false let is_self_link = false;
let valid_reddit_self_domains = ['reddit.com'] let valid_reddit_self_domains = ['reddit.com'];
if(link.domain) { if (link.domain) {
let tld = link.domain.split('self.') let tld = link.domain.split('self.');
if(tld.length > 1) { if (tld.length > 1) {
if(!tld[1].includes('.')) { if (!tld[1].includes('.')) {
is_self_link = true is_self_link = true;
link.url = teddifyUrl(link.url) 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) if (!post_image) post_image = thumbnail;
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,
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++) { for (var i = 0; i < processed_json.links.length; i++) {
let link = processed_json.links[i] let link = processed_json.links[i];
let valid_reddit_self_domains = ['reddit.com'] let valid_reddit_self_domains = ['reddit.com'];
let is_self_link = false let is_self_link = false;
if(link.domain) { if (link.domain) {
let tld = link.domain.split('self.') let tld = link.domain.split('self.');
if(tld.length > 1) { if (tld.length > 1) {
if(!tld[1].includes('.')) { if (!tld[1].includes('.')) {
is_self_link = true is_self_link = true;
link.url = teddifyUrl(link.url) 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,73 +1,85 @@
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>
@ -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 = '';