From ff27d7ff16da80f6f9191ac9111b9b0075b5673e Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 14 Feb 2024 17:50:57 +0100 Subject: [PATCH] [backend/services] Basic event bus implementation (ISH-60) --- .../Mastodon/Schemas/Entities/Notification.cs | 3 --- .../Core/Extensions/ServiceExtensions.cs | 1 + Iceshrimp.Backend/Core/Services/EventService.cs | 11 +++++++++++ Iceshrimp.Backend/Core/Services/NoteService.cs | 7 +++++-- Iceshrimp.Backend/Iceshrimp.Backend.csproj | 4 ++++ 5 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 Iceshrimp.Backend/Core/Services/EventService.cs diff --git a/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/Notification.cs b/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/Notification.cs index 17374c8a..24a987da 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/Notification.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/Notification.cs @@ -1,9 +1,6 @@ -using System.Text.Json.Serialization; using Iceshrimp.Backend.Core.Database; -using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Middleware; using J = System.Text.Json.Serialization.JsonPropertyNameAttribute; -using JI = System.Text.Json.Serialization.JsonIgnoreAttribute; using static Iceshrimp.Backend.Core.Database.Tables.Notification; namespace Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities; diff --git a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs index d488b53f..5efe38f9 100644 --- a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs @@ -54,6 +54,7 @@ public static class ServiceExtensions { .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Iceshrimp.Backend/Core/Services/EventService.cs b/Iceshrimp.Backend/Core/Services/EventService.cs new file mode 100644 index 00000000..2efadf75 --- /dev/null +++ b/Iceshrimp.Backend/Core/Services/EventService.cs @@ -0,0 +1,11 @@ +using Iceshrimp.Backend.Core.Database.Tables; + +namespace Iceshrimp.Backend.Core.Services; + +public class EventService { + public event EventHandler NotePublished; + public event EventHandler NoteDeleted; + + public void RaiseNotePublished(object? sender, Note note) => NotePublished.Invoke(sender, note); + public void RaiseNoteDeleted(object? sender, Note note) => NoteDeleted.Invoke(sender, note.Id); +} \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Services/NoteService.cs b/Iceshrimp.Backend/Core/Services/NoteService.cs index e20ede5b..a829be7a 100644 --- a/Iceshrimp.Backend/Core/Services/NoteService.cs +++ b/Iceshrimp.Backend/Core/Services/NoteService.cs @@ -36,7 +36,8 @@ public class NoteService( MentionsResolver mentionsResolver, MfmConverter mfmConverter, DriveService driveSvc, - NotificationService notificationSvc + NotificationService notificationSvc, + EventService eventSvc ) { private readonly List _resolverHistory = []; private int _recursionLimit = 100; @@ -84,6 +85,7 @@ public class NoteService( if (reply != null) reply.RepliesCount++; await db.AddAsync(note); await db.SaveChangesAsync(); + eventSvc.RaiseNotePublished(this, note); await notificationSvc.GenerateMentionNotifications(note, mentionedLocalUserIds); var obj = await noteRenderer.RenderAsync(note, mentions); @@ -127,7 +129,7 @@ public class NoteService( user.NotesCount--; db.Remove(dbNote); - + eventSvc.RaiseNoteDeleted(this, dbNote); await db.SaveChangesAsync(); } @@ -212,6 +214,7 @@ public class NoteService( if (dbNote.Reply != null) dbNote.Reply.RepliesCount++; await db.Notes.AddAsync(dbNote); await db.SaveChangesAsync(); + eventSvc.RaiseNotePublished(this, dbNote); await notificationSvc.GenerateMentionNotifications(dbNote, mentionedLocalUserIds); await notificationSvc.GenerateReplyNotifications(dbNote, mentionedLocalUserIds); logger.LogDebug("Note {id} created successfully", dbNote.Id); diff --git a/Iceshrimp.Backend/Iceshrimp.Backend.csproj b/Iceshrimp.Backend/Iceshrimp.Backend.csproj index 1dddfe66..ee1fe118 100644 --- a/Iceshrimp.Backend/Iceshrimp.Backend.csproj +++ b/Iceshrimp.Backend/Iceshrimp.Backend.csproj @@ -57,4 +57,8 @@ + + + +