Browse Source

fix profile username change and avatar sizing

master
nicolas-arnaud 2 years ago
parent
commit
042e6879b1
  1. 1
      .gitignore
  2. 2
      back/volume/src/app.module.ts
  3. 3
      back/volume/src/auth/42.strategy.ts
  4. 2
      back/volume/src/auth/auth.controller.ts
  5. 3
      back/volume/src/users/dto/user.dto.ts
  6. 8
      back/volume/src/users/entity/user.entity.ts
  7. 9
      back/volume/src/users/users.controller.ts
  8. 6
      back/volume/src/users/users.module.ts
  9. 10
      back/volume/src/users/users.service.ts
  10. 6
      front/volume/src/App.svelte
  11. 15
      front/volume/src/components/Profile.svelte

1
.gitignore

@ -1 +1,2 @@
.env
postgres

2
back/volume/src/app.module.ts

@ -2,6 +2,7 @@ import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import * as Joi from 'joi'
import { ScheduleModule } from '@nestjs/schedule'
import { AuthModule } from './auth/auth.module'
import { ChatModule } from './chat/chat.module'
@ -10,6 +11,7 @@ import { UsersModule } from './users/users.module'
@Module({
imports: [
ScheduleModule.forRoot(),
ConfigModule.forRoot({
validationSchema: Joi.object({
POSTGRES_HOST: Joi.string().required(),

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

@ -7,7 +7,6 @@ import { createWriteStream } from 'fs'
import { UsersService } from 'src/users/users.service'
import { User } from 'src/users/entity/user.entity'
import { randomUUID } from 'crypto'
@Injectable()
export class FtStrategy extends PassportStrategy(Strategy, '42') {
@ -37,10 +36,8 @@ export class FtStrategy extends PassportStrategy(Strategy, '42') {
if ((await this.usersService.findUser(ftId)) === null) {
const newUser = new User()
newUser.ftId = profile.id as number
newUser.socketKey = randomUUID()
newUser.username = profile.username as string
newUser.avatar = `${ftId}.jpg`
newUser.lastAccess = Date.now()
void this.usersService.create(newUser)
const file = createWriteStream(`avatars/${ftId}.jpg`)
get(profile._json.image.versions.small, function (response) {

2
back/volume/src/auth/auth.controller.ts

@ -9,7 +9,7 @@ import { FtUser } from './42.decorator'
export class AuthController {
@Get('in')
@UseGuards(FtOauthGuard)
ftAuth () {}
ftAuth (): void {}
@Get('inReturn')
@UseGuards(FtOauthGuard)

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

@ -14,6 +14,9 @@ export class UserDto {
@IsOptional()
readonly status: string
@IsOptional()
readonly avatar: string
}
export class AvatarUploadDto {

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

@ -7,6 +7,7 @@ import {
JoinTable,
UpdateDateColumn
} from 'typeorm'
import { randomUUID } from 'crypto'
import Message from 'src/chat/entity/message.entity'
import Channel from 'src/chat/entity/channel.entity'
@ -17,13 +18,13 @@ export class User {
@PrimaryGeneratedColumn()
id: number
@Column({type: "bigint"})
@Column({ type: 'bigint', default: Date.now() })
lastAccess: number
@Column({ unique: true })
ftId: number
@Column({ unique: true })
@Column({ unique: true, default: randomUUID() })
socketKey: string
@Column({ unique: true })
@ -32,7 +33,7 @@ export class User {
@Column({ default: 'online' })
status: string
@Column({ name: 'avatar' })
@Column({ name: 'avatar'})
avatar: string
@Column({ default: 0 })
@ -71,7 +72,6 @@ export class User {
@ManyToMany(() => User)
@JoinTable()
friends: User[]
}
export default User

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

@ -82,7 +82,9 @@ export class UsersController {
@Get('history/:id')
@UseGuards(AuthenticatedGuard)
async getHistoryById (@Param('id', ParseIntPipe) id: number): Promise<Result[]> {
async getHistoryById (
@Param('id', ParseIntPipe) id: number
): Promise<Result[]> {
return await this.pongService.getHistoryById(id)
}
@ -180,7 +182,10 @@ export class UsersController {
@Post()
@UseGuards(AuthenticatedGuard)
async create (@Body() payload: UserDto, @FtUser() profile: Profile): Promise<User | null> {
async create (
@Body() payload: UserDto,
@FtUser() profile: Profile
): Promise<User | null> {
const user = await this.usersService.findUser(profile.id)
if (user != null) {
return await this.usersService.update(user, payload)

6
back/volume/src/users/users.module.ts

@ -4,10 +4,12 @@ import { User } from './entity/user.entity'
import { UsersController } from './users.controller'
import { UsersService } from './users.service'
import { PongModule } from 'src/pong/pong.module'
import { ScheduleModule } from '@nestjs/schedule'
@Module({
imports: [forwardRef(() => PongModule), TypeOrmModule.forFeature([User]), ScheduleModule.forRoot()],
imports: [
forwardRef(() => PongModule),
TypeOrmModule.forFeature([User])
],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService]

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

@ -34,18 +34,18 @@ export class UsersService {
async updateStatus() {
let users = await this.usersRepository.find({})
users.forEach((usr) => {
if (Date.now() - usr.lastAccess > 60) {
usr.status= "offline"
if (Date.now() - usr.lastAccess > 60000) {
usr.status = 'offline'
this.usersRepository.save(usr)
}
})
}
async findUser (ftId: number): Promise<User | null> {
let user = await this.usersRepository.findOneBy({ ftId })
if (!user) return null
const user = await this.usersRepository.findOneBy({ ftId })
if (user == null) return null
user.lastAccess = Date.now()
user.status = "online"
user.status = 'online'
this.usersRepository.save(user)
return user
}

6
front/volume/src/App.svelte

@ -24,7 +24,7 @@
});
setInterval(() => {
getUser();
},15000)
}, 15000);
let isProfileOpen = false;
function clickProfile() {
@ -90,7 +90,7 @@
friendsInterval = setInterval(async () => {
friends = await getFriends();
invits = await getInvits();
}, 5000)
}, 5000);
}
// SPECTATE
@ -198,7 +198,7 @@
<div
on:click={() => {
isFriendOpen = false;
clearInterval(friendsInterval)
clearInterval(friendsInterval);
}}
on:keydown={() => {
isFriendOpen = false;

15
front/volume/src/components/Profile.svelte

@ -1,7 +1,7 @@
<script lang="ts">
import { API_URL, store, logout } from "../Auth";
export let edit = false
export let edit = 0
export let user = {
username: "",
wins: 0,
@ -15,12 +15,12 @@
let response = await fetch(API_URL, {
headers: { "content-type": "application/json" },
method: "POST",
body: JSON.stringify({ username: username }),
body: JSON.stringify({ username: user.username }),
credentials: "include",
});
if (response.ok) {
alert("Succefully changed username.");
$store.username = username;
$store.username = user.username;
}
}
@ -54,7 +54,7 @@
<input type="file" id="avatar-input" name="avatar" on:change={submitAvatar} />
</form>
<label class="img-class" for="avatar-input">
<img src={API_URL + "/avatar"} alt="avatar"/>
<img src={API_URL + "/avatar"} alt="avatar" class="profile-img"/>
</label>
{/if}
</div>
@ -109,14 +109,13 @@
}
.profile-header {
width: 80px;
height: 80px;
margin:auto;
justify-content: center;
}
.two-factor-auth {
margin-top: 1rem;
.profile-img {
width: 256px;
height: 256px;
}
#avatar-input {

Loading…
Cancel
Save