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

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

3
.prettierrc Normal file
View File

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

42
app.js
View File

@ -12,41 +12,7 @@ const pug = require('pug');
const compression = require('compression');
const express = require('express');
const cookieParser = require('cookie-parser');
const r = require('redis');
const redis = (() => {
if (!config.redis_enabled) {
// Stub Redis if disabled
return {
get: (_, callback) => callback(null, null),
setex: (_, _1, _2, callback) => callback(null),
on: () => {},
};
}
const redisOptions = {
host: '127.0.0.1',
port: 6379,
};
if (config.redis_db) {
redisOptions.db = config.redis_db;
}
if (config.redis_host) {
redisOptions.host = config.redis_host;
}
if (config.redis_port && config.redis_port > 0) {
redisOptions.port = config.redis_port;
}
if (config.redis_password) {
redisOptions.password = config.redis_password;
}
return r.createClient(redisOptions);
})();
const { redis } = require('./inc/redis');
const nodeFetch = require('node-fetch');
const fetch = config.http_proxy
@ -162,12 +128,6 @@ if (config.redirect_http_to_https) {
});
}
redis.on('error', (error) => {
if (error) {
console.error(`Redis error: ${error}`);
}
});
const cacheControl = require('./cacheControl.js');
cacheControl.removeCacheFiles();

View File

