adding the ability to add and delete friends, in progress

This commit is contained in:
Me
2022-12-14 06:31:09 +01:00
parent bd4938b64b
commit 3a6729b9a3
3 changed files with 115 additions and 27 deletions

View File

@@ -14,6 +14,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
findEmpty(@Req() req) {
const user = req.user;
console.log('GET myfriends')
return this.friendshipService.findAllFriends(user.id);
}
@@ -31,11 +32,12 @@ export class FriendshipController {
// return this.friendshipService.findOneFriend(relationshipId, user.username);
// }
@Get('myfriend/:friendUsername')
@Get('myfriends/:friendUsername')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findOneRelationByUsername(@Param('friendUsername') friendUsername : string, @Req() req) {
console.log("Username " + friendUsername);
console.log('GET myfriend')
console.log(friendUsername);
const user = req.user;
return this.friendshipService.findOneFriendByUsername(friendUsername, user.username);
}
@@ -76,6 +78,7 @@ export class FriendshipController {
updateAccept(@Param('relationshipId') relationshipId: string, @Req() req)
{
const user : User = req.user;
console.log('accepting a friendship')
return this.friendshipService.acceptFriendship(relationshipId, user);
}
@@ -103,6 +106,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
remove(@Param('relationshipId') relationshipId: string, @Req() req) {
const user : User = req.user;
console.log('deleting a friendship')
return this.friendshipService.removeFriendship(relationshipId, user);
}

View File

@@ -1,7 +1,7 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/users/entities/user.entity';
import { Repository } from 'typeorm';
import { Repository, Brackets } from 'typeorm';
import { CreateFriendshipDto } from './dto/create-friendship.dto';
import { Friendship, FriendshipStatus } from './entities/friendship.entity';
@@ -26,11 +26,32 @@ export class FriendshipService {
async findOneFriendByUsername(friendUsername : string, username : string) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.senderUsername = : username', {username : username})
.andWhere('friendship.receiverUsername = : friendUsername', {friendUsername : friendUsername})
.andWhere('friendship.status = : status ', {status : FriendshipStatus.REQUESTED})
.orWhere('friendship.status = : status ', {status : FriendshipStatus.ACCEPTED})
.where(
new Brackets((qb) => {
qb.where(
new Brackets((subQb) => {
subQb.where('friendship.senderUsername = :username', {username : username})
.andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : friendUsername})
})
)
.orWhere(
new Brackets((subQb) => {
subQb.where('friendship.senderUsername = :friendUsername', {friendUsername : friendUsername})
.andWhere('friendship.receiverUsername = :username', {username : username})
})
)
}),
)
.andWhere(
new Brackets((qb) => {
qb.where('friendship.status = :status', {status : FriendshipStatus.ACCEPTED})
.orWhere('friendship.status = :status', {status : FriendshipStatus.REQUESTED})
}),
)
.getOne()
if (!friendship) {
throw new HttpException(`There is no such friendship`, HttpStatus.NOT_FOUND);
}
return friendship;
}
@@ -44,13 +65,17 @@ export class FriendshipService {
async findOneBlockedByUsername(blockedUsername : string, username : string) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.senderUsername = : username', {username : username})
.andWhere('friendship.receiverUsername = : friendUsername', {friendUsername : blockedUsername})
.andWhere('friendship.status = : status ', {status : FriendshipStatus.BLOCKED})
.where('friendship.senderUsername = :username', {username : username})
.andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : blockedUsername})
.andWhere('friendship.status = :status ', {status : FriendshipStatus.BLOCKED})
.getOne()
if (!friendship) {
throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND);
}
return friendship;
}
// ERIC: test this
async findAllFriends(username: string) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')

View File

