| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Core.Data.Repository; |
| | | 5 | | |
| | | 6 | | public 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 | | |
| | | 31 | | public enum AssetSet |
| | | 32 | | { |
| | | 33 | | Shown = 0, |
| | | 34 | | Hidden = 1, |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public 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> |
| | | 47 | | public 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 | | |
| | | 56 | | public 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 | | |
| | | 79 | | public 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 | | |
| | | 87 | | public 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> |
| | | 92 | | public sealed record HiddenAssets(HiddenCursor? After, int Take) : IAssetSelection |
| | | 93 | | { |
| | | 94 | | public AssetSet Set => AssetSet.Hidden; |
| | | 95 | | |
| | | 96 | | public IQueryable<MediaAsset> Apply(IQueryable<MediaAsset> assets) => assets; |
| | | 97 | | |
| | | 98 | | public IQueryable<InteractedAsset> Page(IQueryable<InteractedAsset> rows) |
| | | 99 | | { |
| | | 100 | | var after = After is { } cursor |
| | | 101 | | ? rows.Where(r => r.Interaction!.HiddenAt < cursor.HiddenAt |
| | | 102 | | || (r.Interaction.HiddenAt == cursor.HiddenAt && r.Asset.Id.CompareTo(cursor.AssetId) < 0)) |
| | | 103 | | : rows; |
| | | 104 | | return after.OrderByDescending(r => r.Interaction!.HiddenAt).ThenByDescending(r => r.Asset.Id).Take(Take); |
| | | 105 | | } |
| | | 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> |
| | | 112 | | public interface IAssetViewReader |
| | | 113 | | { |
| | | 114 | | Task<IReadOnlyList<AssetView>> ReadAsync(Guid userId, IAssetSelection selection, CancellationToken ct); |
| | | 115 | | } |
| | | 116 | | |
| | 65 | 117 | | public sealed class EfAssetViewReader(AnichronDbContext db) : IAssetViewReader |
| | | 118 | | { |
| | | 119 | | public async Task<IReadOnlyList<AssetView>> ReadAsync(Guid userId, IAssetSelection selection, CancellationToken ct) |
| | 68 | 120 | | => await Query(db, userId, selection).ToListAsync(ct); |
| | | 121 | | |
| | | 122 | | internal static IQueryable<AssetView> Query(AnichronDbContext db, Guid userId, IAssetSelection selection) |
| | 76 | 123 | | { |
| | 76 | 124 | | var visible = db.MediaAssets.VisibleTo(userId); |
| | 76 | 125 | | var set = selection.Set == AssetSet.Hidden ? visible.HiddenFor(userId) : visible.NotHiddenFor(userId); |
| | 76 | 126 | | var rows = from a in selection.Apply(set.NotPairedMovOf(visible).Where(a => a.DateCaptured != null)) |
| | 76 | 127 | | join i in db.Interactions.Where(i => i.UserId == userId) on a.Id equals i.AssetId into interactions |
| | 76 | 128 | | from i in interactions.DefaultIfEmpty() |
| | 76 | 129 | | select new InteractedAsset { Asset = a, Interaction = i }; |
| | 76 | 130 | | return from r in selection.Page(rows) |
| | 76 | 131 | | let a = r.Asset |
| | 76 | 132 | | let i = r.Interaction |
| | 76 | 133 | | select new AssetView( |
| | 76 | 134 | | a.Id, |
| | 76 | 135 | | a.ContentHash, |
| | 76 | 136 | | a.MediaType, |
| | 76 | 137 | | a.Year!.Value, |
| | 76 | 138 | | a.Month!.Value, |
| | 76 | 139 | | a.Day!.Value, |
| | 76 | 140 | | a.DateCaptured!.Value, |
| | 76 | 141 | | a.Metadata!.Description.Width, |
| | 76 | 142 | | a.Metadata.Description.Height, |
| | 76 | 143 | | a.Metadata.Description.Orientation, |
| | 76 | 144 | | a.Metadata.Description.DurationInSeconds, |
| | 76 | 145 | | a.Metadata.Blurhash, |
| | 76 | 146 | | a.ProxyFiles.Any(p => p.ProxyType == ProxyType.Thumbnail), |
| | 76 | 147 | | a.ProxyFiles.Any(p => p.ProxyType == ProxyType.FullPreview), |
| | 76 | 148 | | a.ProxyFiles.Any(p => p.ProxyType == ProxyType.CompactPreview), |
| | 76 | 149 | | a.ProxyFiles.Any(p => p.ProxyType == ProxyType.WebVideo), |
| | 76 | 150 | | a.PairedAsset!.Id, |
| | 76 | 151 | | a.PairedAsset.ContentHash, |
| | 76 | 152 | | a.PairedAsset.ProxyFiles.Any(p => p.ProxyType == ProxyType.WebVideo), |
| | 76 | 153 | | i != null && i.IsStarred, |
| | 76 | 154 | | i != null && i.IsLiked, |
| | 76 | 155 | | a.BurstId, |
| | 76 | 156 | | i.HiddenAt); |
| | 76 | 157 | | } |
| | | 158 | | } |