|
|
|
|
@@ -15,7 +15,46 @@ export class FriendshipService {
|
|
|
|
|
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
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where(
|
|
|
|
|
@@ -34,12 +73,7 @@ export class FriendshipService {
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb2) => {
|
|
|
|
|
qb2.where('friendship.status = :statusA', {statusA : FriendshipStatus.ACCEPTED})
|
|
|
|
|
.orWhere('friendship.status = :statusR', {statusR : FriendshipStatus.REQUESTED})
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.andWhere('friendship.status != :status', {status : FriendshipStatus.BLOCKED})
|
|
|
|
|
.getOne()
|
|
|
|
|
|
|
|
|
|
// console.log('END Find one friend by username: ')
|
|
|
|
|
@@ -94,6 +128,10 @@ export class FriendshipService {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -133,7 +171,12 @@ export class FriendshipService {
|
|
|
|
|
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
|
|
|
|
|
throw new HttpException(`The status is not valid.`, HttpStatus.NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
const friendship = await this.friendshipRepository.findOneBy({ sender: creator, receiver: receiver });
|
|
|
|
|
|
|
|
|
|
console.log('friendship.service create, friendship: ')
|
|
|
|
|
console.log({...friendship})
|
|
|
|
|
|
|
|
|
|
if (friendship) {
|
|
|
|
|
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
|
|
|
|
|
throw new HttpException(`The friendship request has already been accepted.`, HttpStatus.OK);
|
|
|
|
|
@@ -157,7 +200,10 @@ export class FriendshipService {
|
|
|
|
|
receiverUsername: (await savedFriendship).receiverUsername,
|
|
|
|
|
status : (await savedFriendship).status
|
|
|
|
|
}
|
|
|
|
|
console.log('friendship.service create friendship, partial friendship')
|
|
|
|
|
console.log({...partialFriendship})
|
|
|
|
|
console.log('friendship.service create friendship, NEW friendship')
|
|
|
|
|
console.log({...newFriendship})
|
|
|
|
|
return partialFriendship;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -199,6 +245,8 @@ export class FriendshipService {
|
|
|
|
|
receiverUsername: (await savedFriendship).receiverUsername,
|
|
|
|
|
status : (await savedFriendship).status
|
|
|
|
|
}
|
|
|
|
|
console.log('decline friend request')
|
|
|
|
|
console.log({...partialFriendship})
|
|
|
|
|
return partialFriendship
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -206,18 +254,36 @@ export class FriendshipService {
|
|
|
|
|
let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} );
|
|
|
|
|
if (!relation[0])
|
|
|
|
|
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) {
|
|
|
|
|
@@ -237,18 +303,62 @@ export class FriendshipService {
|
|
|
|
|
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) {
|
|
|
|
|
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
|
|
|
|
|
.createQueryBuilder('friendship')
|
|
|
|
|
.where('friendship.senderUsername = :requestee', { requestee: userConnectedId })
|
|
|
|
|
.orWhere('friendship.senderUsername = :requesteeBis', { requesteeBis: userToFindId })
|
|
|
|
|
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
|
|
|
|
|
.getOne();
|
|
|
|
|
.where(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.where(
|
|
|
|
|
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) {
|
|
|
|
|
console.log('we are blocked in friendship.service')
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
console.log('we are not blocked in friendship service')
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|