[backend/services] Basic event bus implementation (ISH-60)

This commit is contained in:
Laura Hausmann 2024-02-14 17:50:57 +01:00
parent fc0f40f8ce
commit ff27d7ff16
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
5 changed files with 21 additions and 5 deletions

View file

@ -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;

View file

@ -54,6 +54,7 @@ public static class ServiceExtensions {
.AddSingleton<HttpRequestService>()
.AddSingleton<QueueService>()
.AddSingleton<ObjectStorageService>()
.AddSingleton<EventService>()
.AddSingleton<RequestBufferingMiddleware>()
.AddSingleton<AuthorizationMiddleware>()
.AddSingleton<RequestVerificationMiddleware>()

View file

@ -0,0 +1,11 @@
using Iceshrimp.Backend.Core.Database.Tables;
namespace Iceshrimp.Backend.Core.Services;
public class EventService {
public event EventHandler<Note> NotePublished;
public event EventHandler<string> NoteDeleted;
public void RaiseNotePublished(object? sender, Note note) => NotePublished.Invoke(sender, note);
public void RaiseNoteDeleted(object? sender, Note note) => NoteDeleted.Invoke(sender, note.Id);
}

View file

@ -36,7 +36,8 @@ public class NoteService(
MentionsResolver mentionsResolver,
MfmConverter mfmConverter,
DriveService driveSvc,
NotificationService notificationSvc
NotificationService notificationSvc,
EventService eventSvc
) {
private readonly List<string> _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);

View file

@ -57,4 +57,8 @@
<None Remove="migrate.sql"/>
</ItemGroup>
<ItemGroup>
<Folder Include="Core\Events\" />
</ItemGroup>
</Project>