@ -1,155 +1,58 @@
module.exports = function(fetch) {
var compilePostComments = require('./compilePostComments.js')();
var procPostMedia = require('./processPostMedia.js')();
this.processJsonPost = (json, parsed, user_preferences) => {
return new Promise(resolve => {
(async () => {
if(!parsed) {
json = JSON.parse(json)
}
const compilePostComments = require('./compilePostComments.js')();
const procPostMedia = require('./processPostMedia.js')();
let post = json[0].data.children[0].data
let post_id = post.name
let comments = json[1].data.children
async function processReplies(data, post_id, depth, user_preferences) {
let return_replies = [];
for (var i = 0; i < data.length; i++) {
let kind = data[i].kind;
let reply = data[i].data;
let obj = {};
if (kind !== 'more') {
obj = {
author: reply.author,
body_html: reply.body_html,
parent_id: reply.parent_id,
created: reply.created_utc,
edited: reply.edited,
score: reply.score,
ups: reply.ups,
id: reply.id,
permalink: teddifyUrl(reply.permalink),
stickied: reply.stickied,
distinguished: reply.distinguished,
score_hidden: reply.score_hidden,
edited: reply.edited,
replies: [],
depth: depth,
user_flair:
user_preferences.flairs != 'false'
? await formatUserFlair(reply)
: '',
controversiality:
user_preferences.highlight_controversial != 'false'
? reply.controversiality
: '',
};
} else {
obj = {
type: 'load_more',
count: reply.count,
id: reply.id,
parent_id: reply.parent_id,
post_id: post_id,
children: [],
depth: depth,
};
}
let obj = {
author: post.author,
created: post.created_utc,
edited: post.edited,
is_video: post.is_video,
locked: post.locked,
link_flair_text: post.link_flair_text,
name: post_id,
num_comments: post.num_comments,
over_18: post.over_18,
permalink: teddifyUrl(post.permalink),
title: post.title,
url: teddifyUrl(post.url, user_preferences),
ups: post.ups,
id: post.id,
domain: post.domain,
contest_mode: post.contest_mode,
upvote_ratio: post.upvote_ratio,
comments: null,
has_media: false,
media: null,
images: null,
crosspost: false,
selftext: unescape(post.selftext_html),
poll_data: post.poll_data,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
}
if (reply.replies && kind !== 'more') {
if (reply.replies.data.children.length) {
for (var j = 0; j < reply.replies.data.children.length; j++) {
let comment = reply.replies.data.children[j].data;
let objct = {};
let valid_embed_video_domains = ['gfycat.com']
let has_gif = false
let gif_to_mp4 = null
let reddit_video = null
let embed_video = false
if(post.media)
if(valid_embed_video_domains.includes(post.media.type))
embed_video = true
if(post.preview && !embed_video) {
if(post.preview.reddit_video_preview) {
if(post.preview.reddit_video_preview.is_gif) {
has_gif = true
gif_url = post.preview.reddit_video_preview.fallback_url
} else {
let file_ext = getFileExtension(post.preview.reddit_video_preview.fallback_url)
if(file_ext === 'mp4')  {
post.media = true
reddit_video = post.preview.reddit_video_preview
}
}
}
if(post.preview.images) {
if(post.preview.images[0].source) {
let file_ext = getFileExtension(post.preview.images[0].source.url)
if(file_ext === 'gif') {
has_gif = true
let resolutions = post.preview.images[0].variants.mp4.resolutions
gif_to_mp4 = resolutions[resolutions.length - 1]
}
}
}
}
obj = await processPostMedia(obj, post, post.media, has_gif, reddit_video, gif_to_mp4)
if(post.crosspost_parent_list) {
post.crosspost = post.crosspost_parent_list[0]
}
if(post.crosspost) {
obj = await processPostMedia(obj, post.crosspost, post.crosspost.media, has_gif, reddit_video, gif_to_mp4)
obj.crosspost = {
author: post.crosspost.author,
created: post.crosspost.created_utc,
subreddit: post.crosspost.subreddit,
title: post.crosspost.title,
name: post.crosspost.name,
num_comments: post.crosspost.num_comments,
over_18: post.crosspost.over_18,
id: post.crosspost.id,
permalink: teddifyUrl(post.crosspost.permalink),
ups: post.crosspost.ups,
selftext: unescape(post.selftext_html),
selftext_crosspost: unescape(post.crosspost.selftext_html),
poll_data: post.poll_data,
is_crosspost: true,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
}
}
if(post.preview && !obj.has_media) {
obj.images = {
source: await downloadAndSave(post.preview.images[0].source.url)
}
}
if(obj.media) {
if(obj.media.source === 'external') {
if(post.preview) {
obj.images = {
source: await downloadAndSave(post.preview.images[0].source.url)
}
}
}
}
if(post.gallery_data) {
obj.gallery = true
obj.gallery_items = []
for(var i = 0; i < post.gallery_data.items.length; i++) {
let id = post.gallery_data.items[i].media_id
if(post.media_metadata[id]) {
if(post.media_metadata[id].p) {
if(post.media_metadata[id].p[0]) {
let item = { source: null, thumbnail: null, large: null }
if(post.media_metadata[id].s && post.media_metadata[id].p[0].u) {
item = {
type: post.media_metadata[id].e,
source: await downloadAndSave(post.media_metadata[id].s.u),
thumbnail: await downloadAndSave(post.media_metadata[id].p[0].u),
large: await downloadAndSave(post.media_metadata[id].p[post.media_metadata[id].p.length - 1].u),
}
}
obj.gallery_items.push(item)
}
}
}
}
}
let comms = []
for(var i = 0; i < comments.length; i++) {
let comment = comments[i].data
let kind = comments[i].kind
let obj = {}
if(kind !== 'more') {
obj = {
if (comment.author && comment.body_html) {
objct = {
author: comment.author,
body_html: comment.body_html,
parent_id: comment.parent_id,
@ -159,174 +62,341 @@ module.exports = function(fetch) {
ups: comment.ups,
id: comment.id,
permalink: teddifyUrl(comment.permalink),
stickied: comment.stickied,
distinguished: comment.distinguished,
score_hidden: comment.score_hidden,
edited: comment.edited,
distinguished: comment.distinguished,
distinguished: comment.edited,
replies: [],
depth: comment.depth,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''),
controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '')
}
depth: depth + 1,
user_flair:
user_preferences.flairs != 'false'
? await formatUserFlair(comment)
: '',
controversiality:
user_preferences.highlight_controversial != 'false'
? comment.controversiality
: '',
};
} else {
obj = {
objct = {
type: 'load_more',
count: comment.count,
id: comment.id,
parent_id: comment.parent_id,
post_id: post.name,
children: []
}
}
if(comment.replies && kind !== 'more') {
if(comment.replies.data) {
if(comment.replies.data.children.length > 0) {
obj.replies = await processReplies(comment.replies.data.children, post_id, 1, user_preferences)
post_id: post_id,
children: [],
depth: depth + 1,
};
if (comment.children) {
for (var k = 0; k < comment.children.length; k++) {
objct.children.push(comment.children[k]);
}
}
}
if(comment.children) {
for(var j = 0; j < comment.children.length; j++) {
obj.children.push(comment.children[j])
if (comment.replies) {
if (comment.replies.data) {
if (comment.replies.data.children.length > 0) {
objct.replies = await processReplies(
comment.replies.data.children,
post_id,
depth,
user_preferences
);
}
}
}
comms.push(obj)
obj.replies.push(objct);
}
obj.comments = comms
resolve(obj)
})()
})
}
this.finalizeJsonPost = async (processed_json, post_id, post_url, morechildren_ids, viewing_comment, user_preferences) => {
let comments_html = `<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: [],
depth: depth
}
if (reply.children) {
for (var j = 0; j < reply.children.length; j++) {
obj.children.push(reply.children[j]);
}
if(reply.replies && kind !== 'more') {
if(reply.replies.data.children.length) {
for(var j = 0; j < reply.replies.data.children.length; j++) {
let comment = reply.replies.data.children[j].data
let objct = {}
if(comment.author && comment.body_html) {
objct = {
author: comment.author,
body_html: comment.body_html,
parent_id: comment.parent_id,
created: comment.created_utc,
edited: comment.edited,
score: comment.score,
ups: comment.ups,
id: comment.id,
permalink: teddifyUrl(comment.permalink),
score_hidden: comment.score_hidden,
distinguished: comment.distinguished,
distinguished: comment.edited,
replies: [],
depth: depth + 1,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''),
controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '')
}
} else {
objct = {
type: 'load_more',
count: comment.count,
id: comment.id,
parent_id: comment.parent_id,
post_id: post_id,
children: [],
depth: depth + 1
}
if(comment.children) {
for(var k = 0; k < comment.children.length; k++) {
objct.children.push(comment.children[k])
}
}
}
if(comment.replies) {
if(comment.replies.data) {
if(comment.replies.data.children.length > 0) {
objct.replies = await processReplies(comment.replies.data.children, post_id, depth, user_preferences)
}
}
}
obj.replies.push(objct)
}
}
}
if(reply.children) {
for(var j = 0; j < reply.children.length; j++) {
obj.children.push(reply.children[j])
}
}
return_replies.push(obj)
}
return return_replies
return_replies.push(obj);
}
return return_replies;
}
async function processJsonPost(json, parsed, user_preferences) {
if (!parsed) {
json = JSON.parse(json);
}
let post = json[0].data.children[0].data;
let post_id = post.name;
let comments = json[1].data.children;
let obj = {
author: post.author,
created: post.created_utc,
edited: post.edited,
is_video: post.is_video,
locked: post.locked,
link_flair_text: post.link_flair_text,
name: post_id,
num_comments: post.num_comments,
over_18: post.over_18,
permalink: teddifyUrl(post.permalink),
title: post.title,
url: teddifyUrl(post.url, user_preferences),
ups: post.ups,
id: post.id,
domain: post.domain,
contest_mode: post.contest_mode,
upvote_ratio: post.upvote_ratio,
comments: null,
has_media: false,
media: null,
images: null,
crosspost: false,
selftext: unescape(post.selftext_html),
poll_data: post.poll_data,
link_flair:
user_preferences.flairs != 'false' ? await formatLinkFlair(post) : '',
user_flair:
user_preferences.flairs != 'false' ? await formatUserFlair(post) : '',
};
let valid_embed_video_domains = ['gfycat.com'];
let has_gif = false;
let gif_to_mp4 = null;
let reddit_video = null;
let embed_video = false;
if (post.media)
if (valid_embed_video_domains.includes(post.media.type)) embed_video = true;
if (post.preview && !embed_video) {
if (post.preview.reddit_video_preview) {
if (post.preview.reddit_video_preview.is_gif) {
has_gif = true;
gif_url = post.preview.reddit_video_preview.fallback_url;
} else {
let file_ext = getFileExtension(
post.preview.reddit_video_preview.fallback_url
);
if (file_ext === 'mp4') {
post.media = true;
reddit_video = post.preview.reddit_video_preview;
}
}
}
if (post.preview.images) {
if (post.preview.images[0].source) {
let file_ext = getFileExtension(post.preview.images[0].source.url);
if (file_ext === 'gif') {
has_gif = true;
let resolutions = post.preview.images[0].variants.mp4.resolutions;
gif_to_mp4 = resolutions[resolutions.length - 1];
}
}
}
}
obj = await processPostMedia(
obj,
post,
post.media,
has_gif,
reddit_video,
gif_to_mp4
);
if (post.crosspost_parent_list) {
post.crosspost = post.crosspost_parent_list[0];
}
if (post.crosspost) {
obj = await processPostMedia(
obj,
post.crosspost,
post.crosspost.media,
has_gif,
reddit_video,
gif_to_mp4
);
obj.crosspost = {
author: post.crosspost.author,
created: post.crosspost.created_utc,
subreddit: post.crosspost.subreddit,
title: post.crosspost.title,
name: post.crosspost.name,
num_comments: post.crosspost.num_comments,
over_18: post.crosspost.over_18,
id: post.crosspost.id,
permalink: teddifyUrl(post.crosspost.permalink),
ups: post.crosspost.ups,
selftext: unescape(post.selftext_html),
selftext_crosspost: unescape(post.crosspost.selftext_html),
poll_data: post.poll_data,
is_crosspost: true,
user_flair:
user_preferences.flairs != 'false' ? await formatUserFlair(post) : '',
};
}
if (post.preview && !obj.has_media) {
obj.images = {
source: await downloadAndSave(post.preview.images[0].source.url),
};
}
if (obj.media) {
if (obj.media.source === 'external') {
if (post.preview) {
obj.images = {
source: await downloadAndSave(post.preview.images[0].source.url),
};
}
}
}
if (post.gallery_data) {
obj.gallery = true;
obj.gallery_items = [];
for (var i = 0; i < post.gallery_data.items.length; i++) {
let id = post.gallery_data.items[i].media_id;
if (post.media_metadata[id]) {
if (post.media_metadata[id].p) {
if (post.media_metadata[id].p[0]) {
let item = { source: null, thumbnail: null, large: null };
if (post.media_metadata[id].s && post.media_metadata[id].p[0].u) {
item = {
type: post.media_metadata[id].e,
source: await downloadAndSave(post.media_metadata[id].s.u),
thumbnail: await downloadAndSave(
post.media_metadata[id].p[0].u
),
large: await downloadAndSave(
post.media_metadata[id].p[
post.media_metadata[id].p.length - 1
].u
),
};
}
obj.gallery_items.push(item);
}
}
}
}
}
let comms = [];
for (var i = 0; i < comments.length; i++) {
let comment = comments[i].data;
let kind = comments[i].kind;
let obj = {};
if (kind !== 'more') {
obj = {
author: comment.author,
body_html: comment.body_html,
parent_id: comment.parent_id,
created: comment.created_utc,
edited: comment.edited,
score: comment.score,
ups: comment.ups,
id: comment.id,
permalink: teddifyUrl(comment.permalink),
stickied: comment.stickied,
distinguished: comment.distinguished,
score_hidden: comment.score_hidden,
edited: comment.edited,
replies: [],
depth: comment.depth,
user_flair:
user_preferences.flairs != 'false'
? await formatUserFlair(comment)
: '',
controversiality:
user_preferences.highlight_controversial != 'false'
? comment.controversiality
: '',
};
} else {
obj = {
type: 'load_more',
count: comment.count,
id: comment.id,
parent_id: comment.parent_id,
post_id: post.name,
children: [],
};
}
if (comment.replies && kind !== 'more') {
if (comment.replies.data) {
if (comment.replies.data.children.length > 0) {
obj.replies = await processReplies(
comment.replies.data.children,
post_id,
1,
user_preferences
);
}
}
}
if (comment.children) {
for (var j = 0; j < comment.children.length; j++) {
obj.children.push(comment.children[j]);
}
}
comms.push(obj);
}
obj.comments = comms;
return obj;
}
async function finalizeJsonPost(
processed_json,
post_id,
post_url,
morechildren_ids,
viewing_comment,
user_preferences
) {
let comments_html = `<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 link = require('./components/link')
this.processJsonSubreddit = (json, from, subreddit_front, user_preferences, saved) => {
return new Promise(resolve => {
(async () => {
if(from === 'redis') {
json = JSON.parse(json)
}
if(json.error) {
resolve({ error: true, error_data: json })
} else {
if(saved) {
let t = {
data: {
before: null,
after: null,
children: json
}
}
json = t
}
let before = json.data.before
let after = json.data.after
const config = require('../config');
const link = require('./components/link');
let ret = {
info: {
before: before,
after: after
},
links: []
}
async function processJsonSubreddit(
json,
from,
subreddit_front,
user_preferences,
saved
) {
if (from === 'redis') {
json = JSON.parse(json);
}
if (json.error) {
return { error: true, error_data: json };
} else {
if (saved) {
let t = {
data: {
before: null,
after: null,
children: json,
},
};
json = t;
}
let children_len = json.data.children.length
let before = json.data.before;
let after = json.data.after;
for(var i = 0; i < children_len; i++) {
let data = json.data.children[i].data
let ret = {
info: {
before: before,
after: after,
},
links: [],
};
if(data.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
let children_len = json.data.children.length;
/*
// Todo: Remove this once the link component is done
// but keep it for now in case we need it later
let obj = {
author: data.author,
created: data.created_utc,
domain: data.domain,
id: data.id,
images: images,
is_video: data.is_video,
link_flair_text: data.link_flair_text,
locked: data.locked,
media: data.media,
selftext_html: data.selftext_html,
num_comments: data.num_comments,
over_18: data.over_18,
permalink: data.permalink,
score: data.score,
subreddit: data.subreddit,
title: data.title,
ups: data.ups,
upvote_ratio: data.upvote_ratio,
url: replaceDomains(data.url, user_preferences),
stickied: data.stickied,
is_self_link: is_self_link,
subreddit_front: subreddit_front,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
} */
for (var i = 0; i < children_len; i++) {
let data = json.data.children[i].data;
let obj = await link.fromJson(data, user_preferences, subreddit_front)
if (data.over_18)
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
ret.links.push(obj)
}
resolve(ret)
}
})()
})
/*
// Todo: Remove this once the link component is done
// but keep it for now in case we need it later
let obj = {
author: data.author,
created: data.created_utc,
domain: data.domain,
id: data.id,
images: images,
is_video: data.is_video,
link_flair_text: data.link_flair_text,
locked: data.locked,
media: data.media,
selftext_html: data.selftext_html,
num_comments: data.num_comments,
over_18: data.over_18,
permalink: data.permalink,
score: data.score,
subreddit: data.subreddit,
title: data.title,
ups: data.ups,
upvote_ratio: data.upvote_ratio,
url: replaceDomains(data.url, user_preferences),
stickied: data.stickied,
is_self_link: is_self_link,
subreddit_front: subreddit_front,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
} */
let obj = await link.fromJson(data, user_preferences, subreddit_front);
ret.links.push(obj);
}
return ret;
}
}
module.exports = processJsonSubreddit;

View File

@ -1,97 +1,106 @@
module.exports = function() {
const config = require('../config');
const link = require('./components/link')
this.processJsonUser = function(json, parsed, after, before, user_preferences, kind, post_type) {
return new Promise(resolve => {
(async () => {
if(!parsed) {
json = JSON.parse(json)
}
const config = require('../config');
const link = require('./components/link');
let about = json.about.data
let posts = []
let view_more_posts = false
let posts_limit = 25
let user_front = false
if(json.overview.data.children.length > posts_limit) {
view_more_posts = true
} else {
posts_limit = json.overview.data.children.length
}
if(!after && !before) {
user_front = true
}
if(json.overview.data.children) {
if(json.overview.data.children[posts_limit - 1]) {
after = json.overview.data.children[posts_limit - 1].data.name
}
if(json.overview.data.children[0]) {
before = json.overview.data.children[0].data.name
}
}
for(var i = 0; i < posts_limit; i++) {
let post = json.overview.data.children[i].data
let thumbnail = 'self'
let type = json.overview.data.children[i].kind
let obj
let post_id = post.permalink.split('/').slice(-2)[0] + '/'
let url = post.permalink.replace(post_id, '')
if(type !== kind && kind)
continue
if(post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
if(type === 't3') {
obj = await link.fromJson(post, user_preferences)
obj.type = 't3'
}
if(type === 't1') {
obj = {
type: type,
subreddit: post.subreddit,
title: post.title,
created: post.created_utc,
subreddit_name_prefixed: post.subreddit_name_prefixed,
ups: post.ups,
url: replaceDomains(url, user_preferences),
edited: post.edited,
body_html: unescape(post.body_html),
num_comments: post.num_comments,
over_18: post.over_18,
permalink: post.permalink,
link_author: post.link_author,
link_title: post.link_title,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
}
}
posts.push(obj)
}
let obj = {
username: about.name,
icon_img: await downloadAndSave(about.icon_img, "icon_"),
created: about.created_utc,
verified: about.verified,
link_karma: about.link_karma,
comment_karma: about.comment_karma,
view_more_posts: view_more_posts,
user_front: user_front,
post_type: post_type,
before: before,
after: after,
posts: posts
}
resolve(obj)
})()
})
async function processJsonUser(
json,
parsed,
after,
before,
user_preferences,
kind,
post_type
) {
if (!parsed) {
json = JSON.parse(json);
}
let about = json.about.data;
let posts = [];
let view_more_posts = false;
let posts_limit = 25;
let user_front = false;
if (json.overview.data.children.length > posts_limit) {
view_more_posts = true;
} else {
posts_limit = json.overview.data.children.length;
}
if (!after && !before) {
user_front = true;
}
if (json.overview.data.children) {
if (json.overview.data.children[posts_limit - 1]) {
after = json.overview.data.children[posts_limit - 1].data.name;
}
if (json.overview.data.children[0]) {
before = json.overview.data.children[0].data.name;
}
}
for (var i = 0; i < posts_limit; i++) {
let post = json.overview.data.children[i].data;
let thumbnail = 'self';
let type = json.overview.data.children[i].kind;
let obj;
let post_id = post.permalink.split('/').slice(-2)[0] + '/';
let url = post.permalink.replace(post_id, '');
if (type !== kind && kind) continue;
if (post.over_18)
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
if (type === 't3') {
obj = await link.fromJson(post, user_preferences);
obj.type = 't3';
}
if (type === 't1') {
obj = {
type: type,
subreddit: post.subreddit,
title: post.title,
created: post.created_utc,
subreddit_name_prefixed: post.subreddit_name_prefixed,
ups: post.ups,
url: replaceDomains(url, user_preferences),
edited: post.edited,
body_html: unescape(post.body_html),
num_comments: post.num_comments,
over_18: post.over_18,
permalink: post.permalink,
link_author: post.link_author,
link_title: post.link_title,
user_flair:
user_preferences.flairs != 'false' ? await formatUserFlair(post) : '',
};
}
posts.push(obj);
}
let obj = {
username: about.name,
icon_img: await downloadAndSave(about.icon_img, 'icon_'),
created: about.created_utc,
verified: about.verified,
link_karma: about.link_karma,
comment_karma: about.comment_karma,
view_more_posts: view_more_posts,
user_front: user_front,
post_type: post_type,
before: before,
after: after,
posts: posts,
};
return obj;
}
module.exports = processJsonUser;

View File

@ -1,62 +1,70 @@
module.exports = function() {
const config = require('../config');
const link = require('./components/link')
this.processSearchResults = (json, parsed, after, before, user_preferences) => {
return new Promise(resolve => {
(async () => {
if(!parsed) {
json = JSON.parse(json)
}
let posts = []
let search_firstpage = false
let before = json.data.before
let after = json.data.after
if(!after && !before) {
search_firstpage = true
}
let suggested_subreddits = false
if(json.suggested_subreddits) {
if(json.suggested_subreddits.data) {
if(json.suggested_subreddits.data.children.length > 0) {
suggested_subreddits = json.suggested_subreddits.data.children
}
}
}
if(json.data.children) {
let view_more_posts = false
let posts_limit = 25
const config = require('../config');
const link = require('./components/link');
if(json.data.children.length > posts_limit) {
view_more_posts = true
} else {
posts_limit = json.data.children.length
}
for(var i = 0; i < posts_limit; i++) {
let post = json.data.children[i].data
if(post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
let obj = await link.fromJson(post, user_preferences)
posts.push(obj)
}
}
let obj = {
search_firstpage: search_firstpage,
before: before,
after: after,
posts: posts,
suggested_subreddits: suggested_subreddits,
}
resolve(obj)
})()
})
async function processSearchResults(
json,
parsed,
after,
before,
user_preferences
) {
if (!parsed) {
json = JSON.parse(json);
}
let posts = [];
let search_firstpage = false;
before = json.data.before;
after = json.data.after;
if (!after && !before) {
search_firstpage = true;
}
let suggested_subreddits = false;
if (json.suggested_subreddits) {
if (json.suggested_subreddits.data) {
if (json.suggested_subreddits.data.children.length > 0) {
suggested_subreddits = json.suggested_subreddits.data.children;
}
}
}
if (json.data.children) {
let view_more_posts = false;
let posts_limit = 25;
if (json.data.children.length > posts_limit) {
view_more_posts = true;
} else {
posts_limit = json.data.children.length;
}
for (var i = 0; i < posts_limit; i++) {
let post = json.data.children[i].data;
if (post.over_18)
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
let obj = await link.fromJson(post, user_preferences);
posts.push(obj);
}
}
let obj = {
search_firstpage: search_firstpage,
before: before,
after: after,
posts: posts,
suggested_subreddits: suggested_subreddits,
};
return obj;
}
module.exports = processSearchResults;

View File

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

View File

@ -1,52 +1,58 @@
module.exports = function() {
const config = require('../config');
this.processJsonSubredditsExplore = (json, from, subreddit_front, user_preferences) => {
return new Promise(resolve => {
(async () => {
if(from === 'redis') {
json = JSON.parse(json)
}
if(json.error) {
resolve({ error: true, error_data: json })
} else {
let before = json.data.before
let after = json.data.after
const config = require('../config');
let ret = {
info: {
before: before,
after: after
},
links: []
}
async function processJsonSubredditsExplore(
json,
from,
subreddit_front,
user_preferences
) {
if (from === 'redis') {
json = JSON.parse(json);
}
if (json.error) {
return { error: true, error_data: json };
} else {
let before = json.data.before;
let after = json.data.after;
let children_len = json.data.children.length
let ret = {
info: {
before: before,
after: after,
},
links: [],
};
for(var i = 0; i < children_len; i++) {
let data = json.data.children[i].data
if(data.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
let obj = {
created: data.created_utc,
id: data.id,
over_18: data.over_18,
display_name: data.display_name,
display_name_prefixed: data.display_name_prefixed,
public_description: data.public_description,
url: replaceDomains(data.url, user_preferences),
subscribers: data.subscribers,
over_18: data.over18,
title: data.title,
subreddit_front: subreddit_front,
}
ret.links.push(obj)
}
resolve(ret)
}
})()
})
let children_len = json.data.children.length;
for (var i = 0; i < children_len; i++) {
let data = json.data.children[i].data;
if (data.over_18)
if (
(config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
let obj = {
created: data.created_utc,
id: data.id,
over_18: data.over_18,
display_name: data.display_name,
display_name_prefixed: data.display_name_prefixed,
public_description: data.public_description,
url: replaceDomains(data.url, user_preferences),
subscribers: data.subscribers,
over_18: data.over18,
title: data.title,
subreddit_front: subreddit_front,
};
ret.links.push(obj);
}
return ret;
}
}
module.exports = processJsonSubredditsExplore;

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

View File

@ -1,74 +1,86 @@
module.exports = function() {
const config = require('../../config')
this.handleTedditApiUser = async (json, req, res, from, api_type, api_target, user) => {
if(!config.api_enabled) {
res.setHeader('Content-Type', 'application/json')
let msg = { info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.' }
return res.end(JSON.stringify(msg))
const processJsonUser = require('../processJsonUser');
module.exports = function () {
const config = require('../../config');
this.handleTedditApiUser = async (
json,
req,
res,
from,
api_type,
api_target,
user
) => {
if (!config.api_enabled) {
res.setHeader('Content-Type', 'application/json');
let msg = {
info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.',
};
return res.end(JSON.stringify(msg));
}
console.log('Teddit API request - user')
let _json = json // Keep the original json
if(from === 'redis')
json = JSON.parse(json)
let protocol = (config.https_enabled ? 'https' : 'http')
let link = `${protocol}://${config.domain}/user/${user}`
if(api_type === 'rss') {
let items = ''
let posts_limit = 25
if(json.overview.data.children.length <= posts_limit) {
posts_limit = json.overview.data.children.length
console.log('Teddit API request - user');
let _json = json; // Keep the original json
if (from === 'redis') json = JSON.parse(json);
let protocol = config.https_enabled ? 'https' : 'http';
let link = `${protocol}://${config.domain}/user/${user}`;
if (api_type === 'rss') {
let items = '';
let posts_limit = 25;
if (json.overview.data.children.length <= posts_limit) {
posts_limit = json.overview.data.children.length;
}
for(var i = 0; i < posts_limit; i++) {
let post = json.overview.data.children[i].data
let post_id = post.permalink.split('/').slice(-2)[0] + '/'
let url = post.permalink.replace(post_id, '')
let permalink = `${protocol}://${config.domain}${post.permalink}`
let comments_url = `${protocol}://${config.domain}${url}`
let kind = json.overview.data.children[i].kind
let t1_elements = ''
let t3_elements = ''
if(kind === 't1') {
let append_desc_html = `<br/><a href="${permalink}">[link]</a> <a href="${comments_url}">[comments]</a>`
for (var i = 0; i < posts_limit; i++) {
let post = json.overview.data.children[i].data;
let post_id = post.permalink.split('/').slice(-2)[0] + '/';
let url = post.permalink.replace(post_id, '');
let permalink = `${protocol}://${config.domain}${post.permalink}`;
let comments_url = `${protocol}://${config.domain}${url}`;
let kind = json.overview.data.children[i].kind;
let t1_elements = '';
let t3_elements = '';
if (kind === 't1') {
let append_desc_html = `<br/><a href="${permalink}">[link]</a> <a href="${comments_url}">[comments]</a>`;
t1_elements = `
<description><![CDATA[${unescape(post.body_html)}${append_desc_html}]]></description>
<description><![CDATA[${unescape(
post.body_html
)}${append_desc_html}]]></description>
<url>${comments_url}</url>
`
`;
}
if(kind === 't3') {
let s = await downloadAndSave(post.thumbnail, 'thumb_')
let thumbnail = ''
let enclosure = ''
if(s !== 'self' && s != '') {
let img = `${protocol}://${config.domain}${s}`
thumbnail = `<thumbnail>${img}</thumbnail>`
let mime = ''
let ext = s.split('.').pop()
if(ext === 'png')
mime = 'image/png'
else
mime = 'image/jpeg'
enclosure = `<enclosure length="0" type="${mime}" url="${img}" />`
if (kind === 't3') {
let s = await downloadAndSave(post.thumbnail, 'thumb_');
let thumbnail = '';
let enclosure = '';
if (s !== 'self' && s != '') {
let img = `${protocol}://${config.domain}${s}`;
thumbnail = `<thumbnail>${img}</thumbnail>`;
let mime = '';
let ext = s.split('.').pop();
if (ext === 'png') mime = 'image/png';
else mime = 'image/jpeg';
enclosure = `<enclosure length="0" type="${mime}" url="${img}" />`;
}
let append_desc_html = `submitted by <a href="${protocol}://${config.domain}/u/${user}>/u/${user}</a> to <a href="${protocol}://${config.domain}/r/${post.subreddit}/">r/${post.subreddit}</a>`
append_desc_html += `<br/><a href="${permalink}">[comments]</a>`
let append_desc_html = `submitted by <a href="${protocol}://${config.domain}/u/${user}>/u/${user}</a> to <a href="${protocol}://${config.domain}/r/${post.subreddit}/">r/${post.subreddit}</a>`;
append_desc_html += `<br/><a href="${permalink}">[comments]</a>`;
t3_elements = `
<description><![CDATA[${unescape(post.selftext_html)}${append_desc_html}]]></description>
<description><![CDATA[${unescape(
post.selftext_html
)}${append_desc_html}]]></description>
${thumbnail}
${enclosure}
`
`;
}
let title = post.title
if(!post.title)
title = post.link_title
let title = post.title;
if (!post.title) title = post.link_title;
items += `
<item>
<title>${title}</title>
@ -76,7 +88,9 @@ module.exports = function() {
<kind>${kind}</kind>
<subreddit>${post.subreddit}</subreddit>
<created>${post.created_utc}</created>
<pubDate>${new Date(post.created_utc*1000).toGMTString()}</pubDate>
<pubDate>${new Date(
post.created_utc * 1000
).toGMTString()}</pubDate>
<ups>${post.ups}</ups>
<link>${permalink}</link>
<edited>${post.edited}</edited>
@ -85,11 +99,10 @@ module.exports = function() {
${t1_elements}
${t3_elements}
</item>
`
`;
}
let xml_output =
`<?xml version="1.0" encoding="UTF-8"?>
let xml_output = `<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<atom:link href="${link}/?api&amp;type=rss" rel="self" type="application/rss+xml" />
@ -97,17 +110,23 @@ module.exports = function() {
<link>${link}</link>
${items}
</channel>
</rss>`
res.setHeader('Content-Type', 'application/rss+xml')
return res.end(xml_output)
</rss>`;
res.setHeader('Content-Type', 'application/rss+xml');
return res.end(xml_output);
} else {
res.setHeader('Content-Type', 'application/json')
if(api_target === 'reddit') {
return res.end(JSON.stringify(json))
res.setHeader('Content-Type', 'application/json');
if (api_target === 'reddit') {
return res.end(JSON.stringify(json));
} else {
let processed_json = await processJsonUser(json, true, null, null, req.cookies)
return res.end(JSON.stringify(processed_json))
}
let processed_json = await processJsonUser(
json,
true,
null,
null,
req.cookies
);
return res.end(JSON.stringify(processed_json));
}
}
}
}
};
};

View File

@ -2,16 +2,9 @@ const config = require('../config');
const { redis, fetch } = require('../app');
const homeRoute = require('express').Router();
const processUser = require('../inc/processJsonUser.js')();
const processPost = require('../inc/processJsonPost.js')();
const processAbout = require('../inc/processSubredditAbout.js')();
const tedditApiUser = require('../inc/teddit_api/handleUser.js')();
const processSearches = require('../inc/processSearchResults.js')();
const processSubreddit = require('../inc/processJsonSubreddit.js')();
const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
require('../inc/processSubredditsExplore.js')();
homeRoute.get('/:sort?', async (req, res, next) => {
let past = req.query.t;

View File

@ -2,16 +2,9 @@ const config = require('../config');
const { redis, fetch } = require('../app');
const saveRoutes = require('express').Router();
const processUser = require('../inc/processJsonUser.js')();
const processPost = require('../inc/processJsonPost.js')();
const processAbout = require('../inc/processSubredditAbout.js')();
const tedditApiUser = require('../inc/teddit_api/handleUser.js')();
const processSearches = require('../inc/processSearchResults.js')();
const processSubreddit = require('../inc/processJsonSubreddit.js')();
const processJsonSubreddit = require('../inc/processJsonSubreddit.js');
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')();
const processSubredditsExplore =
require('../inc/processSubredditsExplore.js')();
saveRoutes.get('/saved', (req, res, next) => {
let saved = req.cookies.saved;

View File

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

View File

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