Example #1
0
        public async Task InitiateUser(string code, long state)
        {
            var token = await _attendance.OAuth(code);

            var whoami = _attendance.GetUserId(token);
            var anime  = _attendance.GetAnimeList(whoami.Id);
            var manga  = _attendance.GetMangaList(whoami.Id);
            await Task.WhenAll(whoami, anime, manga);

            _database.Update(new DatabaseUser
            {
                Token             = token,
                State             = state,
                Animes            = anime.Result,
                Mangas            = manga.Result,
                ShikimoriId       = whoami.Result.Id,
                ShikimoriNickname = whoami.Result.Nickname,
                UpdatedAt         = DateTime.UtcNow
            });

            await Task.WhenAll(
                _database.UpdateMangaInformation(manga.Result, _attendance.GetMangaInformation),
                _database.UpdateLinks(anime.Result, TargetType.Anime, _attendance.GetLinks),
                _database.UpdateLinks(manga.Result, TargetType.Manga, _attendance.GetLinks)
                );
        }
        protected override async void DoWork(object state)
        {
            _logger.LogInformation("Initiating Full Update");
            var all = _database.GetAllUsers().ToList();

            if (all.Count == 0)
            {
                _logger.LogInformation("No users widePeepoSad");
                return;
            }

            var animes  = new HashSet <long>();
            var mangas  = new HashSet <long>();
            var updated = DateTime.UtcNow;

            var updatedUsers = await Task.WhenAll(all.AsParallel().Select(async it =>
            {
                it.Token             = await _renewer.EnsureToken(it);
                var whoami           = await _attendance.GetUserId(it.Token);
                it.ShikimoriNickname = whoami.Nickname;
                it.Animes            = await _attendance.GetAnimeList(whoami.Id);
                it.Mangas            = await _attendance.GetMangaList(whoami.Id);
                it.UpdatedAt         = updated;
                return(it);
            }));

            foreach (var user in updatedUsers)
            {
                _database.Update(user);
                animes.UnionWith(user.Animes);
                mangas.UnionWith(user.Mangas);
            }

            await Task.WhenAll(
                _database.UpdateMangaInformation(mangas, _attendance.GetMangaInformation),
                _database.UpdateLinks(animes, TargetType.Anime, _attendance.GetLinks),
                _database.UpdateLinks(mangas, TargetType.Manga, _attendance.GetLinks)
                );
        }