chore (backend): add prelude/unsafe-cast for unsafe type casting
This commit is contained in:
parent
9705ceda5d
commit
78bdec3af4
4 changed files with 13 additions and 7 deletions
|
@ -9,6 +9,8 @@ import {
|
||||||
import { id } from "../id.js";
|
import { id } from "../id.js";
|
||||||
import { DriveFile } from "./drive-file.js";
|
import { DriveFile } from "./drive-file.js";
|
||||||
|
|
||||||
|
export type EmojiModPerm = "unauthorized" | "add" | "mod" | "full";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
@Index(["usernameLower", "host"], { unique: true })
|
@Index(["usernameLower", "host"], { unique: true })
|
||||||
export class User {
|
export class User {
|
||||||
|
@ -188,7 +190,7 @@ export class User {
|
||||||
enum: ["unauthorized", "add", "mod", "full"],
|
enum: ["unauthorized", "add", "mod", "full"],
|
||||||
default: "unauthorized",
|
default: "unauthorized",
|
||||||
})
|
})
|
||||||
public emojiModPerm: "unauthorized" | "add" | "mod" | "full";
|
public emojiModPerm: EmojiModPerm;
|
||||||
|
|
||||||
@Index()
|
@Index()
|
||||||
@Column("boolean", {
|
@Column("boolean", {
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
|
import { unsafeCast } from "./unsafe-cast.js";
|
||||||
|
|
||||||
export type Promiseable<T> = {
|
export type Promiseable<T> = {
|
||||||
[K in keyof T]: Promise<T[K]> | T[K];
|
[K in keyof T]: Promise<T[K]> | T[K];
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function awaitAll<T>(obj: Promiseable<T>): Promise<T> {
|
export async function awaitAll<T>(obj: Promiseable<T>): Promise<T> {
|
||||||
const target = {} as T;
|
const target = {} as T;
|
||||||
const keys = Object.keys(obj) as unknown as (keyof T)[];
|
const keys = unsafeCast<(keyof T)[]>(Object.keys(obj));
|
||||||
const values = Object.values(obj) as any[];
|
const values = Object.values(obj) as any[];
|
||||||
|
|
||||||
const resolvedValues = await Promise.all(
|
const resolvedValues = await Promise.all(
|
||||||
|
|
4
packages/backend/src/prelude/unsafe-cast.ts
Normal file
4
packages/backend/src/prelude/unsafe-cast.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
// unsafe type cast
|
||||||
|
export function unsafeCast<T>(val: unknown): T {
|
||||||
|
return val as T;
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
import define from "@/server/api/define.js";
|
import define from "@/server/api/define.js";
|
||||||
import { Users } from "@/models/index.js";
|
import { Users } from "@/models/index.js";
|
||||||
import { publishInternalEvent } from "@/services/stream.js";
|
import { publishInternalEvent } from "@/services/stream.js";
|
||||||
|
import type { EmojiModPerm } from "@/models/entities/user.js";
|
||||||
|
import { unsafeCast } from "@/prelude/unsafe-cast.js";
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ["admin"],
|
tags: ["admin"],
|
||||||
|
@ -31,12 +33,8 @@ export default define(meta, paramDef, async (ps) => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const _emojiModPerm =
|
|
||||||
(ps.emojiModPerm as "unauthorized" | "add" | "mod" | "full") ??
|
|
||||||
"unauthorized";
|
|
||||||
|
|
||||||
await Users.update(user.id, {
|
await Users.update(user.id, {
|
||||||
emojiModPerm: _emojiModPerm,
|
emojiModPerm: unsafeCast<EmojiModPerm>(ps.emojiModPerm),
|
||||||
});
|
});
|
||||||
|
|
||||||
publishInternalEvent("userChangeModeratorState", {
|
publishInternalEvent("userChangeModeratorState", {
|
||||||
|
|
Loading…
Reference in a new issue