2020-11-17 21:44:32 +01:00
|
|
|
|
module.exports = function() {
|
2020-12-01 20:16:51 +01:00
|
|
|
|
const config = require('../config');
|
2021-02-06 20:17:36 +01:00
|
|
|
|
this.processJsonSubreddit = (json, from, subreddit_front, user_preferences, saved) => {
|
2020-11-17 21:44:32 +01:00
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
(async () => {
|
|
|
|
|
if(from === 'redis') {
|
|
|
|
|
json = JSON.parse(json)
|
|
|
|
|
}
|
|
|
|
|
if(json.error) {
|
|
|
|
|
resolve({ error: true, error_data: json })
|
|
|
|
|
} else {
|
2021-02-06 20:17:36 +01:00
|
|
|
|
if(saved) {
|
|
|
|
|
let t = {
|
|
|
|
|
data: {
|
|
|
|
|
before: null,
|
|
|
|
|
after: null,
|
|
|
|
|
children: json
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
json = t
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
let before = json.data.before
|
|
|
|
|
let after = json.data.after
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
let ret = {
|
|
|
|
|
info: {
|
|
|
|
|
before: before,
|
|
|
|
|
after: after
|
|
|
|
|
},
|
|
|
|
|
links: []
|
|
|
|
|
}
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
let children_len = json.data.children.length
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
for(var i = 0; i < children_len; i++) {
|
|
|
|
|
let data = json.data.children[i].data
|
|
|
|
|
let images = null
|
|
|
|
|
let is_self_link = false
|
|
|
|
|
let valid_reddit_self_domains = ['reddit.com']
|
2021-01-24 12:54:13 +01:00
|
|
|
|
|
2020-12-27 23:25:39 +01:00
|
|
|
|
if(data.over_18)
|
|
|
|
|
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
|
|
|
|
|
continue
|
2020-11-17 21:44:32 +01:00
|
|
|
|
|
|
|
|
|
if(data.domain) {
|
|
|
|
|
let tld = data.domain.split('self.')
|
|
|
|
|
if(tld.length > 1) {
|
|
|
|
|
if(!tld[1].includes('.')) {
|
|
|
|
|
is_self_link = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 20:16:51 +01:00
|
|
|
|
if(config.valid_media_domains.includes(data.domain) || valid_reddit_self_domains.includes(data.domain)) {
|
2020-11-17 21:44:32 +01:00
|
|
|
|
is_self_link = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(data.preview && data.thumbnail !== 'self') {
|
|
|
|
|
if(!data.url.startsWith('/r/') && isGif(data.url)) {
|
|
|
|
|
images = {
|
|
|
|
|
thumb: await downloadAndSave(data.thumbnail, 'thumb_')
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-11-19 06:50:51 +01:00
|
|
|
|
if(data.preview.images[0].resolutions[0]) {
|
2021-01-24 15:05:59 +01:00
|
|
|
|
let preview = null
|
2021-01-24 15:18:07 +01:00
|
|
|
|
if(!isGif(data.url) && !data.post_hint.includes(':video'))
|
2021-01-24 15:05:59 +01:00
|
|
|
|
preview = await downloadAndSave(data.preview.images[0].source.url)
|
2020-11-19 06:50:51 +01:00
|
|
|
|
images = {
|
2021-01-24 12:54:13 +01:00
|
|
|
|
thumb: await downloadAndSave(data.preview.images[0].resolutions[0].url, 'thumb_'),
|
2021-01-24 15:05:59 +01:00
|
|
|
|
preview: preview
|
2020-11-19 06:50:51 +01:00
|
|
|
|
}
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let obj = {
|
|
|
|
|
author: data.author,
|
|
|
|
|
created: data.created_utc,
|
|
|
|
|
domain: data.domain,
|
|
|
|
|
id: data.id,
|
|
|
|
|
images: images,
|
|
|
|
|
is_video: data.is_video,
|
2020-12-13 20:53:32 +01:00
|
|
|
|
link_flair_text: data.link_flair_text,
|
2020-11-17 21:44:32 +01:00
|
|
|
|
locked: data.locked,
|
|
|
|
|
media: data.media,
|
2021-01-19 17:21:31 +01:00
|
|
|
|
selftext_html: data.selftext_html,
|
2020-11-17 21:44:32 +01:00
|
|
|
|
num_comments: data.num_comments,
|
2020-12-23 15:52:51 +01:00
|
|
|
|
over_18: data.over_18,
|
2020-11-17 21:44:32 +01:00
|
|
|
|
permalink: data.permalink,
|
|
|
|
|
score: data.score,
|
|
|
|
|
subreddit: data.subreddit,
|
|
|
|
|
title: data.title,
|
|
|
|
|
ups: data.ups,
|
|
|
|
|
upvote_ratio: data.upvote_ratio,
|
2021-04-05 17:12:31 +02:00
|
|
|
|
url: replaceDomains(data.url, user_preferences),
|
2020-12-04 18:10:33 +01:00
|
|
|
|
stickied: data.stickied,
|
2020-11-17 21:44:32 +01:00
|
|
|
|
is_self_link: is_self_link,
|
2020-12-21 12:27:31 +01:00
|
|
|
|
subreddit_front: subreddit_front,
|
2020-12-24 22:13:08 +01:00
|
|
|
|
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''),
|
|
|
|
|
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
ret.links.push(obj)
|
|
|
|
|
}
|
|
|
|
|
resolve(ret)
|
|
|
|
|
}
|
|
|
|
|
})()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|