|
|
|
|
@@ -16,23 +16,21 @@ export class FriendshipService {
|
|
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async findOneRelationshipById(friendId : string, userId : string) {
|
|
|
|
|
async findOneRelationshipById(friendId : number, userId : number) {
|
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.leftJoinAndSelect('friendship.sender', 'sender')
|
|
|
|
|
.leftJoinAndSelect('friendship.receiver', 'receiver')
|
|
|
|
|
.where(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.where(
|
|
|
|
|
new Brackets((subAQb) => {
|
|
|
|
|
subAQb.where('sender.id = :userId', { userId : userId})
|
|
|
|
|
.andWhere('receiver.id = :friendId', {friendId : friendId})
|
|
|
|
|
subAQb.where('friendship.senderId = :userId', { userId : userId})
|
|
|
|
|
.andWhere('friendship.receiverId = :friendId', {friendId : friendId})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
new Brackets((subBQb) => {
|
|
|
|
|
subBQb.where('sender.id = :friendId', {friendId : friendId})
|
|
|
|
|
.andWhere('receiver.id = :userId', {userId : userId})
|
|
|
|
|
subBQb.where('friendship.senderId = :friendId', {friendId : friendId})
|
|
|
|
|
.andWhere('friendship.receiverId = :userId', {userId : userId})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
@@ -84,12 +82,12 @@ export class FriendshipService {
|
|
|
|
|
return friendship;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAllFriends(username: string) {
|
|
|
|
|
async findAllFriends(userId: number) {
|
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
|
|
|
|
.andWhere('friendship.receiverUsername = :addressee', { addressee: username })
|
|
|
|
|
.orWhere('friendship.senderUsername = :requester', { requester: username })
|
|
|
|
|
.andWhere('friendship.receiverId = :addressee', { addressee: userId })
|
|
|
|
|
.orWhere('friendship.senderId = :requester', { requester: userId })
|
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
|
|
|
|
|
.getMany();
|
|
|
|
|
// for (const friend of friendship)
|
|
|
|
|
@@ -97,17 +95,17 @@ export class FriendshipService {
|
|
|
|
|
return friendship;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneBlocked(friendshipId: string, username: string) {
|
|
|
|
|
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.BLOCKED } });
|
|
|
|
|
async findOneBlocked(friendshipId: number, userId: number) {
|
|
|
|
|
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderId: userId, status: FriendshipStatus.BLOCKED } });
|
|
|
|
|
if (!friendship)
|
|
|
|
|
throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
return friendship;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOneBlockedByUsername(blockedUsername : string, username : string) {
|
|
|
|
|
async findOneBlockedByUsername(blockedUsername : string, userId : number) {
|
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where('friendship.senderUsername = :username', {username : username})
|
|
|
|
|
.where('friendship.senderId = :senderId', {senderId : userId})
|
|
|
|
|
.andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : blockedUsername})
|
|
|
|
|
.andWhere('friendship.status = :status ', {status : FriendshipStatus.BLOCKED})
|
|
|
|
|
.getOne()
|
|
|
|
|
@@ -117,49 +115,56 @@ export class FriendshipService {
|
|
|
|
|
return friendship;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAllBlockedFriends(username: string) {
|
|
|
|
|
async findAllBlockedFriends(userId: number) {
|
|
|
|
|
const friendships : Friendship[] = await this.friendshipRepository
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where('friendship.senderUsername = :requestee', { requestee: username })
|
|
|
|
|
.where('friendship.senderId = :requestee', { requestee: userId })
|
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
|
|
|
|
.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});
|
|
|
|
|
}
|
|
|
|
|
// 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});
|
|
|
|
|
// }
|
|
|
|
|
console.log('friendship.service findAllBlockedFriends, friendships:')
|
|
|
|
|
console.log({...friendships})
|
|
|
|
|
console.log('friendship.service findAllBlockedFriends, partial friendship:')
|
|
|
|
|
console.log({...partialFriendship})
|
|
|
|
|
return partialFriendship;
|
|
|
|
|
// console.log({...partialFriendship})
|
|
|
|
|
// return partialFriendship;
|
|
|
|
|
return friendships;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAllPendantRequestsForFriendship(username: string) {
|
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
|
async findAllPendantRequestsForFriendship(userId: number) {
|
|
|
|
|
const friendships = await this.friendshipRepository
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where('friendship.senderUsername = :requestee', { requestee: username })
|
|
|
|
|
.where('friendship.senderId = :requestee', { requestee: userId })
|
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
|
|
|
|
|
.getMany();
|
|
|
|
|
let partialFriendship : Partial<Friendship>[] = [];
|
|
|
|
|
for (const friend of friendship) {
|
|
|
|
|
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;
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAllReceivedRequestsForFriendship(username: string) {
|
|
|
|
|
const friendship = await this.friendshipRepository
|
|
|
|
|
async findAllReceivedRequestsForFriendship(userId: number) {
|
|
|
|
|
const friendships = await this.friendshipRepository
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where('friendship.receiverUsername = :addressee', { addressee: username })
|
|
|
|
|
.where('friendship.receiverId = :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;
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(createFriendshipDto: CreateFriendshipDto, creator : User) : Promise <Partial<Friendship>> {
|
|
|
|
|
@@ -189,15 +194,17 @@ export class FriendshipService {
|
|
|
|
|
const newFriendship = new Friendship();
|
|
|
|
|
newFriendship.sender = creator;
|
|
|
|
|
newFriendship.senderUsername = creator.username;
|
|
|
|
|
newFriendship.senderId = creator.id;
|
|
|
|
|
newFriendship.receiver = receiver;
|
|
|
|
|
newFriendship.receiverUsername = receiver.username;
|
|
|
|
|
newFriendship.receiverId = receiver.id;
|
|
|
|
|
newFriendship.status = createFriendshipDto.status;
|
|
|
|
|
const savedFriendship = this.friendshipRepository.save(newFriendship);
|
|
|
|
|
const partialFriendship : Partial<Friendship> = {
|
|
|
|
|
id : (await savedFriendship).id,
|
|
|
|
|
date : (await savedFriendship).date,
|
|
|
|
|
receiverUsername: (await savedFriendship).receiverUsername,
|
|
|
|
|
// receiverId: (await savedFriendship).receiver.id,
|
|
|
|
|
receiverId: (await savedFriendship).receiverId,
|
|
|
|
|
status : (await savedFriendship).status
|
|
|
|
|
}
|
|
|
|
|
console.log('friendship.service create friendship, partial friendship')
|
|
|
|
|
@@ -208,20 +215,21 @@ export class FriendshipService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async acceptFriendship(relationshipId: string, user: User) {
|
|
|
|
|
const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
|
|
|
|
|
const relation = await this.friendshipRepository.findOneBy({id: +relationshipId });
|
|
|
|
|
// console.log('.service accept friendship')
|
|
|
|
|
// console.log({...relation})
|
|
|
|
|
if (!relation[0])
|
|
|
|
|
if (!relation)
|
|
|
|
|
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
if (relation[0].sender.id === user.id) {
|
|
|
|
|
if (relation.senderId === user.id) {
|
|
|
|
|
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
relation[0].status = FriendshipStatus.ACCEPTED;
|
|
|
|
|
const savedFriendship = this.friendshipRepository.save(relation[0]);
|
|
|
|
|
relation.status = FriendshipStatus.ACCEPTED;
|
|
|
|
|
const savedFriendship = this.friendshipRepository.save(relation);
|
|
|
|
|
const partialFriendship : Partial<Friendship> = {
|
|
|
|
|
id : (await savedFriendship).id,
|
|
|
|
|
date : (await savedFriendship).date,
|
|
|
|
|
receiverUsername: (await savedFriendship).receiverUsername,
|
|
|
|
|
receiverId: (await savedFriendship).receiverId,
|
|
|
|
|
status : (await savedFriendship).status
|
|
|
|
|
}
|
|
|
|
|
console.log('.service accept friendship END')
|
|
|
|
|
@@ -251,20 +259,20 @@ export class FriendshipService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async blockFriendship(relationshipId: string, user: User) {
|
|
|
|
|
let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
|
|
|
|
|
if (!relation[0])
|
|
|
|
|
let relation = await this.friendshipRepository.find({id: +relationshipId });
|
|
|
|
|
if (!relation)
|
|
|
|
|
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
// do i need to check if they've already been blocked?
|
|
|
|
|
|
|
|
|
|
if (relation[0].receiver.id === user.id) {
|
|
|
|
|
if (relation.receiverId === 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};
|
|
|
|
|
const newFriendshipDto = {"receiverUsername": relation.senderUsername, "receiverId": relation.senderId, "status": FriendshipStatus.BLOCKED};
|
|
|
|
|
// can't do it this way cuz READONLY
|
|
|
|
|
// const newFriendshipDto = new CreateFriendshipDto();
|
|
|
|
|
// newFriendshipDto.receiverUsername = relation[0].sender.username;
|
|
|
|
|
@@ -274,12 +282,13 @@ export class FriendshipService {
|
|
|
|
|
// 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]);
|
|
|
|
|
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,
|
|
|
|
|
receiverId: (await savedFriendship).receiverId,
|
|
|
|
|
status : (await savedFriendship).status
|
|
|
|
|
}
|
|
|
|
|
return partialFriendship
|
|
|
|
|
@@ -289,45 +298,43 @@ export class FriendshipService {
|
|
|
|
|
async removeFriendship(relationshipId: string, 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.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
|
|
|
|
|
const friendship = await this.friendshipRepository.find({id: +relationshipId });
|
|
|
|
|
console.log('deleting a friendship .service')
|
|
|
|
|
console.log({...friendship})
|
|
|
|
|
console.log('who is the user')
|
|
|
|
|
console.log({...user})
|
|
|
|
|
if (!friendship[0])
|
|
|
|
|
if (!friendship)
|
|
|
|
|
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
if (friendship[0].sender.id !== user.id && friendship[0].receiver.id !== user.id) {
|
|
|
|
|
if (friendship.senderId !== user.id && friendship.receiverId !== user.id) {
|
|
|
|
|
throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
console.log('.service deleted a friendship')
|
|
|
|
|
return this.friendshipRepository.remove(friendship[0]);
|
|
|
|
|
return this.friendshipRepository.remove(friendship);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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: number, userToFindId: number) {
|
|
|
|
|
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')
|
|
|
|
|
.leftJoinAndSelect('friendship.sender', 'sender')
|
|
|
|
|
.leftJoinAndSelect('friendship.receiver', 'receiver')
|
|
|
|
|
.where(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.where(
|
|
|
|
|
new Brackets((subAQb) => {
|
|
|
|
|
subAQb.where('sender.id = :senderId', { senderId: userConnectedId})
|
|
|
|
|
.andWhere('receiver.id = :receiverId', { receiverId: userToFindId})
|
|
|
|
|
subAQb.where('friendship.senderId = :senderId', { senderId: userConnectedId})
|
|
|
|
|
.andWhere('friendship.receiverId = :receiverId', { receiverId: userToFindId})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
new Brackets((subBQb) => {
|
|
|
|
|
subBQb.where('sender.id = :senderId', {senderId : userToFindId})
|
|
|
|
|
.andWhere('receiver.id = :receiverId', {receiverUsername : userConnectedId})
|
|
|
|
|
subBQb.where('friendship.senderId = :senderId', {senderId : userToFindId})
|
|
|
|
|
.andWhere('friendship.receiverId = :receiverId', {receiverUsername : userConnectedId})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
@@ -346,8 +353,4 @@ export class FriendshipService {
|
|
|
|
|
console.log('we are not blocked in friendship service')
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SELECT "friendship"."id" AS "friendship_id", "friendship"."date" AS "friendship_date", "friendship"."senderUsername" AS "friendship_senderUsername", "friendship"."receiverUsername" AS "friendship_receiverUsername", "friendship"."status" AS "friendship_status", "friendship"."senderId" AS "friendship_senderId", "friendship"."receiverId" AS "friendship_receiverId", "sender"."id" AS "sender_id", "sender"."fortyTwoId" AS "sender_fortyTwoId", "sender"."username" AS "sender_username", "sender"."email" AS "sender_email", "sender"."image_url" AS "sender_image_url", "sender"."phone" AS "sender_phone", "sender"."status" AS "sender_status", "sender"."isEnabledTwoFactorAuth" AS "sender_isEnabledTwoFactorAuth", "sender"."isTwoFactorAuthenticated" AS "sender_isTwoFactorAuthenticated", "sender"."secretTwoFactorAuth" AS "sender_secretTwoFactorAuth", "sender"."statsId" AS "sender_statsId", "receiver"."id" AS "receiver_id", "receiver"."fortyTwoId" AS "receiver_fortyTwoId", "receiver"."username" AS "receiver_username", "receiver"."email" AS "receiver_email", "receiver"."image_url" AS "receiver_image_url", "receiver"."phone" AS "receiver_phone", "receiver"."status" AS "receiver_status", "receiver"."isEnabledTwoFactorAuth" AS "receiver_isEnabledTwoFactorAuth", "receiver"."isTwoFactorAuthenticated" AS "receiver_isTwoFactorAuthenticated", "receiver"."secretTwoFactorAuth" AS "receiver_secretTwoFactorAuth", "receiver"."statsId" AS "receiver_statsId" FROM "friendships" "friendship" LEFT JOIN "user" "sender" ON "sender"."id"="friendship"."senderId" LEFT JOIN "user" "receiver" ON "receiver"."id"="friendship"."receiverId" WHERE (("sender"."id" = $1 AND "receiver"."id" = :receiverId) OR ("sender"."id" = $2 AND "receiver"."id" = :receiverId)) AND "friendship"."status" = $3
|
|
|
|
|
}
|