Skip to content

Commit 44e0b0b

Browse files
committed
send message frontend fixes
1 parent c86a5bc commit 44e0b0b

11 files changed

Lines changed: 82 additions & 161 deletions

File tree

MangoAPI.Application/Services/MailgunSettings.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

MangoAPI.BusinessLogic/ApiCommands/Messages/SendMessageCommandHandler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,22 @@ public async Task<Result<SendMessageResponse>> Handle(
9696

9797
await dbContext.SaveChangesAsync(cancellationToken);
9898

99+
var authorPictureUrl = $"{blobServiceSettings.MangoBlobAccess}/{user.Image}";
100+
101+
var attachmentUrl = attachmentUniqueFileName == null
102+
? null
103+
: $"{blobServiceSettings.MangoBlobAccess}/{attachmentUniqueFileName}";
104+
99105
var messageDto = messageEntity.ToMessage(
100106
user.DisplayName,
101107
user.Id,
102-
user.Image,
103-
blobServiceSettings.MangoBlobAccess,
104-
user.DisplayNameColour);
108+
user.DisplayNameColour,
109+
authorPictureUrl,
110+
attachmentUrl);
105111

106112
await hubContext.Clients.Group(request.ChatId.ToString()).BroadcastMessageAsync(messageDto);
107113

108-
return responseFactory.SuccessResponse(SendMessageResponse.FromSuccess(messageEntity.Id));
114+
return responseFactory.SuccessResponse(SendMessageResponse.FromSuccess(messageEntity.Id, attachmentUrl));
109115
}
110116

111117
private async Task<string> UploadAttachmentIfExistsAsync(SendMessageCommand request)

MangoAPI.BusinessLogic/ApiCommands/Messages/SendMessageResponse.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ public record SendMessageResponse : ResponseBase
1010
[DefaultValue("01108b4a-27f0-4be4-a9bd-e6e71feccd46")]
1111
public Guid MessageId { get; init; }
1212

13-
public static SendMessageResponse FromSuccess(Guid messageId)
13+
[DefaultValue("http://127.0.0.1:10000/devstoreaccount1/testcontainer/image.jpg")]
14+
public string AttachmentUrl { get; init; }
15+
16+
public static SendMessageResponse FromSuccess(Guid messageId, string attachmentUrl)
1417
{
1518
return new SendMessageResponse
1619
{
1720
Success = true,
1821
Message = ResponseMessageCodes.Success,
1922
MessageId = messageId,
23+
AttachmentUrl = attachmentUrl
2024
};
2125
}
2226
}

MangoAPI.BusinessLogic/Models/Message.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,27 @@ public record Message
1919
[DefaultValue("11aed827-db8a-47de-bc81-13337703091f")]
2020
public Guid UserId { get; init; }
2121

22-
[DefaultValue("Amelit")]
23-
public string UserDisplayName { get; init; }
22+
[DefaultValue("Amelit")] public string UserDisplayName { get; init; }
2423

25-
[DefaultValue(5)]
26-
public DisplayNameColour UserDisplayNameColour { get; init; }
24+
[DefaultValue(5)] public DisplayNameColour UserDisplayNameColour { get; init; }
2725

28-
[DefaultValue("Hello World!")]
29-
public string MessageText { get; init; }
26+
[DefaultValue("Hello World!")] public string MessageText { get; init; }
3027

31-
[DefaultValue("12:56")]
32-
public DateTime CreatedAt { get; init; }
28+
[DefaultValue("12:56")] public DateTime CreatedAt { get; init; }
3329

34-
[DefaultValue("12:57")]
35-
public DateTime? UpdatedAt { get; init; }
30+
[DefaultValue("12:57")] public DateTime? UpdatedAt { get; init; }
3631

37-
[DefaultValue(false)]
38-
public bool Self { get; init; }
32+
[DefaultValue(false)] public bool Self { get; init; }
3933

4034
[DefaultValue("https://localhost:5001/Uploads/amelit_picture.jpg")]
4135
public string MessageAuthorPictureUrl { get; init; }
4236

4337
[DefaultValue("https://localhost:5001/Uploads/message_attachment.pdf")]
4438
public string MessageAttachmentUrl { get; init; }
4539

46-
[DefaultValue("John Doe")]
47-
public string InReplayToAuthor { get; init; }
40+
[DefaultValue("John Doe")] public string InReplayToAuthor { get; init; }
4841

49-
[DefaultValue("Hello world!")]
50-
public string InReplayToText { get; init; }
42+
[DefaultValue("Hello world!")] public string InReplayToText { get; init; }
5143
}
5244

5345
public static class MessageMapper
@@ -56,9 +48,9 @@ public static Message ToMessage(
5648
this MessageEntity message,
5749
string displayName,
5850
Guid userId,
59-
string image,
60-
string mangoBlobAccess,
61-
DisplayNameColour displayNameColour)
51+
DisplayNameColour displayNameColour,
52+
string authorPictureUrl,
53+
string attachmentUrl)
6254
{
6355
var messageDto = new Message
6456
{
@@ -73,16 +65,10 @@ public static Message ToMessage(
7365
Self = message.UserId == userId,
7466
InReplayToAuthor = message.InReplayToAuthor,
7567
InReplayToText = message.InReplayToText,
76-
77-
MessageAuthorPictureUrl = image != null
78-
? $"{mangoBlobAccess}/{image}"
79-
: null,
80-
81-
MessageAttachmentUrl = message.AttachmentFileName != null
82-
? $"{mangoBlobAccess}/{message.AttachmentFileName}"
83-
: null,
68+
MessageAuthorPictureUrl = authorPictureUrl,
69+
MessageAttachmentUrl = attachmentUrl
8470
};
8571

8672
return messageDto;
8773
}
88-
}
74+
}

MangoAPI.Client/src/app/app-routing.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ChatsComponent } from './components/chats/chats.component';
66
import { ContactsComponent } from './components/contacts/contacts.component';
77
import { SettingsComponent } from './components/settings/settings.component';
88
import { CreateGroupComponent } from './components/create-group/create-group.component';
9+
import { NotFoundComponent } from './components/not-found/not-found.component';
910

