ok the friendship and user refactor is done, there are still a lot of comments but that's fine, it's duct taped together, but it works, time to merge

This commit is contained in:
Me
2023-01-05 11:21:34 +01:00
parent d3be6e5fe8
commit 46090b1e33
10 changed files with 98 additions and 164 deletions

View File

@@ -1,10 +1,7 @@
import { IsEnum, IsNotEmpty, IsString, IsPositive } from 'class-validator';
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto {
// @IsPositive()
// @Max(1000) ?
// readonly receiverId: number;
@IsNotEmpty()
@IsString()
readonly receiverUsername: string;

View File

@@ -20,23 +20,9 @@ export class Friendship {
@ManyToOne(type => User, user => user.username)
sender: User;
// hold on do i want this instead?
// @ManyToOne(type => User, user => user.id)
@ManyToOne(type => User, user => user.username)
receiver: User;
// ok so the username is still here because i think it might be useful for the frontend to have access to it, but really all i need is the ID's
// since i have the ID's do i even need the User ?
// @Column()
// senderUsername : string;
// @Column()
// senderId : number;
// @Column()
// receiverUsername : string;
// @Column()
// receiverId : number;
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
status: FriendshipStatus;
}

View File

@@ -8,18 +8,7 @@ import { FriendshipService } from './friendship.service';
export class FriendshipController {
constructor(private readonly friendshipService: FriendshipService) { }
// GET http://transcendance:8080/api/v2/network/myfriends
// @Get('myfriends')
// @UseGuards(AuthenticateGuard)
// @UseGuards(TwoFactorGuard)
// findEmpty(@Req() req) {
// const user = req.user;
// console.log('GET myfriends')
// return this.friendshipService.findAllFriends(user.id);
// }
// @Query('username') username: string,
// new and improved finder of people
// GET http://transcendance:8080/api/v2/network/myfriends
@Get('myfriends')
@@ -32,23 +21,10 @@ export class FriendshipController {
console.log('my friend: ' + otherUsername)
return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username);
}
// might change this
return this.friendshipService.findAllFriends(user.id);
// return this.friendshipService.findOneRelationshipByUsername(friendUsername, user.username);
return this.friendshipService.findAllFriendships(user.id);
// return this.friendshipService.findAllFriends(user.id);
}
// GET http://transcendance:8080/api/v2/network/:friendUsername
// @Get('myfriends/:friendUsername')
// @UseGuards(AuthenticateGuard)
// @UseGuards(TwoFactorGuard)
// findOneRelationByUsername(@Param('friendUsername') friendUsername : string, @Req() req) {
// console.log('GET myfriend')
// console.log(friendUsername);
// const user = req.user;
// return this.friendshipService.findOneRelationshipByUsername(friendUsername, user.username);
// }
// POST http://transcendance:8080/api/v2/network/relations
@Post('relations')
@HttpCode(HttpStatus.CREATED)

View File

