Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion MangoAPI.BusinessLogic/HubConfig/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Threading.Tasks;
using MangoAPI.BusinessLogic.Notifications;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using System;

namespace MangoAPI.BusinessLogic.HubConfig;

Expand All @@ -9,4 +11,11 @@ public Task SubscribeToGroup(string groupId)
{
return Groups.AddToGroupAsync(Context.ConnectionId, groupId);
}

public Task ShowTyping(Guid userId, Guid groupId, string displayName)
{
var notification = new TypingEventNotification(userId, groupId, displayName);

return Clients.Group(groupId.ToString()).PrivateChatSentTypingEventAsync(notification);
}
}
5 changes: 5 additions & 0 deletions MangoAPI.BusinessLogic/HubConfig/IHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public interface IHubClient
/// Notifies chat subscribers on the message delete via SignalR.
/// </summary>
Task MessageDeletedAsync(DeleteMessageNotification notification);

/// <summary>
/// Notifies chat subscribers on the typing event via SignalR.
/// </summary>
Task PrivateChatSentTypingEventAsync(TypingEventNotification notification);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

namespace MangoAPI.BusinessLogic.Notifications;

public record TypingEventNotification(Guid UserId, Guid ChatId, string DisplayName);
281 changes: 83 additions & 198 deletions MangoAPI.Client/src/app/components/chats/chats.component.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@
}
.color-orange {
color: #ffc588;
}
}
57 changes: 57 additions & 0 deletions MangoAPI.Client/src/app/components/chats/chats.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { SignalrConstants } from './signalr.constants';
import { SendMessageNotification } from '../../types/notifications/SendMessageNotification';
import { PrivateChatDeletedNotification } from '../../types/notifications/PrivateChatDeletedNotification';
import { PrivateChatCreatedNotification } from '../../types/notifications/PrivateChatCreatedNotification';
import { Tokens } from '../../types/models/Tokens';
import { TypingEventNotification } from 'src/app/types/notifications/TypingEventNotification';

@Component({
selector: 'app-chats',
Expand Down Expand Up @@ -57,8 +59,12 @@ export class ChatsComponent implements OnInit {
private signalRConnected = false;
public realTimeConnections: string[] = [];
public userId: string | undefined = '';
public userData: Tokens | undefined;
Comment thread
kolosovpetro marked this conversation as resolved.
Outdated
public chats: Chat[] = [];

public typingEventArray: TypingEventNotification[] = [];
public typingMessage = '';
Comment thread
kolosovpetro marked this conversation as resolved.
Outdated

public activeChat: Chat = this._defaultChatHelper.getEmptyChat();

public activeChatId = '';
Expand Down Expand Up @@ -90,6 +96,7 @@ export class ChatsComponent implements OnInit {
}

this.userId = tokens.userId;
this.userData = tokens;
Comment thread
kolosovpetro marked this conversation as resolved.
Outdated
this.chatFilter = 'All chats';

const chatsSub$ = this._communitiesService.getUserChats();
Expand Down Expand Up @@ -158,6 +165,13 @@ export class ChatsComponent implements OnInit {
this.onMessageDeleteHandler(notification);
}
);

this.connection.on(
this.signalRConstants.PrivateChatSentTypingEventAsync,
(notification: TypingEventNotification) => {
this.onTypingEventHandler(notification);
}
);
}

private onPrivateChatCreatedHandler(notification: PrivateChatCreatedNotification) {
Expand Down Expand Up @@ -521,6 +535,36 @@ export class ChatsComponent implements OnInit {
return message;
}

private onTypingEventHandler(notification: TypingEventNotification) {
if (notification.userId === this.userId) return;

const existingNotification = this.typingEventArray.find((x) => x.userId === notification.userId);

if (existingNotification) {
clearTimeout(existingNotification.timeout);
existingNotification.timeout = setTimeout(
() => this.deleteTypingEventFromArray(notification.userId),
1000
);
return;
}

notification.timeout = setTimeout(
() => this.deleteTypingEventFromArray(notification.userId),
1000
);

this.typingEventArray.push(notification);
}

private deleteTypingEventFromArray(userId: string) {
const index = this.typingEventArray.findIndex((item) => item.userId === userId);

if (index !== -1) {
this.typingEventArray.splice(index, 1);
}
}

private onMessageDeleteHandler(notification: DeleteMessageNotification) {
const selfMessage = notification.userId === this.userId;

Expand All @@ -545,6 +589,19 @@ export class ChatsComponent implements OnInit {
}
}

onTypingHandler(event: KeyboardEvent) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!this.userData) return;

if (event.key.match(/^[a-zA-Zа-яА-ЯёЁ0-9+\-[\]{}(),./'"]$/)) {
this.connection.invoke(
Comment thread
kolosovpetro marked this conversation as resolved.
Outdated
this.signalRConstants.ShowTyping,
this.userData.userId,
this.activeChatId,
this.userData.userDisplayName
);
}
}

private clearMessageInput(): void {
this.messageText = '';
}
Expand Down
3 changes: 3 additions & 0 deletions MangoAPI.Client/src/app/components/chats/signalr.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export class SignalrConstants {
static PrivateChatCreatedAsync = 'PrivateChatCreatedAsync';
static PrivateChatDeletedAsync = 'PrivateChatDeletedAsync';
static MessageDeletedAsync = 'MessageDeletedAsync';
static PrivateChatSentTypingEventAsync = 'PrivateChatSentTypingEventAsync';

static SubscribeToGroup = 'SubscribeToGroup';
static ShowTyping = 'ShowTyping';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface TypingEventNotification {
userId: string;
chatId: string;
displayName: string;

timeout: NodeJS.Timeout;
}
Loading