Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] RequestDto request)
        {
            var redditUser = await _redditUserStorage.FindAsync(request.RedditUserId);

            if (redditUser == null)
            {
                return(BadRequest());
            }
            var postRecurrence     = new PostRecurrenceEntity(request.Title, request.Body, string.Join(",", request.Subreddits), request.Interval, request.Email, request.RedditUserId);
            var savedPostRecrrence = await _postRecurrenceStorage.CreateAsync(postRecurrence);

            return(Ok(new MessageDto($"Posts scheduled to begin {savedPostRecrrence.NextPost.UtcDateTime}. To stop recurring posts revoke access to app via reddit.")));
        }
Exemplo n.º 2
0
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var posts = await _postRecurrenceStorage.FindAllAsync();

                    foreach (var post in posts)
                    {
                        if (!ShouldStart(post.NextPost))
                        {
                            continue;
                        }

                        var redditUser = await _redditUserStorage.FindAsync(post.RedditUserId);

                        var accessToken = await _redditService.RefreshIfExpiredAsync(redditUser.RefreshToken, redditUser.TokenExpiresAt, redditUser.AccessToken);

                        if (accessToken != null)
                        {
                            // update record with refreshed token
                            redditUser.AccessToken    = accessToken.AccessToken;
                            redditUser.TokenExpiresAt = accessToken.TokenExpiration;
                            redditUser.Scope          = accessToken.Scope;
                            await _redditUserStorage.UpdateAsync(redditUser);
                        }

                        var subs = post.Subreddits.Split(",");

                        foreach (var sub in subs)
                        {
                            try
                            {
                                await _redditService.CreatePostAsync(sub, post.Title, post.Body);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogCritical($"Post failed for Post Id: {post.Id}, on Subreddit {sub}");
                                _logger.LogCritical(ex.Message, ex.StackTrace);
                            }
                        }

                        // reset next send
                        post.LastPost = DateTimeOffset.UtcNow;
                        post.NextPost = DateTimeOffset.UtcNow.AddSeconds(post.IntervalSeconds);
                        await _postRecurrenceStorage.UpdateAsync(post);
                    }

                    await Task.Delay(TimeSpan.FromSeconds(15), stoppingToken);
                }
                catch (Exception ex)
                {
                    _logger.LogCritical($"Post job through unhandled exception. Will try to continue.");
                    _logger.LogCritical(ex.Message, ex.StackTrace);
                    await Task.Delay(TimeSpan.FromSeconds(15), stoppingToken);
                }
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Get(Guid Id)
        {
            var redditUser = await _redditUserStorage.FindAsync(Id);

            if (redditUser == null)
            {
                return(BadRequest());
            }
            return(Ok(new RedditIdentity(redditUser.RedditUsername)));
        }