@@ -1,6 +1,7 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/users/entities/user.entity';
import { SendableUser } from 'src/users/sendableUsers';
import { Repository, Brackets } from 'typeorm';
import { CreateFriendshipDto } from './dto/create-friendship.dto';
import { Friendship, FriendshipStatus } from './entities/friendship.entity';
@@ -30,6 +31,7 @@ export class FriendshipService {
// return ret;
// };
//kinda useless but someone else might use it
async findOneRelationshipById(friendId : number, userId : number) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
@@ -45,16 +47,16 @@ export class FriendshipService {
)
.orWhere(
new Brackets((subBQb) => {
subBQb.where('sender.id = :friendId', {friendId : friendId})
.andWhere('receiver.id = :userId', {userId : userId})
subBQb.where('sender.id = :friendId2', {friendId2 : friendId})
.andWhere('receiver.id = :userId2', {userId2 : userId})
})
)
}),
)
.andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
// .andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
.getOne()
console.log('END Find one friend by username: ')
console.log('END Find one friend by ID: ')
console.log({...friendship})
if (!friendship) {
@@ -65,7 +67,6 @@ export class FriendshipService {
return new SendableFriendship(friendship);
}
// basically useless now
async findOneRelationshipByUsername(friendUsername : string, username : string) {
// This seems to work, finding friendships by username but checking the actual users not the friendship
const friendship = await this.friendshipRepository
@@ -82,13 +83,13 @@ export class FriendshipService {
)
.orWhere(
new Brackets((subBQb) => {
subBQb.where('sender.username = :friendUsername', {friendUsername : friendUsername})
.andWhere('receiver.username = :username', {username : username})
subBQb.where('sender.username = :friendUsername2', {friendUsername2 : friendUsername})
.andWhere('receiver.username = :username2', {username2 : username})
})
)
}),
)
.andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
// .andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
.getOne()
console.log('END Find one friend by username: ')
@@ -101,8 +102,9 @@ export class FriendshipService {
return new SendableFriendship(friendship);
}
// lets see what happens here, doing directly receiver.id not LeftJoinAndSelect ...
async findAllFriends(userId: number) {
// maybe i should be returning a list of users not a list of friendships
// async findAllFriends(userId: number) {
async findAllFriendships(userId: number) {
const friendships = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
@@ -115,13 +117,28 @@ export class FriendshipService {
// for (const friend of friendship)
// console.log("FRIENDSHIP : " + friend.status);
// return friendship;
// return new SendableFriendship(friendship);
let sendFrienships: SendableFriendship[] = []
let sendFrienships: SendableFriendship[] = [];
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
}
// return new SendableFriendship(friendship);
console.log('friendship.service my friends:')
console.log({...sendFrienships})
return sendFrienships;
// let sendFriends: SendableUser[] = [];
// for (const friendship of friendships) {
// let user: User;
// if (friendship.sender.id !== userId)
// user = friendship.sender;
// else
// user = friendship.receiver;
// sendFriends.push(new SendableUser(user));
// }
// console.log('friendship.service my friends as Users:')
// console.log({...sendFriends})
// return sendFriends;
}
async findOneBlocked(friendshipId: number, userId: number) {
@@ -153,7 +170,6 @@ export class FriendshipService {
if (!friendship) {
throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND);
}
// return friendship;
return new SendableFriendship(friendship);
}
@@ -191,16 +207,10 @@ export class FriendshipService {
.where('sender.id = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany();
// let partialFriendship : Partial<Friendship>[] = [];
// for (const friend of friendships) {
// console.log("FRIENDSHIP : " + friend);
// partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status});
// }
// console.log("Pendant requests : " + partialFriendship);
// return partialFriendship;
console.log('friendships services pendant friendships:')
console.log({...friendships})
// return friendships;
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
@@ -216,14 +226,10 @@ export class FriendshipService {
.where('receiver.id = :addressee', { addressee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.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;
console.log('friendship service received requests')
console.log({...friendships})
// return friendships;
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
@@ -239,8 +245,8 @@ export class FriendshipService {
// These throws don't seem to work...
const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername});
console.log('receiver: ')
console.log({...receiver})
// console.log('receiver: ')
// console.log({...receiver})
if (!receiver)
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
@@ -291,10 +297,10 @@ export class FriendshipService {
// }
// console.log('friendship.service create friendship, partial friendship')
// console.log({...partialFriendship})
console.log('friendship.service create friendship, NEW friendship')
console.log({...newFriendship})
console.log('friendship.service create friendship, SAVED friendship')
console.log({...savedFriendship})
// console.log('friendship.service create friendship, NEW friendship')
// console.log({...newFriendship})
// console.log('friendship.service create friendship, SAVED friendship')
// console.log({...savedFriendship})
// return partialFriendship;
return new SendableFriendship(savedFriendship);
}
@@ -347,15 +353,6 @@ export class FriendshipService {
}
relation.status = FriendshipStatus.DECLINED;
const savedFriendship = await this.friendshipRepository.save(relation);
// const partialFriendship : Partial<Friendship> = {
// id : (await savedFriendship).id,
// date : (await savedFriendship).date,
// receiverUsername: (await savedFriendship).receiverUsername,
// status : (await savedFriendship).status
// }
// console.log('decline friend request')
// console.log({...partialFriendship})
// return partialFriendship
return new SendableFriendship(savedFriendship);
}
@@ -403,8 +400,6 @@ export class FriendshipService {
}
async removeFriendship(relationshipId: number, user : User) {
// const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId });
// once again we need find() not findOneBy() so once again have to grab first elem of an array
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
@@ -422,12 +417,10 @@ export class FriendshipService {
}
console.log('.service deleted a friendship')
return this.friendshipRepository.remove(friendship);
// what is the return here? am i getting something right?
}
//i think maybe i should be using ID rather than username...
// async findIfUserIsBlockedOrHasBlocked(userConnectedUsername: string, userToFindUsername: string) {
// ok for some reason this isn't working...
async findIfUserIsBlockedOrHasBlocked(userConnectedId: number, userToFindId: number) {
console.log("finding if user is blocked")
console.log('user connected: ' + userConnectedId)
@@ -447,8 +440,8 @@ export class FriendshipService {
)
.orWhere(
new Brackets((subBQb) => {
subBQb.where('sender.id = :senderId', {senderId : userToFindId})
.andWhere('receiver.id = :receiverId', {receiverUsername : userConnectedId})
subBQb.where('sender.id = :senderId2', {senderId2 : userToFindId})
.andWhere('receiver.id = :receiverId2', {receiverId2 : userConnectedId})
})
)
}),
@@ -457,9 +450,8 @@ export class FriendshipService {
.getOne()
// console.log('printing friendship queried')
// console.log({...friendship})
console.log('printing friendship queried')
console.log({...friendship})
if (friendship) {
console.log('we are blocked in friendship.service')
return true;

View File

@@ -10,9 +10,7 @@ export class SendableFriendship {
id: number;
date: Date;
senderUsername: string;
// senderId: number;
receiverUsername: string;
// receiverId: number;
status: FriendshipStatus;
// if my constructor is here won't it get sent all over the place too?
@@ -20,9 +18,7 @@ export class SendableFriendship {
this.id = friendship.id;
this.date = friendship.date
this.senderUsername = friendship.sender.username;
// this.senderId = friendship.sender.id;
this.receiverUsername = friendship.receiver.username;
// this.receiverId = friendship.receiver.id;
this.status = friendship.status;
};
}

View File

@@ -0,0 +1,18 @@
import { User } from "./entities/user.entity";
import { UserStats } from "./entities/userStat.entities";
export class SendableUser {
username: string;
image_url : string;
status: string;
stats: UserStats;
// if my constructor is here won't it get sent all over the place too?
constructor(user: User) {
this.username = user.username;
this.image_url = user.image_url;
this.status = user.status;
this.stats = user.stats;
};
}

View File

@@ -24,11 +24,7 @@ export class UsersController {
@UseGuards(TwoFactorGuard)
@Get('all')
findAll(@Req() req) {
// findAll(@Query() paginationquery : PaginationQueryDto, @Req() req) {
//const { limit, offset } = query;
// not convinced i want pagination... i mean maybe for loading and such, we shall see
const user : User = req.user;
// return this.usersService.findAll(paginationquery, user);
return this.usersService.findAll(user);
}
@@ -71,7 +67,6 @@ export class UsersController {
return this.usersService.findOne(req.user.username);
else
return this.usersService.findOne(usernameToFind);
// i would rather just use numbers but i'm guessing Cherif uses this all over
}
// also seems useless...

View File

@@ -64,49 +64,21 @@ export class UsersService {
return true;
}
// ok yea this shit makes no fucking sense any more since we're always gonna have the id, this is meant to return someone by username, fuck that
// async findOneById(userId : number, toFindId: number) {
// const userToFind : User = await this.userRepository.createQueryBuilder('user')
// .leftJoinAndSelect('user.stats', 'stats')
// .where('user.id = :toFindId', { toFindId: toFindId })
// .getOne();
// console.log('USERNAME OF FOUND USER : ' + userToFind.username);
// // console.log({...user})
// // you can't do that, you need to do user === undefined
// // if (user === undefined)
// if (!userToFind)
// throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND);
// if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id)) {
// console.log('we are blocked in user.service')
// throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND);
// }
// const partialUser : Partial<User> = {
// username: userToFind.username,
// image_url: userToFind.image_url,
// status: userToFind.status,
// stats: userToFind.stats,
// };
// return partialUser;
// }
// fuck pagination Query
// async findAll(paginationquery : PaginationQueryDto, currentUser: User) {
async findAll(currentUser: User) {
// const { limit, offset } = paginationquery;
// const otherUsers = await this.userRepository.find({where: {id: Not(+currentUser.id)}, order: {username: "ASC"}, skip: offset, take: limit,});
const otherUsers = await this.userRepository.find({where: {id: Not(+currentUser.id)}, order: {username: "ASC"}});
let partialUsers : Partial<User>[] = [];
for (const otherUser of otherUsers) {
// console.log('other user: ')
// console.log({...otherUser})
// let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(user.username, otherUser.username);
// console.log('user.services findIF Blocked... : ')
// console.log(tmp)
// if (tmp === false) {
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id) === false) {
partialUsers.push({id: otherUser.id, username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats});
console.log('other user: ')
console.log({...otherUser})
let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id);
console.log('user.services findIF Blocked... : ')
console.log(tmp)
if (tmp === false) {
// if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id) === false) {
partialUsers.push({username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats});
}
}
console.log('user.services findAll, partialUsers:')