add strike system (#18)
All checks were successful
delpoy / build-and-deploy (push) Successful in 1m25s
All checks were successful
delpoy / build-and-deploy (push) Successful in 1m25s
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import { getSession } from '$lib/server/session';
|
||||
import { Permissions } from '$lib/permissions';
|
||||
import { Admin, Report, User } from '$lib/server/database';
|
||||
import { Admin, Report, Strike, StrikePunishment, StrikeReason, User } from '$lib/server/database';
|
||||
import type { Attributes } from 'sequelize';
|
||||
import { Op } from 'sequelize';
|
||||
import { env } from '$env/dynamic/private';
|
||||
@@ -55,7 +55,8 @@ export const POST = (async ({ request, cookies }) => {
|
||||
include: [
|
||||
{ model: User, as: 'reporter' },
|
||||
{ model: User, as: 'reported' },
|
||||
{ model: Admin, as: 'auditor' }
|
||||
{ model: Admin, as: 'auditor' },
|
||||
{ model: StrikeReason, as: 'strike_reason' }
|
||||
],
|
||||
order: ['created_at'],
|
||||
offset: data.from || 0,
|
||||
@@ -77,6 +78,9 @@ export const POST = (async ({ request, cookies }) => {
|
||||
} else if (r.auditor) {
|
||||
delete r.dataValues.auditor.password;
|
||||
}
|
||||
if (!r.strike_reason) {
|
||||
r.strike_reason_id = -1;
|
||||
}
|
||||
return r;
|
||||
});
|
||||
|
||||
@@ -97,6 +101,7 @@ export const PATCH = (async ({ request, cookies }) => {
|
||||
notice: string | null;
|
||||
statement: string | null;
|
||||
status: 'none' | 'review' | 'reviewed' | null;
|
||||
strike_reason: number | null;
|
||||
} = await request.json();
|
||||
|
||||
if (data.id === null || data.auditor === null) return new Response(null, { status: 400 });
|
||||
@@ -113,6 +118,74 @@ export const PATCH = (async ({ request, cookies }) => {
|
||||
if (data.notice != null) report.notice = data.notice;
|
||||
if (data.statement != null) report.statement = data.statement;
|
||||
if (data.status != null) report.status = data.status;
|
||||
if (data.strike_reason != null) {
|
||||
if (data.status !== 'reviewed') {
|
||||
if (data.strike_reason == -1) {
|
||||
report.strike_reason_id = null;
|
||||
} else {
|
||||
const strike_reason = await StrikeReason.findByPk(data.strike_reason);
|
||||
if (strike_reason == null) return new Response(null, { status: 400 });
|
||||
report.strike_reason_id = strike_reason.id;
|
||||
}
|
||||
} else if (data.strike_reason == -1) {
|
||||
const strike = await Strike.findOne({ where: { user_id: report.reported_id } });
|
||||
if (strike != null && report.strike_reason_id != null) {
|
||||
const strike_weight = (await StrikeReason.findByPk(report.strike_reason_id, {
|
||||
attributes: ['weight']
|
||||
}))!.weight;
|
||||
const current_ban_in_seconds = (
|
||||
await StrikePunishment.findOne({
|
||||
attributes: ['ban_in_seconds'],
|
||||
where: { weight: { [Op.lte]: strike.weight } }
|
||||
})
|
||||
)?.ban_in_seconds;
|
||||
if (current_ban_in_seconds) {
|
||||
const new_ban_in_seconds = (
|
||||
await StrikePunishment.findOne({
|
||||
attributes: ['ban_in_seconds'],
|
||||
where: { weight: { [Op.lte]: strike.weight - strike_weight } }
|
||||
})
|
||||
)?.ban_in_seconds;
|
||||
const time_left =
|
||||
strike.ban_until.getTime() / 1000 - current_ban_in_seconds + (new_ban_in_seconds || 0);
|
||||
strike.ban_until = new Date(time_left * 1000);
|
||||
}
|
||||
strike.weight -= strike_weight;
|
||||
await strike.save();
|
||||
report.strike_reason_id = null;
|
||||
}
|
||||
} else if (data.strike_reason != report.strike_reason_id) {
|
||||
if (!report.reported_id) return new Response(null, { status: 400 });
|
||||
const strike_reason = await StrikeReason.findByPk(data.strike_reason);
|
||||
if (strike_reason == null) return new Response(null, { status: 400 });
|
||||
const [strike] = await Strike.findOrCreate({ where: { user_id: report.reported_id } });
|
||||
const old_punishment = (
|
||||
await StrikePunishment.findOne({
|
||||
attributes: ['ban_in_seconds'],
|
||||
where: { weight: { [Op.lte]: strike.weight } }
|
||||
})
|
||||
)?.ban_in_seconds;
|
||||
if (report.strike_reason_id) {
|
||||
strike.weight -=
|
||||
(await StrikeReason.findByPk(report.strike_reason_id, { attributes: ['weight'] }))
|
||||
?.weight || 0;
|
||||
}
|
||||
strike.weight += (await StrikeReason.findByPk(strike_reason.id, {
|
||||
attributes: ['weight']
|
||||
}))!.weight;
|
||||
const new_punishment = (
|
||||
await StrikePunishment.findOne({
|
||||
attributes: ['ban_in_seconds'],
|
||||
where: { weight: { [Op.lte]: strike.weight } }
|
||||
})
|
||||
)?.ban_in_seconds;
|
||||
if (old_punishment != new_punishment && new_punishment != null) {
|
||||
strike.ban_until = new Date(Date.now() + new_punishment / 1000);
|
||||
}
|
||||
await strike.save();
|
||||
report.strike_reason_id = strike_reason.id;
|
||||
}
|
||||
}
|
||||
if (admin != null) report.auditor_id = admin.id;
|
||||
|
||||
await report.save();
|
||||
|
||||
Reference in New Issue
Block a user