leave room
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Body, Req, Res } from '@nestjs/common';
|
||||
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Delete, Body, Req, Res } from '@nestjs/common';
|
||||
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||
import { ConnectedSocket } from '@nestjs/websockets';
|
||||
import { ChatService } from './chat.service';
|
||||
@@ -108,12 +108,24 @@ export class ChatController {
|
||||
async getRoomUsers(@Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in getRoomUsers controller");
|
||||
const current_room = await this.chatService.getCurrentRoom(req.user.username);
|
||||
const room = await this.chatService.getRoomByName(current_room);
|
||||
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||
const room = await this.chatService.getRoomByName(room_name);
|
||||
const users = room.users;
|
||||
console.log("- out getRoomUsers controller");
|
||||
return res.status(HttpStatus.OK).json({ users: users });
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Delete('removeuser')
|
||||
async removeUser(@Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in removeUser controller");
|
||||
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name);
|
||||
console.log("- out removeUser controller");
|
||||
return res.status(HttpStatus.OK).json({ message: response });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ export class ChatGateway
|
||||
console.log('- in handleMessage gateway');
|
||||
//let room_name = await this.chatService.getCurrentRoom(socket.username);
|
||||
socket.to(socket.room).emit('message', socket.username, message);
|
||||
let room_name = await this.chatService.getCurrentRoom(socket.username);
|
||||
let room_name = await this.chatService.getCurrentRoomName(socket.username);
|
||||
await this.chatService.addMessageToRoom(room_name, socket.username, message);
|
||||
|
||||
console.log('- out handleMessage gateway');
|
||||
|
||||
@@ -80,14 +80,14 @@ export class ChatService {
|
||||
return currentRoom.messages;
|
||||
}
|
||||
|
||||
async getCurrentRoom(username: string)
|
||||
async getCurrentRoomName(username: string)
|
||||
{
|
||||
console.log("-- in getCurrentRoom service");
|
||||
console.log("-- in getCurrentRoomName service");
|
||||
console.log('username:', username);
|
||||
const user_db = await this.getUserByName(username);
|
||||
//const user_db = await this.usersService.findOne(username);
|
||||
|
||||
console.log("-- out getCurrentRoom service");
|
||||
console.log("-- out getCurrentRoomName service");
|
||||
return user_db.currentRoom;
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ export class ChatService {
|
||||
await this.setCurrentRoom(username, room_name);
|
||||
|
||||
console.log("-- out addUserToRoom service");
|
||||
return "successfull joining room";
|
||||
return "successfully joining room";
|
||||
}
|
||||
|
||||
async addMessageToRoom(room_name: string, username: string, message: string)
|
||||
@@ -196,8 +196,20 @@ export class ChatService {
|
||||
async removeUserFromRoom(username: string, room_name: string)
|
||||
{
|
||||
console.log("-- in removeUserFromRoom service");
|
||||
// get room
|
||||
// remove user
|
||||
const room = await this.getRoomByName(room_name);
|
||||
if (!room.users.includes(username))
|
||||
throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT);
|
||||
|
||||
// delete user from room
|
||||
room.users.push(username);
|
||||
room.users = room.users.filter(name => name !== username);
|
||||
this.chatroomRepository.save(room);
|
||||
|
||||
// set current room to nothing
|
||||
await this.setCurrentRoom(username, "");
|
||||
|
||||
console.log("-- out removeUserFromRoom service");
|
||||
return "successfully leaving room";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3922,7 +3922,6 @@ var app = (function () {
|
||||
}
|
||||
async function get_my_rooms() {
|
||||
console.log("in get_my_rooms");
|
||||
// ask api for the rooms
|
||||
const response = await fetch('/api/v2/chat/myrooms');
|
||||
const data = await response.json();
|
||||
console.log("data.rooms:", data.rooms);
|
||||
@@ -3933,7 +3932,6 @@ var app = (function () {
|
||||
}
|
||||
async function get_all_rooms() {
|
||||
console.log("in get_all_rooms");
|
||||
// ask api for the rooms
|
||||
const response = await fetch('/api/v2/chat/allrooms');
|
||||
const data = await response.json();
|
||||
console.log("data.rooms:", data.rooms);
|
||||
@@ -3944,7 +3942,6 @@ var app = (function () {
|
||||
}
|
||||
async function get_room_users() {
|
||||
console.log("in get_room_users");
|
||||
// ask api for the rooms
|
||||
const response = await fetch('/api/v2/chat/roomusers');
|
||||
const data = await response.json();
|
||||
console.log("data.users:", data.users);
|
||||
@@ -3953,6 +3950,14 @@ var app = (function () {
|
||||
let users = data.users;
|
||||
return users;
|
||||
}
|
||||
async function user_leave_room() {
|
||||
console.log("in leave_room");
|
||||
const response = await fetch('/api/v2/chat/removeuser', {
|
||||
method: 'DELETE',
|
||||
});
|
||||
const data = await response.json();
|
||||
console.log("data", data);
|
||||
}
|
||||
|
||||
/* src/pieces/chat/Layout_home.svelte generated by Svelte v3.53.1 */
|
||||
|
||||
@@ -6202,11 +6207,11 @@ var app = (function () {
|
||||
|
||||
function get_each_context$5(ctx, list, i) {
|
||||
const child_ctx = ctx.slice();
|
||||
child_ctx[3] = list[i];
|
||||
child_ctx[4] = list[i];
|
||||
return child_ctx;
|
||||
}
|
||||
|
||||
// (21:1) <Button new_layout={back} my_class="back icon" my_title="go back {back}">
|
||||
// (28:1) <Button new_layout={back} my_class="back icon" my_title="go back {back}">
|
||||
function create_default_slot_4$2(ctx) {
|
||||
let t;
|
||||
|
||||
@@ -6226,14 +6231,14 @@ var app = (function () {
|
||||
block,
|
||||
id: create_default_slot_4$2.name,
|
||||
type: "slot",
|
||||
source: "(21:1) <Button new_layout={back} my_class=\\\"back icon\\\" my_title=\\\"go back {back}\\\">",
|
||||
source: "(28:1) <Button new_layout={back} my_class=\\\"back icon\\\" my_title=\\\"go back {back}\\\">",
|
||||
ctx
|
||||
});
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
// (26:1) <Button my_class="room_name deactivate">
|
||||
// (33:1) <Button my_class="room_name deactivate">
|
||||
function create_default_slot_3$3(ctx) {
|
||||
let t;
|
||||
|
||||
@@ -6256,14 +6261,14 @@ var app = (function () {
|
||||
block,
|
||||
id: create_default_slot_3$3.name,
|
||||
type: "slot",
|
||||
source: "(26:1) <Button my_class=\\\"room_name deactivate\\\">",
|
||||
source: "(33:1) <Button my_class=\\\"room_name deactivate\\\">",
|
||||
ctx
|
||||
});
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
// (31:1) <Button new_layout="close" my_class="close icon">
|
||||
// (38:1) <Button new_layout="close" my_class="close icon">
|
||||
function create_default_slot_2$6(ctx) {
|
||||
let t;
|
||||
|
||||
@@ -6283,14 +6288,14 @@ var app = (function () {
|
||||
block,
|
||||
id: create_default_slot_2$6.name,
|
||||
type: "slot",
|
||||
source: "(31:1) <Button new_layout=\\\"close\\\" my_class=\\\"close icon\\\">",
|
||||
source: "(38:1) <Button new_layout=\\\"close\\\" my_class=\\\"close icon\\\">",
|
||||
ctx
|
||||
});
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
// (37:2) <Button new_layout="create" my_class="create">
|
||||
// (44:2) <Button on_click={leave_room}>
|
||||
function create_default_slot_1$6(ctx) {
|
||||
let t;
|
||||
|
||||
@@ -6310,7 +6315,7 @@ var app = (function () {
|
||||
block,
|
||||
id: create_default_slot_1$6.name,
|
||||
type: "slot",
|
||||
source: "(37:2) <Button new_layout=\\\"create\\\" my_class=\\\"create\\\">",
|
||||
source: "(44:2) <Button on_click={leave_room}>",
|
||||
ctx
|
||||
});
|
||||
|
||||
@@ -6339,11 +6344,11 @@ var app = (function () {
|
||||
return block;
|
||||
}
|
||||
|
||||
// (47:3) {:then users}
|
||||
// (54:3) {:then users}
|
||||
function create_then_block$1(ctx) {
|
||||
let each_1_anchor;
|
||||
let current;
|
||||
let each_value = /*users*/ ctx[2];
|
||||
let each_value = /*users*/ ctx[3];
|
||||
validate_each_argument(each_value);
|
||||
let each_blocks = [];
|
||||
|
||||
@@ -6372,8 +6377,8 @@ var app = (function () {
|
||||
current = true;
|
||||
},
|
||||
p: function update(ctx, dirty) {
|
||||
if (dirty & /*user_profile, users*/ 4) {
|
||||
each_value = /*users*/ ctx[2];
|
||||
if (dirty & /*user_profile, users*/ 8) {
|
||||
each_value = /*users*/ ctx[3];
|
||||
validate_each_argument(each_value);
|
||||
let i;
|
||||
|
||||
@@ -6428,16 +6433,16 @@ var app = (function () {
|
||||
block,
|
||||
id: create_then_block$1.name,
|
||||
type: "then",
|
||||
source: "(47:3) {:then users}",
|
||||
source: "(54:3) {:then users}",
|
||||
ctx
|
||||
});
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
// (49:5) <Button new_layout="user" my_class="list" on_click={user_profile}>
|
||||
// (56:5) <Button new_layout="user" my_class="list" on_click={user_profile}>
|
||||
function create_default_slot$7(ctx) {
|
||||
let t0_value = /*user*/ ctx[3] + "";
|
||||
let t0_value = /*user*/ ctx[4] + "";
|
||||
let t0;
|
||||
let t1;
|
||||
|
||||
@@ -6461,14 +6466,14 @@ var app = (function () {
|
||||
block,
|
||||
id: create_default_slot$7.name,
|
||||
type: "slot",
|
||||
source: "(49:5) <Button new_layout=\\\"user\\\" my_class=\\\"list\\\" on_click={user_profile}>",
|
||||
source: "(56:5) <Button new_layout=\\\"user\\\" my_class=\\\"list\\\" on_click={user_profile}>",
|
||||
ctx
|
||||
});
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
// (48:4) {#each users as user}
|
||||
// (55:4) {#each users as user}
|
||||
function create_each_block$5(ctx) {
|
||||
let button;
|
||||
let current;
|
||||
@@ -6495,7 +6500,7 @@ var app = (function () {
|
||||
p: function update(ctx, dirty) {
|
||||
const button_changes = {};
|
||||
|
||||
if (dirty & /*$$scope*/ 64) {
|
||||
if (dirty & /*$$scope*/ 128) {
|
||||
button_changes.$$scope = { dirty, ctx };
|
||||
}
|
||||
|
||||
@@ -6519,14 +6524,14 @@ var app = (function () {
|
||||
block,
|
||||
id: create_each_block$5.name,
|
||||
type: "each",
|
||||
source: "(48:4) {#each users as user}",
|
||||
source: "(55:4) {#each users as user}",
|
||||
ctx
|
||||
});
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
// (45:17) <p>list of users is loading...</p> {:then users}
|
||||
// (52:17) <p>list of users is loading...</p> {:then users}
|
||||
function create_pending_block$1(ctx) {
|
||||
let p;
|
||||
|
||||
@@ -6534,7 +6539,7 @@ var app = (function () {
|
||||
c: function create() {
|
||||
p = element("p");
|
||||
p.textContent = "list of users is loading...";
|
||||
add_location(p, file$l, 45, 4, 965);
|
||||
add_location(p, file$l, 52, 4, 1049);
|
||||
},
|
||||
m: function mount(target, anchor) {
|
||||
insert_dev(target, p, anchor);
|
||||
@@ -6551,7 +6556,7 @@ var app = (function () {
|
||||
block,
|
||||
id: create_pending_block$1.name,
|
||||
type: "pending",
|
||||
source: "(45:17) <p>list of users is loading...</p> {:then users}",
|
||||
source: "(52:17) <p>list of users is loading...</p> {:then users}",
|
||||
ctx
|
||||
});
|
||||
|
||||
@@ -6609,8 +6614,7 @@ var app = (function () {
|
||||
|
||||
button3 = new Element_button({
|
||||
props: {
|
||||
new_layout: "create",
|
||||
my_class: "create",
|
||||
on_click: /*leave_room*/ ctx[2],
|
||||
$$slots: { default: [create_default_slot_1$6] },
|
||||
$$scope: { ctx }
|
||||
},
|
||||
@@ -6625,11 +6629,11 @@ var app = (function () {
|
||||
pending: create_pending_block$1,
|
||||
then: create_then_block$1,
|
||||
catch: create_catch_block$1,
|
||||
value: 2,
|
||||
value: 3,
|
||||
blocks: [,,,]
|
||||
};
|
||||
|
||||
handle_promise(/*users*/ ctx[2], info);
|
||||
handle_promise(/*users*/ ctx[3], info);
|
||||
|
||||
const block = {
|
||||
c: function create() {
|
||||
@@ -6652,17 +6656,17 @@ var app = (function () {
|
||||
p1.textContent = "/ there are no public rooms yet /";
|
||||
t7 = space();
|
||||
info.block.c();
|
||||
add_location(p0, file$l, 39, 2, 786);
|
||||
add_location(p0, file$l, 46, 2, 870);
|
||||
attr_dev(p1, "class", "__center");
|
||||
add_location(p1, file$l, 42, 4, 875);
|
||||
add_location(p1, file$l, 49, 4, 959);
|
||||
attr_dev(div0, "class", "__show_if_only_child");
|
||||
add_location(div0, file$l, 41, 3, 836);
|
||||
add_location(div0, file$l, 48, 3, 920);
|
||||
attr_dev(div1, "class", "room_users");
|
||||
add_location(div1, file$l, 40, 2, 808);
|
||||
add_location(div1, file$l, 47, 2, 892);
|
||||
attr_dev(div2, "class", "panel panel_room_set __border_top");
|
||||
add_location(div2, file$l, 35, 1, 666);
|
||||
add_location(div2, file$l, 42, 1, 766);
|
||||
attr_dev(div3, "class", "grid_box svelte-1sv7l8q");
|
||||
add_location(div3, file$l, 17, 0, 323);
|
||||
add_location(div3, file$l, 24, 0, 423);
|
||||
},
|
||||
l: function claim(nodes) {
|
||||
throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");
|
||||
@@ -6695,28 +6699,28 @@ var app = (function () {
|
||||
if (dirty & /*back*/ 1) button0_changes.new_layout = /*back*/ ctx[0];
|
||||
if (dirty & /*back*/ 1) button0_changes.my_title = "go back " + /*back*/ ctx[0];
|
||||
|
||||
if (dirty & /*$$scope*/ 64) {
|
||||
if (dirty & /*$$scope*/ 128) {
|
||||
button0_changes.$$scope = { dirty, ctx };
|
||||
}
|
||||
|
||||
button0.$set(button0_changes);
|
||||
const button1_changes = {};
|
||||
|
||||
if (dirty & /*$$scope, $current_room_name*/ 66) {
|
||||
if (dirty & /*$$scope, $current_room_name*/ 130) {
|
||||
button1_changes.$$scope = { dirty, ctx };
|
||||
}
|
||||
|
||||
button1.$set(button1_changes);
|
||||
const button2_changes = {};
|
||||
|
||||
if (dirty & /*$$scope*/ 64) {
|
||||
if (dirty & /*$$scope*/ 128) {
|
||||
button2_changes.$$scope = { dirty, ctx };
|
||||
}
|
||||
|
||||
button2.$set(button2_changes);
|
||||
const button3_changes = {};
|
||||
|
||||
if (dirty & /*$$scope*/ 64) {
|
||||
if (dirty & /*$$scope*/ 128) {
|
||||
button3_changes.$$scope = { dirty, ctx };
|
||||
}
|
||||
|
||||
@@ -6769,7 +6773,7 @@ var app = (function () {
|
||||
}
|
||||
|
||||
function user_profile() {
|
||||
console / log("got to user profile");
|
||||
console / log("in user_profile");
|
||||
}
|
||||
|
||||
function instance$o($$self, $$props, $$invalidate) {
|
||||
@@ -6780,6 +6784,13 @@ var app = (function () {
|
||||
validate_slots('Layout_room_set', slots, []);
|
||||
let { back = "" } = $$props;
|
||||
let users = get_room_users();
|
||||
|
||||
function leave_room() {
|
||||
console.log("in leave_room");
|
||||
user_leave_room();
|
||||
layout.set("home");
|
||||
}
|
||||
|
||||
const writable_props = ['back'];
|
||||
|
||||
Object.keys($$props).forEach(key => {
|
||||
@@ -6794,23 +6805,25 @@ var app = (function () {
|
||||
layout,
|
||||
current_room_name,
|
||||
get_room_users,
|
||||
user_leave_room,
|
||||
Button: Element_button,
|
||||
back,
|
||||
users,
|
||||
user_profile,
|
||||
leave_room,
|
||||
$current_room_name
|
||||
});
|
||||
|
||||
$$self.$inject_state = $$props => {
|
||||
if ('back' in $$props) $$invalidate(0, back = $$props.back);
|
||||
if ('users' in $$props) $$invalidate(2, users = $$props.users);
|
||||
if ('users' in $$props) $$invalidate(3, users = $$props.users);
|
||||
};
|
||||
|
||||
if ($$props && "$$inject" in $$props) {
|
||||
$$self.$inject_state($$props.$$inject);
|
||||
}
|
||||
|
||||
return [back, $current_room_name, users];
|
||||
return [back, $current_room_name, leave_room, users];
|
||||
}
|
||||
|
||||
class Layout_room_set extends SvelteComponentDev {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,16 +1,23 @@
|
||||
<script>
|
||||
|
||||
import { layout, current_room_name } from './Store_chat';
|
||||
import { get_room_users } from './Request_rooms';
|
||||
import { get_room_users, user_leave_room } from './Request_rooms';
|
||||
import Button from './Element_button.svelte';
|
||||
|
||||
export let back = "";
|
||||
|
||||
let users = get_room_users($current_room_name);
|
||||
let users = get_room_users();
|
||||
|
||||
function user_profile()
|
||||
{
|
||||
console/log("got to user profile");
|
||||
console/log("in user_profile");
|
||||
}
|
||||
|
||||
function leave_room()
|
||||
{
|
||||
console.log("in leave_room");
|
||||
user_leave_room();
|
||||
layout.set("home");
|
||||
}
|
||||
|
||||
</script>
|
||||
@@ -34,7 +41,7 @@
|
||||
|
||||
<!-- panel_room_set -->
|
||||
<div class="panel panel_room_set __border_top">
|
||||
<Button new_layout="create" my_class="create">
|
||||
<Button on_click={leave_room}>
|
||||
leave
|
||||
</Button>
|
||||
<p>room users :</p>
|
||||
|
||||
@@ -90,7 +90,6 @@ export async function get_my_rooms()
|
||||
{
|
||||
console.log("in get_my_rooms");
|
||||
|
||||
// ask api for the rooms
|
||||
const response = await fetch('/api/v2/chat/myrooms');
|
||||
const data = await response.json();
|
||||
|
||||
@@ -106,7 +105,6 @@ export async function get_all_rooms()
|
||||
{
|
||||
console.log("in get_all_rooms");
|
||||
|
||||
// ask api for the rooms
|
||||
const response = await fetch('/api/v2/chat/allrooms');
|
||||
const data = await response.json();
|
||||
|
||||
@@ -122,7 +120,6 @@ export async function get_room_users()
|
||||
{
|
||||
console.log("in get_room_users");
|
||||
|
||||
// ask api for the rooms
|
||||
const response = await fetch('/api/v2/chat/roomusers');
|
||||
const data = await response.json();
|
||||
|
||||
@@ -134,4 +131,16 @@ export async function get_room_users()
|
||||
return users;
|
||||
}
|
||||
|
||||
export async function user_leave_room()
|
||||
{
|
||||
console.log("in leave_room");
|
||||
|
||||
const response = await fetch('/api/v2/chat/removeuser', {
|
||||
method: 'DELETE',
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
console.log("data", data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user