diff --git a/inc/processJsonUser.js b/inc/processJsonUser.js
index 83c8fce..86dc084 100644
--- a/inc/processJsonUser.js
+++ b/inc/processJsonUser.js
@@ -1,97 +1,106 @@
-module.exports = function() {
- const config = require('../config');
- const link = require('./components/link')
- this.processJsonUser = function(json, parsed, after, before, user_preferences, kind, post_type) {
- return new Promise(resolve => {
- (async () => {
- if(!parsed) {
- json = JSON.parse(json)
- }
+const config = require('../config');
+const link = require('./components/link');
- let about = json.about.data
- let posts = []
- let view_more_posts = false
- let posts_limit = 25
- let user_front = false
-
- if(json.overview.data.children.length > posts_limit) {
- view_more_posts = true
- } else {
- posts_limit = json.overview.data.children.length
- }
-
- if(!after && !before) {
- user_front = true
- }
-
- if(json.overview.data.children) {
- if(json.overview.data.children[posts_limit - 1]) {
- after = json.overview.data.children[posts_limit - 1].data.name
- }
- if(json.overview.data.children[0]) {
- before = json.overview.data.children[0].data.name
- }
- }
-
- for(var i = 0; i < posts_limit; i++) {
- let post = json.overview.data.children[i].data
- let thumbnail = 'self'
- let type = json.overview.data.children[i].kind
- let obj
-
- let post_id = post.permalink.split('/').slice(-2)[0] + '/'
- let url = post.permalink.replace(post_id, '')
-
- if(type !== kind && kind)
- continue
-
- if(post.over_18)
- if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
- continue
-
- if(type === 't3') {
- obj = await link.fromJson(post, user_preferences)
- obj.type = 't3'
- }
- if(type === 't1') {
- obj = {
- type: type,
- subreddit: post.subreddit,
- title: post.title,
- created: post.created_utc,
- subreddit_name_prefixed: post.subreddit_name_prefixed,
- ups: post.ups,
- url: replaceDomains(url, user_preferences),
- edited: post.edited,
- body_html: unescape(post.body_html),
- num_comments: post.num_comments,
- over_18: post.over_18,
- permalink: post.permalink,
- link_author: post.link_author,
- link_title: post.link_title,
- user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
- }
- }
- posts.push(obj)
- }
-
- let obj = {
- username: about.name,
- icon_img: await downloadAndSave(about.icon_img, "icon_"),
- created: about.created_utc,
- verified: about.verified,
- link_karma: about.link_karma,
- comment_karma: about.comment_karma,
- view_more_posts: view_more_posts,
- user_front: user_front,
- post_type: post_type,
- before: before,
- after: after,
- posts: posts
- }
-
- resolve(obj)
- })()
- })
+async function processJsonUser(
+ json,
+ parsed,
+ after,
+ before,
+ user_preferences,
+ kind,
+ post_type
+) {
+ if (!parsed) {
+ json = JSON.parse(json);
}
+
+ let about = json.about.data;
+ let posts = [];
+ let view_more_posts = false;
+ let posts_limit = 25;
+ let user_front = false;
+
+ if (json.overview.data.children.length > posts_limit) {
+ view_more_posts = true;
+ } else {
+ posts_limit = json.overview.data.children.length;
+ }
+
+ if (!after && !before) {
+ user_front = true;
+ }
+
+ if (json.overview.data.children) {
+ if (json.overview.data.children[posts_limit - 1]) {
+ after = json.overview.data.children[posts_limit - 1].data.name;
+ }
+ if (json.overview.data.children[0]) {
+ before = json.overview.data.children[0].data.name;
+ }
+ }
+
+ for (var i = 0; i < posts_limit; i++) {
+ let post = json.overview.data.children[i].data;
+ let thumbnail = 'self';
+ let type = json.overview.data.children[i].kind;
+ let obj;
+
+ let post_id = post.permalink.split('/').slice(-2)[0] + '/';
+ let url = post.permalink.replace(post_id, '');
+
+ if (type !== kind && kind) continue;
+
+ if (post.over_18)
+ if (
+ (config.nsfw_enabled === false &&
+ user_preferences.nsfw_enabled != 'true') ||
+ user_preferences.nsfw_enabled === 'false'
+ )
+ continue;
+
+ if (type === 't3') {
+ obj = await link.fromJson(post, user_preferences);
+ obj.type = 't3';
+ }
+ if (type === 't1') {
+ obj = {
+ type: type,
+ subreddit: post.subreddit,
+ title: post.title,
+ created: post.created_utc,
+ subreddit_name_prefixed: post.subreddit_name_prefixed,
+ ups: post.ups,
+ url: replaceDomains(url, user_preferences),
+ edited: post.edited,
+ body_html: unescape(post.body_html),
+ num_comments: post.num_comments,
+ over_18: post.over_18,
+ permalink: post.permalink,
+ link_author: post.link_author,
+ link_title: post.link_title,
+ user_flair:
+ user_preferences.flairs != 'false' ? await formatUserFlair(post) : '',
+ };
+ }
+ posts.push(obj);
+ }
+
+ let obj = {
+ username: about.name,
+ icon_img: await downloadAndSave(about.icon_img, 'icon_'),
+ created: about.created_utc,
+ verified: about.verified,
+ link_karma: about.link_karma,
+ comment_karma: about.comment_karma,
+ view_more_posts: view_more_posts,
+ user_front: user_front,
+ post_type: post_type,
+ before: before,
+ after: after,
+ posts: posts,
+ };
+
+ return obj;
}
+
+module.exports = processJsonUser;
diff --git a/inc/teddit_api/handleUser.js b/inc/teddit_api/handleUser.js
index 3efde60..f07c2bb 100644
--- a/inc/teddit_api/handleUser.js
+++ b/inc/teddit_api/handleUser.js
@@ -1,74 +1,86 @@
-module.exports = function() {
- const config = require('../../config')
- this.handleTedditApiUser = async (json, req, res, from, api_type, api_target, user) => {
- if(!config.api_enabled) {
- res.setHeader('Content-Type', 'application/json')
- let msg = { info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.' }
- return res.end(JSON.stringify(msg))
+const processJsonUser = require('../processJsonUser');
+
+module.exports = function () {
+ const config = require('../../config');
+ this.handleTedditApiUser = async (
+ json,
+ req,
+ res,
+ from,
+ api_type,
+ api_target,
+ user
+ ) => {
+ if (!config.api_enabled) {
+ res.setHeader('Content-Type', 'application/json');
+ let msg = {
+ info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.',
+ };
+ return res.end(JSON.stringify(msg));
}
-
- console.log('Teddit API request - user')
- let _json = json // Keep the original json
- if(from === 'redis')
- json = JSON.parse(json)
-
- let protocol = (config.https_enabled ? 'https' : 'http')
- let link = `${protocol}://${config.domain}/user/${user}`
-
- if(api_type === 'rss') {
- let items = ''
- let posts_limit = 25
-
- if(json.overview.data.children.length <= posts_limit) {
- posts_limit = json.overview.data.children.length
+
+ console.log('Teddit API request - user');
+ let _json = json; // Keep the original json
+ if (from === 'redis') json = JSON.parse(json);
+
+ let protocol = config.https_enabled ? 'https' : 'http';
+ let link = `${protocol}://${config.domain}/user/${user}`;
+
+ if (api_type === 'rss') {
+ let items = '';
+ let posts_limit = 25;
+
+ if (json.overview.data.children.length <= posts_limit) {
+ posts_limit = json.overview.data.children.length;
}
-
- for(var i = 0; i < posts_limit; i++) {
- let post = json.overview.data.children[i].data
- let post_id = post.permalink.split('/').slice(-2)[0] + '/'
- let url = post.permalink.replace(post_id, '')
- let permalink = `${protocol}://${config.domain}${post.permalink}`
- let comments_url = `${protocol}://${config.domain}${url}`
- let kind = json.overview.data.children[i].kind
-
- let t1_elements = ''
- let t3_elements = ''
- if(kind === 't1') {
- let append_desc_html = `
[link] [comments]`
+
+ for (var i = 0; i < posts_limit; i++) {
+ let post = json.overview.data.children[i].data;
+ let post_id = post.permalink.split('/').slice(-2)[0] + '/';
+ let url = post.permalink.replace(post_id, '');
+ let permalink = `${protocol}://${config.domain}${post.permalink}`;
+ let comments_url = `${protocol}://${config.domain}${url}`;
+ let kind = json.overview.data.children[i].kind;
+
+ let t1_elements = '';
+ let t3_elements = '';
+ if (kind === 't1') {
+ let append_desc_html = `
[link] [comments]`;
t1_elements = `
-
+
${comments_url}
- `
+ `;
}
- if(kind === 't3') {
- let s = await downloadAndSave(post.thumbnail, 'thumb_')
- let thumbnail = ''
- let enclosure = ''
- if(s !== 'self' && s != '') {
- let img = `${protocol}://${config.domain}${s}`
- thumbnail = `${img}`
-
- let mime = ''
- let ext = s.split('.').pop()
- if(ext === 'png')
- mime = 'image/png'
- else
- mime = 'image/jpeg'
- enclosure = ``
+ if (kind === 't3') {
+ let s = await downloadAndSave(post.thumbnail, 'thumb_');
+ let thumbnail = '';
+ let enclosure = '';
+ if (s !== 'self' && s != '') {
+ let img = `${protocol}://${config.domain}${s}`;
+ thumbnail = `${img}`;
+
+ let mime = '';
+ let ext = s.split('.').pop();
+ if (ext === 'png') mime = 'image/png';
+ else mime = 'image/jpeg';
+ enclosure = ``;
}
- let append_desc_html = `submitted by r/${post.subreddit}`
- append_desc_html += `
[comments]`
+ let append_desc_html = `submitted by r/${post.subreddit}`;
+ append_desc_html += `
[comments]`;
t3_elements = `
-
+
${thumbnail}
${enclosure}
- `
+ `;
}
-
- let title = post.title
- if(!post.title)
- title = post.link_title
-
+
+ let title = post.title;
+ if (!post.title) title = post.link_title;
+
items += `
-
${title}
@@ -76,7 +88,9 @@ module.exports = function() {
${kind}
${post.subreddit}
${post.created_utc}
- ${new Date(post.created_utc*1000).toGMTString()}
+ ${new Date(
+ post.created_utc * 1000
+ ).toGMTString()}
${post.ups}
${permalink}
${post.edited}
@@ -85,11 +99,10 @@ module.exports = function() {
${t1_elements}
${t3_elements}
- `
+ `;
}
-
- let xml_output =
- `
+
+ let xml_output = `
@@ -97,17 +110,23 @@ module.exports = function() {
${link}
${items}
- `
- res.setHeader('Content-Type', 'application/rss+xml')
- return res.end(xml_output)
+ `;
+ res.setHeader('Content-Type', 'application/rss+xml');
+ return res.end(xml_output);
} else {
- res.setHeader('Content-Type', 'application/json')
- if(api_target === 'reddit') {
- return res.end(JSON.stringify(json))
+ res.setHeader('Content-Type', 'application/json');
+ if (api_target === 'reddit') {
+ return res.end(JSON.stringify(json));
} else {
- let processed_json = await processJsonUser(json, true, null, null, req.cookies)
- return res.end(JSON.stringify(processed_json))
- }
+ let processed_json = await processJsonUser(
+ json,
+ true,
+ null,
+ null,
+ req.cookies
+ );
+ return res.end(JSON.stringify(processed_json));
+ }
}
- }
-}
+ };
+};
diff --git a/routes/user.js b/routes/user.js
index 4abd6cf..e57889a 100644
--- a/routes/user.js
+++ b/routes/user.js
@@ -2,7 +2,7 @@ const config = require('../config');
const { redis, fetch } = require('../app');
const userRoutes = require('express').Router();
-const processUser = require('../inc/processJsonUser.js')();
+const processJsonUser = require('../inc/processJsonUser.js');
const processPost = require('../inc/processJsonPost.js')();
const processAbout = require('../inc/processSubredditAbout.js')();
const tedditApiUser = require('../inc/teddit_api/handleUser.js')();