teddit-reddit-frontend-alte.../inc/components/link.js

83 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-05-24 21:18:50 +02:00
/*
* Corresponds to `components/link.pug`
*/
const config = require('../../config')
let valid_reddit_self_domains = ['reddit.com']
2021-05-26 18:21:05 +02:00
// Parses a link from a response returned by reddit.
async function fromJson(data, user_preferences, subreddit_front) {
let result = {}
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
// 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
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
// 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
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
// Moderation attributes
result.locked = data.locked
result.stickied = data.stickied
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
// Subreddit
result.subreddit_front = subreddit_front
result.subreddit = data.subreddit
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
// 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
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
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
}
}
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
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
2021-05-24 21:18:50 +02:00
}
2021-05-26 18:21:05 +02:00
}
2021-05-24 21:18:50 +02:00
}
2021-05-26 18:21:05 +02:00
}
2021-05-24 21:18:50 +02:00
2021-05-26 18:21:05 +02:00
return result
2021-05-24 21:18:50 +02:00
}
2021-05-26 18:21:05 +02:00
module.exports = {
fromJson,
}