From 533519df65e9d6628ce50ea9825579339459654b Mon Sep 17 00:00:00 2001 From: StevenNMeza Date: Mon, 24 May 2021 21:18:50 +0200 Subject: [PATCH] Implement a central link object --- inc/components/link.js | 83 +++++++++++++++++++++++++++++++++++++ inc/processJsonSubreddit.js | 41 ++++-------------- inc/processJsonUser.js | 29 ++----------- inc/processSearchResults.js | 52 +---------------------- static/css/styles.css | 5 +-- views/search.pug | 48 +-------------------- views/user.pug | 1 - 7 files changed, 99 insertions(+), 160 deletions(-) create mode 100644 inc/components/link.js diff --git a/inc/components/link.js b/inc/components/link.js new file mode 100644 index 0000000..66863d5 --- /dev/null +++ b/inc/components/link.js @@ -0,0 +1,83 @@ +/* + * Corresponds to `components/link.pug` + */ + +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) { + + // 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 + + // 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 + + // Moderation attributes + this.locked = data.locked + this.stickied = data.stickied + + // Subreddit + this.subreddit_front = subreddit_front + this.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 + + 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)) + } + +} + +module.exports = Link; diff --git a/inc/processJsonSubreddit.js b/inc/processJsonSubreddit.js index 597033e..cb9f058 100644 --- a/inc/processJsonSubreddit.js +++ b/inc/processJsonSubreddit.js @@ -1,5 +1,6 @@ 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 () => { @@ -35,43 +36,14 @@ module.exports = function() { 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'] if(data.over_18) if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') continue - if(data.domain) { - let tld = data.domain.split('self.') - if(tld.length > 1) { - if(!tld[1].includes('.')) { - is_self_link = true - } - } - if(config.valid_media_domains.includes(data.domain) || valid_reddit_self_domains.includes(data.domain)) { - is_self_link = true - } - } - - if(data.preview && data.thumbnail !== 'self') { - if(!data.url.startsWith('/r/') && isGif(data.url)) { - 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) - images = { - thumb: await downloadAndSave(data.preview.images[0].resolutions[0].url, 'thumb_'), - preview: preview - } - } - } - } + /* + // 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, @@ -97,7 +69,10 @@ module.exports = function() { 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) } resolve(ret) diff --git a/inc/processJsonUser.js b/inc/processJsonUser.js index 4a0ec76..83c8fce 100644 --- a/inc/processJsonUser.js +++ b/inc/processJsonUser.js @@ -1,5 +1,6 @@ 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 () => { @@ -49,32 +50,8 @@ module.exports = function() { continue if(type === 't3') { - let duration = null - if(post.media) { - if(post.is_video) { - if(post.media.reddit_video) { - duration = post.media.reddit_video.duration - } - } - } - - obj = { - type: type, - subreddit: post.subreddit, - title: post.title, - created: post.created_utc, - ups: post.ups, - url: replaceDomains(url, user_preferences), - domain: post.domain, - thumbnail: await downloadAndSave(post.thumbnail), - duration: duration, - edited: post.edited, - selftext_html: unescape(post.selftext_html), - num_comments: post.num_comments, - over_18: post.over_18, - permalink: post.permalink, - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') - } + obj = await link.fromJson(post, user_preferences) + obj.type = 't3' } if(type === 't1') { obj = { diff --git a/inc/processSearchResults.js b/inc/processSearchResults.js index bb71ef2..aa97b9d 100644 --- a/inc/processSearchResults.js +++ b/inc/processSearchResults.js @@ -1,5 +1,6 @@ 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 () => { @@ -36,61 +37,12 @@ module.exports = function() { for(var i = 0; i < posts_limit; i++) { let post = json.data.children[i].data - let images = null - let is_self_link = false - let valid_reddit_self_domains = ['reddit.com'] if(post.over_18) if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') continue - if(post.domain) { - let tld = post.domain.split('self.') - if(tld.length > 1) { - if(!tld[1].includes('.')) { - is_self_link = true - } - } - if(config.valid_media_domains.includes(post.domain) || valid_reddit_self_domains.includes(post.domain)) { - is_self_link = true - } - } - - if(post.preview && post.thumbnail !== 'self') { - if(!post.url.startsWith('/r/') && isGif(post.url)) { - images = { - thumb: await downloadAndSave(post.thumbnail, 'thumb_') - } - } else { - if(post.preview.images[0].resolutions[0]) { - images = { - thumb: await downloadAndSave(post.preview.images[0].resolutions[0].url, 'thumb_') - } - } - } - } - - let obj = { - subreddit: post.subreddit, - title: post.title, - created: post.created_utc, - domain: post.domain, - subreddit_name_prefixed: post.subreddit_name_prefixed, - link_flair_text: post.link_flair_text, - ups: post.ups, - images: images, - url: replaceDomains(post.url, user_preferences), - edited: post.edited, - selftext_html: unescape(post.body_html), - num_comments: post.num_comments, - over_18: post.over_18, - permalink: post.permalink, - is_self_link: is_self_link, - author: post.author, - link_title: post.link_title, - link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''), - user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') - } + let obj = await link.fromJson(post, user_preferences) posts.push(obj) } } diff --git a/static/css/styles.css b/static/css/styles.css index 87e91e3..5e88733 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -1039,8 +1039,7 @@ footer a { margin-right: 8px; position: relative; } -#user .entries .commententry .image a span { - position: absolute; +#user .entries .link .image a span { bottom: 0; background: #0000005e; left: 0; @@ -1050,7 +1049,7 @@ footer a { font-size: var(--sm-font); margin-bottom: 4px; } -#user .entries .commententry .image img { +#user .entries .link .image img { max-width: 80px; } #user .entries .commententry .title a { diff --git a/views/search.pug b/views/search.pug index a3a07c8..666658f 100644 --- a/views/search.pug +++ b/views/search.pug @@ -137,53 +137,7 @@ html span.tag.nsfw NSFW a(href="/subreddits/search?q="+ q +"", class="btn") show more similar subreddits each link in json.posts - .link - .upvotes - .arrow - span #{kFormatter(link.ups)} - .arrow.down - .image - if link.images - if link.is_self_link - a(href="" + link.permalink + "") - img(src="" + link.images.thumb + "", alt="") - else - a(href=""+ link.url +"", rel="noopener noreferrer") - img(src="" + link.images.thumb + "", alt="") - else - a(href="" + link.permalink + "") - .no-image no image - .entry - .title - if link.is_self_link - a(href="" + link.permalink + "") - h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} - != link.link_flair - span (#{link.domain}) - else - a(href="" + link.url + "", rel="noopener noreferrer") - h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} - != link.link_flair - span (#{link.domain}) - .meta - p.submitted submitted - span(title="" + toUTCString(link.created) + "") #{timeDifference(link.created)} by - if link.author === '[deleted]' - span(class="deleted") [deleted] - else - a(href="/u/" + link.author + "") - | #{link.author} - != link.user_flair - p.to to - a(href="/r/" + link.subreddit + "") - | #{link.subreddit} - if link.stickied - span(class="green") stickied - .links - if link.over_18 - span.tag.nsfw NSFW - a(href="" + link.permalink + "", class="comments") - | #{link.num_comments} comments + include components/link.pug if json.before || json.after .view-more-links if json.before && !subreddit_front diff --git a/views/user.pug b/views/user.pug index c5aae74..dfdf973 100644 --- a/views/user.pug +++ b/views/user.pug @@ -84,7 +84,6 @@ html if post.type === 't3' - var link = post; - link.author = data.username; include components/link.pug //-.entry.t3 //- .upvotes