Changements amitiés
This commit is contained in:
@@ -1,11 +1,10 @@
|
|||||||
import { IsEnum, IsString } from 'class-validator';
|
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
||||||
import { FriendshipStatus } from '../entities/friendship.entity';
|
import { FriendshipStatus } from '../entities/friendship.entity';
|
||||||
|
|
||||||
export class CreateFriendshipDto {
|
export class CreateFriendshipDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
readonly requesterUsername: string;
|
@IsNotEmpty()
|
||||||
@IsString()
|
readonly receiverUsername: string;
|
||||||
readonly addresseeUsername: string;
|
|
||||||
@IsEnum(FriendshipStatus)
|
@IsEnum(FriendshipStatus)
|
||||||
readonly status: FriendshipStatus;
|
readonly status: FriendshipStatus;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,17 @@ export class Friendship {
|
|||||||
@CreateDateColumn()
|
@CreateDateColumn()
|
||||||
date : Date;
|
date : Date;
|
||||||
|
|
||||||
@Column()
|
|
||||||
@ManyToOne(type => User, user => user.username)
|
@ManyToOne(type => User, user => user.username)
|
||||||
requesterUsername: string;
|
sender: User;
|
||||||
|
|
||||||
|
@ManyToOne(type => User, user => user.username)
|
||||||
|
receiver: User;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
@ManyToOne(type => User, user => user.username)
|
senderUsername : string;
|
||||||
addresseeUsername: string;
|
|
||||||
|
@Column()
|
||||||
|
receiverUsername : string;
|
||||||
|
|
||||||
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
|
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
|
||||||
status: FriendshipStatus;
|
status: FriendshipStatus;
|
||||||
|
|||||||
@@ -12,59 +12,20 @@ export class FriendshipController {
|
|||||||
@Get('myfriends')
|
@Get('myfriends')
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
findEmpty(@Query('username') username: string, @Req() req) {
|
findEmpty(@Req() req) {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
if (username === undefined) {
|
return this.friendshipService.findAllFriends(user.id);
|
||||||
console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE 11111");
|
|
||||||
return this.friendshipService.findAllFriends(user.id);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET http://transcendance:8080/api/v2/network/myfriends/relationshipId
|
// GET http://transcendance:8080/api/v2/network/myfriends/relationshipId
|
||||||
@Get('myfriends/:relationshipId')
|
@Get('myfriend/:relationshipId')
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
findOneFriend(@Param('relationshipId') relationshipId: string, @Req() req) {
|
findOneFriend(@Param('relationshipId') relationshipId: string, @Req() req) {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
return this.friendshipService.findOneFriend(relationshipId, user.id);
|
return this.friendshipService.findOneFriend(relationshipId, user.username);
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST http://transcendance:8080/api/v2/network/myfriends
|
|
||||||
@Post('myfriends')
|
|
||||||
@HttpCode(HttpStatus.CREATED)
|
|
||||||
@UseGuards(AuthenticateGuard)
|
|
||||||
@UseGuards(TwoFactorGuard)
|
|
||||||
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
|
|
||||||
const user = req.user;
|
|
||||||
|
|
||||||
console.log(`User id: ${user.id}\nUser name: ${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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// PATCH http://transcendance:8080/api/v2/network/myfriends/relationshipId?status=A
|
|
||||||
@Patch('myfriends/:relationshipId')
|
|
||||||
@UseGuards(AuthenticateGuard)
|
|
||||||
@UseGuards(TwoFactorGuard)
|
|
||||||
update(@Param('relationshipId') relationshipId: string, @Query('status') status : string, @Req() req)
|
|
||||||
{
|
|
||||||
const user : User = req.user;
|
|
||||||
return this.friendshipService.updateFriendship(relationshipId, user, status);
|
|
||||||
}
|
|
||||||
|
|
||||||
// DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId
|
|
||||||
@Delete('myfriends/:relationshipId')
|
|
||||||
@UseGuards(AuthenticateGuard)
|
|
||||||
@UseGuards(TwoFactorGuard)
|
|
||||||
remove(@Param('relationshipId') relationshipId: string) {
|
|
||||||
return this.friendshipService.removeFriendship(relationshipId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// GET http://transcendance:8080/api/v2/network/blocked
|
// GET http://transcendance:8080/api/v2/network/blocked
|
||||||
@Get('blocked')
|
@Get('blocked')
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@@ -74,15 +35,69 @@ export class FriendshipController {
|
|||||||
return this.friendshipService.findAllBlockedFriends(user.username);
|
return this.friendshipService.findAllBlockedFriends(user.username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('blocked/:relationshipId')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
findOneBlocked(@Param('relationshipId') relationshipId: string, @Req() req) {
|
||||||
|
const user = req.user;
|
||||||
|
return this.friendshipService.findOneBlocked(relationshipId, user.username);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST http://transcendance:8080/api/v2/network/myfriends
|
||||||
|
@Post('myfriends')
|
||||||
|
@HttpCode(HttpStatus.CREATED)
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
|
||||||
|
const user = req.user;
|
||||||
|
if (user.username !== createFriendshipDto.receiverUsername)
|
||||||
|
return this.friendshipService.create(createFriendshipDto, user);
|
||||||
|
return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PATCH http://transcendance:8080/api/v2/network/myfriends/relationshipId/accept
|
||||||
|
@Patch('myfriends/:relationshipId/accept')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
updateAccept(@Param('relationshipId') relationshipId: string, @Req() req)
|
||||||
|
{
|
||||||
|
const user : User = req.user;
|
||||||
|
return this.friendshipService.acceptFriendship(relationshipId, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch('myfriends/:relationshipId/decline')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
updateDecline(@Param('relationshipId') relationshipId: string, @Req() req)
|
||||||
|
{
|
||||||
|
const user : User = req.user;
|
||||||
|
return this.friendshipService.declineFriendship(relationshipId, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch('myfriends/:relationshipId/block')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
updateBlock(@Param('relationshipId') relationshipId: string, @Req() req)
|
||||||
|
{
|
||||||
|
const user : User = req.user;
|
||||||
|
return this.friendshipService.blockFriendship(relationshipId, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId
|
||||||
|
@Delete(':relationshipId')
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
remove(@Param('relationshipId') relationshipId: string, @Req() req) {
|
||||||
|
const user : User = req.user;
|
||||||
|
return this.friendshipService.removeFriendship(relationshipId, user);
|
||||||
|
}
|
||||||
|
|
||||||
// GET http://transcendance:8080/api/v2/network/pending
|
// GET http://transcendance:8080/api/v2/network/pending
|
||||||
@Get('pending')
|
@Get('pending')
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
findAllPendantFriendshipRequested(@Req() req) {
|
findAllPendantFriendshipRequested(@Req() req) {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
// console.log("WHAT IS UP MY GUYS IT IS YOUR BOI THAT IDIOT YOU HATE Pending 33333");
|
|
||||||
|
|
||||||
return this.friendshipService.findAllPendantRequestsForFriendship(user.id);
|
return this.friendshipService.findAllPendantRequestsForFriendship(user.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,23 +17,16 @@ export class FriendshipService {
|
|||||||
|
|
||||||
|
|
||||||
async findOneFriend(friendshipId: string, username: string) {
|
async findOneFriend(friendshipId: string, username: string) {
|
||||||
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } });
|
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.ACCEPTED } });
|
||||||
if (!friendship)
|
if (!friendship)
|
||||||
throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
|
||||||
return friendship;
|
return friendship;
|
||||||
}
|
}
|
||||||
|
|
||||||
// async findOneFriendByUsername(friendUsername: string, username: string) {
|
async findOneBlocked(friendshipId: string, username: string) {
|
||||||
// const friendship = await this.friendshipRepository.find({ where: { friendUsername: +friendshipId, requesterUsername: username, status: FriendshipStatus.ACCEPTED } });
|
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.BLOCKED } });
|
||||||
// if (!friendship)
|
|
||||||
// throw new HttpException(`The requested friend not found.`, HttpStatus.NOT_FOUND);
|
|
||||||
// return friendship;
|
|
||||||
// }
|
|
||||||
|
|
||||||
async findOneBlocked(friendshipId: string) {
|
|
||||||
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, status: FriendshipStatus.BLOCKED } });
|
|
||||||
if (!friendship)
|
if (!friendship)
|
||||||
throw new HttpException(`The requested user not found.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND);
|
||||||
return friendship;
|
return friendship;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,8 +34,8 @@ export class FriendshipService {
|
|||||||
const friendship = await this.friendshipRepository
|
const friendship = await this.friendshipRepository
|
||||||
.createQueryBuilder('friendship')
|
.createQueryBuilder('friendship')
|
||||||
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
||||||
.andWhere('friendship.addresseeUsername = :addressee', { addressee: username })
|
.andWhere('friendship.receiverUsername = :addressee', { addressee: username })
|
||||||
.orWhere('friendship.requesterUsername = :requester', { requester: username })
|
.orWhere('friendship.senderUsername = :requester', { requester: username })
|
||||||
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
||||||
.getMany();
|
.getMany();
|
||||||
for (const friend of friendship)
|
for (const friend of friendship)
|
||||||
@@ -51,49 +44,53 @@ export class FriendshipService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findAllBlockedFriends(username: string) {
|
async findAllBlockedFriends(username: string) {
|
||||||
return await this.friendshipRepository
|
const friendships : Friendship[] = await this.friendshipRepository
|
||||||
.createQueryBuilder('friendship')
|
.createQueryBuilder('friendship')
|
||||||
.where('friendship.requesterUsername = :requestee', { requestee: username })
|
.where('friendship.senderUsername = :requestee', { requestee: username })
|
||||||
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
||||||
.getMany();
|
.getMany();
|
||||||
|
let partialFriendship : Partial<Friendship>[] = [];
|
||||||
|
for (const friendship of friendships) {
|
||||||
|
partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status});
|
||||||
|
}
|
||||||
|
return partialFriendship;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAllPendantRequestsForFriendship(username: string) {
|
async findAllPendantRequestsForFriendship(username: string) {
|
||||||
return await this.friendshipRepository
|
const friendship = await this.friendshipRepository
|
||||||
.createQueryBuilder('friendship')
|
.createQueryBuilder('friendship')
|
||||||
.where('friendship.requesterUsername = :requestee', { requestee: username })
|
.where('friendship.senderUsername = :requestee', { requestee: username })
|
||||||
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
||||||
.getMany();
|
.getMany();
|
||||||
|
let partialFriendship : Partial<Friendship>[] = [];
|
||||||
|
for (const friend of friendship) {
|
||||||
|
partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status});
|
||||||
|
}
|
||||||
|
return partialFriendship;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAllReceivedRequestsForFriendship(username: string) {
|
async findAllReceivedRequestsForFriendship(username: string) {
|
||||||
return await this.friendshipRepository
|
const friendship = await this.friendshipRepository
|
||||||
.createQueryBuilder('friendship')
|
.createQueryBuilder('friendship')
|
||||||
.where('friendship.addresseeUsername = :addressee', { addressee: username })
|
.where('friendship.receiverUsername = :addressee', { addressee: username })
|
||||||
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
||||||
.getMany();
|
.getMany();
|
||||||
|
let partialFriendship : Partial<Friendship>[] = [];
|
||||||
|
for (const friend of friendship) {
|
||||||
|
partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status});
|
||||||
|
}
|
||||||
|
return partialFriendship;
|
||||||
}
|
}
|
||||||
//GROS CHANTIER
|
|
||||||
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
|
|
||||||
const addressee = await this.userRepository.findOneBy({username: createFriendshipDto.addresseeUsername});
|
|
||||||
|
|
||||||
console.log('made it to create friendship from friendship controller')
|
async create(createFriendshipDto: CreateFriendshipDto, creator : User) : Promise <Partial<Friendship>> {
|
||||||
|
console.log("DTO : \n")
|
||||||
if (!addressee)
|
console.log({...createFriendshipDto})
|
||||||
|
const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername});
|
||||||
|
if (!receiver)
|
||||||
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
|
||||||
console.log('Addressee ID: ' + addressee.id + ' Username: ' + addressee.username)
|
|
||||||
console.log('Creator ID: ' + creator.id + ' Username: ' + creator.username)
|
|
||||||
if (creator.id === addressee.id)
|
|
||||||
throw new HttpException(`You can't add yourself.`, HttpStatus.FORBIDDEN);
|
|
||||||
console.log('test 1')
|
|
||||||
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
|
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
|
||||||
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
|
||||||
console.log('test 2')
|
const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver });
|
||||||
|
|
||||||
const friendship = await this.friendshipRepository.findOneBy({ requesterUsername: createFriendshipDto.requesterUsername, addresseeUsername: createFriendshipDto.addresseeUsername });
|
|
||||||
|
|
||||||
console.log('test 3')
|
|
||||||
|
|
||||||
if (friendship) {
|
if (friendship) {
|
||||||
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
|
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
|
||||||
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
|
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
|
||||||
@@ -104,57 +101,95 @@ export class FriendshipService {
|
|||||||
else if (friendship.status && friendship.status === FriendshipStatus.DECLINED)
|
else if (friendship.status && friendship.status === FriendshipStatus.DECLINED)
|
||||||
throw new HttpException(`The request has been declined.`, HttpStatus.OK);
|
throw new HttpException(`The request has been declined.`, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
console.log('test 7')
|
const newFriendship = new Friendship();
|
||||||
|
newFriendship.sender = creator;
|
||||||
const newFriendship = this.friendshipRepository.create(createFriendshipDto);
|
newFriendship.senderUsername = creator.username;
|
||||||
console.log('test 8, New Friendship: ')
|
newFriendship.receiver = receiver;
|
||||||
console.log({...newFriendship})
|
newFriendship.receiverUsername = receiver.username;
|
||||||
|
newFriendship.status = createFriendshipDto.status;
|
||||||
const tmp = this.friendshipRepository.save(newFriendship);
|
const savedFriendship = this.friendshipRepository.save(newFriendship);
|
||||||
console.log('tmp: ')
|
const partialFriendship : Partial<Friendship> = {
|
||||||
console.log({...tmp})
|
id : (await savedFriendship).id,
|
||||||
return tmp;
|
date : (await savedFriendship).date,
|
||||||
// return this.friendshipRepository.save(newFriendship);
|
receiverUsername: (await savedFriendship).receiverUsername,
|
||||||
|
status : (await savedFriendship).status
|
||||||
|
}
|
||||||
|
console.log({...partialFriendship})
|
||||||
|
return partialFriendship;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateFriendship(relationshipId: string, user: User, status: string) {
|
async acceptFriendship(relationshipId: string, user: User) {
|
||||||
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
||||||
if (!relation)
|
if (!relation)
|
||||||
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
||||||
if (+relation.requesterUsername === user.id) {
|
if (relation.sender.id === user.id) {
|
||||||
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
if (status === FriendshipStatus.ACCEPTED)
|
relation.status = FriendshipStatus.ACCEPTED;
|
||||||
relation.status = FriendshipStatus.ACCEPTED;
|
const savedFriendship = this.friendshipRepository.save(relation);
|
||||||
else if (status === FriendshipStatus.DECLINED)
|
const partialFriendship : Partial<Friendship> = {
|
||||||
relation.status = FriendshipStatus.DECLINED;
|
id : (await savedFriendship).id,
|
||||||
else if (status === FriendshipStatus.BLOCKED)
|
date : (await savedFriendship).date,
|
||||||
relation.status = FriendshipStatus.BLOCKED;
|
receiverUsername: (await savedFriendship).receiverUsername,
|
||||||
else
|
status : (await savedFriendship).status
|
||||||
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
|
}
|
||||||
if (relation.status !== status)
|
return partialFriendship;
|
||||||
throw new HttpException(`We could not update the status.`, HttpStatus.OK);
|
|
||||||
return this.friendshipRepository.save(relation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async removeFriendship(relationshipId: string) {
|
async declineFriendship(relationshipId: string, user: User) {
|
||||||
|
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
||||||
|
if (!relation)
|
||||||
|
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
||||||
|
if (relation.sender.id === user.id) {
|
||||||
|
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
relation.status = FriendshipStatus.DECLINED;
|
||||||
|
const savedFriendship = this.friendshipRepository.save(relation);
|
||||||
|
const partialFriendship : Partial<Friendship> = {
|
||||||
|
id : (await savedFriendship).id,
|
||||||
|
date : (await savedFriendship).date,
|
||||||
|
receiverUsername: (await savedFriendship).receiverUsername,
|
||||||
|
status : (await savedFriendship).status
|
||||||
|
}
|
||||||
|
return partialFriendship
|
||||||
|
}
|
||||||
|
|
||||||
|
async blockFriendship(relationshipId: string, user: User) {
|
||||||
|
const relation = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
||||||
|
if (!relation)
|
||||||
|
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
||||||
|
if (relation.sender.id === user.id) {
|
||||||
|
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
relation.status = FriendshipStatus.BLOCKED;
|
||||||
|
const savedFriendship = this.friendshipRepository.save(relation);
|
||||||
|
const partialFriendship : Partial<Friendship> = {
|
||||||
|
id : (await savedFriendship).id,
|
||||||
|
date : (await savedFriendship).date,
|
||||||
|
receiverUsername: (await savedFriendship).receiverUsername,
|
||||||
|
status : (await savedFriendship).status
|
||||||
|
}
|
||||||
|
return partialFriendship
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeFriendship(relationshipId: string, user : User) {
|
||||||
const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId });
|
||||||
if (!friendship)
|
if (!friendship)
|
||||||
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
|
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
|
||||||
|
if (friendship.sender.id !== user.id || friendship.receiver.id !== user.id) {
|
||||||
|
throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN);
|
||||||
|
}
|
||||||
return this.friendshipRepository.remove(friendship);
|
return this.friendshipRepository.remove(friendship);
|
||||||
}
|
}
|
||||||
|
|
||||||
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) {
|
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) {
|
||||||
console.log("finding if user is blocked")
|
console.log("finding if user is blocked")
|
||||||
const friendship = await this.friendshipRepository
|
const friendship = await this.friendshipRepository
|
||||||
.createQueryBuilder('friendship')
|
.createQueryBuilder('friendship')
|
||||||
.where('friendship.requesterUsername = :requestee', { requestee: userConnectedId })
|
.where('friendship.senderUsername = :requestee', { requestee: userConnectedId })
|
||||||
.orWhere('friendship.requesterUsername = :requesteeBis', { requesteeBis: userToCheckId })
|
.orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId })
|
||||||
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
||||||
.getOne();
|
.getOne();
|
||||||
console.log("printing friendship")
|
|
||||||
console.log(friendship)
|
|
||||||
// console.log({...friendship})
|
|
||||||
if (friendship) {
|
if (friendship) {
|
||||||
console.log('we are blocked in friendship.service')
|
console.log('we are blocked in friendship.service')
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -43,13 +43,11 @@ export class User {
|
|||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
secretTwoFactorAuth: string;
|
secretTwoFactorAuth: string;
|
||||||
|
|
||||||
@JoinTable()
|
@OneToMany(type => Friendship , (friendship) => friendship.sender)
|
||||||
@OneToMany(type => Friendship , (friendship) => friendship.requesterUsername)
|
sentFriendRequest: Friendship[];
|
||||||
requesterId: Friendship[];
|
|
||||||
|
|
||||||
@JoinTable()
|
@OneToMany(type => Friendship , (friendship) => friendship.receiver)
|
||||||
@OneToMany(type => Friendship , (friendship) => friendship.addresseeUsername)
|
receivedFriendRequest: Friendship[];
|
||||||
addresseeId: Friendship[];
|
|
||||||
|
|
||||||
@JoinColumn()
|
@JoinColumn()
|
||||||
@OneToOne(() => UserStats, { cascade: true })
|
@OneToOne(() => UserStats, { cascade: true })
|
||||||
|
|||||||
@@ -60,31 +60,26 @@ export class UsersService {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOneByUsername(userConnectedId : string, username: string) {
|
async findOneByUsername(userConnectedId : string, usernameToFind: string) {
|
||||||
const user : User = await this.userRepository.createQueryBuilder('user')
|
const userToFind : User = await this.userRepository.createQueryBuilder('user')
|
||||||
.leftJoinAndSelect('user.stats', 'stats')
|
.leftJoinAndSelect('user.stats', 'stats')
|
||||||
.where('user.username = :username', { username: username })
|
.where('user.username = :username', { username: usernameToFind })
|
||||||
.getOne();
|
.getOne();
|
||||||
console.log('USERNAME OF FOUND USER : ' + user.username);
|
console.log('USERNAME OF FOUND USER : ' + userToFind.username);
|
||||||
// console.log({...user})
|
// console.log({...user})
|
||||||
// you can't do that, you need to do user === undefined
|
// you can't do that, you need to do user === undefined
|
||||||
// if (user === undefined)
|
// if (user === undefined)
|
||||||
if (!user)
|
if (!userToFind)
|
||||||
throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND);
|
throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND);
|
||||||
|
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id.toString())) {
|
||||||
let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString())
|
|
||||||
console.log('printing return of checking if blocked')
|
|
||||||
console.log(tmp)
|
|
||||||
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString())) {
|
|
||||||
console.log('we are blocked in user.service')
|
console.log('we are blocked in user.service')
|
||||||
throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND);
|
throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
const partialUser : Partial<User> = {
|
const partialUser : Partial<User> = {
|
||||||
username: user.username,
|
username: userToFind.username,
|
||||||
image_url: user.image_url,
|
image_url: userToFind.image_url,
|
||||||
status: user.status,
|
status: userToFind.status,
|
||||||
stats: user.stats,
|
stats: userToFind.stats,
|
||||||
};
|
};
|
||||||
return partialUser;
|
return partialUser;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,8 +110,7 @@ could be a list of friends and if they're active but i can't see that yet
|
|||||||
method : "POST",
|
method : "POST",
|
||||||
headers: { 'Content-Type': 'application/json'},
|
headers: { 'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
"requesterUsername": user.username,
|
"receiverUsername": set.friendUsername,
|
||||||
"addresseeUsername": set.friendUsername,
|
|
||||||
"status": "R"
|
"status": "R"
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user