nicolas-arnaud
2 years ago
10 changed files with 150 additions and 169 deletions
@ -1,179 +1,169 @@ |
|||||
import { Catch, Injectable, NotFoundException } from '@nestjs/common' |
import { |
||||
import { InjectRepository } from '@nestjs/typeorm' |
BadRequestException, |
||||
import { EntityNotFoundError, QueryFailedError, Repository } from 'typeorm' |
Catch, |
||||
import { User } from './entity/user.entity' |
Injectable, |
||||
import { type UserDto } from './dto/user.dto' |
NotFoundException, |
||||
import { type Channel } from 'src/chat/entity/channel.entity' |
} from "@nestjs/common"; |
||||
import type Result from 'src/pong/entity/result.entity' |
import { InjectRepository } from "@nestjs/typeorm"; |
||||
import { Cron } from '@nestjs/schedule' |
import { EntityNotFoundError, QueryFailedError, Repository } from "typeorm"; |
||||
|
import { User } from "./entity/user.entity"; |
||||
|
import { type UserDto } from "./dto/user.dto"; |
||||
|
import { type Channel } from "src/chat/entity/channel.entity"; |
||||
|
import type Result from "src/pong/entity/result.entity"; |
||||
|
import { Cron } from "@nestjs/schedule"; |
||||
|
|
||||
@Injectable() |
@Injectable() |
||||
@Catch(QueryFailedError, EntityNotFoundError) |
@Catch(QueryFailedError, EntityNotFoundError) |
||||
export class UsersService { |
export class UsersService { |
||||
constructor ( |
constructor( |
||||
@InjectRepository(User) private readonly usersRepository: Repository<User> |
@InjectRepository(User) private readonly usersRepository: Repository<User> |
||||
) {} |
) {} |
||||
|
|
||||
save (user: User) { |
save(user: User) { |
||||
this.usersRepository.save(user) |
this.usersRepository.save(user); |
||||
} |
} |
||||
|
|
||||
async findUsers (): Promise<User[]> { |
async findUsers(): Promise<User[]> { |
||||
return await this.usersRepository.find({}) |
return await this.usersRepository.find({}); |
||||
} |
} |
||||
|
|
||||
async findUserByName (username: string): Promise<User | null> { |
async findUserByName(username: string): Promise<User | null> { |
||||
const user = await this.usersRepository.findOne({ |
const user = await this.usersRepository.findOne({ |
||||
where: { username }, |
where: { username }, |
||||
relations: { results: true } |
relations: { results: true }, |
||||
}) |
}); |
||||
return user |
return user; |
||||
} |
} |
||||
|
|
||||
@Cron('0 * * * * *') |
@Cron("0 * * * * *") |
||||
async updateStatus () { |
async updateStatus() { |
||||
const users = await this.usersRepository.find({}) |
const users = await this.usersRepository.find({}); |
||||
users.forEach((usr) => { |
users.forEach((usr) => { |
||||
if (Date.now() - usr.lastAccess > 60000) { |
if (Date.now() - usr.lastAccess > 60000) { |
||||
usr.status = 'offline' |
usr.status = "offline"; |
||||
this.usersRepository.save(usr) |
this.usersRepository.save(usr); |
||||
} |
} |
||||
}) |
}); |
||||
} |
} |
||||
|
|
||||
async findUser (ftId: number): Promise<User | null> { |
async findUser(ftId: number): Promise<User | null> { |
||||
const user = await this.usersRepository.findOneBy({ ftId }) |
const user = await this.usersRepository.findOneBy({ ftId }); |
||||
if (user == null) return null |
if (user == null) return null; |
||||
user.lastAccess = Date.now() |
user.lastAccess = Date.now(); |
||||
user.status = 'online' |
user.status = "online"; |
||||
this.usersRepository.save(user) |
this.usersRepository.save(user); |
||||
return user |
return user; |
||||
} |
} |
||||
|
|
||||
async findOnlineUsers (): Promise<User[]> { |
async findOnlineUsers(): Promise<User[]> { |
||||
return await this.usersRepository.find({ where: { status: 'online' } }) |
return await this.usersRepository.find({ where: { status: "online" } }); |
||||
} |
} |
||||
|
|
||||
async create (userData: UserDto) { |
async create(userData: UserDto) { |
||||
try { |
try { |
||||
const newUser = this.usersRepository.create(userData) |
const newUser = this.usersRepository.create(userData); |
||||
return await this.usersRepository.save(newUser) |
return await this.usersRepository.save(newUser); |
||||
} catch (err) { |
} catch (err) { |
||||
throw new Error(`Error creating user ${err}`) |
throw new Error(`Error creating user ${err}`); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
async findOnlineInChannel (channel: Channel): Promise<User[]> { |
async findOnlineInChannel(channel: Channel): Promise<User[]> { |
||||
return await this.usersRepository |
return await this.usersRepository |
||||
.createQueryBuilder('user') |
.createQueryBuilder("user") |
||||
.where('user.channel = :chan', { chan: channel }) |
.where("user.channel = :chan", { chan: channel }) |
||||
.andWhere('user.status := status)', { status: 'online' }) |
.andWhere("user.status := status)", { status: "online" }) |
||||
.getMany() |
.getMany(); |
||||
} |
} |
||||
|
|
||||
async update (user: User, changes: UserDto): Promise<User | null> { |
async update(user: User, changes: UserDto): Promise<User | null> { |
||||
this.usersRepository.merge(user, changes) |
this.usersRepository.merge(user, changes); |
||||
return await this.usersRepository.save(user) |
return await this.usersRepository.save(user); |
||||
} |
} |
||||
|
|
||||
async addAvatar (ftId: number, filename: string) { |
async addAvatar(ftId: number, filename: string) { |
||||
return await this.usersRepository.update( |
return await this.usersRepository.update( |
||||
{ ftId }, |
{ ftId }, |
||||
{ |
{ |
||||
avatar: filename |
avatar: filename, |
||||
} |
} |
||||
) |
); |
||||
} |
} |
||||
|
|
||||
async getFriends (ftId: number): Promise<User[]> { |
async getFriends(ftId: number): Promise<User[]> { |
||||
const user = await this.usersRepository.findOne({ |
const user = await this.usersRepository.findOne({ |
||||
where: { ftId }, |
where: { ftId }, |
||||
relations: { |
relations: { |
||||
friends: true |
friends: true, |
||||
} |
}, |
||||
}) |
}); |
||||
if (user != null) return user.friends |
if (user != null) return user.friends; |
||||
return [] |
return []; |
||||
} |
} |
||||
|
|
||||
async getInvits (ftId: number): Promise<User[]> { |
async getInvits(ftId: number): Promise<User[]> { |
||||
const user = await this.usersRepository.findOne({ |
const user = await this.usersRepository.findOne({ |
||||
where: { ftId }, |
where: { ftId }, |
||||
relations: { |
relations: { |
||||
followers: true |
followers: true, |
||||
} |
}, |
||||
}) |
}); |
||||
if (user != null) return user.followers |
if (user != null) return user.followers; |
||||
return [] |
return []; |
||||
} |
} |
||||
|
|
||||
async getResults (ftId: number): Promise<Result[]> { |
async getResults(ftId: number): Promise<Result[]> { |
||||
const user = await this.usersRepository.findOne({ |
const user = await this.usersRepository.findOne({ |
||||
where: { ftId }, |
where: { ftId }, |
||||
relations: { |
relations: { |
||||
results: { |
results: { |
||||
players: true |
players: true, |
||||
} |
}, |
||||
} |
}, |
||||
}) |
}); |
||||
if (user != null) return user.results |
if (user != null) return user.results; |
||||
return [] |
return []; |
||||
} |
} |
||||
|
|
||||
async getLeader (): Promise<User[]> { |
async getLeaderboard(): Promise<User[]> { |
||||
return await this.usersRepository.find({ |
return await this.usersRepository.find({ |
||||
order: { |
order: { |
||||
winrate: 'DESC' |
winrate: "DESC", |
||||
} |
}, |
||||
}) |
}); |
||||
} |
} |
||||
|
|
||||
async getRank (ftId: number): Promise<number> { |
async getRank(ftId: number): Promise<number> { |
||||
const leader = await this.getLeader() |
const leader = await this.getLeaderboard(); |
||||
return leader.findIndex((user) => user.ftId === ftId) |
return leader.findIndex((user) => user.ftId === ftId); |
||||
} |
} |
||||
|
|
||||
async invit (ftId: number, targetFtId: number): Promise<any> { |
async invit(ftId: number, targetFtId: number): Promise<any> { |
||||
const user = await this.usersRepository.findOne({ |
const user: User = (await this.usersRepository.findOne({ |
||||
where: { ftId }, |
where: { ftId }, |
||||
relations: { |
relations: { |
||||
followers: true, |
followers: true, |
||||
friends: true |
friends: true, |
||||
} |
}, |
||||
}) |
}))!; |
||||
if (user == null) { |
if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) |
||||
return new NotFoundException(`Error: user id ${ftId} isn't in our db.`) |
return new BadRequestException("You are already friends."); |
||||
} |
const target = (await this.usersRepository.findOne({ |
||||
if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { |
|
||||
return null |
|
||||
} |
|
||||
const target = await this.usersRepository.findOne({ |
|
||||
where: { ftId: targetFtId }, |
where: { ftId: targetFtId }, |
||||
relations: { |
relations: { |
||||
followers: true, |
followers: true, |
||||
friends: true |
friends: true, |
||||
} |
}, |
||||
}) |
}))!; |
||||
if (target == null) { |
|
||||
return new NotFoundException( |
|
||||
`Error: user id ${targetFtId} isn't in our db.` |
|
||||
) |
|
||||
} |
|
||||
const id = user.followers.findIndex( |
const id = user.followers.findIndex( |
||||
(follower) => follower.ftId === targetFtId |
(follower) => follower.ftId === targetFtId |
||||
) |
); |
||||
if (id !== -1) { |
if (id !== -1) { |
||||
console.log( |
user.friends.push(target); |
||||
`Friend relation complete between ${user.username} and ${target.username}` |
if (user.ftId !== target.ftId) target.friends.push(user); |
||||
) |
user.followers.slice(id, 1); |
||||
user.friends.push(target) |
await this.usersRepository.save(user); |
||||
if (user.ftId !== target.ftId) target.friends.push(user) |
} else target.followers.push(user); |
||||
user.followers.slice(id, 1) |
await this.usersRepository.save(target); |
||||
await this.usersRepository.save(user) |
|
||||
} else { |
|
||||
console.log(`You asked ${target.username} to be your friend.`) |
|
||||
target.followers.push(user) |
|
||||
} |
|
||||
await this.usersRepository.save(target) |
|
||||
} |
} |
||||
} |
} |
||||
|
Loading…
Reference in new issue