From d02781b13abe25ef1ba6eaa466c9895f453656f9 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Thu, 23 Feb 2023 11:39:58 +0100 Subject: [PATCH] Added typing indicators --- .../Services/Support/SupportAdminService.cs | 56 +++++++++++++++++++ .../Services/Support/SupportClientService.cs | 56 +++++++++++++++++++ .../Shared/Views/Admin/Support/View.razor | 54 ++++++++++++++++-- Moonlight/Shared/Views/Support.razor | 43 +++++++++++++- Moonlight/resources/lang/de_de.lang | 3 + Moonlight/wwwroot/assets/css/utils.css | 27 +++++++++ 6 files changed, 233 insertions(+), 6 deletions(-) diff --git a/Moonlight/App/Services/Support/SupportAdminService.cs b/Moonlight/App/Services/Support/SupportAdminService.cs index ce11083..682ddf6 100644 --- a/Moonlight/App/Services/Support/SupportAdminService.cs +++ b/Moonlight/App/Services/Support/SupportAdminService.cs @@ -11,6 +11,9 @@ public class SupportAdminService public EventHandler OnNewMessage; + public EventHandler OnUpdateTyping; + private List TypingUsers = new(); + private User Self; private User Recipient; @@ -38,6 +41,48 @@ public class SupportAdminService return Task.CompletedTask; }); + + MessageService.Subscribe( + $"support.{Self.Id}.typing", + this, + user => + { + HandleTyping(user); + return Task.CompletedTask; + }); + } + + private void HandleTyping(User user) + { + var name = $"{user.FirstName} {user.LastName}"; + + lock (TypingUsers) + { + if (!TypingUsers.Contains(name)) + { + TypingUsers.Add(name); + OnUpdateTyping!.Invoke(this, null!); + + Task.Run(async () => + { + await Task.Delay(TimeSpan.FromSeconds(5)); + + if (TypingUsers.Contains(name)) + { + TypingUsers.Remove(name); + OnUpdateTyping!.Invoke(this, null!); + } + }); + } + } + } + + public string[] GetTypingUsers() + { + lock (TypingUsers) + { + return TypingUsers.ToArray(); + } } public async Task GetMessages() @@ -65,8 +110,19 @@ public class SupportAdminService await SupportServerService.Close(Recipient); } + public Task TriggerTyping() + { + Task.Run(async () => + { + await MessageService.Emit($"support.{Recipient.Id}.admintyping", Self); + }); + + return Task.CompletedTask; + } + public void Dispose() { MessageService.Unsubscribe($"support.{Recipient.Id}.message", this); + MessageService.Unsubscribe($"support.{Recipient.Id}.typing", this); } } \ No newline at end of file diff --git a/Moonlight/App/Services/Support/SupportClientService.cs b/Moonlight/App/Services/Support/SupportClientService.cs index c08b9e0..328b3a9 100644 --- a/Moonlight/App/Services/Support/SupportClientService.cs +++ b/Moonlight/App/Services/Support/SupportClientService.cs @@ -10,6 +10,9 @@ public class SupportClientService : IDisposable private readonly MessageService MessageService; public EventHandler OnNewMessage; + + public EventHandler OnUpdateTyping; + private List TypingUsers = new(); private User Self; @@ -36,6 +39,48 @@ public class SupportClientService : IDisposable return Task.CompletedTask; }); + + MessageService.Subscribe( + $"support.{Self.Id}.admintyping", + this, + user => + { + HandleTyping(user); + return Task.CompletedTask; + }); + } + + private void HandleTyping(User user) + { + var name = $"{user.FirstName} {user.LastName}"; + + lock (TypingUsers) + { + if (!TypingUsers.Contains(name)) + { + TypingUsers.Add(name); + OnUpdateTyping!.Invoke(this, null!); + + Task.Run(async () => + { + await Task.Delay(TimeSpan.FromSeconds(5)); + + if (TypingUsers.Contains(name)) + { + TypingUsers.Remove(name); + OnUpdateTyping!.Invoke(this, null!); + } + }); + } + } + } + + public string[] GetTypingUsers() + { + lock (TypingUsers) + { + return TypingUsers.ToArray(); + } } public async Task GetMessages() @@ -56,9 +101,20 @@ public class SupportClientService : IDisposable Self ); } + + public Task TriggerTyping() + { + Task.Run(async () => + { + await MessageService.Emit($"support.{Self.Id}.typing", Self); + }); + + return Task.CompletedTask; + } public void Dispose() { MessageService.Unsubscribe($"support.{Self.Id}.message", this); + MessageService.Unsubscribe($"support.{Self.Id}.admintyping", this); } } \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Support/View.razor b/Moonlight/Shared/Views/Admin/Support/View.razor index 8300475..e81ede7 100644 --- a/Moonlight/Shared/Views/Admin/Support/View.razor +++ b/Moonlight/Shared/Views/Admin/Support/View.razor @@ -51,7 +51,7 @@ Logo - +
@if (message.IsSystem) { @@ -80,7 +80,7 @@ @(Formatter.FormatAgoFromDateTime(message.CreatedAt, SmartTranslateService))
- +
@(message.Message)
@@ -92,8 +92,34 @@