2021-01-06 16:06:11 +01:00
module . exports = function ( ) {
const config = require ( '../../config' )
this . handleTedditApiUser = async ( json , req , res , from , api _type , api _target , user ) => {
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 )
let protocol = ( config . https _enabled ? 'https' : 'http' )
let link = ` ${ protocol } :// ${ config . domain } /user/ ${ user } `
if ( api _type === 'rss' ) {
let items = ''
let posts _limit = 25
if ( json . overview . data . children . length <= posts _limit ) {
posts _limit = json . overview . data . children . length
}
for ( var i = 0 ; i < posts _limit ; i ++ ) {
let post = json . overview . data . children [ i ] . data
let post _id = post . permalink . split ( '/' ) . slice ( - 2 ) [ 0 ] + '/'
let url = post . permalink . replace ( post _id , '' )
let permalink = ` ${ protocol } :// ${ config . domain } ${ post . permalink } `
let comments _url = ` ${ protocol } :// ${ config . domain } ${ url } `
let kind = json . overview . data . children [ i ] . kind
let t1 _elements = ''
let t3 _elements = ''
if ( kind === 't1' ) {
let append _desc _html = ` <br/><a href=" ${ permalink } ">[link]</a> <a href=" ${ comments _url } ">[comments]</a> `
t1 _elements = `
< description > < ! [ CDATA [ $ { unescape ( post . body _html ) } $ { append _desc _html } ] ] > < / d e s c r i p t i o n >
< url > $ { comments _url } < / u r l >
`
}
if ( kind === 't3' ) {
let s = await downloadAndSave ( post . thumbnail , 'thumb_' )
let thumbnail = ''
2021-01-06 16:20:44 +01:00
let enclosure = ''
if ( s !== 'self' && s != '' ) {
2021-01-06 16:06:11 +01:00
let img = ` ${ protocol } :// ${ config . domain } ${ s } `
thumbnail = ` <thumbnail> ${ img } </thumbnail> `
2021-01-06 16:20:44 +01:00
let mime = ''
let ext = s . split ( '.' ) . pop ( )
if ( ext === 'png' )
mime = 'image/png'
else
mime = 'image/jpeg'
enclosure = ` <enclosure length="0" type=" ${ mime } " url=" ${ img } " /> `
2021-01-06 16:06:11 +01:00
}
let append _desc _html = ` <br/><a href=" ${ permalink } ">[comments]</a> `
t3 _elements = `
< description > < ! [ CDATA [ $ { unescape ( post . selftext _html ) } $ { append _desc _html } ] ] > < / d e s c r i p t i o n >
$ { thumbnail }
2021-01-06 16:20:44 +01:00
$ { enclosure }
2021-01-06 16:06:11 +01:00
`
}
let title = post . title
if ( ! post . title )
title = post . link _title
items += `
< item >
< title > $ { title } < / t i t l e >
< dc : creator > /u/ $ { user } < / d c : c r e a t o r >
< kind > $ { kind } < / k i n d >
< subreddit > $ { post . subreddit } < / s u b r e d d i t >
< created > $ { post . created _utc } < / c r e a t e d >
< ups > $ { post . ups } < / u p s >
< link > $ { permalink } < / l i n k >
< edited > $ { post . edited } < / e d i t e d >
< num _comments > $ { post . num _comments } < / n u m _ c o m m e n t s >
< over _18 > $ { post . over _18 } < / o v e r _ 1 8 >
$ { t1 _elements }
$ { t3 _elements }
< / i t e m >
`
}
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 > overview for $ { user } < / t i t l e >
< link > $ { link } < / l i n k >
$ { 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 {
let processed _json = await processJsonUser ( json , true , null , null , req . cookies )
return res . end ( JSON . stringify ( processed _json ) )
}
}
}
}