1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-22 07:37:29 +00:00
cloudtube/api/search.js

33 lines
1.1 KiB
JavaScript
Raw Normal View History

const {request} = require("../utils/request")
2020-08-24 10:43:55 +00:00
const {render} = require("pinski/plugins")
const {getUser} = require("../utils/getuser")
const converters = require("../utils/converters")
2020-08-24 10:43:55 +00:00
module.exports = [
{
route: "/(?:search|results)", methods: ["GET"], code: async ({req, url}) => {
2020-08-24 10:43:55 +00:00
const query = url.searchParams.get("q") || url.searchParams.get("search_query")
2021-05-11 12:29:44 +00:00
const user = getUser(req)
const settings = user.getSettingsOrDefaults()
const instanceOrigin = settings.instance
2021-01-01 04:15:02 +00:00
const fetchURL = new URL(`${instanceOrigin}/api/v1/search`)
2020-08-24 10:43:55 +00:00
fetchURL.searchParams.set("q", query)
2021-05-11 12:29:44 +00:00
let results = await request(fetchURL.toString()).then(res => res.json())
const error = results.error || results.message || results.code
if (error) throw new Error(`Instance said: ${error}`)
for (const video of results) {
converters.normaliseVideoInfo(video)
}
2021-05-11 12:29:44 +00:00
const filters = user.getFilters()
results = converters.applyVideoFilters(results, filters).videos
return render(200, "pug/search.pug", {req, settings, url, query, results, instanceOrigin})
2020-08-24 10:43:55 +00:00
}
}
]