Skip to content

Commit

Permalink
Merge pull request #264 from SaintAngeLs/notifications_service
Browse files Browse the repository at this point in the history
(#263) AddOrUpdate async method update, favicon update, new message retrieval affecting the title of the page
  • Loading branch information
SaintAngeLs committed Jun 2, 2024
2 parents d9fc79f + b404c56 commit f8b6210
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task HandleAsync(FriendInvited @event, CancellationToken cancellati
{
var inviter = await _studentsServiceClient.GetAsync(@event.InviterId);
var notificationMessage = $"You have been invited by {inviter.FirstName} {inviter.LastName} to be friends.";
var detailsHtml = $"<p>View <a href='https://minispace.itsharppro.com/user-details/{@event.InviterId}'>{inviter.FirstName} {inviter.LastName}</a>'s profile to respond to the friend invitation.</p>";
var detailsHtml = $"<p>View <a href='https://minispace.itsharppro.com/student-details/{@event.InviterId}'>{inviter.FirstName} {inviter.LastName}</a>'s profile to respond to the friend invitation.</p>";

var notificationId = Guid.NewGuid();
var notification = new Notification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task HandleAsync(FriendRequestSent @event, CancellationToken cancel
{
var inviter = await _studentsServiceClient.GetAsync(@event.InviterId);
var notificationMessage = $"You have been invited by {inviter.FirstName} {inviter.LastName} to be friends.";
var detailsHtml = $"<p>View <a href='https://minispace.itsharppro.com/user-details/{@event.InviterId}'>{inviter.FirstName} {inviter.LastName}</a>'s profile to respond to the friend invitation.</p>";
var detailsHtml = $"<p>View <a href='https://minispace.itsharppro.com/student-details/{@event.InviterId}'>{inviter.FirstName} {inviter.LastName}</a>'s profile to respond to the friend invitation.</p>";

var notification = new Notification(
notificationId: Guid.NewGuid(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,63 @@ public async Task UpdateAsync(StudentNotifications studentNotifications)
await _repository.Collection.UpdateOneAsync(filter, update, options);
}

public async Task AddOrUpdateAsync(StudentNotifications studentNotifications)
// public async Task AddOrUpdateAsync(StudentNotifications studentNotifications)
// {
// var document = studentNotifications.AsDocument();
// var filter = Builders<StudentNotificationsDocument>.Filter.Eq(doc => doc.StudentId, studentNotifications.StudentId);
// var updates = new List<UpdateDefinition<StudentNotificationsDocument>>();

// foreach (var notification in studentNotifications.Notifications)
// {
// updates.Add(Builders<StudentNotificationsDocument>.Update.Push(doc => doc.Notifications, notification.AsDocument()));
// }

// var update = Builders<StudentNotificationsDocument>.Update
// .SetOnInsert(doc => doc.Id, studentNotifications.StudentId)
// .AddToSetEach(doc => doc.Notifications, studentNotifications.Notifications.Select(n => n.AsDocument()));

// var options = new UpdateOptions { IsUpsert = true };
// await _repository.Collection.UpdateOneAsync(filter, update, options);
// }

public async Task AddOrUpdateAsync(StudentNotifications studentNotifications)
{
var document = studentNotifications.AsDocument();
var filter = Builders<StudentNotificationsDocument>.Filter.Eq(doc => doc.StudentId, studentNotifications.StudentId);
var updates = new List<UpdateDefinition<StudentNotificationsDocument>>();

// Ensure the document is initialized with an empty list if not present
var initializationUpdate = Builders<StudentNotificationsDocument>.Update
.SetOnInsert(doc => doc.Notifications, new List<NotificationDocument>());

var addToSetUpdates = new List<UpdateDefinition<StudentNotificationsDocument>>();
foreach (var notification in studentNotifications.Notifications)
{
updates.Add(Builders<StudentNotificationsDocument>.Update.Push(doc => doc.Notifications, notification.AsDocument()));
}
var notificationDocument = notification.AsDocument();
var notificationFilter = Builders<StudentNotificationsDocument>.Filter.And(
Builders<StudentNotificationsDocument>.Filter.Eq("Notifications.Message", notificationDocument.Message),
Builders<StudentNotificationsDocument>.Filter.Eq("Notifications.CreatedAt", notificationDocument.CreatedAt),
Builders<StudentNotificationsDocument>.Filter.Eq("Notifications.EventType", notificationDocument.EventType)
);

var update = Builders<StudentNotificationsDocument>.Update
.SetOnInsert(doc => doc.Id, studentNotifications.StudentId)
.AddToSetEach(doc => doc.Notifications, studentNotifications.Notifications.Select(n => n.AsDocument()));
var combinedFilter = Builders<StudentNotificationsDocument>.Filter.And(filter, notificationFilter);
var update = Builders<StudentNotificationsDocument>.Update.AddToSet(doc => doc.Notifications, notificationDocument);

addToSetUpdates.Add(update);
}

var options = new UpdateOptions { IsUpsert = true };
await _repository.Collection.UpdateOneAsync(filter, update, options);
// Initialize document if it doesn't exist
await _repository.Collection.UpdateOneAsync(filter, initializationUpdate, options);

// Apply each AddToSet operation
foreach (var update in addToSetUpdates)
{
await _repository.Collection.UpdateOneAsync(filter, update, options);
}
}



public async Task<List<StudentNotifications>> FindAsync(FilterDefinition<StudentNotificationsDocument> filter, FindOptions options)
{
var documents = await _repository.Collection.Find(filter, options).ToListAsync();
Expand Down
7 changes: 6 additions & 1 deletion MiniSpace.Web/src/MiniSpace.Web/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MiniSpace.Web</title>
<title>MiniSpace | Social</title>
<base href="~/" />
<link rel="stylesheet" href="_content/Radzen.Blazor/css/humanistic-base.css">
<link href="css/site.css" rel="stylesheet" />
Expand Down Expand Up @@ -56,6 +56,11 @@
return new Date().getTimezoneOffset();
}
function updateTitle(message) {
document.title = message;
setTimeout(() => document.title = "MiniSpace | Social", 8000);
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@
var paginatedResponse = await NotificationsService.GetNotificationsByUserAsync(userId, status: "Unread");
var latestNotifications = paginatedResponse.Results;

if (latestNotifications.Any(n => n.CreatedAt > lastCheckedTime))
if (latestNotifications.Any(n => n.CreatedAt > lastCheckedTime))
{
PlayNotificationSound();
await JSRuntime.InvokeVoidAsync("updateTitle", "New Notification - MiniSpace | Social");
lastCheckedTime = DateTime.Now;
}

Expand Down
Binary file modified MiniSpace.Web/src/MiniSpace.Web/wwwroot/favicon.ico
Binary file not shown.

0 comments on commit f8b6210

Please sign in to comment.