ok so i think i need to change a whole bunch of stuff all over in the User module and Friendship module, namely that we should be searching the DB by ID's not Usernames... currently my Blocking of a user doesn't wquite work cuz findAll() sends ID to findIfUserIsBlockedOrHasBlocked() when it should be sending usernames, but everything else sends Usernames so i'm saving now in case changing everything fucks everything up...

This commit is contained in:
Me
2022-12-23 23:36:04 +01:00
parent 139da075da
commit 7e906de128
4 changed files with 153 additions and 31 deletions

View File

@@ -26,7 +26,7 @@ export class FriendshipController {
console.log('GET myfriend') console.log('GET myfriend')
console.log(friendUsername); console.log(friendUsername);
const user = req.user; const user = req.user;
return this.friendshipService.findOneFriendByUsername(friendUsername, user.username); return this.friendshipService.findOneRelationshipByUsername(friendUsername, user.username);
} }
// GET http://transcendance:8080/api/v2/network/blocked // GET http://transcendance:8080/api/v2/network/blocked

View File

@@ -15,7 +15,46 @@ export class FriendshipService {
private readonly userRepository: Repository<User>, private readonly userRepository: Repository<User>,
) { } ) { }
async findOneFriendByUsername(friendUsername : string, username : string) { // old, only Accept and Request but not Decline or Block
// async findOneFriendByUsername(friendUsername : string, username : string) {
// const friendship = await this.friendshipRepository
// .createQueryBuilder('friendship')
// .where(
// new Brackets((qb) => {
// qb.where(
// new Brackets((subAQb) => {
// subAQb.where('friendship.senderUsername = :username', {username : username})
// .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : friendUsername})
// })
// )
// .orWhere(
// new Brackets((subBQb) => {
// subBQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername})
// .andWhere('friendship.receiverUsername = :username', {username : username})
// })
// )
// }),
// )
// .andWhere(
// new Brackets((qb2) => {
// qb2.where('friendship.status = :statusA', {statusA : FriendshipStatus.ACCEPTED})
// .orWhere('friendship.status = :statusR', {statusR : FriendshipStatus.REQUESTED})
// }),
// )
// .getOne()
// // console.log('END Find one friend by username: ')
// // console.log({...friendship})
// if (!friendship) {
// throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND);
// }
// return friendship;
// }
// should i be using UserID rather than Username???? i mean prolly...
async findOneRelationshipByUsername(friendUsername : string, username : string) {
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where( .where(
@@ -34,12 +73,7 @@ export class FriendshipService {
) )
}), }),
) )
.andWhere( .andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
new Brackets((qb2) => {
qb2.where('friendship.status = :statusA', {statusA : FriendshipStatus.ACCEPTED})
.orWhere('friendship.status = :statusR', {statusR : FriendshipStatus.REQUESTED})
}),
)
.getOne() .getOne()
// console.log('END Find one friend by username: ') // console.log('END Find one friend by username: ')
@@ -94,6 +128,10 @@ export class FriendshipService {
for (const friendship of friendships) { for (const friendship of friendships) {
partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status}); partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status});
} }
console.log('friendship.service findAllBlockedFriends, friendships:')
console.log({...friendships})
console.log('friendship.service findAllBlockedFriends, partial friendship:')
console.log({...partialFriendship})
return partialFriendship; return partialFriendship;
} }
@@ -133,7 +171,12 @@ export class FriendshipService {
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND); throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
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);
const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver }); const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver });
console.log('friendship.service create, friendship: ')
console.log({...friendship})
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);
@@ -157,7 +200,10 @@ export class FriendshipService {
receiverUsername: (await savedFriendship).receiverUsername, receiverUsername: (await savedFriendship).receiverUsername,
status : (await savedFriendship).status status : (await savedFriendship).status
} }
console.log('friendship.service create friendship, partial friendship')
console.log({...partialFriendship}) console.log({...partialFriendship})
console.log('friendship.service create friendship, NEW friendship')
console.log({...newFriendship})
return partialFriendship; return partialFriendship;
} }
@@ -199,6 +245,8 @@ export class FriendshipService {
receiverUsername: (await savedFriendship).receiverUsername, receiverUsername: (await savedFriendship).receiverUsername,
status : (await savedFriendship).status status : (await savedFriendship).status
} }
console.log('decline friend request')
console.log({...partialFriendship})
return partialFriendship return partialFriendship
} }
@@ -206,18 +254,36 @@ export class FriendshipService {
let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
if (!relation[0]) if (!relation[0])
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation[0].sender.id === user.id) {
throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND); // do i need to check if they've already been blocked?
if (relation[0].receiver.id === user.id) {
// throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND);
console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block')
console.log({...relation})
// const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED)
// const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"})
// we create a new one where you are the sender
const newFriendshipDto = {"receiverUsername": relation[0].sender.username, "status": FriendshipStatus.BLOCKED};
// can't do it this way cuz READONLY
// const newFriendshipDto = new CreateFriendshipDto();
// newFriendshipDto.receiverUsername = relation[0].sender.username;
// newFriendshipDto.status = FriendshipStatus.BLOCKED;
// we delete that relation
await this.removeFriendship(relationshipId, user);
// we set it to blocked like we do below...
return await this.create(newFriendshipDto, user);
} else {
relation[0].status = FriendshipStatus.BLOCKED;
const savedFriendship = this.friendshipRepository.save(relation[0]);
const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id,
date : (await savedFriendship).date,
receiverUsername: (await savedFriendship).receiverUsername,
status : (await savedFriendship).status
}
return partialFriendship
} }
relation[0].status = FriendshipStatus.BLOCKED;
const savedFriendship = this.friendshipRepository.save(relation[0]);
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) { async removeFriendship(relationshipId: string, user : User) {
@@ -237,18 +303,62 @@ export class FriendshipService {
return this.friendshipRepository.remove(friendship[0]); return this.friendshipRepository.remove(friendship[0]);
} }
//i think maybe i should be using ID rather than username...
// async findIfUserIsBlockedOrHasBlocked(userConnectedUsername: string, userToFindUsername: string) {
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) { async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) {
console.log("finding if user is blocked") console.log("finding if user is blocked")
console.log('user connected: ' + userConnectedId)
console.log('user to find: ' + userToFindId)
// const friendship = await this.friendshipRepository
// .createQueryBuilder('friendship')
// .where('friendship.senderUsername = :requestee', { requestee: userConnectedId })
// .orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId })
// .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
// .getOne();
// const friendship = await this.friendshipRepository
// .createQueryBuilder('friendship')
// .where(
// new Brackets((qb) => {
// qb.where('friendship.senderUsername = :requestee', { requestee: userConnectedId })
// .orWhere('friendship.receiverUsername = :requesteeBis', { requesteeBis: userToFindId })
// }),
// )
// .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
// .getOne();
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.senderUsername = :requestee', { requestee: userConnectedId }) .where(
.orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId }) new Brackets((qb) => {
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) qb.where(
.getOne(); new Brackets((subAQb) => {
subAQb.where('friendship.senderUsername = :username', {username : userConnectedId})
.andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : userToFindId})
})
)
.orWhere(
new Brackets((subBQb) => {
subBQb.where('friendship.senderUsername = :friendUsername', {friendUsername : userToFindId})
.andWhere('friendship.receiverUsername = :username', {username : userConnectedId})
})
)
}),
)
// .andWhere('friendship.status = :status', {status : FriendshipStatus.BLOCKED})
.getOne()
console.log('printing friendship queried')
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;
} }
console.log('we are not blocked in friendship service')
return false; return false;
} }
} }

