add user support for teddit api
This commit is contained in:
parent
ea70ef9296
commit
d2769937ac
|
@ -0,0 +1,100 @@
|
|||
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}]]></description>
|
||||
<url>${comments_url}</url>
|
||||
`
|
||||
}
|
||||
if(kind === 't3') {
|
||||
let s = await downloadAndSave(post.thumbnail, 'thumb_')
|
||||
let thumbnail = ''
|
||||
if(s !== 'self') {
|
||||
let img = `${protocol}://${config.domain}${s}`
|
||||
thumbnail = `<thumbnail>${img}</thumbnail>`
|
||||
}
|
||||
let append_desc_html = `<br/><a href="${permalink}">[comments]</a>`
|
||||
t3_elements = `
|
||||
<description><![CDATA[${unescape(post.selftext_html)}${append_desc_html}]]></description>
|
||||
${thumbnail}
|
||||
`
|
||||
}
|
||||
|
||||
let title = post.title
|
||||
if(!post.title)
|
||||
title = post.link_title
|
||||
|
||||
items += `
|
||||
<item>
|
||||
<title>${title}</title>
|
||||
<dc:creator>/u/${user}</dc:creator>
|
||||
<kind>${kind}</kind>
|
||||
<subreddit>${post.subreddit}</subreddit>
|
||||
<created>${post.created_utc}</created>
|
||||
<ups>${post.ups}</ups>
|
||||
<link>${permalink}</link>
|
||||
<edited>${post.edited}</edited>
|
||||
<num_comments>${post.num_comments}</num_comments>
|
||||
<over_18>${post.over_18}</over_18>
|
||||
${t1_elements}
|
||||
${t3_elements}
|
||||
</item>
|
||||
`
|
||||
}
|
||||
|
||||
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}</title>
|
||||
<link>${link}</link>
|
||||
${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 processJsonUser(json, true, null, null, req.cookies)
|
||||
return res.end(JSON.stringify(processed_json))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
routes.js
46
routes.js
|
@ -9,6 +9,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
let processSearches = require('./inc/processSearchResults.js')();
|
||||
let processAbout = require('./inc/processSubredditAbout.js')();
|
||||
let tedditApiSubreddit = require('./inc/teddit_api/handleSubreddit.js')();
|
||||
let tedditApiUser = require('./inc/teddit_api/handleUser.js')();
|
||||
|
||||
app.get('/about', (req, res, next) => {
|
||||
return res.render('about', { user_preferences: req.cookies })
|
||||
|
@ -714,6 +715,15 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
let after = req.query.after
|
||||
let before = req.query.before
|
||||
let user_data = {}
|
||||
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
|
||||
|
||||
if(!after) {
|
||||
after = ''
|
||||
}
|
||||
|
@ -763,13 +773,17 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
if(json) {
|
||||
console.log(`Got user ${user} key from redis.`);
|
||||
(async () => {
|
||||
let processed_json = await processJsonUser(json, false, after, before, req.cookies)
|
||||
return res.render('user', {
|
||||
data: processed_json,
|
||||
sortby: sortby,
|
||||
past: past,
|
||||
user_preferences: req.cookies
|
||||
})
|
||||
if(api_req) {
|
||||
return handleTedditApiUser(json, req, res, 'redis', api_type, api_target, user, after, before)
|
||||
} else {
|
||||
let processed_json = await processJsonUser(json, false, after, before, req.cookies)
|
||||
return res.render('user', {
|
||||
data: processed_json,
|
||||
sortby: sortby,
|
||||
past: past,
|
||||
user_preferences: req.cookies
|
||||
})
|
||||
}
|
||||
})()
|
||||
} else {
|
||||
let url = ''
|
||||
|
@ -800,13 +814,17 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
|||
return res.render('index', { post: null, user_preferences: req.cookies })
|
||||
} else {
|
||||
(async () => {
|
||||
let processed_json = await processJsonUser(user_data, true, after, before, req.cookies)
|
||||
return res.render('user', {
|
||||
data: processed_json,
|
||||
sortby: sortby,
|
||||
past: past,
|
||||
user_preferences: req.cookies
|
||||
})
|
||||
if(api_req) {
|
||||
return handleTedditApiUser(user_data, req, res, 'online', api_type, api_target, user, after, before)
|
||||
} else {
|
||||
let processed_json = await processJsonUser(user_data, true, after, before, req.cookies)
|
||||
return res.render('user', {
|
||||
data: processed_json,
|
||||
sortby: sortby,
|
||||
past: past,
|
||||
user_preferences: req.cookies
|
||||
})
|
||||
}
|
||||
})()
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue