public async Task <OneOf <DbUser, NotFound> > UnrestrictAsync(string id, SnapshotArgs snapshot, CancellationToken cancellationToken = default) { var entry = await _client.GetEntryAsync <DbUser>(id, cancellationToken); do { if (entry.Value == null) { return(new NotFound()); } if (entry.Value.Restrictions == null) { break; } var now = DateTime.UtcNow; var changed = false; foreach (var restriction in entry.Value.Restrictions) { // currently active restrictions if (now <= restriction.EndTime || restriction.EndTime == null) { // move restriction end time to current time, ending it immediately restriction.EndTime = now; changed = true; } // future restrictions else if (now < restriction.StartTime) { // move restriction end time to start time, invalidating it restriction.EndTime = restriction.StartTime; changed = true; } } if (!changed) { break; } }while (!await entry.TryUpdateAsync(cancellationToken)); if (snapshot != null) { await _snapshots.CreateAsync(entry.Value, snapshot, cancellationToken); } return(entry.Value); }
public async Task <OneOf <DbUser, NotFound> > UpdateAsync(string id, UserBase user, SnapshotArgs snapshot, CancellationToken cancellationToken = default) { var entry = await _client.GetEntryAsync <DbUser>(id, cancellationToken); do { if (entry.Value == null) { return(new NotFound()); } if (!entry.Value.TryApplyBase(user, _services)) { break; } }while (!await entry.TryUpdateAsync(cancellationToken)); if (snapshot != null) { await _snapshots.CreateAsync(entry.Value, snapshot, cancellationToken); } return(entry.Value); }
public async Task <OneOf <DbUser, NotFound> > RestrictAsync(string id, string moderatorId, TimeSpan?duration, SnapshotArgs snapshot, CancellationToken cancellationToken = default) { var entry = await _client.GetEntryAsync <DbUser>(id, cancellationToken); do { if (entry.Value == null) { return(new NotFound()); } var now = DateTime.UtcNow; var restriction = new DbUserRestriction { StartTime = now, EndTime = now + duration, ModeratorId = moderatorId, Reason = snapshot?.Reason }; entry.Value.Restrictions = (entry.Value.Restrictions ?? Enumerable.Empty <DbUserRestriction>()).Append(restriction).ToArray(); }while (!await entry.TryUpdateAsync(cancellationToken)); if (snapshot != null) { await _snapshots.CreateAsync(entry.Value, snapshot, cancellationToken); } return(entry.Value); }