< Summary

Information
Class: Anichron.Core.Data.Repository.HiddenAssets
Assembly: Anichron.Core
File(s): /home/runner/work/anichron/anichron/src/Anichron.Core/Data/Repository/AssetViewReader.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 158
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Set()100%11100%
Apply(...)100%11100%
Page(...)100%22100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Core/Data/Repository/AssetViewReader.cs

#LineLine coverage
 1using Anichron.Core.Domain;
 2using Microsoft.EntityFrameworkCore;
 3
 4namespace Anichron.Core.Data.Repository;
 5
 6public sealed record AssetView(
 7    Guid Id,
 8    string ContentHash,
 9    MediaType MediaType,
 10    int Year,
 11    int Month,
 12    int Day,
 13    LocalDateTime DateCaptured,
 14    int? Width,
 15    int? Height,
 16    int? Orientation,
 17    int? DurationInSeconds,
 18    string? Blurhash,
 19    bool HasThumbnail,
 20    bool HasPreview,
 21    bool HasCompactPreview,
 22    bool HasVideo,
 23    Guid? PairedMovId,
 24    string? PairedMovContentHash,
 25    bool PairedMovHasVideo,
 26    bool IsStarred,
 27    bool IsLiked,
 28    Guid? BurstId,
 29    Instant? HiddenAt);
 30
 31public enum AssetSet
 32{
 33    Shown = 0,
 34    Hidden = 1,
 35}
 36
 37public sealed class InteractedAsset
 38{
 39    public required MediaAsset Asset { get; init; }
 40    public AssetInteraction? Interaction { get; init; }
 41}
 42
 43/// <summary>
 44/// Narrows the assets an <see cref="IAssetViewReader"/> returns from the set it declares, filtering on entity
 45/// columns only, and may order and page the caller's rows.
 46/// </summary>
 47public interface IAssetSelection
 48{
 49    AssetSet Set { get; }
 50
 51    IQueryable<MediaAsset> Apply(IQueryable<MediaAsset> assets);
 52
 53    IQueryable<InteractedAsset> Page(IQueryable<InteractedAsset> rows) => rows;
 54}
 55
 56public sealed record OnThisDay(LocalDate Date) : IAssetSelection
 57{
 58    public AssetSet Set => AssetSet.Shown;
 59
 60    private int LastDay
 61        => Date is { Month: 2, Day: 28 } && !CalendarSystem.Iso.IsLeapYear(Date.Year) ? 29 : Date.Day;
 62
 63    public bool FallsOnTheDay(AssetView view)
 64        => view.Month == Date.Month && view.Day >= Date.Day && view.Day <= LastDay;
 65
 66    public IQueryable<MediaAsset> Apply(IQueryable<MediaAsset> assets)
 67    {
 68        var lastDay = LastDay;
 69        var onTheDay = assets.Where(a => a.Month == Date.Month && a.Day >= Date.Day && a.Day <= lastDay && a.Year < Date
 70        return onTheDay.Union(
 71            from d in onTheDay
 72            where d.BurstId != null
 73            from a in assets
 74            where a.BurstId == d.BurstId
 75            select a);
 76    }
 77}
 78
 79public sealed record BurstMembers(Guid BurstId) : IAssetSelection
 80{
 81    public AssetSet Set => AssetSet.Shown;
 82
 83    public IQueryable<MediaAsset> Apply(IQueryable<MediaAsset> assets)
 84        => assets.Where(a => a.BurstId == BurstId);
 85}
 86
 87public sealed record HiddenCursor(Instant HiddenAt, Guid AssetId);
 88
 89/// <summary>The caller's hidden assets, newest hidden first, as one page after a cursor.</summary>
 90/// <param name="After">The last row of the previous page, or <c>null</c> for the first page.</param>
 91/// <param name="Take">The most rows to read.</param>
 92public sealed record HiddenAssets(HiddenCursor? After, int Take) : IAssetSelection
 93{
 1294    public AssetSet Set => AssetSet.Hidden;
 95
 1296    public IQueryable<MediaAsset> Apply(IQueryable<MediaAsset> assets) => assets;
 97
 98    public IQueryable<InteractedAsset> Page(IQueryable<InteractedAsset> rows)
 1299    {
 12100        var after = After is { } cursor
 12101            ? rows.Where(r => r.Interaction!.HiddenAt < cursor.HiddenAt
 12102                || (r.Interaction.HiddenAt == cursor.HiddenAt && r.Asset.Id.CompareTo(cursor.AssetId) < 0))
 12103            : rows;
 12104        return after.OrderByDescending(r => r.Interaction!.HiddenAt).ThenByDescending(r => r.Asset.Id).Take(Take);
 12105    }
 106}
 107
 108/// <summary>
 109/// Reads the caller's visible assets in the set a selection declares and matching it, with the caller's own
 110/// interaction state, in one query.
 111/// </summary>
 112public interface IAssetViewReader
 113{
 114    Task<IReadOnlyList<AssetView>> ReadAsync(Guid userId, IAssetSelection selection, CancellationToken ct);
 115}
 116
 117public sealed class EfAssetViewReader(AnichronDbContext db) : IAssetViewReader
 118{
 119    public async Task<IReadOnlyList<AssetView>> ReadAsync(Guid userId, IAssetSelection selection, CancellationToken ct)
 120        => await Query(db, userId, selection).ToListAsync(ct);
 121
 122    internal static IQueryable<AssetView> Query(AnichronDbContext db, Guid userId, IAssetSelection selection)
 123    {
 124        var visible = db.MediaAssets.VisibleTo(userId);
 125        var set = selection.Set == AssetSet.Hidden ? visible.HiddenFor(userId) : visible.NotHiddenFor(userId);
 126        var rows = from a in selection.Apply(set.NotPairedMovOf(visible).Where(a => a.DateCaptured != null))
 127                   join i in db.Interactions.Where(i => i.UserId == userId) on a.Id equals i.AssetId into interactions
 128                   from i in interactions.DefaultIfEmpty()
 129                   select new InteractedAsset { Asset = a, Interaction = i };
 130        return from r in selection.Page(rows)
 131               let a = r.Asset
 132               let i = r.Interaction
 133               select new AssetView(
 134                   a.Id,
 135                   a.ContentHash,
 136                   a.MediaType,
 137                   a.Year!.Value,
 138                   a.Month!.Value,
 139                   a.Day!.Value,
 140                   a.DateCaptured!.Value,
 141                   a.Metadata!.Description.Width,
 142                   a.Metadata.Description.Height,
 143                   a.Metadata.Description.Orientation,
 144                   a.Metadata.Description.DurationInSeconds,
 145                   a.Metadata.Blurhash,
 146                   a.ProxyFiles.Any(p => p.ProxyType == ProxyType.Thumbnail),
 147                   a.ProxyFiles.Any(p => p.ProxyType == ProxyType.FullPreview),
 148                   a.ProxyFiles.Any(p => p.ProxyType == ProxyType.CompactPreview),
 149                   a.ProxyFiles.Any(p => p.ProxyType == ProxyType.WebVideo),
 150                   a.PairedAsset!.Id,
 151                   a.PairedAsset.ContentHash,
 152                   a.PairedAsset.ProxyFiles.Any(p => p.ProxyType == ProxyType.WebVideo),
 153                   i != null && i.IsStarred,
 154                   i != null && i.IsLiked,
 155                   a.BurstId,
 156                   i.HiddenAt);
 157    }
 158}