mirror of
https://codeberg.org/teddit/teddit.git
synced 2026-03-05 13:30:33 -05:00
Add support for subreddits explore API
This commit is contained in:
@@ -2,6 +2,7 @@ const processJsonSubreddit = require('../processJsonSubreddit');
|
|||||||
const { processJsonSubredditAbout } = require('../processSubredditAbout');
|
const { processJsonSubredditAbout } = require('../processSubredditAbout');
|
||||||
const processSearchResults = require('../processSearchResults.js');
|
const processSearchResults = require('../processSearchResults.js');
|
||||||
const { processJsonPostList, getPostItem } = require('../processJsonPost');
|
const { processJsonPostList, getPostItem } = require('../processJsonPost');
|
||||||
|
const processJsonSubredditsExplore = require('../processSubredditsExplore.js');
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
@@ -169,6 +170,86 @@ module.exports = function () {
|
|||||||
|
|
||||||
await processJsonPostList(processed_json.posts, mode);
|
await processJsonPostList(processed_json.posts, mode);
|
||||||
|
|
||||||
|
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}</title>
|
||||||
|
<created>${data.created}</created>
|
||||||
|
<pubDate>${new Date(
|
||||||
|
data.created_utc * 1000
|
||||||
|
).toGMTString()}</pubDate>
|
||||||
|
<id>${data.id}</id>
|
||||||
|
<name>${data.display_name}</name>
|
||||||
|
<name_prefixed>${data.display_name_prefixed}</name_prefixed>
|
||||||
|
<url>${replaceDomains(data.url, req.cookies)}</url>
|
||||||
|
<description><![CDATA[${unescape(
|
||||||
|
data.public_description_html
|
||||||
|
)}]]></description>
|
||||||
|
<subscribers>${data.subscribers}</subscribers>
|
||||||
|
<over_18>${data.over18}</over_18>
|
||||||
|
</item>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
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}</title>
|
||||||
|
<link>${link}</link>
|
||||||
|
<description>Results for: ${query}</description>
|
||||||
|
${items}
|
||||||
|
</channel>
|
||||||
|
</rss>`;
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
return res.end(JSON.stringify(processed_json));
|
return res.end(JSON.stringify(processed_json));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1010,6 +1010,14 @@ subredditRoutes.get('/subreddits/:sort?', (req, res, next) => {
|
|||||||
let before = req.query.before;
|
let before = req.query.before;
|
||||||
let sortby = req.params.sort;
|
let sortby = req.params.sort;
|
||||||
let searching = false;
|
let searching = false;
|
||||||
|
let api_req = req.query.api;
|
||||||
|
let api_type = req.query.type;
|
||||||
|
let api_target = req.query.target;
|
||||||
|
|
||||||
|
if (req.query.hasOwnProperty('api')) api_req = true;
|
||||||
|
else api_req = false;
|
||||||
|
|
||||||
|
let raw_json = api_req && req.query.raw_json == '1' ? 1 : 0;
|
||||||
|
|
||||||
if (!after) {
|
if (!after) {
|
||||||
after = '';
|
after = '';
|
||||||
@@ -1031,12 +1039,12 @@ subredditRoutes.get('/subreddits/:sort?', (req, res, next) => {
|
|||||||
sortby = '';
|
sortby = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
let key = `subreddits:sort:${sortby}${d}`;
|
let key = `subreddits:sort:${sortby}${d}:raw_json:${raw_json}`;
|
||||||
|
|
||||||
if (sortby === 'search') {
|
if (sortby === 'search') {
|
||||||
if (typeof q == 'undefined' || q == '') return res.redirect('/subreddits');
|
if (typeof q == 'undefined' || q == '') return res.redirect('/subreddits');
|
||||||
|
|
||||||
key = `subreddits:search:q:${q}:nsfw:${nsfw}${d}`;
|
key = `subreddits:search:q:${q}:nsfw:${nsfw}${d}:raw_json:${raw_json}`;
|
||||||
searching = true;
|
searching = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1052,48 +1060,60 @@ subredditRoutes.get('/subreddits/:sort?', (req, res, next) => {
|
|||||||
if (json) {
|
if (json) {
|
||||||
console.log(`Got subreddits key from redis.`);
|
console.log(`Got subreddits key from redis.`);
|
||||||
(async () => {
|
(async () => {
|
||||||
let processed_json = await processJsonSubredditsExplore(
|
if (api_req) {
|
||||||
json,
|
return handleTedditApiSubredditsExplore(
|
||||||
'redis',
|
json,
|
||||||
null,
|
req,
|
||||||
req.cookies
|
res,
|
||||||
);
|
'redis',
|
||||||
if (!processed_json.error) {
|
api_type,
|
||||||
return res.render('subreddits_explore', {
|
api_target,
|
||||||
json: processed_json,
|
q
|
||||||
sortby: sortby,
|
);
|
||||||
after: after,
|
|
||||||
before: before,
|
|
||||||
q: q,
|
|
||||||
nsfw: nsfw,
|
|
||||||
searching: searching,
|
|
||||||
subreddits_front: !before && !after ? true : false,
|
|
||||||
user_preferences: req.cookies,
|
|
||||||
instance_nsfw_enabled: config.nsfw_enabled,
|
|
||||||
instance_config: config,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
return res.render('subreddits_explore', {
|
let processed_json = await processJsonSubredditsExplore(
|
||||||
json: null,
|
json,
|
||||||
error: true,
|
'redis',
|
||||||
data: processed_json,
|
null,
|
||||||
user_preferences: req.cookies,
|
req.cookies
|
||||||
instance_config: config,
|
);
|
||||||
});
|
if (!processed_json.error) {
|
||||||
|
return res.render('subreddits_explore', {
|
||||||
|
json: processed_json,
|
||||||
|
sortby: sortby,
|
||||||
|
after: after,
|
||||||
|
before: before,
|
||||||
|
q: q,
|
||||||
|
nsfw: nsfw,
|
||||||
|
searching: searching,
|
||||||
|
subreddits_front: !before && !after ? true : false,
|
||||||
|
user_preferences: req.cookies,
|
||||||
|
instance_nsfw_enabled: config.nsfw_enabled,
|
||||||
|
instance_config: config,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return res.render('subreddits_explore', {
|
||||||
|
json: null,
|
||||||
|
error: true,
|
||||||
|
data: processed_json,
|
||||||
|
user_preferences: req.cookies,
|
||||||
|
instance_config: config,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
} else {
|
} else {
|
||||||
let url = '';
|
let url = '';
|
||||||
if (config.use_reddit_oauth) {
|
if (config.use_reddit_oauth) {
|
||||||
if (!searching)
|
if (!searching)
|
||||||
url = `https://oauth.reddit.com/subreddits/${sortby}?api_type=json&count=25&g=GLOBAL&t=${d}`;
|
url = `https://oauth.reddit.com/subreddits/${sortby}?api_type=json&count=25&g=GLOBAL&t=${d}&raw_json=${raw_json}`;
|
||||||
else
|
else
|
||||||
url = `https://oauth.reddit.com/subreddits/search?api_type=json&q=${q}&include_over_18=${nsfw}${d}`;
|
url = `https://oauth.reddit.com/subreddits/search?api_type=json&q=${q}&include_over_18=${nsfw}${d}&raw_json=${raw_json}`;
|
||||||
} else {
|
} else {
|
||||||
if (!searching)
|
if (!searching)
|
||||||
url = `https://reddit.com/subreddits/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${d}`;
|
url = `https://reddit.com/subreddits/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${d}&raw_json=${raw_json}`;
|
||||||
else
|
else
|
||||||
url = `https://reddit.com/subreddits/search.json?api_type=json&q=${q}&include_over_18=${nsfw}${d}`;
|
url = `https://reddit.com/subreddits/search.json?api_type=json&q=${q}&include_over_18=${nsfw}${d}&raw_json=${raw_json}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch(encodeURI(url), redditApiGETHeaders())
|
fetch(encodeURI(url), redditApiGETHeaders())
|
||||||
@@ -1117,25 +1137,37 @@ subredditRoutes.get('/subreddits/:sort?', (req, res, next) => {
|
|||||||
} else {
|
} else {
|
||||||
console.log(`Fetched the JSON from reddit.com/subreddits.`);
|
console.log(`Fetched the JSON from reddit.com/subreddits.`);
|
||||||
(async () => {
|
(async () => {
|
||||||
let processed_json = await processJsonSubredditsExplore(
|
if (api_req) {
|
||||||
json,
|
return handleTedditApiSubredditsExplore(
|
||||||
'from_online',
|
json,
|
||||||
null,
|
req,
|
||||||
req.cookies
|
res,
|
||||||
);
|
'from_online',
|
||||||
return res.render('subreddits_explore', {
|
api_type,
|
||||||
json: processed_json,
|
api_target,
|
||||||
sortby: sortby,
|
q
|
||||||
after: after,
|
);
|
||||||
before: before,
|
} else {
|
||||||
q: q,
|
let processed_json = await processJsonSubredditsExplore(
|
||||||
nsfw: nsfw,
|
json,
|
||||||
searching: searching,
|
'from_online',
|
||||||
subreddits_front: !before && !after ? true : false,
|
null,
|
||||||
user_preferences: req.cookies,
|
req.cookies
|
||||||
instance_nsfw_enabled: config.nsfw_enabled,
|
);
|
||||||
instance_config: config,
|
return res.render('subreddits_explore', {
|
||||||
});
|
json: processed_json,
|
||||||
|
sortby: sortby,
|
||||||
|
after: after,
|
||||||
|
before: before,
|
||||||
|
q: q,
|
||||||
|
nsfw: nsfw,
|
||||||
|
searching: searching,
|
||||||
|
subreddits_front: !before && !after ? true : false,
|
||||||
|
user_preferences: req.cookies,
|
||||||
|
instance_nsfw_enabled: config.nsfw_enabled,
|
||||||
|
instance_config: config,
|
||||||
|
});
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user