[backend/masto-client] Add public timeline filtering

This commit is contained in:
Laura Hausmann 2024-02-20 20:11:29 +01:00
parent 907f7ed55c
commit 0f9a51d8f2
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 31 additions and 1 deletions

View file

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc;
namespace Iceshrimp.Backend.Controllers.Mastodon.Schemas;
public abstract class TimelineSchemas
{
public class PublicTimelineRequest
{
[FromQuery(Name = "local")] public bool OnlyLocal { get; set; } = false;
[FromQuery(Name = "remote")] public bool OnlyRemote { get; set; } = false;
[FromQuery(Name = "only_media")] public bool OnlyMedia { get; set; } = false;
}
}

View file

@ -51,13 +51,16 @@ public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer, I
[Authorize("read:statuses")] [Authorize("read:statuses")]
[HttpGet("public")] [HttpGet("public")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<StatusEntity>))] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<StatusEntity>))]
public async Task<IActionResult> GetPublicTimeline(MastodonPaginationQuery query) public async Task<IActionResult> GetPublicTimeline(
MastodonPaginationQuery query, TimelineSchemas.PublicTimelineRequest request
)
{ {
var user = HttpContext.GetUserOrFail(); var user = HttpContext.GetUserOrFail();
var res = await db.Notes var res = await db.Notes
.IncludeCommonProperties() .IncludeCommonProperties()
.HasVisibility(Note.NoteVisibility.Public) .HasVisibility(Note.NoteVisibility.Public)
.FilterByPublicTimelineRequest(request)
.FilterBlocked(user) .FilterBlocked(user)
.FilterMuted(user) .FilterMuted(user)
.Paginate(query, ControllerContext) .Paginate(query, ControllerContext)

View file

@ -312,6 +312,20 @@ public static class QueryableExtensions
return query; return query;
} }
public static IQueryable<Note> FilterByPublicTimelineRequest(
this IQueryable<Note> query, TimelineSchemas.PublicTimelineRequest request
)
{
if (request.OnlyLocal)
query = query.Where(p => p.UserHost == null);
if (request.OnlyRemote)
query = query.Where(p => p.UserHost != null);
if (request.OnlyMedia)
query = query.Where(p => p.FileIds.Count != 0);
return query;
}
#pragma warning disable CS8602 // Dereference of a possibly null reference. #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. // 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 // Source: https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#navigating-and-including-nullable-relationships