WalidMoovin 2 years ago
parent
commit
398dd4a21e
  1. 76
      back/volume/src/users/users.controller.ts
  2. 55
      front/volume/src/components/Chat2.svelte
  3. 7
      front/volume/src/components/Profile.svelte

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

@ -43,28 +43,6 @@ export class UsersController {
return await this.usersService.findOnlineUsers()
}
@Get(':id')
async getUserById (@Param('id', ParseIntPipe) ftId: number): Promise<User | null> {
return await this.usersService.findUser(ftId)
}
@Get()
@UseGuards(AuthenticatedGuard)
async getUser (@FtUser() profile: Profile): Promise<User | null> {
return await this.usersService.findUser(profile.id)
}
@Post()
@UseGuards(AuthenticatedGuard)
async create (@Body() payload: UserDto, @FtUser() profile: Profile) {
const user = await this.usersService.findUser(profile.id)
if (user) {
return await this.usersService.update(user.id, payload)
} else {
return await this.usersService.create(payload)
}
}
@Get('friends')
@UseGuards(AuthenticatedGuard)
async getFriends (@FtUser() profile: Profile) {
@ -77,15 +55,6 @@ export class UsersController {
return await this.usersService.getInvits(profile.id)
}
@Post('invit/:id')
@UseGuards(AuthenticatedGuard)
async invitUser (
@FtUser() profile: Profile,
@Param('id', ParseIntPipe) id: number
) {
return await this.usersService.invit(profile.id, id)
}
@Post('avatar')
@UseGuards(AuthenticatedGuard)
@UseInterceptors(
@ -114,7 +83,24 @@ export class UsersController {
return await this.usersService.addAvatar(profile.id, file.filename)
}
@Get(':id/avatar')
@Get('avatar')
@UseGuards(AuthenticatedGuard)
async getAvatar (
@FtUser() profile: Profile,
@Res({ passthrough: true }) response: Response
){
const user = await this.usersService.findUser(profile.id)
if (!user) return
const filename = user.avatar
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename))
response.set({
'Content-Diposition': `inline; filename="${filename}"`,
'Content-Type': 'image/jpg'
})
return new StreamableFile(stream)
}
@Get('avatar/:id')
async getAvatarById (
@Param('id', ParseIntPipe) ftId: number,
@Res({ passthrough: true }) response: Response
@ -130,11 +116,25 @@ export class UsersController {
return new StreamableFile(stream)
}
@Get('avatar')
async getAvatar (
@FtUser() profile: Profile,
@Res({ passthrough: true }) response: Response
) {
return await this.getAvatarById(profile.id, response);
@Get(':id')
async getUserById (@Param('id', ParseIntPipe) ftId: number): Promise<User | null> {
return await this.usersService.findUser(ftId)
}
@Get()
@UseGuards(AuthenticatedGuard)
async getUser (@FtUser() profile: Profile): Promise<User | null> {
return await this.usersService.findUser(profile.id)
}
@Post()
@UseGuards(AuthenticatedGuard)
async create (@Body() payload: UserDto, @FtUser() profile: Profile) {
const user = await this.usersService.findUser(profile.id)
if (user) {
return await this.usersService.update(user.id, payload)
} else {
return await this.usersService.create(payload)
}
}
}

55
front/volume/src/components/Chat2.svelte

@ -15,8 +15,12 @@
name: "You",
text: newText,
};
chatMessages = [...chatMessages.slice(-7 + 1), newMessage];
chatMessages = [...chatMessages.slice(-5 + 1), newMessage];
newText = "";
const messagesDiv = document.querySelector(".messages");
if (messagesDiv) {
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
}
// TODO: save to database
};
@ -24,18 +28,25 @@
export let chatMessages: Array<chatMessagesType> = [];
let newText = "";
// const openProfile = (id: number) => (event: Event) => {
// const message = chatMessages.find((m) => m.id === id);
// if (message) {
// const { name } = message;
// const options = ["View profile", "Send private message", "Block user"];
// const option = prompt(`Select an option for ${name}: ${options.join(", ")}`);
// if (option === "View profile") {
// } else if (option === "Send private message") {
// } else if (option === "Block user") {
// }
// }
// };
const openProfile = (id: number) => (event: Event) => {
const message = chatMessages.find((m) => m.id === id);
if (message) {
const optionsModal = document.createElement("div");
optionsModal.classList.add("options-modal");
optionsModal.innerHTML = `
<h3>${message.name}</h3>
<ul>
<li>View profile</li>
<li>View posts</li>
<li>View comments</li>
</ul>
`;
document.querySelector('.overlay')?.appendChild(optionsModal);
optionsModal.addEventListener("click", () => {
document.body.removeChild(optionsModal);
});
}
};
</script>
<div class="overlay">
@ -43,7 +54,7 @@
<div class="messages">
{#each chatMessages as message}
<p class="message">
<span class="message-name">
<span class="message-name" on:click={openProfile(message.id)} on:keydown={openProfile(message.id)} style="cursor: pointer;">
{message.name}
</span>: {message.text}
</p>
@ -79,4 +90,20 @@
padding: 1rem;
width: 300px;
}
.messages {
height: 200px;
overflow-y: scroll;
}
.options-modal {
position: absolute;
top: 0;
left: 0;
background-color: white;
padding: 1rem;
border: 1px solid #ccc;
border-radius: 5px;
z-index: 9999;
}
</style>

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

@ -1,4 +1,5 @@
<script lang="ts">
let api = "http://" + import.meta.env.VITE_HOST + ":" + import.meta.env.VITE_BACK_PORT
export let username = "";
export let realname = "";
export let wins = 0;
@ -34,7 +35,7 @@
formData.append('avatar', file);
try {
const response = await fetch('http://localhost:3001/avatar', {
const response = await fetch(api + '/avatar', {
method: 'POST',
body: formData
});
@ -56,10 +57,10 @@
<div class="profile-header">
<img class="profile-img" src="img/profileicon.png" alt="Profile Icon" />
<h3>{realname}</h3>
<form>
<form on:submit={handleAvatarUpload}>
<label for="avatar-input">Choose avatar:</label>
<input type="file" id="avatar-input" accept="image/*">
<button type="submit" id="upload-button" on:click={handleAvatarUpload}>Upload</button>
<button type="submit" id="upload-button">Upload</button>
</form>
</div>
<div class="profile-body">

Loading…
Cancel
Save