[backend/masto-client] Update followed users on user follow/unfollow/block (ISH-218)
This commit is contained in:
parent
a3b543c37a
commit
b4adfe7067
1 changed files with 34 additions and 6 deletions
|
@ -3,6 +3,7 @@ using Iceshrimp.Backend.Controllers.Mastodon.Renderers;
|
||||||
using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities;
|
using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities;
|
||||||
using Iceshrimp.Backend.Core.Database;
|
using Iceshrimp.Backend.Core.Database;
|
||||||
using Iceshrimp.Backend.Core.Database.Tables;
|
using Iceshrimp.Backend.Core.Database.Tables;
|
||||||
|
using Iceshrimp.Backend.Core.Events;
|
||||||
using Iceshrimp.Backend.Core.Middleware;
|
using Iceshrimp.Backend.Core.Middleware;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
@ -36,6 +37,9 @@ public class UserChannel(WebSocketConnection connection, bool notificationsOnly)
|
||||||
connection.EventService.NotePublished += OnNotePublished;
|
connection.EventService.NotePublished += OnNotePublished;
|
||||||
connection.EventService.NoteUpdated += OnNoteUpdated;
|
connection.EventService.NoteUpdated += OnNoteUpdated;
|
||||||
connection.EventService.NoteDeleted += OnNoteDeleted;
|
connection.EventService.NoteDeleted += OnNoteDeleted;
|
||||||
|
connection.EventService.UserFollowed += OnRelationChange;
|
||||||
|
connection.EventService.UserUnfollowed += OnRelationChange;
|
||||||
|
connection.EventService.UserBlocked += OnRelationChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.EventService.Notification += OnNotification;
|
connection.EventService.Notification += OnNotification;
|
||||||
|
@ -56,6 +60,9 @@ public class UserChannel(WebSocketConnection connection, bool notificationsOnly)
|
||||||
connection.EventService.NotePublished -= OnNotePublished;
|
connection.EventService.NotePublished -= OnNotePublished;
|
||||||
connection.EventService.NoteUpdated -= OnNoteUpdated;
|
connection.EventService.NoteUpdated -= OnNoteUpdated;
|
||||||
connection.EventService.NoteDeleted -= OnNoteDeleted;
|
connection.EventService.NoteDeleted -= OnNoteDeleted;
|
||||||
|
connection.EventService.UserFollowed -= OnRelationChange;
|
||||||
|
connection.EventService.UserUnfollowed -= OnRelationChange;
|
||||||
|
connection.EventService.UserBlocked -= OnRelationChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.EventService.Notification -= OnNotification;
|
connection.EventService.Notification -= OnNotification;
|
||||||
|
@ -64,6 +71,9 @@ public class UserChannel(WebSocketConnection connection, bool notificationsOnly)
|
||||||
private bool IsApplicable(Note note) => _followedUsers.Prepend(connection.Token.User.Id).Contains(note.UserId);
|
private bool IsApplicable(Note note) => _followedUsers.Prepend(connection.Token.User.Id).Contains(note.UserId);
|
||||||
private bool IsApplicable(Notification notification) => notification.NotifieeId == connection.Token.User.Id;
|
private bool IsApplicable(Notification notification) => notification.NotifieeId == connection.Token.User.Id;
|
||||||
|
|
||||||
|
private bool IsApplicable(UserInteraction interaction) => interaction.Actor.Id == connection.Token.User.Id ||
|
||||||
|
interaction.Object.Id == connection.Token.User.Id;
|
||||||
|
|
||||||
private async void OnNotePublished(object? _, Note note)
|
private async void OnNotePublished(object? _, Note note)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -162,4 +172,22 @@ public class UserChannel(WebSocketConnection connection, bool notificationsOnly)
|
||||||
Logger.LogError("Event handler OnNotification threw exception: {e}", e);
|
Logger.LogError("Event handler OnNotification threw exception: {e}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void OnRelationChange(object? _, UserInteraction interaction)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!IsApplicable(interaction)) return;
|
||||||
|
await using var scope = connection.ScopeFactory.CreateAsyncScope();
|
||||||
|
await using var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||||
|
_followedUsers = await db.Users.Where(p => p == connection.Token.User)
|
||||||
|
.SelectMany(p => p.Following)
|
||||||
|
.Select(p => p.Id)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.LogError("Event handler OnRelationChange threw exception: {e}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue