wip entities

This commit is contained in:
simplonco
2023-01-05 14:39:52 +01:00
parent 286d79ed06
commit f0736ab20b
11 changed files with 248 additions and 99 deletions

View File

@@ -1,11 +1,54 @@
import { Controller, Post, Body } from '@nestjs/common'; import { Controller, UseGuards, Get, Post, Body, Req } from '@nestjs/common';
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
import { ChatService } from './chat.service';
@Controller('/chat/create') @Controller('chat')
export class ChatController { export class ChatController {
@Post()
async handleSubmit(@Body() formData: any) { constructor(
console.log("------ create:"); private chatService: ChatService,
console.log(formData); ) {}
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('rooms')
async get_rooms() {
const rooms = await this.chatService.getRooms();
return { rooms };
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('join')
async join_room(@Body() body) {
console.log("------ create :");
console.log(typeof body);
console.log(body);
console.log(body.room_name);
const { room_name } = body;
console.log("room_name:");
console.log(room_name);
const { room_id } = body;
// get user
let user;
await this.chatService.addUserToRoom(user, room_id);
//return { message: 'Successfully joined room.' };
return body;
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('leave')
async leaveRoom(@Body() body) {
const { room_id } = body;
// get user
let user;
await this.chatService.removeUserFromRoom(user, room_id);
return { message: 'Successfully left room.' };
}
} }

View File

@@ -52,6 +52,5 @@ export class ChatGateway
client.local.emit('message', client.username, message); client.local.emit('message', client.username, message);
// this.chatService.add_message(this.server, message); // this.chatService.add_message(this.server, message);
} }
} }

View File

