From bc2fecc50d195c0f228cde1d7cd25cce1f6a1566 Mon Sep 17 00:00:00 2001 From: jasinco Date: Mon, 9 Jun 2025 00:14:51 +0800 Subject: [PATCH] Change Admin Verfify to a list of opinions --- admin_panel/src/Panel.tsx | 41 ++++++++++++-------- internal/handlers/admin.go | 77 +++++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 46 deletions(-) diff --git a/admin_panel/src/Panel.tsx b/admin_panel/src/Panel.tsx index 90a448d..2e8670a 100644 --- a/admin_panel/src/Panel.tsx +++ b/admin_panel/src/Panel.tsx @@ -29,6 +29,7 @@ const POST = z.object({ const Panel = () => { const [posts, setPosts] = useState[]>([]) const [loading, setLoading] = useState(false) + const [verifyli, setVerifyli] = useState<{ id: number, ck: Boolean }[]>([]) const load = () => { setLoading(true) fetch(rootstr + "/api/admin/fetch_post", { credentials: "include" }).then(async val => { @@ -41,19 +42,30 @@ const Panel = () => { } const verify = (check: boolean, post: number) => { - fetch(rootstr + "/api/admin/verify_post", { method: "PUT", credentials: "include", body: JSON.stringify({ "post": post, "check": check, }), headers: { "Content-Type": "application/json" } }).then(async e => { - if (e.status == 200) { - setPosts(posts.filter(v => v.id != post)) - } else - if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") { - window.location.href = "/admin/login" - } else { - alert(e.statusText) - } - }) - + setVerifyli([...verifyli, { ck: check, id: post }]) + setPosts(posts.filter(v => v.id != post)) } + const Send = () => { + fetch(rootstr + "/api/admin/verify_post", + { + method: "put", + credentials: "include", + body: JSON.stringify({ "choice": verifyli }), + headers: { "content-type": "application/json" } + } + ) + .then(async e => { + if (e.status == 200) { + alert("成功") + } else + if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") { + window.location.href = "/admin/login" + } else { + alert(e.statusText) + } + }) + } return (
@@ -73,9 +85,10 @@ const Panel = () => {
-
+
-
+ +
{posts.length > 0 ? posts.map(post => { return (

@@ -92,11 +105,9 @@ const Panel = () => {

-
) }) : "NULL"} -
) diff --git a/internal/handlers/admin.go b/internal/handlers/admin.go index 70308ba..a9f5880 100644 --- a/internal/handlers/admin.go +++ b/internal/handlers/admin.go @@ -160,6 +160,9 @@ type Verify struct { Id int32 `json:"post"` Check bool `json:"check"` } +type VerifyList struct { + Choice []Verify `json:"choice"` +} func AdminVerify(c *fiber.Ctx) error { user := c.Locals("user").(*jwt.Token) @@ -172,49 +175,61 @@ func AdminVerify(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusInternalServerError) } qtx := internal.NIMDB.WithTx(tx) - post_verify := new(Verify) + post_verify_list := new(VerifyList) log.Println(string(c.Body())) - if err = c.BodyParser(post_verify); err != nil { + if err = c.BodyParser(post_verify_list); err != nil { return c.SendStatus(fiber.StatusBadRequest) } if !super { - if post_verify.Check { - _, err = qtx.AdminVerify(ctx, nimdb.AdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseOk}) + for _, post_verify := range post_verify_list.Choice { - } else { - _, err = qtx.AdminVerify(ctx, nimdb.AdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseRejected}) - } - if err != nil { - log.Println(err.Error()) - return c.SendStatus(fiber.StatusBadRequest) + if post_verify.Check { + _, err = qtx.AdminVerify(ctx, nimdb.AdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseOk}) + + } else { + _, err = qtx.AdminVerify(ctx, nimdb.AdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseRejected}) + } + if err != nil { + log.Println(err.Error()) + return c.SendStatus(fiber.StatusBadRequest) + } + + err = qtx.AdminUpdateImage(ctx, pgtype.Int4{Int32: post_verify.Id, Valid: true}) + if err != nil { + return c.SendStatus(fiber.StatusInternalServerError) + + } + + // Verified and send to IGAPI to handle synchronization of IG posts + if internal.IGAPI_ACTIVATE { + internal.IGAPI_CHAN <- internal.PR{ACT_TYPE: 0, ID: post_verify.Id} + } } } else { - if post_verify.Check { - _, err = qtx.SuperAdminVerify(ctx, nimdb.SuperAdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseOk}) + for _, post_verify := range post_verify_list.Choice { + if post_verify.Check { + _, err = qtx.SuperAdminVerify(ctx, nimdb.SuperAdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseOk}) - } else { - _, err = qtx.SuperAdminVerify(ctx, nimdb.SuperAdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseAdminRejected}) + } else { + _, err = qtx.SuperAdminVerify(ctx, nimdb.SuperAdminVerifyParams{ID: post_verify.Id, Phase: nimdb.PostPhaseAdminRejected}) + } + if err != nil { + log.Println(err.Error()) + return c.SendStatus(fiber.StatusBadRequest) + } + err = qtx.AdminUpdateImage(ctx, pgtype.Int4{Int32: post_verify.Id, Valid: true}) + if err != nil { + return c.SendStatus(fiber.StatusInternalServerError) + } + + // Verified and send to IGAPI to handle synchronization of IG posts + if internal.IGAPI_ACTIVATE { + internal.IGAPI_CHAN <- internal.PR{ACT_TYPE: 0, ID: post_verify.Id} + } } - if err != nil { - log.Println(err.Error()) - return c.SendStatus(fiber.StatusBadRequest) - } - } - - err = qtx.AdminUpdateImage(ctx, pgtype.Int4{Int32: post_verify.Id, Valid: true}) - if err != nil { - return c.SendStatus(fiber.StatusInternalServerError) - } - - // Verified and send to IGAPI to handle synchronization of IG posts - if internal.IGAPI_ACTIVATE { - internal.IGAPI_CHAN <- internal.PR{ACT_TYPE: 0, ID: post_verify.Id} - } - return tx.Commit(ctx) - } const adminpage_basepath = "./admin_panel/dist"