|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using CommunityToolkit.Diagnostics; |
| 4 | +using Ipfs; |
| 5 | +using OwlCore.ComponentModel; |
| 6 | +using OwlCore.Kubo; |
| 7 | +using OwlCore.Nomad; |
| 8 | +using OwlCore.Nomad.Kubo; |
| 9 | +using OwlCore.Nomad.Kubo.Events; |
| 10 | +using WindowsAppCommunity.Sdk.Models; |
| 11 | + |
| 12 | +namespace WindowsAppCommunity.Sdk.Nomad; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Represents a modifiable connection collection. |
| 16 | +/// </summary> |
| 17 | +public class ModifiableConnectionCollection : NomadKuboEventStreamHandler<ValueUpdateEvent>, IModifiableConnectionsCollection, IDelegable<ReadOnlyConnectionCollection> |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Raised when items are added to the collection. |
| 21 | + /// </summary> |
| 22 | + public event EventHandler<IReadOnlyConnection[]>? ConnectionsAdded; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Raised when items are removed from the collection. |
| 26 | + /// </summary> |
| 27 | + public event EventHandler<IReadOnlyConnection[]>? ConnectionsRemoved; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The unique identifier for this collection. |
| 31 | + /// </summary> |
| 32 | + public required string Id { get; init; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The inner read-only connections collection. |
| 36 | + /// </summary> |
| 37 | + public required ReadOnlyConnectionCollection Inner { get; init; } |
| 38 | + |
| 39 | + /// <inheritdoc /> |
| 40 | + public IAsyncEnumerable<IReadOnlyConnection> GetConnectionsAsync(CancellationToken cancellationToken = default) => Inner.GetConnectionsAsync(cancellationToken); |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Adds a connection to this entity. |
| 44 | + /// </summary> |
| 45 | + public async Task AddConnectionAsync(IReadOnlyConnection connection, CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + var value = await connection.GetValueAsync(cancellationToken); |
| 48 | + |
| 49 | + var keyCid = (DagCid)await Client.Dag.PutAsync(connection.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 50 | + var valueCid = (DagCid)await Client.Dag.PutAsync(value, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 51 | + |
| 52 | + var updateEvent = new ValueUpdateEvent(keyCid, valueCid, Unset:false); |
| 53 | + |
| 54 | + var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(AddConnectionAsync), updateEvent, DateTime.UtcNow, cancellationToken); |
| 55 | + await ApplyEntryUpdateAsync(appendedEntry, updateEvent, new Connection { Id = connection.Id, Value = valueCid }, connection, cancellationToken); |
| 56 | + |
| 57 | + EventStreamPosition = appendedEntry; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Removes a connection from this entity. |
| 62 | + /// </summary> |
| 63 | + public async Task RemoveConnectionAsync(IReadOnlyConnection connection, CancellationToken cancellationToken) |
| 64 | + { |
| 65 | + var existing = Inner.Inner.Connections.FirstOrDefault(x => x.Id == connection.Id); |
| 66 | + if (existing == null) |
| 67 | + throw new ArgumentException("Connection not found in the collection.", nameof(connection)); |
| 68 | + |
| 69 | + var value = await connection.GetValueAsync(cancellationToken); |
| 70 | + |
| 71 | + var keyCid = (DagCid)await Client.Dag.PutAsync(connection.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 72 | + var valueCid = (DagCid)await Client.Dag.PutAsync(value, pin: KuboOptions.ShouldPin, cancel: cancellationToken); |
| 73 | + |
| 74 | + var updateEvent = new ValueUpdateEvent(keyCid, valueCid, Unset: true); |
| 75 | + |
| 76 | + var appendedEntry = await AppendNewEntryAsync(targetId: Id, eventId: nameof(RemoveConnectionAsync), updateEvent, DateTime.UtcNow, cancellationToken); |
| 77 | + await ApplyEntryUpdateAsync(appendedEntry, updateEvent, new Connection { Id = connection.Id, Value = valueCid }, connection, cancellationToken); |
| 78 | + |
| 79 | + EventStreamPosition = appendedEntry; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Applies an event stream update event and raises the relevant events. |
| 84 | + /// </summary> |
| 85 | + /// <remarks> |
| 86 | + /// This method will call <see cref="ReadOnlyConnectionCollection.GetAsync(string, CancellationToken)"/> and create a new instance to pass to the event handlers. |
| 87 | + /// <para/> |
| 88 | + /// If already have a resolved instance of <see cref="Connection"/>, you should call <see cref="ApplyEntryUpdateAsync(EventStreamEntry{DagCid}, ValueUpdateEvent, Connection, IReadOnlyConnection?, CancellationToken)"/> instead. |
| 89 | + /// </remarks> |
| 90 | + /// <param name="eventStreamEntry">The event stream entry to apply.</param> |
| 91 | + /// <param name="updateEvent">The update event to apply.</param> |
| 92 | + /// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param> |
| 93 | + public override async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, CancellationToken cancellationToken) |
| 94 | + { |
| 95 | + cancellationToken.ThrowIfCancellationRequested(); |
| 96 | + |
| 97 | + if (eventStreamEntry.TargetId != Id) |
| 98 | + return; |
| 99 | + |
| 100 | + Guard.IsNotNull(updateEvent.Value); |
| 101 | + var (connection, _) = await Client.ResolveDagCidAsync<Connection>(updateEvent.Value.Value, nocache: !KuboOptions.UseCache, cancellationToken); |
| 102 | + |
| 103 | + Guard.IsNotNull(connection); |
| 104 | + await ApplyEntryUpdateAsync(eventStreamEntry, updateEvent, connection, null, cancellationToken); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Applies an event stream update event and raises the relevant events. |
| 109 | + /// </summary> |
| 110 | + /// <param name="eventStreamEntry">The event stream entry to apply.</param> |
| 111 | + /// <param name="updateEvent">The update event to apply.</param> |
| 112 | + /// <param name="connection">The resolved connection data for this event.</param> |
| 113 | + /// <param name="connectionInstance">The existing instance of <see cref="IReadOnlyConnection"/> to emit on raised collection add/remove events.</param> |
| 114 | + /// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param> |
| 115 | + public async Task ApplyEntryUpdateAsync(EventStreamEntry<DagCid> eventStreamEntry, ValueUpdateEvent updateEvent, Connection connection, IReadOnlyConnection? connectionInstance, CancellationToken cancellationToken) |
| 116 | + { |
| 117 | + cancellationToken.ThrowIfCancellationRequested(); |
| 118 | + |
| 119 | + switch (eventStreamEntry.EventId) |
| 120 | + { |
| 121 | + case nameof(AddConnectionAsync): |
| 122 | + { |
| 123 | + Inner.Inner.Connections = [.. Inner.Inner.Connections, connection]; |
| 124 | + var connectionFile = connectionInstance ??= await Inner.GetAsync(connection.Id, cancellationToken); |
| 125 | + ConnectionsAdded?.Invoke(this, [connectionFile]); |
| 126 | + break; |
| 127 | + } |
| 128 | + case nameof(RemoveConnectionAsync): |
| 129 | + { |
| 130 | + Inner.Inner.Connections = [.. Inner.Inner.Connections.Except([connection])]; |
| 131 | + var connectionFile = connectionInstance ??= await Inner.GetAsync(connection.Id, cancellationToken); |
| 132 | + ConnectionsRemoved?.Invoke(this, [connectionFile]); |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <inheritdoc/> |
| 139 | + public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken) |
| 140 | + { |
| 141 | + cancellationToken.ThrowIfCancellationRequested(); |
| 142 | + |
| 143 | + // TODO: Reset inner virtual event stream handlers |
| 144 | + // Connections, Links, Images |
| 145 | + throw new NotImplementedException(); |
| 146 | + } |
| 147 | +} |
0 commit comments