this merge with master should come with the right backend stuff to make friendship requests work

This commit is contained in:
Me
2022-12-09 15:46:40 +01:00
94 changed files with 694 additions and 162 deletions

View File

@@ -3,9 +3,9 @@ import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto {
@IsString()
readonly requesterId: string;
readonly requesterUsername: string;
@IsString()
readonly addresseeId: string;
readonly addresseeUsername: string;
@IsEnum(FriendshipStatus)
readonly status: FriendshipStatus;
}

View File

@@ -18,12 +18,12 @@ export class Friendship {
date : Date;
@Column()
@ManyToOne(type => User, user => user.requesterId)
requesterId: string;
@ManyToOne(type => User, user => user.username)
requesterUsername: string;
@Column()
@ManyToOne(type => User, user => user.addresseeId)
addresseeId: string;
@ManyToOne(type => User, user => user.username)
addresseeUsername: string;
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
status: FriendshipStatus;

View File

@@ -36,14 +36,8 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
const user = req.user;
console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE 22222");
// console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterId}\nStatus: ${createFriendshipDto.status}`);
// console.log(`My User id: ${createFriendshipDto.requesterId}`)
console.log(`My User id: ${user.id}`)
console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.addresseeId}\nStatus: ${createFriendshipDto.status}`);
if (user.id === +createFriendshipDto.requesterId)
console.log(`User id: ${user.id}\nFriend id: ${createFriendshipDto.requesterUsername}\nStatus: ${createFriendshipDto.status}`);
if (user.username === createFriendshipDto.requesterUsername)
return this.friendshipService.create(createFriendshipDto, user);
return new HttpException('You can\'t request a frienship for another user', HttpStatus.FORBIDDEN);
}
@@ -73,7 +67,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
findAllBlocked(@Req() req) {
const user = req.user;
return this.friendshipService.findAllBlockedFriends(user.id);
return this.friendshipService.findAllBlockedFriends(user.username);
}
// GET http://transcendance:8080/api/v2/network/pending

View File

@@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/users/entities/user.entity';
import { Repository } from 'typeorm';
import { CreateFriendshipDto } from './dto/create-friendship.dto';
import { UpdateFriendshipDto } from './dto/update-friendship.dto';
import { Friendship, FriendshipStatus } from './entities/friendship.entity';
@Injectable()
@@ -17,8 +16,8 @@ export class FriendshipService {
) { }
async findOneFriend(friendshipId: string, userId: string) {
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterId: userId, status: FriendshipStatus.ACCEPTED } });
async findOneFriend(friendshipId: string, username: string) {
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } });
if (!friendship)
throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
return friendship;
@@ -31,12 +30,12 @@ export class FriendshipService {
return friendship;
}
async findAllFriends(userId: string) {
async findAllFriends(username: string) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
.andWhere('friendship.addresseeId = :addressee', { addressee: userId })
.orWhere('friendship.requesterId = :requester', { requester: userId })
.andWhere('friendship.addresseeUsername = :addressee', { addressee: username })
.orWhere('friendship.requesterUsername = :requester', { requester: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
.getMany();
for (const friend of friendship)
@@ -44,40 +43,39 @@ export class FriendshipService {
return friendship;
}
async findAllBlockedFriends(userId: string) {
async findAllBlockedFriends(username: string) {
return await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userId })
.orWhere('friendship.addresseeId = :addressee', { addressee: userId })
.where('friendship.requesterUsername = :requestee', { requestee: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getMany();
}
async findAllPendantRequestsForFriendship(userId: string) {
async findAllPendantRequestsForFriendship(username: string) {
return await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userId })
.where('friendship.requesterUsername = :requestee', { requestee: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany();
}
async findAllReceivedRequestsForFriendship(userId: string) {
async findAllReceivedRequestsForFriendship(username: string) {
return await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.addresseeId = :addressee', { addressee: userId })
.where('friendship.addresseeUsername = :addressee', { addressee: username })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany();
}
//GROS CHANTIER
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
const addressee = await this.userRepository.findOneBy({ id: +createFriendshipDto.addresseeId });
const addressee = await this.userRepository.findOneBy({username: createFriendshipDto.requesterUsername});
if (!addressee)
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
if (creator.id === addressee.id)
throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN);
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
const friendship = await this.friendshipRepository.findOneBy({ requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId });
const friendship = await this.friendshipRepository.findOneBy({ requesterUsername: createFriendshipDto.requesterUsername, addresseeUsername: createFriendshipDto.addresseeUsername });
if (friendship) {
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
@@ -96,7 +94,7 @@ export class FriendshipService {
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (+relation.requesterId === user.id) {
if (+relation.requesterUsername === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
}
if (status === FriendshipStatus.ACCEPTED)
@@ -122,8 +120,8 @@ export class FriendshipService {
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userConnectedId })
.orWhere('friendship.requesterId = :requesteeBis', { requesteeBis: userToCheckId })
.where('friendship.requesterUsername = :requestee', { requestee: userConnectedId })
.orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getOne();
if (friendship)

View File

@@ -0,0 +1,16 @@
import { IsBoolean, IsEmail, IsNotEmpty, IsString } from 'class-validator';
export class CreateUsersDto {
@IsString()
@IsNotEmpty()
readonly username: string;
readonly fortyTwoId: string;
@IsEmail()
readonly email: string;
@IsString()
readonly image_url: string;
@IsString()
readonly status: string;
@IsBoolean()
readonly isEnabledTwoFactorAuth: boolean;
}

View File

@@ -0,0 +1,22 @@
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity('gameParty')
export class gameParty {
@PrimaryGeneratedColumn()
id: number;
@Column()
playerOne: string
@Column()
playerTwo: string
@Column()
resultOfTheMatch: string
@Column()
gameServerIdOfTheMatch: string
}

View File

@@ -44,11 +44,11 @@ export class User {
secretTwoFactorAuth: string;
@JoinTable()
@OneToMany(type => Friendship , (friendship) => friendship.requesterId)
@OneToMany(type => Friendship , (friendship) => friendship.requesterUsername)
requesterId: Friendship[];
@JoinTable()
@OneToMany(type => Friendship , (friendship) => friendship.addresseeId)
@OneToMany(type => Friendship , (friendship) => friendship.addresseeUsername)
addresseeId: Friendship[];
@JoinColumn()