Merge branch 'api-v1' into jwt

This commit is contained in:
May 2024-05-24 15:49:12 -07:00
commit 2f32644b3d
3 changed files with 16 additions and 15 deletions

View file

@ -57,14 +57,6 @@ if (config.forceSSL) {
})
}
// funcs
// init data
const __dirname = dirname(fileURLToPath(import.meta.url))
if (!fs.existsSync(__dirname + "/../.data/"))
fs.mkdirSync(__dirname + "/../.data/")
// discord
let files = new Files(config)

View file

@ -1,4 +1,5 @@
import { readFile, writeFile, rename, readdir } from "fs/promises"
import { readFile, writeFile, readdir, mkdir } from "fs/promises"
import { existsSync } from "fs"
import path from "node:path"
const DATADIR = `./.data`
@ -92,6 +93,11 @@ export default class DbFile<Structure extends ({}|[])> {
}
private async findAvailable() {
// would it be worth it to remove existsSync here?
// mkdir seems to already do it itself when recursive is true
if (!existsSync(DATADIR))
await mkdir(DATADIR, { recursive: true })
return (await readdir(DATADIR))
.filter(e => e.match(new RegExp(`^${this.name}(?:-b)?.json$`)))
.map(e => path.join(DATADIR, e))

View file

@ -17,6 +17,14 @@ export const getAccount: RequestHandler = async function (ctx, next) {
return next()
}
export function resolveTarget(actor: Accounts.Account, targetString: string) {
return targetString == "me"
? actor
: targetString.startsWith("@")
? Accounts.getFromUsername(targetString.slice(1))
: Accounts.getFromId(targetString)
}
/**
* @description use :user param to get a target for this route
*/
@ -29,12 +37,7 @@ export const getTarget: RequestHandler = async (ctx, next) => {
let actor = ctx.get("account")
let target =
ctx.req.param("user") == "me"
? actor
: ctx.req.param("user").startsWith("@")
? Accounts.getFromUsername(ctx.req.param("user").slice(1))
: Accounts.getFromId(ctx.req.param("user"))
let target = resolveTarget(actor, ctx.req.param("user"))
if (!target) return ServeError(ctx, 404, "account does not exist")