Example #1
0
        public async Task <OneOf <DbCollection> > CreateAsync(ObjectType type, CollectionBase model, string userId, string[] items = null, CancellationToken cancellationToken = default)
        {
            if (items != null && items.Length != 0)
            {
                var newItems = new string[items.Length];
                var set      = new HashSet <string>(items.Length, StringComparer.Ordinal);
                var count    = 0;

                foreach (var item in items)
                {
                    if (set.Add(item))
                    {
                        newItems[count++] = item;
                    }
                }

                Array.Resize(ref newItems, count);

                items = newItems;
            }

            var collection = new DbCollection
            {
                Type     = type,
                OwnerIds = new[] { userId },
                Items    = items ?? Array.Empty <string>()
            }.ApplyBase(model, _services);

            return(await _client.Entry(collection).CreateAsync(cancellationToken));
        }
Example #2
0
        public async Task <OneOf <DbCollection> > CreateAsync(ObjectType type, CollectionBase model, string userId, CancellationToken cancellationToken = default)
        {
            var collection = new DbCollection
            {
                Type     = type,
                OwnerIds = new[] { userId },
                Items    = Array.Empty <string>()
            }.ApplyBase(model, _services);

            return(await _client.Entry(collection).CreateAsync(cancellationToken));
        }
Example #3
0
        public async Task <DbUser> GetOrCreateUserAsync(DiscordOAuthUser user, CancellationToken cancellationToken = default)
        {
            await using (await _locker.EnterAsync($"oauth:discord:{user.Id}", cancellationToken))
            {
                var entry = await GetByIdAsync(user.Id, cancellationToken);

                // if user already exists, update their info
                if (entry != null)
                {
                    do
                    {
                        user.ApplyOn(entry.Value);
                    }while (!await entry.TryUpdateAsync(cancellationToken));
                }

                // otherwise create new user
                else
                {
                    entry = _client.Entry(_users.MakeUserObject());

                    user.ApplyOn(entry.Value);

                    await entry.CreateAsync(cancellationToken);

                    await _snapshots.CreateAsync(entry.Value, new SnapshotArgs
                    {
                        Source    = SnapshotSource.User,
                        Committer = entry.Value,
                        Event     = SnapshotEvent.AfterCreation,
                        Reason    = $"Registered via Discord OAuth2 '{user.Username}'."
                    }, cancellationToken);
                }

                return(entry.Value);
            }
        }