[backend/masto-client] Add notification filtering
This commit is contained in:
parent
cbd4b576e2
commit
df726f6f9f
4 changed files with 54 additions and 2 deletions
|
@ -27,7 +27,9 @@ public class NotificationController(DatabaseContext db, NotificationRenderer not
|
|||
[LinkPagination(40, 80)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<NotificationEntity>))]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(MastodonErrorResponse))]
|
||||
public async Task<IActionResult> GetNotifications(MastodonPaginationQuery query)
|
||||
public async Task<IActionResult> GetNotifications(
|
||||
MastodonPaginationQuery query, NotificationSchemas.GetNotificationsRequest request
|
||||
)
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
var res = await db.Notifications
|
||||
|
@ -42,6 +44,7 @@ public class NotificationController(DatabaseContext db, NotificationRenderer not
|
|||
p.Type == NotificationType.PollEnded ||
|
||||
p.Type == NotificationType.FollowRequestReceived ||
|
||||
p.Type == NotificationType.Edit)
|
||||
.FilterByGetNotificationsRequest(request)
|
||||
.EnsureNoteVisibilityFor(p => p.Note, user)
|
||||
.FilterBlocked(p => p.Notifier, user)
|
||||
.FilterBlocked(p => p.Note, user)
|
||||
|
|
|
@ -32,4 +32,20 @@ public class NotificationEntity : IEntity
|
|||
_ => throw new GracefulException($"Unsupported notification type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
public static IEnumerable<NotificationType> DecodeType(string type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
"follow" => [NotificationType.Follow],
|
||||
"mention" => [NotificationType.Mention, NotificationType.Reply],
|
||||
"renote" => [NotificationType.Renote],
|
||||
"reblog" => [NotificationType.Quote],
|
||||
"favourite" => [NotificationType.Like],
|
||||
"poll" => [NotificationType.PollEnded],
|
||||
"follow_request" => [NotificationType.FollowRequestReceived],
|
||||
"update" => [NotificationType.Edit],
|
||||
_ => []
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Iceshrimp.Backend.Controllers.Mastodon.Schemas;
|
||||
|
||||
public abstract class NotificationSchemas
|
||||
{
|
||||
public class GetNotificationsRequest
|
||||
{
|
||||
[FromQuery(Name = "types")] public List<string>? Types { get; set; }
|
||||
[FromQuery(Name = "exclude_types")] public List<string>? ExcludeTypes { get; set; }
|
||||
[FromQuery(Name = "account_id")] public string? AccountId { get; set; }
|
||||
}
|
||||
}
|
|
@ -292,6 +292,26 @@ public static class QueryableExtensions
|
|||
|
||||
return query;
|
||||
}
|
||||
|
||||
public static IQueryable<Notification> FilterByGetNotificationsRequest(
|
||||
this IQueryable<Notification> query,
|
||||
NotificationSchemas.GetNotificationsRequest request
|
||||
)
|
||||
{
|
||||
if (request.AccountId != null)
|
||||
query = query.Where(p => p.NotifierId == request.AccountId);
|
||||
if (request.Types != null)
|
||||
query = query.Where(p => request.Types.SelectMany(NotificationEntity.DecodeType)
|
||||
.Distinct()
|
||||
.Contains(p.Type));
|
||||
if (request.ExcludeTypes != null)
|
||||
query = query.Where(p => !request.ExcludeTypes.SelectMany(NotificationEntity.DecodeType)
|
||||
.Distinct()
|
||||
.Contains(p.Type));
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
||||
// Justification: in the context of nullable EF navigation properties, null values are ignored and therefore irrelevant.
|
||||
// Source: https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#navigating-and-including-nullable-relationships
|
||||
|
|
Loading…
Add table
Reference in a new issue