@@ -2,6 +2,7 @@
import { onMount } from "svelte";
import { binding_callbacks } from "svelte/internal"; // WTF is this?
import App from "../../App.svelte";
import Button from "../../pieces/Button.svelte";
import DisplayAUser from "../../pieces/DisplayAUser.svelte";
import Tabs from "../../pieces/Tabs.svelte";
@@ -14,6 +15,8 @@
let myFriends;
let requestsMade, requestsRecieved;
let usernameBeingViewed;
let friendshipStatusFull; // id, date, senderUsername, reveiverUsername, status
let friendshipFetch; // like a generic var for any friendship fetch actions that might happen
/**** Layout variables ****/
let tabItems: string[] = ['All Users', 'My Friends', 'Friend Requests']
@@ -49,10 +52,20 @@
const fetchMyFriends = async() => {
myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends')
.then( x => x.json() );
// console.log('got all friends ')
// console.log({...allFriends})
console.log('got my friends ')
console.log({...myFriends})
};
let cherif;
const fetchAFriend = async() => {
cherif = await fetch('http://transcendance:8080/api/v2/network/myfriends/chbadad')
.then( x => x.json() );
console.log('got Cherif ')
console.log(cherif)
// console.log({...cherif})
};
const fetchRequestsMade = async() => {
requestsMade = await fetch('http://transcendance:8080/api/v2/network/pending')
.then( x => x.json() );
@@ -93,19 +106,47 @@
}
};
const areWeFriends = async(aUsername) => {
console.log("Are We Friends?")
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`)
.then( x => x.json());
};
const viewAUser = async(aUsername) => {
console.log('Profile Friend updating userBeingViewed')
usernameBeingViewed = aUsername;
friendshipStatusFull = undefined;
// id, date, senderUsername, reveiverUsername, status
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`)
.then( x => x.json());
// sendUsername = userBeingViewed.username;
console.log('Friendship Status Full')
console.log({...friendshipStatusFull})
// prolly like fetch if you're friends or not?
// GET http://transcendance:8080/api/v2/networks/myfriends?username=aUser but i need to make that as long as Cherif
// doesn't have a better option
// like i want this thing to return the Friendship ID ideally
};
const acceptFriendRequest = async(relationshipId) => {
console.log('accept friend request')
friendshipFetch = undefined;
// PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/accept
friendshipFetch = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/accept`, {
method: "PATCH"})
.then( x => x.json());
};
const unfriend = async(relationshipId) => {
console.log('Unfriend')
friendshipFetch = undefined;
// PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/accept
friendshipFetch = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}`, {
method: "DELETE"})
.then( x => x.json());
};
const blockAUser = async(friendshipId) => {
};
@@ -143,12 +184,14 @@
<!-- does this work? -->
<!-- {#each allUsers as aUser (aUser.username)} -->
{#each allUsers as aUser}
{#if aUser.username !== user.username}
<div class="sidebar-item" on:click={() => viewAUser(aUser.username)}>{aUser.username}</div>
<!-- i could make an indicator component? like green for connected or something?
i could use words but color them?
i could make it so if they're in a game -->
<div class="status sidebar-item">{aUser.status}</div>
<br>
{/if}
{/each}
{:else if activeTabItem === 'My Friends' && myFriends !== undefined}
<h3>{activeTabItem}</h3>
@@ -178,11 +221,26 @@
<DisplayAUser aUsername={usernameBeingViewed}/>
<div class="buttons-area">
<!-- Add Friend -->
<!-- not the current user, not blocked, not request already sent or received, basically no friendshipID -->
<!-- so far not dealing with Blocked people -->
{#if usernameBeingViewed !== user.username}
<!-- uhhhg why can't it be simple... -->
<!-- {#if friendshipStatusFull === undefined} -->
{#if friendshipStatusFull && friendshipStatusFull.id}
{#if friendshipStatusFull.status === 'R'}
{#if friendshipStatusFull.senderUsername === user.username}
<div class="tile">Friend Request Sent</div>
{:else}
<!-- <Button type="secondary" on:click={() => acceptFriendRequest(usernameBeingViewed)}>Unfriend</Button> -->
<Button type="secondary" on:click={() => acceptFriendRequest(friendshipStatusFull.id)}>Accept Friend Request</Button>
{/if}
{:else if friendshipStatusFull.status === 'A'}
<div class="tile">You are Friends</div>
<Button on:click={() => unfriend(friendshipStatusFull.id)}>Unfriend</Button>
{/if}
{:else}
<Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button>
{/if}
{/if}
<!-- {#if user not blocked}
<Button on:click={() => blockAUser(userBeingViewed.username)}>Add Friend</Button>
@@ -218,6 +276,7 @@
{/if} -->
<!-- {:else if userBeingViewed !== undefined} -->
<!-- <Button on:click={fetchAFriend}>Get Cherif</Button> -->
</div>
</div>