FeedBroadcaster.cs 858 B

123456789101112131415161718192021222324252627282930313233
  1. using Application.Abstractions.Hub;
  2. using Microsoft.AspNetCore.SignalR;
  3. namespace Infrastructure.Hubs;
  4. internal sealed class FeedBroadcaster(IHubContext<AppHub, IAppHubClient>? hub = null) : IFeedBroadcaster
  5. {
  6. public async Task BroadcastNewPostAsync(IEnumerable<int> followerMemberIDs, FeedPostToast toast, CancellationToken ct = default)
  7. {
  8. if (hub is null)
  9. {
  10. return;
  11. }
  12. var groups = followerMemberIDs.Select(id => $"member:{id}").ToList();
  13. if (groups.Count == 0)
  14. {
  15. return;
  16. }
  17. await hub.Clients.Groups(groups).ReceiveFeedPost(new
  18. {
  19. toast.PostID,
  20. toast.AuthorSID,
  21. toast.AuthorName,
  22. toast.AuthorThumb,
  23. toast.Subject,
  24. toast.Thumbnail,
  25. toast.CreatedAt
  26. });
  27. }
  28. }