update report admin endpoint
All checks were successful
delpoy / build-and-deploy (push) Successful in 56s

This commit is contained in:
bytedream 2024-12-24 01:17:36 +01:00
parent 7357ad9e88
commit 1596fb605e
2 changed files with 52 additions and 57 deletions

View File

@ -24,16 +24,16 @@ export class User extends Model {
@Column({ type: DataTypes.DATE, allowNull: false }) @Column({ type: DataTypes.DATE, allowNull: false })
declare birthday: Date; declare birthday: Date;
@Column({ type: DataTypes.STRING }) @Column({ type: DataTypes.STRING })
declare telephone: string; declare telephone: string | null;
@Column({ type: DataTypes.STRING, allowNull: false }) @Column({ type: DataTypes.STRING, allowNull: false })
declare username: string; declare username: string;
@Column({ type: DataTypes.ENUM('java', 'bedrock', 'noauth'), allowNull: false }) @Column({ type: DataTypes.ENUM('java', 'bedrock', 'noauth'), allowNull: false })
declare playertype: 'java' | 'bedrock' | 'noauth'; declare playertype: 'java' | 'bedrock' | 'noauth';
@Column({ type: DataTypes.STRING }) @Column({ type: DataTypes.STRING })
declare password: string; declare password: string | null;
@Column({ type: DataTypes.UUID, unique: true }) @Column({ type: DataTypes.UUID, unique: true })
@Index @Index
declare uuid: string; declare uuid: string | null;
} }
@Table({ modelName: 'report', underscored: true }) @Table({ modelName: 'report', underscored: true })
@ -44,15 +44,15 @@ export class Report extends Model {
@Column({ type: DataTypes.STRING, allowNull: false }) @Column({ type: DataTypes.STRING, allowNull: false })
declare subject: string; declare subject: string;
@Column({ type: DataTypes.STRING }) @Column({ type: DataTypes.STRING })
declare body: string; declare body: string | null;
@Column({ type: DataTypes.BOOLEAN, allowNull: false }) @Column({ type: DataTypes.BOOLEAN, allowNull: false })
declare draft: boolean; declare draft: boolean;
@Column({ type: DataTypes.ENUM('none', 'review', 'reviewed'), allowNull: false }) @Column({ type: DataTypes.ENUM('none', 'review', 'reviewed'), allowNull: false })
declare status: 'none' | 'review' | 'reviewed'; declare status: 'none' | 'review' | 'reviewed';
@Column({ type: DataTypes.STRING }) @Column({ type: DataTypes.STRING })
declare notice: string; declare notice: string | null;
@Column({ type: DataTypes.STRING }) @Column({ type: DataTypes.STRING })
declare statement: string; declare statement: string | null;
@Column({ type: DataTypes.DATE }) @Column({ type: DataTypes.DATE })
declare striked_at: Date | null; declare striked_at: Date | null;
@Column({ type: DataTypes.INTEGER, allowNull: false }) @Column({ type: DataTypes.INTEGER, allowNull: false })
@ -60,10 +60,10 @@ export class Report extends Model {
declare reporter_id: number; declare reporter_id: number;
@Column({ type: DataTypes.INTEGER }) @Column({ type: DataTypes.INTEGER })
@ForeignKey(() => User) @ForeignKey(() => User)
declare reported_id: number; declare reported_id: number | null;
@Column({ type: DataTypes.INTEGER }) @Column({ type: DataTypes.INTEGER })
@ForeignKey(() => Admin) @ForeignKey(() => Admin)
declare auditor_id: number; declare auditor_id: number | null;
@Column({ type: DataTypes.INTEGER }) @Column({ type: DataTypes.INTEGER })
@ForeignKey(() => StrikeReason) @ForeignKey(() => StrikeReason)
declare strike_reason_id: number | null; declare strike_reason_id: number | null;
@ -72,22 +72,22 @@ export class Report extends Model {
onDelete: 'CASCADE', onDelete: 'CASCADE',
foreignKey: 'reporter_id' foreignKey: 'reporter_id'
}) })
declare reporter: User; declare reporter: User | null;
@BelongsTo(() => User, { @BelongsTo(() => User, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
foreignKey: 'reported_id' foreignKey: 'reported_id'
}) })
declare reported: User; declare reported: User | null;
@BelongsTo(() => Admin, { @BelongsTo(() => Admin, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
foreignKey: 'auditor_id' foreignKey: 'auditor_id'
}) })
declare auditor: Admin; declare auditor: Admin | null;
@BelongsTo(() => StrikeReason, { @BelongsTo(() => StrikeReason, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
foreignKey: 'strike_reason_id' foreignKey: 'strike_reason_id'
}) })
declare strike_reason: StrikeReason; declare strike_reason: StrikeReason | null;
} }
@Table({ modelName: 'strike_reason', underscored: true, createdAt: false, updatedAt: false }) @Table({ modelName: 'strike_reason', underscored: true, createdAt: false, updatedAt: false })
@ -113,19 +113,19 @@ export class Feedback extends Model {
@Column({ type: DataTypes.STRING, allowNull: false }) @Column({ type: DataTypes.STRING, allowNull: false })
declare event: string; declare event: string;
@Column({ type: DataTypes.STRING }) @Column({ type: DataTypes.STRING })
declare content: string; declare content: string | null;
@Column({ type: DataTypes.STRING, allowNull: false, unique: true }) @Column({ type: DataTypes.STRING, allowNull: false, unique: true })
@Index @Index
declare url_hash: string; declare url_hash: string;
@Column({ type: DataTypes.INTEGER }) @Column({ type: DataTypes.INTEGER })
@ForeignKey(() => User) @ForeignKey(() => User)
declare user_id: number; declare user_id: number | null;
@BelongsTo(() => User, { @BelongsTo(() => User, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
foreignKey: 'user_id' foreignKey: 'user_id'
}) })
declare user: User; declare user: User | null;
} }
@Table({ modelName: 'admin', underscored: true }) @Table({ modelName: 'admin', underscored: true })

