Tokens + new cmd

This commit is contained in:
Daniel 2024-02-04 00:48:33 -03:00
parent 90b5b6c4c4
commit ef7d326360
6 changed files with 121 additions and 21 deletions

View File

@ -13,8 +13,11 @@ datasource db {
model User { model User {
id String @id @unique @map("_id") id String @id @unique @map("_id")
token String @unique @default("no")
name String @default("Anonymous") name String @default("Anonymous")
color String @default("#ffffff") color String @default("#ffffff")
tag String @default("{}")
rank String @default("member")
flags String @default("{}") // JSON flags object flags String @default("{}") // JSON flags object
} }

View File

@ -146,7 +146,9 @@ export class Channel extends EventEmitter {
}); });
this.on("command", (msg, socket) => { this.on("command", (msg, socket) => {
// TODO commands if(msg.message.startsWith("/tag")) {
}
}); });
} }
@ -333,6 +335,7 @@ export class Channel extends EventEmitter {
_id: part._id, _id: part._id,
name: part.name, name: part.name,
color: part.color, color: part.color,
tag: part.tag,
id: part.id, id: part.id,
x: cursorPos.x, x: cursorPos.x,
y: cursorPos.y y: cursorPos.y

View File

@ -4,12 +4,23 @@ import { UserFlags } from "../util/types";
export async function createUser( export async function createUser(
_id: string, _id: string,
token?: string,
name?: string, name?: string,
color?: string, color?: string,
tag?: object,
rank?: string,
flags?: UserFlags flags?: UserFlags
) { ) {
return await prisma.user.create({ return await prisma.user.create({
data: { id: _id, name, color, flags: JSON.stringify(flags) } data: {
id: _id,
token:token,
name:name,
color:color,
tag:JSON.stringify(tag),
rank:rank,
flags: JSON.stringify(flags)
}
}); });
} }
@ -26,9 +37,12 @@ export async function deleteUser(_id: string) {
}); });
} }
export async function readUser(_id: string) { export async function readUser(token: string) {
const data = await prisma.user.findUnique({ const data = await prisma.user.findFirst({
where: { id: _id } where: {
token: token,
// id:_id
}
}); });
if (!data) return null; if (!data) return null;
@ -39,7 +53,7 @@ export async function readUser(_id: string) {
// color: data.color, // color: data.color,
// flags: data.flags // flags: data.flags
// }; // };
data.tag = JSON.parse(data.tag)
return data; return data;
} }

View File

@ -1,5 +1,8 @@
import { create } from "domain";
import { ChannelList } from "../../channel/ChannelList"; import { ChannelList } from "../../channel/ChannelList";
import { deleteUser, getUsers } from "../../data/user"; import { prisma } from "../../data/prisma";
import { deleteUser, getUsers, createUser } from "../../data/user";
import { config } from "../../ws/usersConfig";
import Command from "./Command"; import Command from "./Command";
Command.addCommand( Command.addCommand(
@ -49,6 +52,30 @@ Command.addCommand(
}) })
); );
Command.addCommand(
new Command(["usercreate", "createuser"], "createuser <id> <token> <tag> <rank>", async msg => {
if(msg.args.length > 4) {
var id = msg.args[1]
var token = msg.args[2]
var tag = JSON.parse(msg.args[3])
var rank = msg.args[4]
createUser(id, token, "Anonymous", "#8d3f50", tag, rank, config.defaultFlags)
var checkCreated = await prisma.user.findFirst({
where: {
id:id,
token:token
}
})
if(checkCreated) {
return("User Created. : "+JSON.stringify(checkCreated));
}
} else {
return("Usage: createuser <id> <token> <tag> <rank>")
}
})
);
Command.addCommand( Command.addCommand(
new Command(["list", "ls"], "list <channels, users>", async msg => { new Command(["list", "ls"], "list <channels, users>", async msg => {
if(msg.args.length > 1) { if(msg.args.length > 1) {

View File

@ -16,6 +16,7 @@ import {
Vector2 Vector2
} from "../util/types"; } from "../util/types";
import type { User } from "@prisma/client"; import type { User } from "@prisma/client";
import { prisma } from "../data/prisma";
import { createUser, readUser, updateUser } from "../data/user"; import { createUser, readUser, updateUser } from "../data/user";
import { eventGroups } from "./events"; import { eventGroups } from "./events";
import { Gateway } from "./Gateway"; import { Gateway } from "./Gateway";
@ -28,6 +29,7 @@ import { adminLimits } from "./ratelimit/limits/admin";
import { userLimits } from "./ratelimit/limits/user"; import { userLimits } from "./ratelimit/limits/user";
import { NoteQuota } from "./ratelimit/NoteQuota"; import { NoteQuota } from "./ratelimit/NoteQuota";
import { config } from "./usersConfig"; import { config } from "./usersConfig";
import { randomUUID } from "crypto";
const logger = new Logger("Sockets"); const logger = new Logger("Sockets");
@ -38,6 +40,7 @@ export class Socket extends EventEmitter {
private _id: string; private _id: string;
private ip: string; private ip: string;
private user: User | null = null; private user: User | null = null;
public hasprocessedhi: boolean | null = null;
public gateway = new Gateway(); public gateway = new Gateway();
@ -57,13 +60,15 @@ export class Socket extends EventEmitter {
constructor( constructor(
private ws: ServerWebSocket<{ ip: string }>, private ws: ServerWebSocket<{ ip: string }>,
public socketID: string public socketID: string,
public token: string
) { ) {
super(); super();
this.ip = ws.data.ip; this.ip = ws.data.ip;
// User ID // User ID
this._id = createUserID(this.getIP()); this._id = createUserID(this.getIP());
if(!token) this.token = this._id+"."+randomUUID(); else this.token = token;
// Check if we're already connected // Check if we're already connected
// We need to skip ourselves, so we loop here instead of using a helper // We need to skip ourselves, so we loop here instead of using a helper
@ -92,10 +97,10 @@ export class Socket extends EventEmitter {
} }
(async () => { (async () => {
await this.loadUser(); this.sendArray([{m:"b", code:""}])
// TODO Permissions // TODO Permissions
let isAdmin = false; let isAdmin = this.getParticipant()?.rank == "admin";
this.setRateLimits(isAdmin ? adminLimits : userLimits); this.setRateLimits(isAdmin ? adminLimits : userLimits);
@ -134,6 +139,7 @@ export class Socket extends EventEmitter {
if (channel) { if (channel) {
// Exists, join normally // Exists, join normally
(async () => { (async () => {
if(!this.hasprocessedhi) return;
await this.loadUser(); await this.loadUser();
channel.join(this); channel.join(this);
})(); })();
@ -152,6 +158,12 @@ export class Socket extends EventEmitter {
public admin = new EventEmitter(); public admin = new EventEmitter();
private bindEventListeners() { private bindEventListeners() {
this.on("hi", async msg => {
if(msg.token) {
this.token = msg.token
}
await this.loadUser();
})
for (const group of eventGroups) { for (const group of eventGroups) {
if (group.id == "admin") { if (group.id == "admin") {
for (const event of group.eventList) { for (const event of group.eventList) {
@ -173,24 +185,60 @@ export class Socket extends EventEmitter {
this.ws.send(JSON.stringify(arr)); this.ws.send(JSON.stringify(arr));
} }
private async loadUser() { public async loadUser() {
let user = await readUser(this._id); if(!this.hasprocessedhi) return;
var newId = await prisma.user.findFirst({
where: {
token: this.token
}
});
if(newId) this._id = newId.id; else this.token = this._id+"."+randomUUID();
var idExists = await prisma.user.findFirst({
where: {
id: this._id
}
});
if(idExists) this._id = idExists.id;
if(idExists) this.token = idExists.token;
let user = await readUser(this.token);
if (!user) { if (!user) {
await createUser( await createUser(
this._id, this._id,
this.token,
config.defaultName, config.defaultName,
createColor(this.ip), createColor(this.ip),
{},
"member",
config.defaultFlags config.defaultFlags
); );
user = await readUser(this._id); user = await readUser(this.token);
} }
this.user = user; this.user = user;
} }
public getUser() { public async getUser() {
var newId = await prisma.user.findFirst({
where: {
token: this.token
}
});
if(newId) this._id = newId.id; else this.token = this._id+"."+randomUUID();
var idExists = await prisma.user.findFirst({
where: {
id: this._id
}
});
if(idExists) this._id = idExists.id;
if(idExists) this.token = idExists.token;
let user = await readUser(this.token);
this.user = user;
if (this.user) { if (this.user) {
return this.user; return this.user;
} }
@ -242,6 +290,8 @@ export class Socket extends EventEmitter {
_id: facadeID, _id: facadeID,
name: this.user.name, name: this.user.name,
color: this.user.color, color: this.user.color,
tag: this.user.tag,
rank: this.user.rank,
id: this.id id: this.id
}; };
} else { } else {

View File

@ -2,19 +2,25 @@ import { ServerEventListener } from "../../../../util/types";
export const hi: ServerEventListener<"hi"> = { export const hi: ServerEventListener<"hi"> = {
id: "hi", id: "hi",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Handshake message // Handshake message
// TODO Hi message tokens // TODO Hi message tokens
// I'm not actually sure if I'm up for doing tokens, // I'm not actually sure if I'm up for doing tokens,
// but if someone wants to submit a pull request, I // but if someone wants to submit a pull request, I
// look forward to watching you do all the work // look forward to watching you do all the work
if (socket.gateway.hasProcessedHi) return; if (socket.gateway.hasProcessedHi) return;
if(msg.token) {
socket.token = msg.token;
}
let part = socket.getParticipant(); let part = socket.getParticipant();
socket.hasprocessedhi = true;
socket.loadUser()
socket.setChannel("lobby")
if (!part) { if (!part) {
part = { part = {
_id: socket.getUserID(), _id: socket.getUserID(),
name: "Anonymous", name: "broken",
color: "#777", color: "#777",
id: "" id: ""
}; };
@ -25,12 +31,9 @@ export const hi: ServerEventListener<"hi"> = {
m: "hi", m: "hi",
accountInfo: undefined, accountInfo: undefined,
permissions: undefined, permissions: undefined,
token: (await socket.getUser()).token,
t: Date.now(), t: Date.now(),
u: { u: part
_id: part._id,
color: part.color,
name: part.name
}
} }
]); ]);