From 28ff78d3e2b8418c0e4286b369bdf9a2dab5ae2f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 31 May 2025 19:54:10 +0800 Subject: [PATCH] :loud_sound: Add logs to notification pushing --- .../Account/NotificationService.cs | 14 +++++++-- DysonNetwork.Sphere/Chat/ChatService.cs | 30 ++++++++++++------- DysonNetwork.sln.DotSettings.user | 1 + 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/DysonNetwork.Sphere/Account/NotificationService.cs b/DysonNetwork.Sphere/Account/NotificationService.cs index 5306b8d..cb73c8b 100644 --- a/DysonNetwork.Sphere/Account/NotificationService.cs +++ b/DysonNetwork.Sphere/Account/NotificationService.cs @@ -226,6 +226,9 @@ public class NotificationService { try { + _logger.LogDebug( + $"Pushing notification {notification.Topic} #{notification.Id} to device #{subscription.DeviceId}"); + var body = string.Empty; switch (subscription.Provider) { @@ -245,7 +248,7 @@ public class NotificationService ["message"] = new Dictionary { ["token"] = subscription.DeviceToken, - ["notification"] = new Dictionary + ["notification"] = new Dictionary { ["title"] = notification.Title ?? string.Empty, ["body"] = body @@ -265,14 +268,14 @@ public class NotificationService throw new InvalidOperationException("The apple notification push service is not initialized."); var alertDict = new Dictionary(); - + if (!string.IsNullOrEmpty(notification.Title)) alertDict["title"] = notification.Title; if (!string.IsNullOrEmpty(notification.Subtitle)) alertDict["subtitle"] = notification.Subtitle; if (!string.IsNullOrEmpty(notification.Content)) alertDict["body"] = notification.Content; - + var payload = new Dictionary { ["topic"] = notification.Topic, @@ -303,7 +306,12 @@ public class NotificationService // Log the exception // Consider implementing a retry mechanism // Rethrow or handle as needed + _logger.LogError( + $"Failed to push notification #{notification.Id} to device... {ex.Message} {ex.StackTrace}"); throw new Exception($"Failed to send notification to {subscription.Provider}: {ex.Message}", ex); } + + _logger.LogInformation( + $"Pushed notification #{notification.Id} to device #{subscription.DeviceId}"); } } \ No newline at end of file diff --git a/DysonNetwork.Sphere/Chat/ChatService.cs b/DysonNetwork.Sphere/Chat/ChatService.cs index caa1218..6331678 100644 --- a/DysonNetwork.Sphere/Chat/ChatService.cs +++ b/DysonNetwork.Sphere/Chat/ChatService.cs @@ -69,21 +69,31 @@ public class ChatService( var members = await scopedCrs.ListRoomMembers(room.Id); + var metaDict = + new Dictionary + { + ["sender_name"] = sender.Nick ?? sender.Account.Nick, + ["user_id"] = sender.AccountId, + ["sender_id"] = sender.Id, + ["message_id"] = message.Id, + ["room_id"] = room.Id, + ["images"] = message.Attachments + .Where(a => a.MimeType != null && a.MimeType.StartsWith("image")) + .Select(a => a.Id).ToList() + }; + if (sender.Account.Profile is not { PictureId: null }) + metaDict["pfp"] = sender.Account.Profile.PictureId; + if (!string.IsNullOrEmpty(room.Name)) + metaDict["room_name"] = room.Name; + var notification = new Notification { Topic = "messages.new", Title = $"{sender.Nick ?? sender.Account.Nick ?? "Unknown"} ({roomSubject})", Content = !string.IsNullOrEmpty(message.Content) ? message.Content[..Math.Min(message.Content.Length, 100)] - : "", - Meta = new Dictionary - { - ["message_id"] = message.Id, - ["room_id"] = room.Id, - ["images"] = message.Attachments - .Where(a => a.MimeType != null && a.MimeType.StartsWith("image")) - .Select(a => a.Id).ToList() - }, + : "", + Meta = metaDict, Priority = 10, }; @@ -96,7 +106,7 @@ public class ChatService( Type = type, Data = message }); - + // Only add accounts that aren't null if (member.Account.Id != sender.AccountId) accountsToNotify.Add(member.Account); diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index 7ae25af..4500b73 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -1,6 +1,7 @@  ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded