Browse Source

friends

master
nicolas-arnaud 2 years ago
parent
commit
8c6a876890
  1. 10
      back/volume/src/auth/42.strategy.ts
  2. 4
      back/volume/src/chat/chat.gateway.ts
  3. 8
      back/volume/src/chat/model/update-channel.dto.ts
  4. 2
      back/volume/src/users/user.dto.ts
  5. 7
      back/volume/src/users/user.entity.ts
  6. 12
      back/volume/src/users/users.controller.ts
  7. 52
      back/volume/src/users/users.service.ts

10
back/volume/src/auth/42.strategy.ts

@ -30,15 +30,15 @@ export class FtStrategy extends PassportStrategy(Strategy, '42') {
): Promise<any> {
request.session.accessToken = accessToken
console.log('accessToken', accessToken, 'refreshToken', refreshToken)
const id_42 = profile.id as number
const ftId = profile.id as number
console.log(profile)
if ((await this.usersService.getOneUser42(id_42)) === null) {
if ((await this.usersService.findUser(ftId)) === null) {
const newUser = new User()
newUser.id_42 = profile.id as number
newUser.ftId = profile.id as number
newUser.username = profile.displayName as string
newUser.avatar = id_42 + '.jpg'
newUser.avatar = ftId + '.jpg'
this.usersService.create(newUser)
const file = createWriteStream('avatars/' + id_42 + '.jpg')
const file = createWriteStream('avatars/' + ftId + '.jpg')
get(profile._json.image.versions.small, function (response) {
response.pipe(file)
})

4
back/volume/src/chat/chat.gateway.ts

@ -36,7 +36,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
async handleConnection (socket: Socket) {
try {
const user: User = await this.userService.findOne(socket.data.user.id)
const user: User = await this.userService.findUser(socket.data.user.ftId)
if (!user) {
socket.emit('Error', new UnauthorizedException())
// socket.disconnect();
@ -64,7 +64,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
): Promise<Channel> {
const channel = new Channel()
channel.name = channeldto.name
const owner = await this.userService.findOne(channeldto.owner)
const owner = await this.userService.findUser(channeldto.owner)
channel.owners.push(owner)
channel.password = channeldto.password
/// ...///

8
back/volume/src/chat/model/update-channel.dto.ts

@ -11,11 +11,13 @@ export class UpdateChannelDto extends PartialType(CreateChannelDto) {
messages: [Message]
owners: [number] // user id
owners: [number] // ftId
banned: [number] // user id
admins: [number]
muted: [number] // user id
banned: [number] // ftId
muted: [number] // ftId
@IsString()
password: string

2
back/volume/src/users/user.dto.ts

@ -11,7 +11,7 @@ import { Express } from 'express'
export class UserDto {
@IsPositive()
@IsNotEmpty()
readonly id_42: number
readonly ftId: number
@IsString()
@IsNotEmpty()

7
back/volume/src/users/user.entity.ts

@ -16,7 +16,7 @@ export class User {
id: number;
@Column({ unique: true })
id_42: number
ftId: number
@Column({ unique: true })
username: string
@ -37,13 +37,10 @@ export class User {
@JoinTable()
blocked: User[]
@ManyToMany(() => User, (user) => user.following)
@ManyToMany(() => User)
@JoinTable()
followers: User[]
@ManyToMany(() => User, (user) => user.followers)
following: User[]
@ManyToMany(() => User, (user) => user.friends)
friends: User[]

12
back/volume/src/users/users.controller.ts

@ -36,7 +36,7 @@ export class UsersController {
@Get()
async getAllUsers(): Promise<User[]> {
return await this.usersService.getAllUsers()
return await this.usersService.findUsers()
}
@Post()
@ -44,7 +44,7 @@ export class UsersController {
async create(
@Body() payload: UserDto,
@FtUser() profile: Profile) {
const user = await this.usersService.getOneUser42(profile.id);
const user = await this.usersService.findUser(profile.id);
if (user) {
return await this.usersService.update(user.id, payload)
} else {
@ -52,13 +52,13 @@ export class UsersController {
}
}
@Post("follow/:target")
@Post("invit/:id")
@UseGuards(AuthenticatedGuard)
followUser(
@FtUser() profile: Profile,
@Param('target, ParseIntPipe') target: number,
@Param('id, ParseIntPipe') id: number,
) {
this.usersService.follow(profile.id, target);
this.usersService.invit(profile.id, id);
}
@Post('avatar')
@ -94,7 +94,7 @@ export class UsersController {
@FtUser() profile: Profile,
@Res({ passthrough: true }) response: Response
) {
const user = await this.usersService.getOneUser42(profile.id)
const user = await this.usersService.findUser(profile.id)
const filename = user.avatar
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename))
response.set({

52
back/volume/src/users/users.service.ts

@ -7,23 +7,23 @@ import { type Channel } from 'src/chat/model/channel.entity'
@Injectable()
export class UsersService {
constructor (
constructor(
@InjectRepository(User) private readonly usersRepository: Repository<User>
) {}
) { }
async getAllUsers (): Promise<User[]> {
async findUsers(): Promise<User[]> {
return await this.usersRepository.find({})
}
async getOneUser (username: string): Promise<User | null> {
async findUserByName(username: string): Promise<User | null> {
return await this.usersRepository.findOneBy({ username })
}
async getOneUser42 (id_42: number): Promise<User | null> {
return await this.usersRepository.findOneBy({ id_42 })
async findUser(ftId: number): Promise<User | null> {
return await this.usersRepository.findOneBy({ ftId })
}
async create (userData: UserDto) {
async create(userData: UserDto) {
try {
const newUser = this.usersRepository.create(userData)
return await this.usersRepository.save(newUser)
@ -32,13 +32,7 @@ export class UsersService {
}
}
async findOne (id: number) {
const user = await this.usersRepository.findOneBy({ id })
if (user) return user
throw new NotFoundException(`User #${id} not found`)
}
async findOnlineInChannel (channel: Channel): Promise<User[]> {
async findOnlineInChannel(channel: Channel): Promise<User[]> {
return await this.usersRepository
.createQueryBuilder('user')
.where('user.channel = :chan', { chan: channel })
@ -46,20 +40,36 @@ export class UsersService {
.getMany()
}
async update (id: number, changes: UserDto) {
const updatedUser = await this.findOne(id)
async update(ftId: number, changes: UserDto) {
const updatedUser = await this.findUser(ftId)
this.usersRepository.merge(updatedUser, changes)
return await this.usersRepository.save(updatedUser)
}
async addAvatar (userId: number, filename: string) {
await this.usersRepository.update(userId, {
async addAvatar(ftId: number, filename: string) {
await this.usersRepository.update(ftId, {
avatar: filename
})
}
async follow(userFtId: number, targetFtId: number) {
const user = await this.getOneUser42(userFtId);
const target = await this.getOneUser42(targetFtId);
async invit(ftId: number, targetFtId: number) {
const user = await this.usersRepository.findOne({
where: { ftId },
relations: { friends: true, followers: true },
});
const target = await this.usersRepository.findOne({
where: { ftId: targetFtId },
relations: { friends: true, followers: true },
});
const id = user.followers.findIndex((follower) => follower.ftId == ftId)
if (id != -1) {
user.friends.push(target);
target.friends.push(user);
user.followers.slice(id, 1);
this.usersRepository.save(user);
} else {
target.followers.push(user);
}
this.usersRepository.save(target);
}
}

Loading…
Cancel
Save