-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bot approval queue + layout changes
- Loading branch information
Showing
15 changed files
with
309 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import moment from 'moment'; | ||
|
||
export function dateFormatEurope(date: Date) { | ||
return moment(date).format('DD/MM/YYYY'); | ||
return moment(date).format('DD/MM/YYYY, HH:mm'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
import SafeUser from '$lib/structures/user.js'; | ||
import SafeUser from '$lib/structures/user'; | ||
import type { User } from '@prisma/client'; | ||
import '$lib/server/discord/bot.js'; | ||
import '$lib/server/discord/bot'; | ||
import { RoleUtility, Roles } from '$lib/server/roles'; | ||
import prisma from '$lib/server/prisma'; | ||
|
||
/** @type {import('./$types').LayoutServerLoad} */ | ||
export async function load({ locals }) { | ||
const isApprover = RoleUtility.hasRole(locals?.actor?.role!, Roles.Approver); | ||
let botQueueCount = 0; | ||
|
||
if (isApprover) { | ||
botQueueCount = await prisma.bot.count({ | ||
where: { | ||
approval_status: 'PENDING', | ||
OR: [ | ||
{ | ||
approver_id: locals.actor?.id! | ||
}, | ||
{ | ||
approver_id: null | ||
} | ||
] | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
actor: locals?.actor ? SafeUser(locals.actor as User | null) : null | ||
actor: locals?.actor ? SafeUser(locals.actor as User | null) : null, | ||
isApprover, | ||
botQueueCount | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import requireActorRole from '$lib/server/middleware/requireActorRole'; | ||
import prisma from '$lib/server/prisma'; | ||
import { Roles } from '$lib/server/roles'; | ||
import { BotApprovalStatus } from '@prisma/client'; | ||
import { redirect } from '@sveltejs/kit'; | ||
|
||
export async function load({ locals }) { | ||
await requireActorRole(locals, Roles.Approver); | ||
|
||
const myPendingReview = await prisma.bot.findFirst({ | ||
where: { | ||
approval_status: BotApprovalStatus.PENDING, | ||
approver_id: locals.actor?.id! | ||
} | ||
}); | ||
|
||
if (myPendingReview) throw redirect(302, `/bots/${myPendingReview.id}`); // Approver should finish their review before reviewing another bot | ||
|
||
const pendingBots = await prisma.bot.findMany({ | ||
where: { | ||
approval_status: BotApprovalStatus.PENDING, | ||
approver_id: null | ||
}, | ||
take: 10, | ||
orderBy: { | ||
approval_request_time: 'asc' | ||
} | ||
}); | ||
|
||
const pendingBotsCount = await prisma.bot.count({ | ||
where: { | ||
approval_status: BotApprovalStatus.PENDING, | ||
approver_id: null | ||
} | ||
}); | ||
|
||
return { | ||
pendingBots, | ||
pendingBotsCount | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<script lang="ts"> | ||
import DataTable, { Head, Body, Row, Cell } from '@smui/data-table'; | ||
import Paper, { Title, Subtitle, Content } from '@smui/paper'; | ||
import LinearProgress from '@smui/linear-progress'; | ||
import Button from '@smui/button'; | ||
import axios from 'axios'; | ||
import { goto } from '$app/navigation'; | ||
export let data; | ||
let errorMessage: string = ''; | ||
let tableLoaded = true; | ||
function reviewBot(uid: string) { | ||
tableLoaded = false; | ||
axios | ||
.post(`/api/v1/bots/${uid}/claim`) | ||
.then((d) => { | ||
goto(`/bots/${uid}`); | ||
}) | ||
.catch((e) => { | ||
errorMessage = e?.response?.data?.message ?? 'Unable to send request.'; | ||
}) | ||
.finally(() => { | ||
tableLoaded = true; | ||
}); | ||
} | ||
</script> | ||
|
||
<div class="paper-container align-flex-center"> | ||
<Paper class="paper"> | ||
<Title>Approval Queue</Title> | ||
<Subtitle> | ||
{data?.pendingBotsCount ?? 0} bots in queue. You can only approve one bot at a time, and will be | ||
forced to finish the approval process before claiming another bot. | ||
</Subtitle> | ||
<Content> | ||
{#if errorMessage} | ||
<div class="mb-2"> | ||
<Paper color="primary" class="paper-theme--outline-danger" style="padding: 10px;"> | ||
<Content> | ||
{errorMessage} | ||
</Content> | ||
</Paper> | ||
</div> | ||
{/if} | ||
|
||
<DataTable table$aria-label="People list" style="max-width: 100%;"> | ||
<Head> | ||
<Row> | ||
<Cell>Avatar</Cell> | ||
<Cell>Name</Cell> | ||
<Cell>NSFW</Cell> | ||
<Cell>Action</Cell> | ||
</Row> | ||
</Head> | ||
<Body> | ||
{#each data?.pendingBots as bot} | ||
<Row> | ||
<Cell> | ||
<img src={bot?.avatar} alt="Avatar" class="bot-avatar" /> | ||
</Cell> | ||
<Cell>{bot?.username}</Cell> | ||
<Cell>{bot?.nsfw ? 'Contains NSFW' : 'Not NSFW'}</Cell> | ||
<Cell> | ||
<Button | ||
variant="outlined" | ||
disabled={!tableLoaded} | ||
on:click={() => { | ||
reviewBot(bot?.id); | ||
}} | ||
> | ||
Review | ||
</Button> | ||
</Cell> | ||
</Row> | ||
{/each} | ||
<!-- no data --> | ||
{#if data?.pendingBots.length === 0} | ||
<Row> | ||
<Cell colspan={3}>No bots in queue.</Cell> | ||
</Row> | ||
{/if} | ||
</Body> | ||
|
||
<LinearProgress | ||
indeterminate | ||
bind:closed={tableLoaded} | ||
aria-label="Data is being loaded..." | ||
slot="progress" | ||
/> | ||
</DataTable> | ||
</Content> | ||
</Paper> | ||
</div> | ||
|
||
<style> | ||
.paper-container { | ||
padding: 24px; | ||
} | ||
.bot-avatar { | ||
width: 30px; | ||
height: 30px; | ||
border-radius: 10px; | ||
} | ||
.mb-2 { | ||
margin-bottom: 16px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import requireActorRole from '$lib/server/middleware/requireActorRole'; | ||
import prisma from '$lib/server/prisma'; | ||
import { Roles } from '$lib/server/roles'; | ||
import snowflake from '$lib/server/snowflake'; | ||
import { BotApprovalStatus } from '@prisma/client'; | ||
import { json } from '@sveltejs/kit'; | ||
|
||
/** @type {import('@sveltejs/kit').RequestHandler} */ | ||
export async function POST({ locals, params }) { | ||
await requireActorRole(locals, Roles.Approver); | ||
|
||
const bot = await prisma.bot.findUnique({ | ||
where: { | ||
id: params.bot_id | ||
} | ||
}); | ||
|
||
if (!bot) return json({ message: 'Bot not found' }, { status: 404 }); | ||
|
||
if (bot.approval_status !== BotApprovalStatus.PENDING) | ||
return json({ message: 'Bot is not pending approval' }, { status: 400 }); | ||
|
||
if (bot.approver_id !== null) return json({ message: 'Bot is already claimed' }, { status: 400 }); | ||
|
||
await prisma.bot.update({ | ||
where: { | ||
id: bot.id | ||
}, | ||
data: { | ||
approver_id: locals.actor!.id!, | ||
approval_reason: null, | ||
approved_time: null | ||
} | ||
}); | ||
|
||
await prisma.botApprovalLog.create({ | ||
data: { | ||
id: snowflake.getUniqueID()?.toString(), | ||
bot_id: bot.id, | ||
op_id: locals.actor!.id!, | ||
status: BotApprovalStatus.PENDING, | ||
reason: 'Bot claimed by approver' | ||
} | ||
}); | ||
|
||
return json({ message: 'Claimed bot' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.