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. 44
      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> { ): Promise<any> {
request.session.accessToken = accessToken request.session.accessToken = accessToken
console.log('accessToken', accessToken, 'refreshToken', refreshToken) console.log('accessToken', accessToken, 'refreshToken', refreshToken)
const id_42 = profile.id as number const ftId = profile.id as number
console.log(profile) console.log(profile)
if ((await this.usersService.getOneUser42(id_42)) === null) { if ((await this.usersService.findUser(ftId)) === null) {
const newUser = new User() const newUser = new User()
newUser.id_42 = profile.id as number newUser.ftId = profile.id as number
newUser.username = profile.displayName as string newUser.username = profile.displayName as string
newUser.avatar = id_42 + '.jpg' newUser.avatar = ftId + '.jpg'
this.usersService.create(newUser) 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) { get(profile._json.image.versions.small, function (response) {
response.pipe(file) 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) { async handleConnection (socket: Socket) {
try { 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) { if (!user) {
socket.emit('Error', new UnauthorizedException()) socket.emit('Error', new UnauthorizedException())
// socket.disconnect(); // socket.disconnect();
@ -64,7 +64,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
): Promise<Channel> { ): Promise<Channel> {
const channel = new Channel() const channel = new Channel()
channel.name = channeldto.name 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.owners.push(owner)
channel.password = channeldto.password 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] 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() @IsString()
password: string password: string

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

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

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

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

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

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

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

@ -11,16 +11,16 @@ export class UsersService {
@InjectRepository(User) private readonly usersRepository: Repository<User> @InjectRepository(User) private readonly usersRepository: Repository<User>
) { } ) { }
async getAllUsers (): Promise<User[]> { async findUsers(): Promise<User[]> {
return await this.usersRepository.find({}) 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 }) return await this.usersRepository.findOneBy({ username })
} }
async getOneUser42 (id_42: number): Promise<User | null> { async findUser(ftId: number): Promise<User | null> {
return await this.usersRepository.findOneBy({ id_42 }) return await this.usersRepository.findOneBy({ ftId })
} }
async create(userData: UserDto) { async create(userData: UserDto) {
@ -32,12 +32,6 @@ 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 return await this.usersRepository
.createQueryBuilder('user') .createQueryBuilder('user')
@ -46,20 +40,36 @@ export class UsersService {
.getMany() .getMany()
} }
async update (id: number, changes: UserDto) { async update(ftId: number, changes: UserDto) {
const updatedUser = await this.findOne(id) const updatedUser = await this.findUser(ftId)
this.usersRepository.merge(updatedUser, changes) this.usersRepository.merge(updatedUser, changes)
return await this.usersRepository.save(updatedUser) return await this.usersRepository.save(updatedUser)
} }
async addAvatar (userId: number, filename: string) { async addAvatar(ftId: number, filename: string) {
await this.usersRepository.update(userId, { await this.usersRepository.update(ftId, {
avatar: filename avatar: filename
}) })
} }
async follow(userFtId: number, targetFtId: number) { async invit(ftId: number, targetFtId: number) {
const user = await this.getOneUser42(userFtId); const user = await this.usersRepository.findOne({
const target = await this.getOneUser42(targetFtId); 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