@@ -4,8 +4,12 @@ import { ChatService } from './chat.service';
import { ChatGateway } from './chat.gateway'; import { ChatGateway } from './chat.gateway';
import { UsersModule } from 'src/users/users.module'; import { UsersModule } from 'src/users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Chatroom } from './entities/chat.entity';
@Module({ @Module({
imports: [ imports: [
TypeOrmModule.forFeature([Chatroom]),
UsersModule, UsersModule,
], ],
controllers: [ controllers: [

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
//import { InjectRepository } from '@nestjs/typeorm'; //import { InjectRepository } from '@nestjs/typeorm';
//import { User } from 'src/users/entities/user.entity';
//import { Repository } from 'typeorm'; //import { Repository } from 'typeorm';
@Injectable() @Injectable()
@@ -12,7 +12,28 @@ export class ChatService {
// private readonly userRepository: Repository<User>, // private readonly userRepository: Repository<User>,
) { } ) { }
async add_message(server, message) { //async add_message(server, message) {
return server.emit('message', message); // return server.emit('message', message);
//}
async getRooms()
{
// get rooms
// return rooms;
}
async addUserToRoom(user: User, room_id: string)
{
// get room
//if !room
// create room
// add user to room
}
async removeUserFromRoom(user: User, room_id: string)
{
// get room
// remove user
} }
} }

View File

@@ -0,0 +1,26 @@
import {
Entity,
Column,
ManyToOne,
PrimaryGeneratedColumn
} from "typeorm";
import { User } from 'src/users/entities/user.entity';
@Entity('chatroom')
export class Chatroom {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
type: 'public' | 'private' | 'direct';
@Column({ nullable: true })
protection: boolean;
@ManyToOne(type => User, user => user.ownedRoom)
owner: User;
}

View File

@@ -3,6 +3,7 @@ import { IsEmail, Length } from "class-validator";
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm"; import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm";
import { Friendship } from "../../friendship/entities/friendship.entity"; import { Friendship } from "../../friendship/entities/friendship.entity";
import { UserStats } from "./userStat.entities"; import { UserStats } from "./userStat.entities";
import { Chatroom } from "src/chat/entities/chat.entity";
@Entity('user') @Entity('user')
@@ -49,6 +50,9 @@ export class User {
@OneToMany(type => Friendship , (friendship) => friendship.receiver) @OneToMany(type => Friendship , (friendship) => friendship.receiver)
receivedFriendRequest: Friendship[]; receivedFriendRequest: Friendship[];
@OneToMany(type => Chatroom, chatroom => chatroom.owner)
ownedRoom: Chatroom[];
@JoinColumn() @JoinColumn()
@OneToOne(() => UserStats, { cascade: true }) @OneToOne(() => UserStats, { cascade: true })
stats: UserStats; stats: UserStats;

View File

@@ -10348,7 +10348,7 @@ var app = (function () {
const { console: console_1$8 } = globals; const { console: console_1$8 } = globals;
const file$h = "src/pieces/chat/Layout_create.svelte"; const file$h = "src/pieces/chat/Layout_create.svelte";
// (27:1) <Button bind:layout new_layout={back} my_class="back icon" my_title="go back {back}"> // (30:1) <Button bind:layout new_layout={back} my_class="back icon" my_title="go back {back}">
function create_default_slot_2$3(ctx) { function create_default_slot_2$3(ctx) {
let t; let t;
@@ -10368,14 +10368,14 @@ var app = (function () {
block, block,
id: create_default_slot_2$3.name, id: create_default_slot_2$3.name,
type: "slot", type: "slot",
source: "(27:1) <Button bind:layout new_layout={back} my_class=\\\"back icon\\\" my_title=\\\"go back {back}\\\">", source: "(30:1) <Button bind:layout new_layout={back} my_class=\\\"back icon\\\" my_title=\\\"go back {back}\\\">",
ctx ctx
}); });
return block; return block;
} }
// (32:1) <Button my_class="create deactivate"> // (35:1) <Button my_class="create deactivate">
function create_default_slot_1$3(ctx) { function create_default_slot_1$3(ctx) {
let t; let t;
@@ -10395,14 +10395,14 @@ var app = (function () {
block, block,
id: create_default_slot_1$3.name, id: create_default_slot_1$3.name,
type: "slot", type: "slot",
source: "(32:1) <Button my_class=\\\"create deactivate\\\">", source: "(35:1) <Button my_class=\\\"create deactivate\\\">",
ctx ctx
}); });
return block; return block;
} }
// (37:1) <Button bind:layout new_layout="close" my_class="close icon"> // (40:1) <Button bind:layout new_layout="close" my_class="close icon">
function create_default_slot$5(ctx) { function create_default_slot$5(ctx) {
let t; let t;
@@ -10422,7 +10422,7 @@ var app = (function () {
block, block,
id: create_default_slot$5.name, id: create_default_slot$5.name,
type: "slot", type: "slot",
source: "(37:1) <Button bind:layout new_layout=\\\"close\\\" my_class=\\\"close icon\\\">", source: "(40:1) <Button bind:layout new_layout=\\\"close\\\" my_class=\\\"close icon\\\">",
ctx ctx
}); });
@@ -10562,63 +10562,68 @@ var app = (function () {
t17 = space(); t17 = space();
input5 = element("input"); input5 = element("input");
attr_dev(p0, "class", "svelte-111r258"); attr_dev(p0, "class", "svelte-111r258");
add_location(p0, file$h, 53, 26, 1353); add_location(p0, file$h, 56, 26, 1484);
attr_dev(label0, "for", "chat_name"); attr_dev(label0, "for", "chat_name");
add_location(label0, file$h, 53, 3, 1330); add_location(label0, file$h, 56, 3, 1461);
attr_dev(input0, "id", "chat_name"); attr_dev(input0, "id", "chat_name");
attr_dev(input0, "name", "room_name");
input0.required = true; input0.required = true;
add_location(input0, file$h, 54, 3, 1387); add_location(input0, file$h, 57, 3, 1518);
attr_dev(input1, "id", "chat_public"); attr_dev(input1, "id", "chat_public");
attr_dev(input1, "type", "radio"); attr_dev(input1, "type", "radio");
attr_dev(input1, "name", "chat_create_type"); attr_dev(input1, "name", "room_type");
input1.value = "public";
input1.checked = true; input1.checked = true;
attr_dev(input1, "class", "svelte-111r258"); attr_dev(input1, "class", "svelte-111r258");
add_location(input1, file$h, 56, 3, 1445); add_location(input1, file$h, 59, 3, 1593);
attr_dev(p1, "class", "svelte-111r258"); attr_dev(p1, "class", "svelte-111r258");
add_location(p1, file$h, 57, 43, 1559); add_location(p1, file$h, 60, 43, 1715);
attr_dev(label1, "for", "chat_public"); attr_dev(label1, "for", "chat_public");
attr_dev(label1, "class", "_radio svelte-111r258"); attr_dev(label1, "class", "_radio svelte-111r258");
add_location(label1, file$h, 57, 3, 1519); add_location(label1, file$h, 60, 3, 1675);
attr_dev(input2, "id", "chat_private"); attr_dev(input2, "id", "chat_private");
attr_dev(input2, "type", "radio"); attr_dev(input2, "type", "radio");
attr_dev(input2, "name", "chat_create_type"); attr_dev(input2, "name", "room_type");
input2.value = "private";
attr_dev(input2, "class", "svelte-111r258"); attr_dev(input2, "class", "svelte-111r258");
add_location(input2, file$h, 59, 3, 1608); add_location(input2, file$h, 62, 3, 1764);
attr_dev(p2, "class", "svelte-111r258"); attr_dev(p2, "class", "svelte-111r258");
add_location(p2, file$h, 60, 44, 1716); add_location(p2, file$h, 63, 44, 1881);
attr_dev(label2, "for", "chat_private"); attr_dev(label2, "for", "chat_private");
attr_dev(label2, "class", "_radio svelte-111r258"); attr_dev(label2, "class", "_radio svelte-111r258");
add_location(label2, file$h, 60, 3, 1675); add_location(label2, file$h, 63, 3, 1840);
attr_dev(input3, "id", "chat_protected"); attr_dev(input3, "id", "chat_protected");
attr_dev(input3, "class", "__check_change_next svelte-111r258"); attr_dev(input3, "class", "__check_change_next svelte-111r258");
attr_dev(input3, "type", "radio"); attr_dev(input3, "type", "radio");
attr_dev(input3, "name", "chat_create_type"); attr_dev(input3, "name", "room_type");
add_location(input3, file$h, 62, 3, 1768); input3.value = "protected";
add_location(input3, file$h, 65, 3, 1933);
attr_dev(p3, "class", "svelte-111r258"); attr_dev(p3, "class", "svelte-111r258");
add_location(p3, file$h, 63, 46, 1908); add_location(p3, file$h, 66, 46, 2084);
attr_dev(label3, "for", "chat_protected"); attr_dev(label3, "for", "chat_protected");
attr_dev(label3, "class", "_radio svelte-111r258"); attr_dev(label3, "class", "_radio svelte-111r258");
add_location(label3, file$h, 63, 3, 1865); add_location(label3, file$h, 66, 3, 2041);
attr_dev(p4, "class", "svelte-111r258"); attr_dev(p4, "class", "svelte-111r258");
add_location(p4, file$h, 66, 27, 2013); add_location(p4, file$h, 69, 27, 2189);
attr_dev(label4, "for", "chat_pswd"); attr_dev(label4, "for", "chat_pswd");
add_location(label4, file$h, 66, 4, 1990); add_location(label4, file$h, 69, 4, 2166);
attr_dev(input4, "id", "chat_pswd"); attr_dev(input4, "id", "chat_pswd");
attr_dev(input4, "type", "password"); attr_dev(input4, "type", "password");
attr_dev(input4, "placeholder", "minimum 8 characters"); attr_dev(input4, "placeholder", "minimum 8 characters");
attr_dev(input4, "minlength", "8"); attr_dev(input4, "minlength", "8");
add_location(input4, file$h, 67, 4, 2052); attr_dev(input4, "name", "password");
add_location(input4, file$h, 70, 4, 2228);
attr_dev(div0, "class", "__to_show"); attr_dev(div0, "class", "__to_show");
add_location(div0, file$h, 65, 3, 1962); add_location(div0, file$h, 68, 3, 2138);
attr_dev(input5, "type", "submit"); attr_dev(input5, "type", "submit");
input5.value = "⮡"; input5.value = "⮡";
attr_dev(input5, "class", "svelte-111r258"); attr_dev(input5, "class", "svelte-111r258");
add_location(input5, file$h, 69, 3, 2154); add_location(input5, file$h, 72, 3, 2346);
add_location(form, file$h, 51, 2, 1262); add_location(form, file$h, 54, 2, 1393);
attr_dev(div1, "class", "panel panel_create __border_top svelte-111r258"); attr_dev(div1, "class", "panel panel_create __border_top svelte-111r258");
add_location(div1, file$h, 41, 1, 717); add_location(div1, file$h, 44, 1, 848);
attr_dev(div2, "class", "grid_box svelte-111r258"); attr_dev(div2, "class", "grid_box svelte-111r258");
add_location(div2, file$h, 23, 0, 372); add_location(div2, file$h, 26, 0, 503);
}, },
l: function claim(nodes) { l: function claim(nodes) {
throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option"); throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");
@@ -10738,13 +10743,20 @@ var app = (function () {
return block; return block;
} }
function handleSubmit(evt) { async function handleSubmit(evt) {
let formIsValid = evt.target.checkValidity(); let formIsValid = evt.target.checkValidity();
console.log("----- formIsValid:");
console.log(formIsValid);
if (formIsValid) { if (formIsValid) {
fetch('/api/v2/chat/create', { method: 'POST', body: evt.target }); const formData = new FormData(evt.target);
console.log(formData);
const response = await fetch('/api/v2/chat/join', {
method: 'POST',
//headers: { 'Content-Type': 'multipart/form-data' },
body: formData
});
console.log(await response.json());
} }
} }
@@ -13984,6 +13996,10 @@ var app = (function () {
validate_slots('Chat', slots, []); validate_slots('Chat', slots, []);
let { color = "transparent" } = $$props; let { color = "transparent" } = $$props;
// async function socket_actions()
// {
//
// await init_socket();
// pbm: sometimes socket is still undefined here // pbm: sometimes socket is still undefined here
socket$1.on('connect', function () { socket$1.on('connect', function () {
console.log("socket.io connected"); console.log("socket.io connected");

File diff suppressed because one or more lines are too long

View File

@@ -7,8 +7,13 @@
/* web sockets with socket.io /* web sockets with socket.io
*/ */
import { socket, user } from './Chat_socket.svelte'; import { socket, user } from './Chat_socket.svelte';
// import { init_socket, socket, user } from './Socket';
import { msgs } from './Store_msg.js'; import { msgs } from './Store_msg.js';
// async function socket_actions()
// {
//
// await init_socket();
// pbm: sometimes socket is still undefined here // pbm: sometimes socket is still undefined here
socket.on('connect', function(){ socket.on('connect', function(){
@@ -55,6 +60,9 @@
from = "me"; from = "me";
msgs.update(msgs => [...msgs, { content: message, name: from }]); msgs.update(msgs => [...msgs, { content: message, name: from }]);
}); });
// };
// socket_actions();
</script> </script>

View File

@@ -5,17 +5,20 @@
export let layout = ""; export let layout = "";
export let back = ""; export let back = "";
function handleSubmit(evt) async function handleSubmit(evt)
{ {
let formIsValid = evt.target.checkValidity(); let formIsValid = evt.target.checkValidity();
console.log("----- formIsValid:");
console.log(formIsValid);
if (formIsValid) if (formIsValid)
{ {
fetch('/api/v2/chat/create', { const formData = new FormData(evt.target);
console.log(formData);
const response = await fetch('/api/v2/chat/join', {
method: 'POST', method: 'POST',
body: evt.target, //headers: { 'Content-Type': 'multipart/form-data' },
}) body: formData,
});
console.log(await response.json());
} }
} }
@@ -52,20 +55,20 @@
<form on:submit|preventDefault={handleSubmit}> <form on:submit|preventDefault={handleSubmit}>
<!-- name: --> <!-- name: -->
<label for="chat_name"><p>new room name :</p></label> <label for="chat_name"><p>new room name :</p></label>
<input id="chat_name" required> <input id="chat_name" name="room_name" required>
<!-- [ ] pubic --> <!-- [ ] pubic -->
<input id="chat_public" type="radio" name="chat_create_type" checked> <input id="chat_public" type="radio" name="room_type" value="public" checked>
<label for="chat_public" class="_radio"><p>public</p></label> <label for="chat_public" class="_radio"><p>public</p></label>
<!-- [ ] private --> <!-- [ ] private -->
<input id="chat_private" type="radio" name="chat_create_type"> <input id="chat_private" type="radio" name="room_type" value="private">
<label for="chat_private" class="_radio"><p>private</p></label> <label for="chat_private" class="_radio"><p>private</p></label>
<!-- [ ] protected --> <!-- [ ] protected -->
<input id="chat_protected" class="__check_change_next" type="radio" name="chat_create_type"> <input id="chat_protected" class="__check_change_next" type="radio" name="room_type" value="protected">
<label for="chat_protected" class="_radio"><p>protected</p></label> <label for="chat_protected" class="_radio"><p>protected</p></label>
<!-- [x] protected --> <!-- [x] protected -->
<div class="__to_show"> <div class="__to_show">
<label for="chat_pswd"><p>choose a password :</p></label> <label for="chat_pswd"><p>choose a password :</p></label>
<input id="chat_pswd" type="password" placeholder="minimum 8 characters" minlength="8"> <input id="chat_pswd" type="password" placeholder="minimum 8 characters" minlength="8" name="password">
</div> </div>
<input type="submit" value="&#x2BA1"> <input type="submit" value="&#x2BA1">
</form> </form>

View File

@@ -0,0 +1,25 @@
import io from 'socket.io-client';
export let user;
export let socket;
const address = `http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}`;
export function init_socket()
{
fetch(`${address}/api/v2/user`)
.then((resp) => resp.json())
.then((data) =>
{
user = data;
socket = io(address,
{
path: '/chat',
query:
{
username: user.username,
},
});
});
}