convert processJsonPost to async functions

This commit is contained in:
json 2021-09-07 13:44:34 +01:00
parent 93f70533aa
commit d95642cf80
2 changed files with 372 additions and 299 deletions

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

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