2020-12-19 20:52:22 +01:00
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 ) )
}
2021-03-27 16:08:15 +01:00
console . log ( 'Teddit API request - subreddit' )
2020-12-19 20:52:22 +01:00
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 = ''
2021-03-27 16:08:15 +01:00
let post _image = ''
2020-12-19 20:52:22 +01:00
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 } `
2021-03-27 16:08:15 +01:00
if ( ! isGif ( link . url ) && ! link . post _hint . includes ( ':video' ) ) {
s = await downloadAndSave ( link . preview . images [ 0 ] . source . url )
post _image = ` ${ protocol } :// ${ config . domain } ${ s } `
}
2020-12-19 20:52:22 +01:00
}
}
}
link . permalink = ` ${ protocol } :// ${ config . domain } ${ link . permalink } `
if ( is _self _link )
link . url = link . permalink
2021-01-01 19:56:19 +01:00
2021-04-05 18:02:12 +02:00
if ( req . query . hasOwnProperty ( 'full_thumbs' ) ) {
if ( ! post _image )
post _image = thumbnail
2021-04-05 18:03:40 +02:00
2021-03-27 16:58:56 +01:00
thumbnail = post _image
2021-04-05 18:02:12 +02:00
}
2021-03-27 16:58:56 +01:00
2021-01-01 19:56:19 +01:00
let enclosure = ''
if ( thumbnail != '' ) {
let mime = ''
let ext = thumbnail . split ( '.' ) . pop ( )
if ( ext === 'png' )
mime = 'image/png'
else
mime = 'image/jpeg'
enclosure = ` <enclosure length="0" type=" ${ mime } " url=" ${ thumbnail } " /> `
}
2020-12-19 20:52:22 +01:00
2020-12-29 16:44:24 +01:00
let append _desc _html = ` <br/><a href=" ${ link . url } ">[link]</a> <a href=" ${ link . permalink } ">[comments]</a> `
2020-12-19 20:52:22 +01:00
items += `
< item >
< title > $ { link . title } < / t i t l e >
< author > $ { link . author } < / a u t h o r >
< created > $ { link . created } < / c r e a t e d >
2021-01-30 15:23:26 +01:00
< pubDate > $ { new Date ( link . created _utc * 1000 ) . toGMTString ( ) } < / p u b D a t e >
2020-12-19 20:52:22 +01:00
< domain > $ { link . domain } < / d o m a i n >
< id > $ { link . id } < / i d >
< thumbnail > $ { thumbnail } < / t h u m b n a i l >
2021-01-01 19:56:19 +01:00
$ { enclosure }
2020-12-22 16:23:27 +01:00
< link > $ { link . permalink } < / l i n k >
2020-12-19 20:52:22 +01:00
< url > $ { link . url } < / u r l >
2020-12-29 16:44:24 +01:00
< description > < ! [ CDATA [ $ { unescape ( link . selftext _html ) } $ { append _desc _html } ] ] > < / d e s c r i p t i o n >
2020-12-19 20:52:22 +01:00
< num _comments > $ { link . num _comments } < / n u m _ c o m m e n t s >
< ups > $ { link . ups } < / u p s >
< stickied > $ { link . stickied } < / s t i c k i e d >
< is _self _link > $ { is _self _link } < / i s _ s e l f _ l i n k >
< / i t e m >
`
}
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 =
` <?xml version="1.0" encoding="UTF-8"?>
< rss xmlns : atom = "http://www.w3.org/2005/Atom" xmlns : dc = "http://purl.org/dc/elements/1.1/" version = "2.0" >
< channel >
< atom : link href = "${link}/?api&type=rss" rel = "self" type = "application/rss+xml" / >
< title > $ { title } < / t i t l e >
< link > $ { link } < / l i n k >
< description > Subreddit feed for : $ { r _subreddit } < / d e s c r i p t i o n >
$ { items }
< / c h a n n e l >
< / r s s > `
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 {
2020-12-24 22:13:08 +01:00
let processed _json = await processJsonSubreddit ( _json , from , null , req . cookies )
2020-12-19 20:52:22 +01:00
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 ) )
}
}
}
}