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 {
id String @id @unique @map("_id")
token String @unique @default("no")
name String @default("Anonymous")
color String @default("#ffffff")
tag String @default("{}")
rank String @default("member")
flags String @default("{}") // JSON flags object
}

View File

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

View File

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

View File

@ -1,5 +1,8 @@
import { create } from "domain";
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";
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(
new Command(["list", "ls"], "list <channels, users>", async msg => {
if(msg.args.length > 1) {

View File

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

View File

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