2021-09-08 13:03:35 +02:00
const processJsonSubreddit = require ( '../processJsonSubreddit' ) ;
2022-12-14 22:46:58 +01:00
const { processJsonSubredditAbout } = require ( '../processSubredditAbout' ) ;
2022-12-15 20:16:13 +01:00
const processSearchResults = require ( '../processSearchResults.js' ) ;
const { processJsonPostList , getPostItem } = require ( '../processJsonPost' ) ;
2022-12-15 22:03:49 +01:00
const processJsonSubredditsExplore = require ( '../processSubredditsExplore.js' ) ;
2021-09-08 13:03:35 +02:00
module . exports = function ( ) {
const config = require ( '../../config' ) ;
this . handleTedditApiSubreddit = async (
json ,
req ,
res ,
from ,
api _type ,
api _target ,
2021-12-24 15:37:46 +01:00
subreddit ,
mode
2021-09-08 13:03:35 +02:00
) => {
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 ) ) ;
2020-12-19 20:52:22 +01:00
}
2021-09-08 13:03:35 +02:00
console . log ( 'Teddit API request - subreddit' ) ;
let _json = json ; // Keep the original json
if ( from === 'redis' ) json = JSON . parse ( json ) ;
if ( api _type === 'rss' ) {
2022-01-18 19:47:16 +01:00
let protocol = config . https _enabled || config . api _force _https ? 'https' : 'http' ;
2021-09-08 13:03:35 +02:00
let items = '' ;
2022-12-15 20:25:04 +01:00
for ( var i = 0 ; i < json . data . children . length ; i ++ ) {
let post = json . data . children [ i ] . data ;
items += await getPostItem ( post , req , protocol ) ;
2020-12-19 20:52:22 +01:00
}
2021-09-08 13:03:35 +02:00
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 } ` ;
2020-12-19 20:52:22 +01:00
}
2021-09-08 13:03:35 +02:00
let xml _output = ` <?xml version="1.0" encoding="UTF-8"?>
2020-12-19 20:52:22 +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 > $ { 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 >
2021-09-08 13:03:35 +02:00
< / r s s > ` ;
res . setHeader ( 'Content-Type' , 'application/rss+xml' ) ;
return res . end ( xml _output ) ;
2020-12-19 20:52:22 +01:00
} else {
2021-09-08 13:03:35 +02:00
res . setHeader ( 'Content-Type' , 'application/json' ) ;
if ( api _target === 'reddit' ) {
return res . end ( JSON . stringify ( json ) ) ;
2020-12-19 20:52:22 +01:00
} else {
2021-09-08 13:03:35 +02:00
let processed _json = await processJsonSubreddit (
_json ,
from ,
null ,
req . cookies
) ;
2022-12-15 20:25:04 +01:00
await processJsonPostList ( processed _json . links , mode ) ;
2021-12-28 22:58:34 +01:00
2021-09-08 13:03:35 +02:00
return res . end ( JSON . stringify ( processed _json ) ) ;
}
2020-12-19 20:52:22 +01:00
}
2021-09-08 13:03:35 +02:00
} ;
2022-12-14 22:46:58 +01:00
this . handleTedditApiSubredditAbout = async (
json ,
res ,
from ,
api _target
) => {
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 ) ) ;
}
console . log ( 'Teddit API request - subreddit about' ) ;
if ( from === 'redis' ) json = JSON . parse ( json ) ;
res . setHeader ( 'Content-Type' , 'application/json' ) ;
if ( api _target === 'reddit' ) {
return res . end ( JSON . stringify ( json ) ) ;
} else {
let subreddit _about = await processJsonSubredditAbout (
json ,
true
) ;
return res . end ( JSON . stringify ( subreddit _about ) ) ;
}
} ;
2022-12-15 20:16:13 +01:00
this . handleTedditApiSubredditSearch = async (
json ,
req ,
res ,
from ,
api _type ,
api _target ,
subreddit ,
query ,
mode
) => {
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 ) ) ;
}
console . log ( 'Teddit API request - subreddit search' ) ;
if ( from === 'redis' ) json = JSON . parse ( json ) ;
if ( api _type === 'rss' ) {
let protocol = config . https _enabled || config . api _force _https ? 'https' : 'http' ;
let items = '' ;
for ( var i = 0 ; i < json . data . children . length ; i ++ ) {
let post = json . data . children [ i ] . data ;
items += await getPostItem ( post , req , protocol ) ;
}
let r _subreddit = '/r/' + subreddit ;
let title = ` ${ query } - ${ r _subreddit } ` ;
let link = ` ${ protocol } :// ${ config . domain } ${ r _subreddit } /search?q= ${ query } ` ;
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 >
2022-12-15 20:28:06 +01:00
< atom : link href = "${link}&api&type=rss" rel = "self" type = "application/rss+xml" / >
2022-12-15 20:16:13 +01:00
< title > $ { title } < / t i t l e >
< link > $ { link } < / l i n k >
< description > Results for : $ { query } - $ { 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 {
let processed _json = await processSearchResults (
json ,
true ,
null ,
null ,
req . cookies
) ;
await processJsonPostList ( processed _json . posts , mode ) ;
2022-12-15 22:03:49 +01:00
return res . end ( JSON . stringify ( processed _json ) ) ;
}
}
} ;
this . handleTedditApiSubredditsExplore = async (
json ,
req ,
res ,
from ,
api _type ,
api _target ,
query
) => {
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 ) ) ;
}
console . log ( 'Teddit API request - subreddit explore' ) ;
if ( from === 'redis' ) json = JSON . parse ( json ) ;
if ( api _type === 'rss' ) {
let protocol = config . https _enabled || config . api _force _https ? 'https' : 'http' ;
let items = '' ;
let children _len = json . data . children . length ;
for ( var i = 0 ; i < children _len ; i ++ ) {
let data = json . data . children [ i ] . data ;
items += `
< item >
< title > $ { data . title } < / t i t l e >
< created > $ { data . created } < / c r e a t e d >
< pubDate > $ { new Date (
data . created _utc * 1000
) . toGMTString ( ) } < / p u b D a t e >
< id > $ { data . id } < / i d >
< name > $ { data . display _name } < / n a m e >
< name _prefixed > $ { data . display _name _prefixed } < / n a m e _ p r e f i x e d >
< url > $ { replaceDomains ( data . url , req . cookies ) } < / u r l >
< description > < ! [ CDATA [ $ { unescape (
data . public _description _html
) } ] ] > < / d e s c r i p t i o n >
< subscribers > $ { data . subscribers } < / s u b s c r i b e r s >
< over _18 > $ { data . over18 } < / o v e r _ 1 8 >
< / i t e m >
` ;
}
let title = ` ${ query } ` ;
let link = ` ${ protocol } :// ${ config . domain } /subreddits/search?q= ${ query } ` ;
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 > Results for : $ { query } < / 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 {
let processed _json = await processJsonSubredditsExplore (
json ,
true ,
null ,
req . cookies
) ;
2022-12-15 20:16:13 +01:00
return res . end ( JSON . stringify ( processed _json ) ) ;
}
}
} ;
2021-09-08 13:03:35 +02:00
} ;