View File

@@ -155,15 +155,21 @@ export class UsersService {
// The fuck Pagination version // The fuck Pagination version
async findAll(paginationquery : PaginationQueryDto, userConnectedId: string) { async findAll(paginationquery : PaginationQueryDto, userConnectedId: string) {
const { limit, offset } = paginationquery; const { limit, offset } = paginationquery;
const users = await this.userRepository.find({where: {id: Not(+userConnectedId)}, order: {username: "ASC"}, skip: offset, take: limit,}); const otherUsers = await this.userRepository.find({where: {id: Not(+userConnectedId)}, order: {username: "ASC"}, skip: offset, take: limit,});
let partialUsers : Partial<User>[] = []; let partialUsers : Partial<User>[] = [];
for (const user of users) { for (const otherUser of otherUsers) {
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) { let tmp = await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, otherUser.id.toString());
partialUsers.push({username: user.username, image_url: user.image_url, status: user.status, stats: user.stats}); console.log('user.services findIF Blocked... : ')
console.log(tmp)
// if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) {
if (tmp === false) {
partialUsers.push({username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats});
} }
} }
console.log('user.services findAll, partialUsers:')
console.log({...partialUsers})
return partialUsers; return partialUsers;
} }

View File

@@ -2,7 +2,6 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
import { binding_callbacks, empty } from "svelte/internal"; // WTF is this? import { binding_callbacks, empty } from "svelte/internal"; // WTF is this?
import App from "../../App.svelte";
import Button from "../../pieces/Button.svelte"; import Button from "../../pieces/Button.svelte";
import DisplayAUser from "../../pieces/DisplayAUser.svelte"; import DisplayAUser from "../../pieces/DisplayAUser.svelte";
import Tabs from "../../pieces/Tabs.svelte"; import Tabs from "../../pieces/Tabs.svelte";
@@ -90,9 +89,13 @@
}; };
/**** END OF MAIN FETCH ****/ /**** END OF MAIN FETCH ****/
// returns everything but BLOCKED
const fetchFriendshipFull = async(aUsername) => { const fetchFriendshipFull = async(aUsername) => {
console.log('fetch friendship from a username')
console.log(aUsername)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`) friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`)
.then( x => x.json()); .then( x => x.json());
console.log({...friendshipStatusFull})
}; };
@@ -106,6 +109,7 @@
}) })
}) })
.then( x => x.json()) .then( x => x.json())
fetchFriendshipFull(aUsername);
}; };
const viewAUser = async(aUsername) => { const viewAUser = async(aUsername) => {
@@ -165,8 +169,8 @@
}; };
const blockANonFriendUser = async(aUsername) => { const blockANonFriendUser = async(aUsername) => {
console.log('Block someone username') console.log('Block a non friend user, their username')
console.log({...aUsername}) console.log(aUsername)
const blockResp = await fetch("http://transcendance:8080/api/v2/network/myfriends", { const blockResp = await fetch("http://transcendance:8080/api/v2/network/myfriends", {
method : "POST", method : "POST",
@@ -187,6 +191,8 @@
}; };
const blockAFriend = async(relationshipId) => { const blockAFriend = async(relationshipId) => {
console.log('blocking a friend, the relationshipID')
console.log(relationshipId)
const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/block`, {method: "PATCH"}) const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/block`, {method: "PATCH"})
.then( x => x.json() ); .then( x => x.json() );
console.log('blocked a user response') console.log('blocked a user response')
@@ -279,7 +285,7 @@
<div class="tip">You have not Blocked any Users</div> <div class="tip">You have not Blocked any Users</div>
{/if} {/if}
{#each blockedUsers as aUser} {#each blockedUsers as aUser}
<div class="sidebar-item" on:click={() => viewAUser(aUser.senderUsername)}>{aUser.senderUsername}</div> <div class="sidebar-item" on:click={() => viewAUser(aUser.receiverUsername)}>{aUser.receiverUsername}</div>
<div class="status sidebar-item">{aUser.status}</div> <div class="status sidebar-item">{aUser.status}</div>
<br> <br>
{/each} {/each}