made pretty and fixed message posting

This commit is contained in:
Kevin Puig
2025-09-27 15:55:20 -04:00
parent 74f4e076ce
commit da1310b9c8
10 changed files with 415 additions and 390 deletions

View File

@@ -5,31 +5,35 @@ import {
Role,
UserAuth,
} from "@prisma/client";
import { CreateUserInput } from '../validators/userValidator';
import { CreateUserInput } from "../validators/userValidator";
import shaHash from "../helper/hashing";
const prisma = new PrismaClient();
export async function createUser(data: CreateUserInput): Promise<{
username: string,
nickname: string | null,
bio: string | null,
picture: string | null,
banner: string | null,
status: string,
admin: boolean
username: string;
nickname: string | null;
bio: string | null;
picture: string | null;
banner: string | null;
status: string;
admin: boolean;
} | null> {
const requestingUser = await getUserInformation(data.requestingUserId);
const requestingUserCredentials = await getUserCredentials(data.requestingUserId)
if (!requestingUser
|| !requestingUserCredentials
|| !requestingUser.admin
|| requestingUserCredentials.token == null
|| data.requestingUserToken != requestingUserCredentials.token) {
const requestingUserCredentials = await getUserCredentials(
data.requestingUserId,
);
if (
!requestingUser ||
!requestingUserCredentials ||
!requestingUser.admin ||
requestingUserCredentials.token == null ||
data.requestingUserToken != requestingUserCredentials.token
) {
return null;
}
if (await prisma.user.count({ where: { username: data.username }}) >= 1) {
if ((await prisma.user.count({ where: { username: data.username } })) >= 1) {
return null;
}
@@ -45,13 +49,15 @@ export async function createUser(data: CreateUserInput): Promise<{
},
});
if (!(await prisma.userAuth.create({
data: {
userId: userData.id,
password: shaHash(data.passwordhash, userData.id),
token: null,
}
}))) {
if (
!(await prisma.userAuth.create({
data: {
userId: userData.id,
password: shaHash(data.passwordhash, userData.id),
token: null,
},
}))
) {
return null;
}
@@ -59,52 +65,52 @@ export async function createUser(data: CreateUserInput): Promise<{
}
export async function getUserCredentials(userId: string): Promise<{
userId: string,
password: string,
token: string | null
} | null> {
try {
if (!userId) {
throw new Error("missing userId");
}
userId: string;
password: string;
token: string | null;
} | null> {
try {
if (!userId) {
throw new Error("missing userId");
}
const userAuth = await prisma.userAuth.findUnique({
where: {
userId: userId,
},
});
const userAuth = await prisma.userAuth.findUnique({
where: {
userId: userId,
},
});
if (!userAuth) {
throw new Error("could not find user credentials");
}
if (!userAuth) {
throw new Error("could not find user credentials");
}
return {
userId: userAuth.userId,
password: userAuth.password,
token: userAuth.token,
};
} catch (err) {
const errMessage = err as Error;
return {
userId: userAuth.userId,
password: userAuth.password,
token: userAuth.token,
};
} catch (err) {
const errMessage = err as Error;
if (errMessage.message === "missing userId") {
console.log("services::actions::getUserCredentials - missing userId");
return null;
}
if (errMessage.message === "could not find user credentials") {
console.log(
"services::actions::getUserCredentials - unable to find user credentials",
);
return null;
}
if (errMessage.message === "missing userId") {
console.log("services::actions::getUserCredentials - missing userId");
return null;
}
if (errMessage.message === "could not find user credentials") {
console.log(
"services::actions::getUserCredentials - unknown error",
errMessage,
"services::actions::getUserCredentials - unable to find user credentials",
);
return null;
}
console.log(
"services::actions::getUserCredentials - unknown error",
errMessage,
);
return null;
}
}
export async function getUserInformation(userId: string): Promise<{
id: string;
@@ -196,9 +202,9 @@ export async function getAllUsersFrom(instanceId: string): Promise<
try {
const instances = await prisma.instance.count({
where: {
id: instanceId
}
})
id: instanceId,
},
});
if (instances < 1) {
throw new Error("could not find given instance id");
}
@@ -218,8 +224,8 @@ export async function getAllUsersFrom(instanceId: string): Promise<
const admins = await prisma.user.findMany({
where: {
admin: true
}
admin: true,
},
});
if (!admins) {
throw new Error("could not get all admins");
@@ -249,13 +255,13 @@ export async function getAllUsersFrom(instanceId: string): Promise<
).includes(u.status as any)
? (u.status as "online" | "offline" | "dnd" | "idle" | "invis")
: "offline",
role: adminRoles.map(r => ({
role: adminRoles.map((r) => ({
userId: r.userId,
instanceId: r.instanceId,
})),
}
})
)
};
}),
);
const userData = await Promise.all(
users.map(async (u) => {