diff --git a/src/Bifrost.Web/Features/ConceptVotes/ConceptAppraisalService.cs b/src/Bifrost.Web/Features/ConceptVotes/ConceptAppraisalService.cs index ac98f29..ac96eb8 100644 --- a/src/Bifrost.Web/Features/ConceptVotes/ConceptAppraisalService.cs +++ b/src/Bifrost.Web/Features/ConceptVotes/ConceptAppraisalService.cs @@ -204,15 +204,16 @@ public sealed class ConceptAppraisalService( // The ranked read: live subjects of one collection ordered by net (up minus down), total (up plus down) or // registration time, largest first, ties by key ascending, so the order is total and a cursor names a position. // The cursor is keyset: the next page is whatever sorts after the last row's (value, key), so a vote landing - // between pages can move rows but never makes a page repeat the cursor's own row. With a network tag, - // subjects whose stored tag equals it are left out ("not yet voted by this network"); the tag is only compared + // between pages can move rows but never makes a page repeat the cursor's own row. With unvoted and a client + // address, subjects whose stored tag equals the client's are left out (no address filters nothing) ("not yet voted by this network"); the tag is only compared // inside the query and is never returned. Null when the collection has no live subject. public async Task GetRankedAsync( string collection, RankOrder order, int limit, RankCursor? after, - IPAddress? unvotedBy, + bool unvoted, + IPAddress? client, CancellationToken cancellationToken) { var live = await dbContext.ConceptSubjects.AsNoTracking() @@ -232,10 +233,10 @@ public sealed class ConceptAppraisalService( .ToListAsync(cancellationToken); var voted = new HashSet(); - if (unvotedBy is not null) + if (unvoted && client is not null) { var opts = options.Value; - var tag = NetworkPrefix.Tag(unvotedBy, opts.IPv4PrefixLength, opts.IPv6PrefixLength, opts.NetworkTagSecret); + var tag = NetworkPrefix.Tag(client, opts.IPv4PrefixLength, opts.IPv6PrefixLength, opts.NetworkTagSecret); voted = (await dbContext.ConceptAppraisals.AsNoTracking() .Where(x => ids.Contains(x.SubjectId) && x.NetworkTag == tag) .Select(x => x.SubjectId) @@ -263,7 +264,7 @@ public sealed class ConceptAppraisalService( var page = rows.Take(limit).ToList(); var next = rows.Count > limit - ? new RankCursor(order, unvotedBy is not null, page[^1].Value, page[^1].Key).Encode() + ? new RankCursor(order, unvoted, page[^1].Value, page[^1].Key).Encode() : null; return new RankedPage(page.Select(r => new RankedSubject(r.Key, r.Title, r.Tally)).ToList(), next); } diff --git a/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs b/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs index 3c3cf10..5943a09 100644 --- a/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs +++ b/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs @@ -94,7 +94,7 @@ public static class ConceptTallyExport } var page = await appraisals.GetRankedAsync( - collection, order, limit, cursor, unvoted ? http.Connection.RemoteIpAddress : null, cancellationToken); + collection, order, limit, cursor, unvoted, http.Connection.RemoteIpAddress, cancellationToken); if (page is null) { return Results.NotFound(); diff --git a/src/Bifrost.Web/Features/ConceptVotes/RankCursor.cs b/src/Bifrost.Web/Features/ConceptVotes/RankCursor.cs index 461f7e4..c8375ba 100644 --- a/src/Bifrost.Web/Features/ConceptVotes/RankCursor.cs +++ b/src/Bifrost.Web/Features/ConceptVotes/RankCursor.cs @@ -1,21 +1,18 @@ using System.Globalization; using System.Text; -using System.Text.RegularExpressions; using Microsoft.AspNetCore.WebUtilities; namespace Bifrost.Web.Features.ConceptVotes; // The opaque cursor of the ranked read: the last row's sort value and key, plus the order and filter it was issued // for, so a cursor cannot be replayed under another order or filter. Text "1|order|u|value|key", base64url without -// padding. Not signed: it names a position in public data and grants nothing. Decoding is strict and canonical (a +// padding; the length cap only bounds decoding work. Not signed: it names a position in public data and grants nothing. Decoding is strict and canonical (a // cursor that does not re-encode to the same text is refused), and every field is re-validated, so no cursor text // reaches a query unparsed. public sealed record RankCursor(RankOrder Order, bool Unvoted, long Value, string Key) { public const int MaxLength = 200; - private static readonly Regex Alphabet = new(@"^[A-Za-z0-9_-]+\z", RegexOptions.CultureInvariant); - public string Encode() => WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes( string.Create(CultureInfo.InvariantCulture, $"1|{(int)Order}|{(Unvoted ? 1 : 0)}|{Value}|{Key}"))); @@ -23,7 +20,7 @@ public sealed record RankCursor(RankOrder Order, bool Unvoted, long Value, strin public static bool TryDecode(string? text, out RankCursor cursor) { cursor = null!; - if (string.IsNullOrEmpty(text) || text.Length > MaxLength || !Alphabet.IsMatch(text)) + if (string.IsNullOrEmpty(text) || text.Length > MaxLength) { return false; } diff --git a/tests/Bifrost.Web.Tests/ConceptRankedExportTests.cs b/tests/Bifrost.Web.Tests/ConceptRankedExportTests.cs index 0b31fa5..c6ebd74 100644 --- a/tests/Bifrost.Web.Tests/ConceptRankedExportTests.cs +++ b/tests/Bifrost.Web.Tests/ConceptRankedExportTests.cs @@ -99,6 +99,48 @@ public sealed class ConceptRankedExportTests : IClassFixture(); + foreach (var key in ordinal.Reverse()) + { + db.ConceptSubjects.Add(new ConceptSubject { Collection = c, Key = key, Title = key, RegisteredAtUtc = Epoch }); + } + + await db.SaveChangesAsync(); + } + + var walked = new List(); + string? cursor = null; + do + { + var body = await GetOk(c, "?limit=1" + (cursor is null ? "" : "&cursor=" + cursor)); + walked.AddRange(Keys(body)); + cursor = body.GetProperty("next_cursor").ValueKind == JsonValueKind.Null ? null : body.GetProperty("next_cursor").GetString(); + } + while (cursor is not null && walked.Count < 10); + + Assert.Equal(ordinal, walked); + Assert.Equal(ordinal, Keys(await GetOk(c, "?sort=newest"))); + } + + [Fact] + public async Task Unvoted_without_a_client_address_still_pages() + { + var c = await Seed(); + + var first = await GetOk(c, "?unvoted=network&limit=2"); + var second = await GetOk(c, "?unvoted=network&limit=2&cursor=" + first.GetProperty("next_cursor").GetString()); + + Assert.Equal(["bravo", "alpha"], Keys(first)); + Assert.Equal(["charlie", "delta"], Keys(second)); + } + [Fact] public async Task A_vote_between_pages_never_repeats_the_cursors_own_row() {