This commit is contained in:
728
2024-12-25 14:06:16 +08:00
parent aa412b78cd
commit d1b21fbd30
9 changed files with 67 additions and 44 deletions

View File

@ -1,5 +1,4 @@
import type { Agd, Id, Pub, Soc, Spc, Usr } from "../db/typ.ts"
import type { Has } from "../ser/nav.ts"
import { usr_r } from "../db/usr.ts"
import { pas_of_usr, type Pas } from "./pas.ts"
import { pub_usr } from "./pub.ts"
@ -40,6 +39,15 @@ export type Doc = {
},
}
export type Has = {
usr?: Usr["_id"],
soc?: Soc["_id"],
agd?: Agd["_id"],
spc?: Spc["_id"],
rec?: "cdt" | "dbt" | "ern" | "act",
}
async function usr(
pas: Pas | null,
_id: Usr["_id"],

7
src/frm/frm.ts Normal file
View File

@ -0,0 +1,7 @@
import { Usr } from "../db/typ.ts"
export type Frm = {
frm: "psg",
nbr?: Required<Usr>["nbr"],
pas?: Required<Usr>["pas"],
}

2
src/frm/psg.tsx Normal file
View File

@ -0,0 +1,2 @@
function Psg(
)

10
src/nav/fet.ts Normal file
View File

@ -0,0 +1,10 @@
import type { Nav } from "../ser./nav/nav.ts"
export async function pos<T>(
h: Nav,
) {
const res = await fetch(`/p`, { method: "POST", body: JSON.stringify(h) })
const r = await res.json() as T
if (r && !("psg" in h)) await navpas()
return r
}

View File

@ -1,26 +1,18 @@
import { Agd, Soc, Spc, Usr } from "../db/typ.ts"
import { Psg } from "../que/psg.ts"
import type { Has } from "../doc/doc.ts"
import type { Pas } from "../doc/pas.ts"
import { Frm } from "../frm/frm.ts"
export type Has = {
usr?: Usr["_id"],
soc?: Soc["_id"],
agd?: Agd["_id"],
spc?: Spc["_id"],
rec?: "cdt" | "dbt" | "ern" | "act",
que?: Que,
export type Nav = {
has: Has,
frm?: Frm,
pas?: Pas,
}
export type Que = Psg | {
que: "sig",
sig: string,
} | {
que: "act",
act: "cdt" | "ern",
}
export const nav: Nav = { has: {} }
export function has(
export function hasque(
s: string,
): Has {
) {
const h: Has = {}
const [p, q] = decodeURI(s).substring(1).split("?")
if (p.startsWith("t")) h.spc = p
@ -31,7 +23,9 @@ export function has(
else if (h.spc == undefined && p.startsWith("#")) h.spc = p.substring(1)
else if ((h.usr || h.soc) && ["cdt", "dbt", "ern", "act"].includes(p)) h.rec = p as Has["rec"]
})
if (!q || q.length == 0) return h
else if (q.startsWith("s=")) h.que = { que: "sig", sig: q.substring(2) }
return h
nav.has = h
delete nav.frm
if (!q || q.length == 0) return nav
else if (q == "psg") nav.frm = { frm: "psg" }
return nav
}

View File

@ -1,5 +1,5 @@
import { utc_dt } from "../uti/utc.ts"
import { Has } from "./nav.ts"
import { Nav } from "../nav/nav.ts"
function log(
utc: number,
@ -31,9 +31,9 @@ async function ser(
} case "q": {
const _c = req.headers.get("cookie")?.split(";").find(c => c.startsWith("q="))
try {
const h: Has = await req.json()
log(utc, `${r}?${h.que ? h.que : ""}`, 200)
return new Response(JSON.stringify(h), { status: 200 })
const nav: Nav = await req.json()
log(utc, `${r}?${JSON.stringify(nav.has)}`, 200)
return new Response(JSON.stringify(nav), { status: 200 })
} catch {
log(utc, `${r}`, 400)
return new Response(null, { status: 400 })

View File

@ -1,8 +1,8 @@
import { db } from "../src/db/db.ts"
import { usr_r } from "../src/db/usr.ts"
import { has } from "../src/ser/nav.ts"
import { doc } from "../src/doc/doc.ts"
import { pas_aut, pas_of_usr, pas_soc, pas_usr } from "../src/doc/pas.ts"
import { hasque } from "../src/nav/nav.ts";
import { fil } from "./fil.ts"
import { assertEquals } from "@std/assert"
@ -22,17 +22,6 @@ Deno.test("pas", async () => {
], [false, false, false])
})
Deno.test("nav", () => {
assertEquals(has(""), {})
assertEquals(has("#2"), { usr: 2 })
assertEquals(has("#s1-2-a3"), { soc: 1, usr: 2, agd: 3 })
assertEquals(has("#a3-s1-2"), { soc: 1, usr: 2, agd: 3 })
assertEquals(has("#s1-2-ern"), { soc: 1, usr: 2, rec: "ern" })
assertEquals(has("##中文123"), { spc: "中文123" })
assertEquals(has(encodeURI("##中文123")), { spc: "中文123" })
assertEquals(has("##中文123?s=123abc"), { spc: "中文123", que: { que: "sig", sig: "123abc" } })
})
Deno.test("pas", async () => {
const [u, s] = [fil.usr[2], fil.soc[0]]
assertEquals(
@ -44,12 +33,12 @@ Deno.test("pas", async () => {
Deno.test("doc", async () => {
const [u, s] = [fil.usr[2], fil.soc[0]]
assertEquals(
await doc(null, has(`#s${s._id}-${u._id}`)),
await doc(null, hasque(`#s${s._id}-${u._id}`).has),
{ usr: { _id: u._id } },
)
const p = await pas_of_usr({ _id: u._id, nam: u.nam })
assertEquals(
(await doc(p, has(`#s${s._id}-${u._id}`)))?.usr?.soc,
(await doc(p, hasque(`#s${s._id}-${u._id}`).has))?.usr?.soc,
[{ soc: s._id, nam: s.nam, sec: true }],
)
})

13
tst/nav.test.ts Normal file
View File

@ -0,0 +1,13 @@
import { assertEquals } from "@std/assert/equals"
import { hasque } from "../src/nav/nav.ts"
Deno.test("nav", () => {
assertEquals(hasque(""), { has: {} })
assertEquals(hasque("#2"), { has: { usr: 2 } })
assertEquals(hasque("#s1-2-a3"), { has: { soc: 1, usr: 2, agd: 3 } })
assertEquals(hasque("#a3-s1-2"), { has: { soc: 1, usr: 2, agd: 3 } })
assertEquals(hasque("#s1-2-ern"), { has: { soc: 1, usr: 2, rec: "ern" } })
assertEquals(hasque("##中文123"), { has: { spc: "中文123" } })
assertEquals(hasque(encodeURI("##中文123")), { has: { spc: "中文123" } })
assertEquals(hasque("##中文123?psg"), { has: { spc: "中文123" }, frm: { frm: "psg" } })
})

View File

@ -1,16 +1,16 @@
import { db } from "../src/db/db.ts"
import { has } from "../src/ser/nav.ts"
import { hasque } from "../src/nav/nav.ts"
import { assertEquals } from "@std/assert"
await db("tst", true, true)
Deno.test("ser", async () => {
const h = has("##中文123?s=123abc")
const n = hasque("##中文123?s=123abc")
const res = await fetch("http://localhost:728/p", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(h),
body: JSON.stringify(n),
})
assertEquals(await res.json(), h)
assertEquals(await res.json(), n)
})