2021-01-12 10:49:00 +00:00
|
|
|
const {request} = require("../utils/request")
|
2020-08-24 10:43:55 +00:00
|
|
|
const {render} = require("pinski/plugins")
|
2020-10-18 08:51:09 +00:00
|
|
|
const {getUser} = require("../utils/getuser")
|
2021-01-01 04:30:47 +00:00
|
|
|
const converters = require("../utils/converters")
|
2020-08-24 10:43:55 +00:00
|
|
|
|
|
|
|
module.exports = [
|
|
|
|
{
|
2020-10-18 08:51:09 +00:00
|
|
|
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())
|
2021-01-12 10:49:00 +00:00
|
|
|
const error = results.error || results.message || results.code
|
|
|
|
|
|
|
|
if (error) throw new Error(`Instance said: ${error}`)
|
2021-01-01 04:30:47 +00:00
|
|
|
|
|
|
|
for (const video of results) {
|
2021-01-12 01:53:30 +00:00
|
|
|
converters.normaliseVideoInfo(video)
|
2021-01-01 04:30:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 12:29:44 +00:00
|
|
|
const filters = user.getFilters()
|
|
|
|
results = converters.applyVideoFilters(results, filters).videos
|
|
|
|
|
2021-12-28 03:32:11 +00:00
|
|
|
return render(200, "pug/search.pug", {settings, url, query, results, instanceOrigin})
|
2020-08-24 10:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|