Use a standalone function
This commit is contained in:
parent
5ed29ea1a1
commit
a8974f709d
|
@ -5,79 +5,78 @@
|
||||||
const config = require('../../config')
|
const config = require('../../config')
|
||||||
let valid_reddit_self_domains = ['reddit.com']
|
let valid_reddit_self_domains = ['reddit.com']
|
||||||
|
|
||||||
class Link {
|
// Parses a link from a response returned by reddit.
|
||||||
// Parses a link from a response returned by reddit.
|
async function fromJson(data, user_preferences, subreddit_front) {
|
||||||
static async fromJson(data, user_preferences, subreddit_front) {
|
let result = {}
|
||||||
|
|
||||||
// Meta
|
// Meta
|
||||||
this.id = data.id
|
result.id = data.id
|
||||||
this.permalink = data.permalink
|
result.permalink = data.permalink
|
||||||
this.created = data.created_utc
|
result.created = data.created_utc
|
||||||
this.author = data.author
|
result.author = data.author
|
||||||
this.title = data.title
|
result.title = data.title
|
||||||
this.over_18 = data.over_18
|
result.over_18 = data.over_18
|
||||||
this.score = data.score
|
result.score = data.score
|
||||||
this.ups = data.ups
|
result.ups = data.ups
|
||||||
this.upvote_ratio = data.upvote_ratio
|
result.upvote_ratio = data.upvote_ratio
|
||||||
this.num_comments = data.num_comments
|
result.num_comments = data.num_comments
|
||||||
|
|
||||||
// Content
|
// Content
|
||||||
this.is_self_link = false
|
result.is_self_link = false
|
||||||
this.selftext_html = data.selftext_html
|
result.selftext_html = data.selftext_html
|
||||||
this.url = replaceDomains(data.url, user_preferences)
|
result.url = replaceDomains(data.url, user_preferences)
|
||||||
this.domain = data.domain
|
result.domain = data.domain
|
||||||
this.is_video = data.is_video
|
result.is_video = data.is_video
|
||||||
this.media = data.media
|
result.media = data.media
|
||||||
this.duration = data.is_video ? data.media.reddit_video ? data.media.reddit_video.duration : void 0 : void 0
|
result.duration = data.is_video ? data.media.reddit_video ? data.media.reddit_video.duration : void 0 : void 0
|
||||||
this.images = null
|
result.images = null
|
||||||
|
|
||||||
// Moderation attributes
|
// Moderation attributes
|
||||||
this.locked = data.locked
|
result.locked = data.locked
|
||||||
this.stickied = data.stickied
|
result.stickied = data.stickied
|
||||||
|
|
||||||
// Subreddit
|
// Subreddit
|
||||||
this.subreddit_front = subreddit_front
|
result.subreddit_front = subreddit_front
|
||||||
this.subreddit = data.subreddit
|
result.subreddit = data.subreddit
|
||||||
|
|
||||||
// Flair
|
// Flair
|
||||||
this.link_flair = (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : '')
|
result.link_flair = (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : '')
|
||||||
this.user_flair = (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
|
result.user_flair = (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
|
||||||
this.link_flair_text = data.link_flair_text
|
result.link_flair_text = data.link_flair_text
|
||||||
|
|
||||||
if(data.domain) {
|
if(data.domain) {
|
||||||
let tld = data.domain.split('self.')
|
let tld = data.domain.split('self.')
|
||||||
if(tld.length > 1) {
|
if(tld.length > 1) {
|
||||||
if(!tld[1].includes('.')) {
|
if(!tld[1].includes('.')) {
|
||||||
this.is_self_link = true
|
result.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(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,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue