|
|
@ -1,9 +1,9 @@ |
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common' |
|
|
|
import { InjectRepository } from '@nestjs/typeorm' |
|
|
|
import { Repository } from 'typeorm' |
|
|
|
import { User } from './user.entity' |
|
|
|
import { UserDto } from './user.dto' |
|
|
|
import { type Channel } from 'src/chat/model/channel.entity' |
|
|
|
import { User } from './entity/user.entity' |
|
|
|
import { type UserDto } from './dto/user.dto' |
|
|
|
import { type Channel } from 'src/chat/entity/channel.entity' |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class UsersService { |
|
|
@ -20,13 +20,7 @@ export class UsersService { |
|
|
|
} |
|
|
|
|
|
|
|
async findUser (ftId: number): Promise<User | null> { |
|
|
|
return await this.usersRepository.findOne({ |
|
|
|
where: { ftId }, |
|
|
|
relations: { |
|
|
|
friends: true, |
|
|
|
followers: true, |
|
|
|
} |
|
|
|
}); |
|
|
|
return await this.usersRepository.findOneBy({ftId}) |
|
|
|
} |
|
|
|
|
|
|
|
async create (userData: UserDto) { |
|
|
@ -58,23 +52,49 @@ export class UsersService { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
async getFriends (ftId: number) { |
|
|
|
const user = await this.usersRepository.findOne({ |
|
|
|
where: { ftId }, |
|
|
|
relations: { |
|
|
|
friends: true, |
|
|
|
} |
|
|
|
}) |
|
|
|
return user.friends |
|
|
|
} |
|
|
|
|
|
|
|
async getInvits (ftId: number) { |
|
|
|
const user = await this.usersRepository.findOne({ |
|
|
|
where: { ftId }, |
|
|
|
relations: { |
|
|
|
followers: true, |
|
|
|
} |
|
|
|
}) |
|
|
|
return user.followers |
|
|
|
} |
|
|
|
|
|
|
|
async invit (ftId: number, targetFtId: number) { |
|
|
|
const user = await this.findUser(ftId); |
|
|
|
const target = await this.findUser(targetFtId); |
|
|
|
const user = await this.findUser(ftId) |
|
|
|
const target = await this.findUser(targetFtId) |
|
|
|
if (!target) { |
|
|
|
return new NotFoundException(`Error: user id ${targetFtId} isn't in our db.`) |
|
|
|
return new NotFoundException( |
|
|
|
`Error: user id ${targetFtId} isn't in our db.` |
|
|
|
) |
|
|
|
} |
|
|
|
const id = user.followers.findIndex((follower) => follower.ftId === targetFtId) |
|
|
|
const id = user.followers.findIndex( |
|
|
|
(follower) => follower.ftId === targetFtId |
|
|
|
) |
|
|
|
if (id != -1) { |
|
|
|
console.log(`Friend relation complete between ${user.username} and ${target.username}`); |
|
|
|
user.friends.push(target); |
|
|
|
target.friends.push(user); |
|
|
|
user.followers.slice(id, 1); |
|
|
|
this.usersRepository.save(user); |
|
|
|
console.log( |
|
|
|
`Friend relation complete between ${user.username} and ${target.username}` |
|
|
|
) |
|
|
|
user.friends.push(target) |
|
|
|
target.friends.push(user) |
|
|
|
user.followers.slice(id, 1) |
|
|
|
this.usersRepository.save(user) |
|
|
|
} else { |
|
|
|
console.log(`You asked ${target.username} to be your friend.`); |
|
|
|
target.followers.push(user); |
|
|
|
console.log(`You asked ${target.username} to be your friend.`) |
|
|
|
target.followers.push(user) |
|
|
|
} |
|
|
|
this.usersRepository.save(target); |
|
|
|
this.usersRepository.save(target) |
|
|
|
} |
|
|
|
} |
|
|
|