dev #2

Merged
Exverge merged 21 commits from :dev into dev 2024-03-23 16:52:48 +01:00
3 changed files with 17 additions and 3 deletions
Showing only changes of commit 3107b0e72e - Show all commits

View file

@ -4,7 +4,7 @@ import type { SuyuUser } from "../schema";
import { v4 } from "uuid"; import { v4 } from "uuid";
export class RoomManager { export class RoomManager {
private static rooms: Room[] = []; static rooms: Room[] = [];
static roomTimeout: Record<string, number> = {}; // room id, last heard from static roomTimeout: Record<string, number> = {}; // room id, last heard from
static createRoom(room: IRoomConfig) { static createRoom(room: IRoomConfig) {
const existingRoom = this.rooms.find((r) => r.host.username === room.host.username); const existingRoom = this.rooms.find((r) => r.host.username === room.host.username);

View file

@ -21,13 +21,22 @@ export async function useAuth(
const decoded: IJwtData = jwt.verify(token, Buffer.from(PUBLIC_KEY), { const decoded: IJwtData = jwt.verify(token, Buffer.from(PUBLIC_KEY), {
algorithms: ["RS256"], algorithms: ["RS256"],
}) as IJwtData; }) as IJwtData;
const user = await userRepo.findOne({ let user = await userRepo.findOne({
where: { where: {
apiKey: decoded.apiKey, apiKey: decoded.apiKey,
}, },
loadEagerRelations: eager || false, loadEagerRelations: eager || false,
relations: eager ? ["sentFriendRequests", "receivedFriendRequests"] : [], relations: eager ? ["sentFriendRequests", "receivedFriendRequests"] : [],
}); });
if (!user) {
user = await userRepo.findOne({
where: {
id: decoded.id,
},
loadEagerRelations: eager || false,
relations: eager ? ["sentFriendRequests", "receivedFriendRequests"] : [],
});
}
return user; return user;
} }
const user = await userRepo.findOne({ const user = await userRepo.findOne({

View file

@ -41,7 +41,6 @@ export async function POST({ request, getClientAddress }) {
if (!token) return new Response(null, { status: 401 }); if (!token) return new Response(null, { status: 401 });
// TODO: jwt utils which type and validate automatically // TODO: jwt utils which type and validate automatically
const user = await useAuth(token); const user = await useAuth(token);
console.log(user);
if (!user) return new Response(null, { status: 401 }); if (!user) return new Response(null, { status: 401 });
const borkedIp = getClientAddress(); const borkedIp = getClientAddress();
const room = RoomManager.createRoom({ const room = RoomManager.createRoom({
@ -67,5 +66,11 @@ export async function POST({ request, getClientAddress }) {
hasPassword: body.hasPassword || false, hasPassword: body.hasPassword || false,
}); });
console.log("Room added:", JSON.stringify(room, null, 2)); console.log("Room added:", JSON.stringify(room, null, 2));
// push every room to the top which starts with `[SUYU OFFICIAL]` and was created with username "suyu"
const suyuRoom = RoomManager.rooms.find((r) => r.roomInfo.name.startsWith("[SUYU OFFICIAL]"));
if (suyuRoom && suyuRoom.host.username === "suyu") {
RoomManager.rooms.splice(RoomManager.rooms.indexOf(suyuRoom), 1);
RoomManager.rooms.unshift(suyuRoom);
}
return json(room.toJSON()); return json(room.toJSON());
} }