1011
const routes: Routes = [
1112
{ path: 'register', component: RegisterComponent },
@@ -14,6 +15,8 @@ const routes: Routes = [
1415
{ path: 'contacts', component: ContactsComponent },
1516
{ path: 'settings', component: SettingsComponent },
1617
{ path: 'createGroup', component: CreateGroupComponent },
18+
{ path: '', redirectTo: '/chats', pathMatch: 'full' },
19+
{ path: '**', component: NotFoundComponent }
1720
];
1821

1922
@NgModule({

MangoAPI.Client/src/app/components/chats/chats.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div
44
*ngIf="_modalWindowStateService.isModalWindowShowing"
55
class="black-cover"
6-
(click)="closeModalWindowrClick()"
6+
(click)="closeModalWindowClick()"
77
>
88
<img
99
*ngIf="_modalWindowStateService.picture"
@@ -191,7 +191,7 @@ <h5>Chats</h5>
191191

192192
<span *ngIf="!activeChat.isArchived; else elseBlock">Archive Chat</span>
193193
<ng-template #elseBlock>
194-
<span>Unarchive Chat</span>
194+
<span>Un-Archive Chat</span>
195195
</ng-template>
196196
</div>
197197
<div

MangoAPI.Client/src/app/components/chats/chats.component.ts

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ModalWindowStateService } from './../../services/states/modalWindowState.service';
1+
import { ModalWindowStateService } from '../../services/states/modalWindowState.service';
22
import { Component, OnDestroy, OnInit } from '@angular/core';
33
import { TokensService } from '../../services/messenger/tokens.service';
44
import { Chat } from '../../types/models/Chat';
@@ -12,16 +12,15 @@ import { CommunityType } from '../../types/enums/CommunityType';
1212
import { RoutingConstants } from '../../types/constants/RoutingConstants';
1313
import { UserChatsService } from '../../services/api/user-chats.service';
1414
import { ValidationService } from '../../services/messenger/validation.service';
15-
import { SendMessageCommand } from '../../types/requests/SendMessageCommand';
1615
import * as signalR from '@microsoft/signalr';
1716
import { EditMessageNotification } from '../../types/models/EditMessageNotification';
1817
import { DeleteMessageNotification } from '../../types/models/DeleteMessageNotification';
19-
import { Subject, takeUntil } from 'rxjs';
18+
import { firstValueFrom, Subject, takeUntil } from 'rxjs';
2019
import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours';
21-
import { UsersService } from 'src/app/services/api/users.service';
2220
import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand';
2321
import { DocumentsService } from 'src/app/services/api/documents.service';
2422
import ApiBaseService from 'src/app/services/api/apiBase.service';
23+
import { SendMessageResponse } from '../../types/responses/SendMessageResponse';
2524

2625
@Component({
2726
selector: 'app-chats',
@@ -152,25 +151,13 @@ export class ChatsComponent implements OnInit, OnDestroy {
152151
}
153152

154153
onOpenImageClick(imageLink: string): void {
155-
this._modalWindowStateService.setIsModalWindowShowing(true)
156-
this._modalWindowStateService.setPicture(imageLink)
154+
this._modalWindowStateService.setIsModalWindowShowing(true);
155+
this._modalWindowStateService.setPicture(imageLink);
157156
}
158157

159-
closeModalWindowrClick(): void {
160-
this._modalWindowStateService.setIsModalWindowShowing(false)
161-
this._modalWindowStateService.setPictureNull()
162-
}
163-
164-
async uploadFile(file: File) {
165-
let fileName = null;
166-
if (file) {
167-
const formData = new FormData();
168-
formData.append('formFile', file, file.name);
169-
const response = await this._documentsService.uploadDocument(formData).toPromise();
170-
this.messageAttachmentUrl = response?.fileUrl ?? '';
171-
fileName = response?.fileName ?? '';
172-
}
173-
return fileName;
158+
closeModalWindowClick(): void {
159+
this._modalWindowStateService.setIsModalWindowShowing(false);
160+
this._modalWindowStateService.setPictureNull();
174161
}
175162

176163
setSignalRMethods(): void {
@@ -285,6 +272,10 @@ export class ChatsComponent implements OnInit, OnDestroy {
285272
}
286273

287274
loadChat(chatId: string): void {
275+
if (this.activeChatId === chatId) {
276+
return;
277+
}
278+
288279
this.activeChatId = chatId;
289280
this.activeChat = this.chats.filter((x) => x.chatId === this.activeChatId)[0];
290281
this.getChatMessages(this.activeChatId);
@@ -411,7 +402,7 @@ export class ChatsComponent implements OnInit, OnDestroy {
411402
});
412403
}
413404

414-
async onSendMessageClick() {
405+
public async onSendMessageClick() {
415406
const newMessageText = this.messageText.repeat(1); // deep copy
416407

417408
const messageTextValidationResult = this._validationService.validateField(
@@ -432,7 +423,16 @@ export class ChatsComponent implements OnInit, OnDestroy {
432423

433424
const isoString = new Date().toISOString();
434425
const messageId = crypto.randomUUID();
435-
const sendMessageCommand = new SendMessageCommand(this.messageText, this.activeChatId);
426+
const sendMessageFormData = new FormData();
427+
428+
sendMessageFormData.append('messageText', newMessageText);
429+
sendMessageFormData.append('chatId', this.activeChatId);
430+
sendMessageFormData.append('messageId', messageId);
431+
432+
if (this.messageAttachment) {
433+
sendMessageFormData.append('Attachment', this.messageAttachment);
434+
}
435+
436436
const newMessage = new Message(
437437
messageId,
438438
tokens.userId,
@@ -447,35 +447,22 @@ export class ChatsComponent implements OnInit, OnDestroy {
447447

448448
this.clearMessageInput();
449449

450-
if (this.messageAttachment) {
451-
const fileName = await this.uploadFile(this.messageAttachment);
452-
this.messageAttachment = null;
453-
sendMessageCommand.setAttachmentUrl(fileName);
454-
newMessage.setMessageAttachmentUrl(this.messageAttachmentUrl);
455-
}
456-
457450
this.messages.push(newMessage);
458451

459-
sendMessageCommand.setMessageId(messageId);
460-
sendMessageCommand.setCreatedAt(isoString);
452+
const sendMessage$ = this._messagesService.sendMessage(sendMessageFormData);
461453

462-
this._messagesService
463-
.sendMessage(sendMessageCommand)
464-
.pipe(takeUntil(this.componentDestroyed$))
465-
.subscribe({
466-
next: (data) => {
467-
newMessage.messageId = data.messageId;
468-
this.scrollToEnd();
469-
},
470-
error: (error) => {
471-
this._errorNotificationService.notifyOnError(error);
472-
}
473-
});
454+
const response = await firstValueFrom<SendMessageResponse>(sendMessage$);
455+
456+
newMessage.messageId = response.messageId;
457+
newMessage.messageAttachmentUrl = response.attachmentUrl;
458+
459+
this.clearAttachmentInput();
460+
this.scrollToEnd();
474461
}
475462

476-
onEnterClick(event: any): void {
463+
async onEnterClick(event: any) {
477464
event.preventDefault();
478-
this.onSendMessageClick().then((r) => r);
465+
await this.onSendMessageClick().then((r) => r);
479466
}
480467

481468
private clearMessageInput(): void {
@@ -543,4 +530,15 @@ export class ChatsComponent implements OnInit, OnDestroy {
543530
this.componentDestroyed$.next(true);
544531
this.componentDestroyed$.complete();
545532
}
533+
534+
clearAttachmentInput() {
535+
const fileInput = document.getElementById('attachment') as HTMLInputElement;
536+
537+
if (!fileInput) {
538+
return;
539+
}
540+
541+
fileInput.value = '';
542+
this.messageAttachment = null;
543+
}
546544
}

MangoAPI.Client/src/app/services/api/messages.service.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
22
import { HttpClient, HttpHeaders } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { GetChatMessagesResponse } from '../../types/responses/GetChatMessagesResponse';
5-
import { SendMessageCommand } from '../../types/requests/SendMessageCommand';
65
import { SendMessageResponse } from '../../types/responses/SendMessageResponse';
76
import { DeleteMessageResponse } from '../../types/responses/DeleteMessageResponse';
87
import { EditMessageCommand } from '../../types/requests/EditMessageCommand';
@@ -24,17 +23,12 @@ export class MessagesService extends ApiBaseService {
2423

2524
// GET /api/messages/{chatId}
2625
getChatMessages(chatId: string): Observable<GetChatMessagesResponse> {
27-
return this.httpClient.get<GetChatMessagesResponse>(
28-
this.baseUrl + this.messagesRoute + chatId
29-
);
26+
return this.httpClient.get<GetChatMessagesResponse>(this.baseUrl + this.messagesRoute + chatId);
3027
}
3128

3229
// POST /api/messages
33-
sendMessage(request: SendMessageCommand): Observable<SendMessageResponse> {
34-
return this.httpClient.post<SendMessageResponse>(
35-
this.baseUrl + this.messagesRoute,
36-
request
37-
);
30+
sendMessage(request: FormData): Observable<SendMessageResponse> {
31+
return this.httpClient.post<SendMessageResponse>(this.baseUrl + this.messagesRoute, request);
3832
}
3933

4034
// DELETE /api/messages/{messageId}

MangoAPI.Client/src/app/types/models/Message.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,4 @@ export class Message {
3636
this.self = self;
3737
this.messageAuthorPictureUrl = messageAuthorPictureUrl;
3838
}
39-
40-
setMessageAttachmentUrl(url: string | null): void {
41-
this.messageAttachmentUrl = url;
42-
}
4339
}

0 commit comments

Comments
 (0)