Change Admin Verfify to a list of opinions

This commit is contained in:
jasinco 2025-06-09 00:14:51 +08:00
parent b98d050c7b
commit bc2fecc50d
2 changed files with 72 additions and 46 deletions

View file

@ -29,6 +29,7 @@ const POST = z.object({
const Panel = () => {
const [posts, setPosts] = useState<z.infer<typeof POST>[]>([])
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 (<div className="w-full h-dvh bg-stone-700 relative">
<div className="absolute top-0 right-0 m-3">
@ -73,9 +85,10 @@ const Panel = () => {
</NavigationMenuList>
</NavigationMenu>
</div>
<div className="h-full w-full flex flex-row px-10 pt-20">
<div className="h-full w-full flex flex-col md:flex-row px-10 pt-20">
<Button onClick={load} disabled={loading}>Load</Button>
<div className="grid grid-cols-3 border-3 border-stone-400 rounded w-[80dvw] text-white gap-3 overflow-scroll">
<Button onClick={Send}>Load</Button>
<div className="grid grid-cols-1 md:grid-cols-3 border-3 border-stone-400 rounded w-[80dvw] text-white gap-3 overflow-scroll">
{posts.length > 0 ? posts.map(post => {
return (<div key={post.id} className="border rounded h-fit">
<p>
@ -92,11 +105,9 @@ const Panel = () => {
<div className="flex flex-row gap-3 ml-2">
<Button onClick={() => verify(true, post.id)}><LucideCheck /></Button>
<Button onClick={() => verify(false, post.id)}><LucideX /></Button>
</div>
</div>)
}) : "NULL"}
</div>
</div>
</div>)

View file

@ -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"