[backend/api-shared] Allow searching for notes with polls

This commit is contained in:
Laura Hausmann 2024-06-05 15:53:57 +02:00
parent e810b00644
commit 1e761f5008
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 14 additions and 5 deletions

View file

@ -173,7 +173,9 @@ public static class QueryableFtsExtensions
[Projectable] [Projectable]
internal static IQueryable<Note> ApplyRegularAttachmentFilter(this IQueryable<Note> query, AttachmentFilter filter) internal static IQueryable<Note> ApplyRegularAttachmentFilter(this IQueryable<Note> query, AttachmentFilter filter)
=> AttachmentQuery(filter.Value) => AttachmentQuery(filter.Value)
? query.Where(p => EF.Functions.ILike(p.RawAttachments, GetAttachmentILikeQuery(filter.Value))) ? filter.Value.Equals(AttachmentFilterType.Poll)
? query.Where(p => p.HasPoll)
: query.Where(p => EF.Functions.ILike(p.RawAttachments, GetAttachmentILikeQuery(filter.Value)))
: filter.Value.Equals(AttachmentFilterType.Any) : filter.Value.Equals(AttachmentFilterType.Any)
? query.Where(p => p.AttachedFileTypes.Count != 0) ? query.Where(p => p.AttachedFileTypes.Count != 0)
: query.Where(p => p.AttachedFileTypes.Count != 0 && : query.Where(p => p.AttachedFileTypes.Count != 0 &&
@ -187,7 +189,9 @@ public static class QueryableFtsExtensions
[Projectable] [Projectable]
internal static IQueryable<Note> ApplyNegatedAttachmentFilter(this IQueryable<Note> query, AttachmentFilter filter) internal static IQueryable<Note> ApplyNegatedAttachmentFilter(this IQueryable<Note> query, AttachmentFilter filter)
=> AttachmentQuery(filter.Value) => AttachmentQuery(filter.Value)
? query.Where(p => !EF.Functions.ILike(p.RawAttachments, GetAttachmentILikeQuery(filter.Value))) ? filter.Value.Equals(AttachmentFilterType.Poll)
? query.Where(p => !p.HasPoll)
: query.Where(p => !EF.Functions.ILike(p.RawAttachments, GetAttachmentILikeQuery(filter.Value)))
: filter.Value.Equals(AttachmentFilterType.Any) : filter.Value.Equals(AttachmentFilterType.Any)
? query.Where(p => p.AttachedFileTypes.Count == 0) ? query.Where(p => p.AttachedFileTypes.Count == 0)
: query.Where(p => EF.Functions : query.Where(p => EF.Functions
@ -205,6 +209,8 @@ public static class QueryableFtsExtensions
return true; return true;
if (filter.Equals(AttachmentFilterType.Audio)) if (filter.Equals(AttachmentFilterType.Audio))
return true; return true;
if (filter.Equals(AttachmentFilterType.Poll))
return true;
return false; return false;
} }

View file

@ -84,6 +84,7 @@ module SearchQueryFilters =
| Video | Video
| Audio | Audio
| File | File
| Poll
type AttachmentFilter(neg: bool, value: string) = type AttachmentFilter(neg: bool, value: string) =
inherit Filter() inherit Filter()
@ -96,6 +97,7 @@ module SearchQueryFilters =
| "video" -> Video | "video" -> Video
| "audio" -> Audio | "audio" -> Audio
| "file" -> File | "file" -> File
| "poll" -> Poll
| _ -> failwith $"Invalid type: {value}" | _ -> failwith $"Invalid type: {value}"
type AfterFilter(d: DateOnly) = type AfterFilter(d: DateOnly) =
@ -206,7 +208,7 @@ module private SearchQueryParser =
<| fun n v -> InFilter(n.IsSome, v) :> Filter <| fun n v -> InFilter(n.IsSome, v) :> Filter
let attachmentFilter = let attachmentFilter =
negKeyFilter [ "has"; "attachment"; "attached" ] [ "any"; "image"; "video"; "audio"; "file" ] negKeyFilter [ "has"; "attachment"; "attached" ] [ "any"; "image"; "video"; "audio"; "file"; "poll" ]
<| fun n v -> AttachmentFilter(n.IsSome, v) :> Filter <| fun n v -> AttachmentFilter(n.IsSome, v) :> Filter
let afterFilter = let afterFilter =

View file

@ -88,7 +88,7 @@ public class SearchQueryTests
{ {
List<string> keyCandidates = ["has", "attachment", "attached"]; List<string> keyCandidates = ["has", "attachment", "attached"];
if (negated) keyCandidates = keyCandidates.Select(p => "-" + p).ToList(); if (negated) keyCandidates = keyCandidates.Select(p => "-" + p).ToList();
List<string> candidates = ["any", "image", "video", "audio", "file"]; List<string> candidates = ["any", "image", "video", "audio", "file", "poll"];
var results = var results =
keyCandidates.Select(k => candidates.Select(v => $"{k}:{v}").SelectMany(SearchQuery.parse).ToList()); keyCandidates.Select(k => candidates.Select(v => $"{k}:{v}").SelectMany(SearchQuery.parse).ToList());
List<Filter> expectedResults = List<Filter> expectedResults =
@ -97,7 +97,8 @@ public class SearchQueryTests
new AttachmentFilter(negated, "image"), new AttachmentFilter(negated, "image"),
new AttachmentFilter(negated, "video"), new AttachmentFilter(negated, "video"),
new AttachmentFilter(negated, "audio"), new AttachmentFilter(negated, "audio"),
new AttachmentFilter(negated, "file") new AttachmentFilter(negated, "file"),
new AttachmentFilter(negated, "poll")
]; ];
results.Should() results.Should()
.HaveCount(keyCandidates.Count) .HaveCount(keyCandidates.Count)