2021-09-07 14:27:29 +02:00
const processJsonUser = require ( '../processJsonUser' ) ;
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 ) ) ;
2021-01-06 16:06:11 +01:00
}
2021-09-07 14:27:29 +02:00
console . log ( 'Teddit API request - user' ) ;
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 ;
2021-01-06 16:06:11 +01:00
}
2021-09-07 14:27:29 +02:00
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> ` ;
2021-01-06 16:06:11 +01:00
t1 _elements = `
2021-09-07 14:27:29 +02:00
< description > < ! [ CDATA [ $ { unescape (
post . body _html
) } $ { append _desc _html } ] ] > < / d e s c r i p t i o n >
2021-01-06 16:06:11 +01:00
< url > $ { comments _url } < / u r l >
2021-09-07 14:27:29 +02:00
` ;
2021-01-06 16:06:11 +01:00
}
2021-09-07 14:27:29 +02:00
if ( kind === 't3' ) {
let s = await downloadAndSave ( post . thumbnail , 'thumb_' ) ;
let thumbnail = '' ;
let enclosure = '' ;
if ( s !== 'self' && s != '' ) {
let img = ` ${ protocol } :// ${ config . domain } ${ s } ` ;
thumbnail = ` <thumbnail> ${ img } </thumbnail> ` ;
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
}
2021-09-07 14:27:29 +02:00
let append _desc _html = ` submitted by <a href=" ${ protocol } :// ${ config . domain } /u/ ${ user } >/u/ ${ user } </a> to <a href=" ${ protocol } :// ${ config . domain } /r/ ${ post . subreddit } /">r/ ${ post . subreddit } </a> ` ;
append _desc _html += ` <br/><a href=" ${ permalink } ">[comments]</a> ` ;
2021-01-06 16:06:11 +01:00
t3 _elements = `
2021-09-07 14:27:29 +02:00
< description > < ! [ CDATA [ $ { unescape (
post . selftext _html
) } $ { append _desc _html } ] ] > < / d e s c r i p t i o n >
2021-01-06 16:06:11 +01:00
$ { thumbnail }
2021-01-06 16:20:44 +01:00
$ { enclosure }
2021-09-07 14:27:29 +02:00
` ;
2021-01-06 16:06:11 +01:00
}
2021-09-07 14:27:29 +02:00
let title = post . title ;
if ( ! post . title ) title = post . link _title ;
2021-01-06 16:06:11 +01:00
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 >
2021-09-07 14:27:29 +02:00
< pubDate > $ { new Date (
post . created _utc * 1000
) . toGMTString ( ) } < / p u b D a t e >
2021-01-06 16:06:11 +01:00
< 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 >
2021-09-07 14:27:29 +02:00
` ;
2021-01-06 16:06:11 +01:00
}
2021-09-07 14:27:29 +02:00
let xml _output = ` <?xml version="1.0" encoding="UTF-8"?>
2021-01-06 16:06:11 +01:00
< 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 >
2021-09-07 14:27:29 +02:00
< / r s s > ` ;
res . setHeader ( 'Content-Type' , 'application/rss+xml' ) ;
return res . end ( xml _output ) ;
2021-01-06 16:06:11 +01:00
} else {
2021-09-07 14:27:29 +02:00
res . setHeader ( 'Content-Type' , 'application/json' ) ;
if ( api _target === 'reddit' ) {
return res . end ( JSON . stringify ( json ) ) ;
2021-01-06 16:06:11 +01:00
} else {
2021-09-07 14:27:29 +02:00
let processed _json = await processJsonUser (
json ,
true ,
null ,
null ,
req . cookies
) ;
return res . end ( JSON . stringify ( processed _json ) ) ;
}
2021-01-06 16:06:11 +01:00
}
2021-09-07 14:27:29 +02:00
} ;
} ;