-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#411) notifications: update comment service external events and even…
…ts handlers
- Loading branch information
1 parent
f84de45
commit 4b62d33
Showing
11 changed files
with
171 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...s.Notifications/src/MiniSpace.Services.Notifications.Application/Dto/Comments/ReplyDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace MiniSpace.Services.Notifications.Application.Dto.Comments | ||
{ | ||
public class ReplyDto | ||
{ | ||
public Guid Id { get; set; } | ||
public Guid UserId { get; set; } | ||
public Guid CommentId { get; set; } | ||
public string TextContent { get; set; } | ||
public DateTime CreatedAt { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
....Services.Notifications.Application/Events/External/Comments/Handlers/LikeAddedHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Convey.CQRS.Events; | ||
using MiniSpace.Services.Notifications.Core.Repositories; | ||
using MiniSpace.Services.Notifications.Application.Services.Clients; | ||
using Microsoft.Extensions.Logging; | ||
using MiniSpace.Services.Notifications.Application.Hubs; | ||
using MiniSpace.Services.Notifications.Application.Dto; | ||
using Microsoft.AspNetCore.SignalR; | ||
using MiniSpace.Services.Notifications.Core.Entities; | ||
|
||
namespace MiniSpace.Services.Notifications.Application.Events.External.Comments.Handlers | ||
{ | ||
public class LikeAddedHandler : IEventHandler<LikeAdded> | ||
{ | ||
private readonly ICommentsServiceClient _commentsServiceClient; | ||
private readonly IPostsServiceClient _postsServiceClient; | ||
private readonly IUserNotificationsRepository _userNotificationsRepository; | ||
private readonly ILogger<LikeAddedHandler> _logger; | ||
private readonly IHubContext<NotificationHub> _hubContext; | ||
|
||
public LikeAddedHandler( | ||
ICommentsServiceClient commentsServiceClient, | ||
IPostsServiceClient postsServiceClient, | ||
IUserNotificationsRepository userNotificationsRepository, | ||
ILogger<LikeAddedHandler> logger, | ||
IHubContext<NotificationHub> hubContext) | ||
{ | ||
_commentsServiceClient = commentsServiceClient; | ||
_postsServiceClient = postsServiceClient; | ||
_userNotificationsRepository = userNotificationsRepository; | ||
_logger = logger; | ||
_hubContext = hubContext; | ||
} | ||
|
||
public async Task HandleAsync(LikeAdded @event, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var commentDetails = await _commentsServiceClient.GetCommentAsync(@event.CommentId); | ||
if (commentDetails == null) | ||
{ | ||
_logger.LogError($"No comment details found for LikeAdded event. CommentId: {@event.CommentId}"); | ||
return; | ||
} | ||
|
||
var entityOwnerId = commentDetails.UserId; | ||
var entityName = commentDetails.TextContent; | ||
|
||
var notification = new Notification( | ||
notificationId: Guid.NewGuid(), | ||
userId: entityOwnerId, | ||
message: $"{@event.UserName} liked your comment '{entityName}'.", | ||
status: NotificationStatus.Unread, | ||
createdAt: DateTime.UtcNow, | ||
updatedAt: null, | ||
relatedEntityId: @event.CommentId, | ||
eventType: NotificationEventType.CommentLikeAdded | ||
); | ||
|
||
var userNotifications = await _userNotificationsRepository.GetByUserIdAsync(entityOwnerId) | ||
?? new UserNotifications(entityOwnerId); | ||
|
||
userNotifications.AddNotification(notification); | ||
await _userNotificationsRepository.AddOrUpdateAsync(userNotifications); | ||
|
||
var notificationDetails = $"<p>{@event.UserName} liked your comment: '{@event.CommentContext}'.</p>" + | ||
$"<img src='{@event.ProfileImageUrl}' alt='Profile Image' style='width:50px;height:50px;' />"; | ||
|
||
var notificationDto = new NotificationDto | ||
{ | ||
UserId = entityOwnerId, | ||
Message = notification.Message, | ||
CreatedAt = notification.CreatedAt, | ||
EventType = NotificationEventType.CommentLikeAdded, | ||
RelatedEntityId = @event.CommentId, | ||
Details = notificationDetails | ||
}; | ||
|
||
await NotificationHub.BroadcastNotification(_hubContext, notificationDto, _logger); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError($"Failed to handle LikeAdded event: {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.