module.exports = function() { const config = require('../../config') this.handleTedditApiSubreddit = async (json, req, res, from, api_type, api_target, subreddit) => { if(!config.api_enabled) { res.setHeader('Content-Type', 'application/json') let msg = { info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.' } return res.end(JSON.stringify(msg)) } let _json = json // Keep the original json if(from === 'redis') json = JSON.parse(json) if(api_type === 'rss') { let protocol = (config.https_enabled ? 'https' : 'http') let items = '' for(var i = 0; i < json.data.children.length; i++) { let link = json.data.children[i].data let thumbnail = '' let is_self_link = false let valid_reddit_self_domains = ['reddit.com'] if(link.domain) { let tld = link.domain.split('self.') if(tld.length > 1) { if(!tld[1].includes('.')) { is_self_link = true link.url = teddifyUrl(link.url) } } if(config.valid_media_domains.includes(link.domain) || valid_reddit_self_domains.includes(link.domain)) { is_self_link = true link.url = teddifyUrl(link.url) } } if(link.preview && link.thumbnail !== 'self') { if(!link.url.startsWith('/r/') && isGif(link.url)) { let s = await downloadAndSave(link.thumbnail, 'thumb_') thumbnail = `${protocol}://${config.domain}${s}` } else { if(link.preview.images[0].resolutions[0]) { let s = await downloadAndSave(link.preview.images[0].resolutions[0].url, 'thumb_') thumbnail = `${protocol}://${config.domain}${s}` } } } link.permalink = `${protocol}://${config.domain}${link.permalink}` if(is_self_link) link.url = link.permalink let append_desc_html = `
[link] [comments]` items += ` ${link.title} ${link.author} ${link.created} ${link.domain} ${link.id} ${thumbnail} ${link.permalink} ${link.url} ${link.num_comments} ${link.ups} ${link.stickied} ${is_self_link} ` } let r_subreddit = '/r/' + subreddit let title = r_subreddit let link = `${protocol}://${config.domain}${r_subreddit}` if(subreddit === '/') { r_subreddit = 'frontpage' title = 'teddit frontpage' link = `${protocol}://${config.domain}` } let xml_output = ` ${title} ${link} Subreddit feed for: ${r_subreddit} ${items} ` res.setHeader('Content-Type', 'application/rss+xml') return res.end(xml_output) } else { res.setHeader('Content-Type', 'application/json') if(api_target === 'reddit') { return res.end(JSON.stringify(json)) } else { let processed_json = await processJsonSubreddit(_json, from, null, req.cookies) let protocol = (config.https_enabled ? 'https' : 'http') for(var i = 0; i < processed_json.links.length; i++) { let link = processed_json.links[i] let valid_reddit_self_domains = ['reddit.com'] let is_self_link = false if(link.domain) { let tld = link.domain.split('self.') if(tld.length > 1) { if(!tld[1].includes('.')) { is_self_link = true link.url = teddifyUrl(link.url) } } if(config.valid_media_domains.includes(link.domain) || valid_reddit_self_domains.includes(link.domain)) { is_self_link = true link.url = teddifyUrl(link.url) } } link.permalink = `${protocol}://${config.domain}${link.permalink}` if(is_self_link) link.url = link.permalink if(link.images) { if(link.images.thumb !== 'self') { link.images.thumb = `${protocol}://${config.domain}${link.images.thumb}` } } } return res.end(JSON.stringify(processed_json)) } } } }