♻️ Refactored notification meta

This commit is contained in:
2025-11-23 13:20:40 +08:00
parent 2b95d58611
commit c42befed6b
2 changed files with 21 additions and 18 deletions

View File

@@ -202,7 +202,7 @@ public partial class ChatService(
// Create file references if message has attachments // Create file references if message has attachments
await CreateFileReferencesForMessageAsync(message); await CreateFileReferencesForMessageAsync(message);
// Copy the value to ensure the delivery is correct // Copy the value to ensure the delivery is correct
message.Sender = sender; message.Sender = sender;
message.ChatRoom = room; message.ChatRoom = room;
@@ -287,6 +287,7 @@ public partial class ChatService(
if (await scopedCrs.IsSubscribedChatRoom(member.ChatRoomId, member.Id)) if (await scopedCrs.IsSubscribedChatRoom(member.ChatRoomId, member.Id))
subscribedMemberIds.Add(member.AccountId); subscribedMemberIds.Add(member.AccountId);
} }
accountsToNotify = accountsToNotify.Where(a => !subscribedMemberIds.Contains(Guid.Parse(a.Id))).ToList(); accountsToNotify = accountsToNotify.Where(a => !subscribedMemberIds.Contains(Guid.Parse(a.Id))).ToList();
logger.LogInformation("Trying to deliver message to {count} accounts...", accountsToNotify.Count); logger.LogInformation("Trying to deliver message to {count} accounts...", accountsToNotify.Count);
@@ -301,7 +302,8 @@ public partial class ChatService(
logger.LogInformation("Delivered message to {count} accounts.", accountsToNotify.Count); logger.LogInformation("Delivered message to {count} accounts.", accountsToNotify.Count);
} }
private PushNotification BuildNotification(SnChatMessage message, SnChatMember sender, SnChatRoom room, string roomSubject, private PushNotification BuildNotification(SnChatMessage message, SnChatMember sender, SnChatRoom room,
string roomSubject,
string type) string type)
{ {
var metaDict = new Dictionary<string, object> var metaDict = new Dictionary<string, object>
@@ -311,11 +313,14 @@ public partial class ChatService(
["sender_id"] = sender.Id, ["sender_id"] = sender.Id,
["message_id"] = message.Id, ["message_id"] = message.Id,
["room_id"] = room.Id, ["room_id"] = room.Id,
["images"] = message.Attachments
.Where(a => a.MimeType != null && a.MimeType.StartsWith("image"))
.Select(a => a.Id).ToList(),
}; };
var imageId = message.Attachments
.Where(a => a.MimeType != null && a.MimeType.StartsWith("image"))
.Select(a => a.Id).FirstOrDefault();
if (imageId is not null)
metaDict["image"] = imageId;
if (sender.Account!.Profile is not { Picture: null }) if (sender.Account!.Profile is not { Picture: null })
metaDict["pfp"] = sender.Account!.Profile.Picture.Id; metaDict["pfp"] = sender.Account!.Profile.Picture.Id;
if (!string.IsNullOrEmpty(room.Name)) if (!string.IsNullOrEmpty(room.Name))
@@ -365,7 +370,8 @@ public partial class ChatService(
} }
} }
private List<Account> FilterAccountsForNotification(List<SnChatMember> members, SnChatMessage message, SnChatMember sender) private List<Account> FilterAccountsForNotification(List<SnChatMember> members, SnChatMessage message,
SnChatMember sender)
{ {
var now = SystemClock.Instance.GetCurrentInstant(); var now = SystemClock.Instance.GetCurrentInstant();
@@ -824,4 +830,4 @@ public class SyncResponse
public List<SnChatMessage> Messages { get; set; } = []; public List<SnChatMessage> Messages { get; set; } = [];
public Instant CurrentTimestamp { get; set; } public Instant CurrentTimestamp { get; set; }
public int TotalCount { get; set; } = 0; public int TotalCount { get; set; } = 0;
} }

View File

@@ -60,22 +60,19 @@ public class PublisherSubscriptionService(
var (title, message) = ps.ChopPostForNotification(post); var (title, message) = ps.ChopPostForNotification(post);
// Data to include with the notification // Data to include with the notification
var images = new List<string>();
if (post.Attachments.Any(p => p.MimeType?.StartsWith("image/") ?? false))
images.AddRange(
post.Attachments
.Where(p => p.MimeType?.StartsWith("image/") ?? false)
.Select(p => p.Id)
);
else if (post.Publisher.Picture is not null) images.Add(post.Publisher.Picture.Id);
var data = new Dictionary<string, object> var data = new Dictionary<string, object>
{ {
["post_id"] = post.Id, ["post_id"] = post.Id,
["publisher_id"] = post.Publisher.Id.ToString(), ["publisher_id"] = post.Publisher.Id.ToString()
["images"] = images,
}; };
if (post.Attachments.Any(p => p.MimeType?.StartsWith("image/") ?? false))
data["image"] =
post.Attachments
.Where(p => p.MimeType?.StartsWith("image/") ?? false)
.Select(p => p.Id).First();
if (post.Publisher.Picture is not null) data["pfp"] = post.Publisher.Picture.Id;
// Gather subscribers // Gather subscribers
var subscribers = await db.PublisherSubscriptions var subscribers = await db.PublisherSubscriptions
.Where(p => p.PublisherId == post.PublisherId && .Where(p => p.PublisherId == post.PublisherId &&