From a8974f709d0a3eb359386ed280bd08b91039a8ee Mon Sep 17 00:00:00 2001 From: StevenNMeza Date: Wed, 26 May 2021 18:21:05 +0200 Subject: [PATCH] Use a standalone function --- inc/components/link.js | 131 ++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/inc/components/link.js b/inc/components/link.js index 66863d5..e39ec89 100644 --- a/inc/components/link.js +++ b/inc/components/link.js @@ -5,79 +5,78 @@ const config = require('../../config') let valid_reddit_self_domains = ['reddit.com'] -class Link { - // Parses a link from a response returned by reddit. - static async fromJson(data, user_preferences, subreddit_front) { +// Parses a link from a response returned by reddit. +async function fromJson(data, user_preferences, subreddit_front) { + let result = {} - // Meta - this.id = data.id - this.permalink = data.permalink - this.created = data.created_utc - this.author = data.author - this.title = data.title - this.over_18 = data.over_18 - this.score = data.score - this.ups = data.ups - this.upvote_ratio = data.upvote_ratio - this.num_comments = data.num_comments + // Meta + result.id = data.id + result.permalink = data.permalink + result.created = data.created_utc + result.author = data.author + result.title = data.title + result.over_18 = data.over_18 + result.score = data.score + result.ups = data.ups + result.upvote_ratio = data.upvote_ratio + result.num_comments = data.num_comments - // Content - this.is_self_link = false - this.selftext_html = data.selftext_html - this.url = replaceDomains(data.url, user_preferences) - this.domain = data.domain - this.is_video = data.is_video - this.media = data.media - this.duration = data.is_video ? data.media.reddit_video ? data.media.reddit_video.duration : void 0 : void 0 - this.images = null + // Content + result.is_self_link = false + result.selftext_html = data.selftext_html + result.url = replaceDomains(data.url, user_preferences) + result.domain = data.domain + result.is_video = data.is_video + result.media = data.media + result.duration = data.is_video ? data.media.reddit_video ? data.media.reddit_video.duration : void 0 : void 0 + result.images = null - // Moderation attributes - this.locked = data.locked - this.stickied = data.stickied + // Moderation attributes + result.locked = data.locked + result.stickied = data.stickied - // Subreddit - this.subreddit_front = subreddit_front - this.subreddit = data.subreddit + // Subreddit + result.subreddit_front = subreddit_front + result.subreddit = data.subreddit - // Flair - this.link_flair = (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : '') - this.user_flair = (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '') - this.link_flair_text = data.link_flair_text + // Flair + result.link_flair = (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : '') + result.user_flair = (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '') + result.link_flair_text = data.link_flair_text - if(data.domain) { - let tld = data.domain.split('self.') - if(tld.length > 1) { - if(!tld[1].includes('.')) { - this.is_self_link = true - } - } - if(config.valid_media_domains.includes(data.domain) || valid_reddit_self_domains.includes(data.domain)) { - this.is_self_link = true - } - } - - if(data.preview && data.thumbnail !== 'self') { - if(!data.url.startsWith('/r/') && isGif(data.url)) { - this.images = { - thumb: await downloadAndSave(data.thumbnail, 'thumb_') - } - } else { - if(data.preview.images[0].resolutions[0]) { - let preview = null - if(!isGif(data.url) && !data.post_hint.includes(':video')) - preview = await downloadAndSave(data.preview.images[0].source.url) - this.images = { - thumb: await downloadAndSave(data.preview.images[0].resolutions[0].url, 'thumb_'), - preview: preview - } - } - } - } - - // Use black magic in order to return a normal object - return Object.fromEntries(Object.entries(this)) + if(data.domain) { + let tld = data.domain.split('self.') + if(tld.length > 1) { + if(!tld[1].includes('.')) { + result.is_self_link = true + } } + if(config.valid_media_domains.includes(data.domain) || valid_reddit_self_domains.includes(data.domain)) { + result.is_self_link = true + } + } + if(data.preview && data.thumbnail !== 'self') { + if(!data.url.startsWith('/r/') && isGif(data.url)) { + result.images = { + thumb: await downloadAndSave(data.thumbnail, 'thumb_') + } + } else { + if(data.preview.images[0].resolutions[0]) { + let preview = null + if(!isGif(data.url) && !data.post_hint.includes(':video')) + preview = await downloadAndSave(data.preview.images[0].source.url) + result.images = { + thumb: await downloadAndSave(data.preview.images[0].resolutions[0].url, 'thumb_'), + preview: preview + } + } + } + } + + return result } -module.exports = Link; +module.exports = { + fromJson, +}