View File

@ -97,65 +97,60 @@ export const PATCH = (async ({ request, cookies }) => {
} }
const data = parseResult.data; const data = parseResult.data;
const report = await Report.findOne({ where: { id: data.id } }); const report = await Report.findOne({ where: { id: data.id }, include: [{ all: true }] });
const admin = await Admin.findOne({ where: { id: data.auditor } }); const admin = await Admin.findOne({ where: { id: data.auditor } });
const reported = data.reported const reported = data.reported ? await User.findOne({ where: { uuid: data.reported } }) : null;
? await User.findOne({ where: { uuid: data.reported } }) if (report === null || (admin === null && data.auditor != -1))
: undefined;
if (report === null || (admin === null && data.auditor != -1) || reported === null)
return new Response(null, { status: 400 }); return new Response(null, { status: 400 });
const webhookTriggerUsers: string[] = []; const webhookTriggerUsers: string[] = [];
if (report.reported_id != reported?.id) {
const oldReportUser = await User.findByPk(report.reported_id); // check if strike reason has changed and return 400 if it doesn't exist
if (oldReportUser) webhookTriggerUsers.push(oldReportUser.uuid); if (
if (reported) webhookTriggerUsers.push(reported.uuid); (report.strike_reason?.id ?? -1) != data.strike_reason &&
} else if ( data.strike_reason != null &&
reported && data.strike_reason != -1
report.reported_id != null &&
report.strike_reason_id != data.strike_reason
) { ) {
webhookTriggerUsers.push(reported.uuid); const strike_reason = await StrikeReason.findByPk(data.strike_reason);
if (strike_reason == null) return new Response(null, { status: 400 });
} }
report.reported_id = reported?.id ?? null; if (data.status === 'reviewed') {
if (data.strike_reason != null) { // trigger webhook if status changed to reviewed
if (data.status !== 'reviewed') { if (report.status !== 'reviewed' && data.strike_reason != -1 && reported) {
if (data.strike_reason == -1) { webhookTriggerUsers.push(reported.uuid!);
report.strike_reason_id = null; }
} else { // trigger webhook if strike reason has changed
const strike_reason = await StrikeReason.findByPk(data.strike_reason); else if (
if (strike_reason == null) return new Response(null, { status: 400 }); (report.strike_reason?.id ?? -1) != data.strike_reason &&
report.strike_reason_id = strike_reason.id; report.reported &&
} reported
} else if (data.strike_reason == -1 && report.strike_reason_id != null) {
report.strike_reason_id = null;
report.striked_at = null;
} else if (
data.strike_reason != -1 &&
(data.strike_reason != report.strike_reason_id || data.status != report.status)
) { ) {
if (!report.reported_id) return new Response(null, { status: 400 }); webhookTriggerUsers.push(reported.uuid!);
const strike_reason = await StrikeReason.findByPk(data.strike_reason); }
if (strike_reason == null) return new Response(null, { status: 400 }); } else if (report.status === 'reviewed') {
report.strike_reason_id = strike_reason.id; // trigger webhook if report status is reviewed and reported user has changed
report.striked_at = new Date(Date.now()); if (report.strike_reason != null && report.reported) {
webhookTriggerUsers.push(report.reported.uuid!);
} }
} }
if (data.notice != null) report.notice = data.notice; if (data.notice != null) report.notice = data.notice;
if (data.statement != null) report.statement = data.statement; if (data.statement != null) report.statement = data.statement;
if (data.status != null) report.status = data.status; if (data.status != null) report.status = data.status;
if (data.strike_reason != null)
report.strike_reason_id = data.strike_reason == -1 ? null : data.strike_reason;
if (data.strike_reason != null)
report.striked_at = data.strike_reason == -1 ? null : new Date(Date.now());
if (admin != null) report.auditor_id = admin.id; if (admin != null) report.auditor_id = admin.id;
await report.save(); await report.save();
if (webhookTriggerUsers.length > 0 && data.status == 'reviewed' && env.REPORTED_WEBHOOK) { for (const webhookTriggerUser of webhookTriggerUsers) {
for (const webhookTriggerUser of webhookTriggerUsers) { // no `await` to avoid blocking
// no `await` to avoid blocking webhookUserReported(env.REPORTED_WEBHOOK, webhookTriggerUser).catch((e) =>
webhookUserReported(env.REPORTED_WEBHOOK, webhookTriggerUser).catch((e) => console.error(`failed to send reported webhook: ${e}`)
console.error(`failed to send reported webhook: ${e}`) );
);
}
} }
return new Response(); return new Response();