interaction blocked users and view profile ok, still block message todo

This commit is contained in:
simplonco
2023-01-16 13:35:15 +01:00
parent 2a4e076f3f
commit 20ea0bcef5
12 changed files with 240 additions and 81 deletions

View File

@@ -116,7 +116,7 @@ export class ChatController {
const room_db = await this.chatService.getRoomByName(room_name, fields);
const is_admin = room_db.admins.includes(req.user.username);
res.status(HttpStatus.OK).json({ is_admin: is_admin });
res.status(HttpStatus.OK).json({ condition: is_admin });
printCaller("- out ");
}
@@ -418,6 +418,7 @@ export class ChatController {
const room = await this.chatService.getRoomByName(room_name);
const users = room.users;
const admins = room.admins;
const blocked = await this.chatService.getListBlockUser(req.user.username);
let index = users.indexOf(req.user.username);
if (index > -1)
@@ -429,9 +430,13 @@ export class ChatController {
{
name: username,
isadmin: false,
isblocked: false,
};
if (admins.includes(username))
new_user.isadmin = true;
if (blocked.includes(username))
new_user.isblocked = true;
console.log("new_user:", new_user);
return new_user;
});
@@ -532,7 +537,8 @@ export class ChatController {
{
printCaller("- in ");
await this.chatService.setBlockUser(req.user.username, username);
await this.chatService.addBlockUser(req.user.username, username);
let user_socket: socketDto = this.chatGateway.sockets.get(req.user.username);
user_socket.join(`${username}_not_emit`);
@@ -547,6 +553,7 @@ export class ChatController {
{
printCaller("- in ");
await this.chatService.removeBlockUser(req.user.username, username);
let user_socket: socketDto = this.chatGateway.sockets.get(req.user.username);
user_socket.leave(`${username}_not_emit`);
@@ -554,5 +561,26 @@ export class ChatController {
printCaller("- out ");
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('listblock')
async listBlockUser(@Req() req, @Res() res): Promise<void>
{
printCaller("- in ");
let block_users = await this.chatService.getListBlockUser(req.user.username);
let users = block_users.map(user =>
{
return {
name: user,
isadmin: false,
isblocked: true,
}
});
res.status(HttpStatus.OK).json({ users: users });
printCaller("- out ");
}
}

View File

@@ -3,14 +3,17 @@ import { ChatController } from './chat.controller';
import { ChatService } from './chat.service';
import { ChatGateway } from './chat.gateway';
import { UsersModule } from 'src/users/users.module';
import { FriendshipsModule } from 'src/friendship/friendships.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Chatroom } from './entities/chatroom.entity';
import { User } from 'src/users/entities/user.entity';
import { Friendship } from 'src/friendship/entities/friendship.entity';
@Module({
imports: [
TypeOrmModule.forFeature([Chatroom, User]),
TypeOrmModule.forFeature([Chatroom, User, Friendship]),
UsersModule,
FriendshipsModule,
],
controllers: [
ChatController,

View File

@@ -1,8 +1,11 @@
import { HttpException, HttpStatus, Injectable, Res } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
import { Friendship, FriendshipStatus } from 'src/friendship/entities/friendship.entity';
import { Chatroom } from './entities/chatroom.entity';
import { Repository } from 'typeorm';
import { UsersService } from 'src/users/users.service';
import { FriendshipService } from 'src/friendship/friendship.service';
import { SendableFriendship } from 'src/friendship/sendableFriendship';
import { Repository, Brackets } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { roomDto } from './dto/room.dto';
import { messagesDto } from './dto/messages.dto';
@@ -17,10 +20,13 @@ export class ChatService {
constructor(
private usersService: UsersService,
private friendshipService: FriendshipService,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
@InjectRepository(Chatroom)
private readonly chatroomRepository: Repository<Chatroom>,
@InjectRepository(Friendship)
private readonly friendshipRepository: Repository<Friendship>,
) {}
// temp for test
@@ -298,14 +304,111 @@ export class ChatService {
printCaller("-- out ");
}
async setBlockUser(username: string, to_block_username: string): Promise<void>
async findOneRelationshipByUsername(friendUsername : string, username : string)
{
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where
(
new Brackets((qb) =>
{
qb.where
(
new Brackets((subAQb) =>
{
subAQb.where('sender.username = :username', {username : username})
.andWhere('receiver.username = :friendUsername', {friendUsername : friendUsername})
})
)
.orWhere
(
new Brackets((subBQb) =>
{
subBQb.where('sender.username = :friendUsername2', {friendUsername2 : friendUsername})
.andWhere('receiver.username = :username2', {username2 : username})
.andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
})
)
}),
)
.getOne()
if (!friendship)
return null;
return new SendableFriendship(friendship);
}
async addBlockUser(username: string, to_block_username: string): Promise<void>
{
printCaller("-- in ");
let user = await this.getUserByName(username);
let relation = await this.findOneRelationshipByUsername(to_block_username, username);
if (relation)
{
let blocked_friendship = await this.friendshipService.blockFriendship(relation.id, user);
}
else
{
const newFriendshipDto = {"receiverUsername": to_block_username, "status": FriendshipStatus.BLOCKED};
await this.friendshipService.create(newFriendshipDto, user);
}
printCaller("-- out ");
}
async removeBlockUser(username: string, to_unblock_username: string): Promise<void>
{
printCaller("-- in ");
let user = await this.getUserByName(username);
let relation = await this.findOneRelationshipByUsername(to_unblock_username, username);
await this.friendshipService.removeFriendship(relation.id, user);
printCaller("-- out ");
}
async getListBlockUser(username: string): Promise<string[]>
{
printCaller("-- in ");
let user = await this.getUserByName(username);
let friends_users = await this.friendshipService.findAllBlockedFriends(user.id);
let users = friends_users.map(user => user.receiverUsername);
console.log(users);
printCaller("-- out ");
return users;
}
/*
// find list blocked
findAllBlockedFriends
extract the receiver_username (i am the sender)
// bock a user
findfriendshipbyname
return friendship
blockfriendship
return nothing
createfriendship
return friendship (it worked)
return http exception (cannot create friendship)
// unblock
findfriendshipbyname
return friendship
removefriendship
return nothing
*/
/* ADDERS *************************************************
*/

View File

@@ -57,7 +57,4 @@ export class User {
@Column({ nullable: true })
currentRoom: string; // chatroom name
@Column("simple-array", { nullable: true })
blockedUsers: string[]; // usernames
}