예제 #1
0
        public async Task Execute(string email, CancellationToken cancellationToken)
        {
            var patron = await Database.Patrons.FirstOrDefaultAsync(p => p.Email == email, cancellationToken);

            // TODO: alias handling
            var forumEmail = patron?.Email ?? email;

            var forumUser = await DiscourseAPI.FindUserByEmail(forumEmail, cancellationToken);

            if (forumUser == null)
            {
                logger.LogInformation("Single user has no forum account, no handling needed for groups");

                // TODO: maybe re-queueing a check in 15 minutes should be done once
                return;
            }

            if (!await LoadSettingsOrSkip(logger, cancellationToken))
            {
                logger.LogWarning("Skipping Single patron groups apply because patreon settings are missing");
                return;
            }

            logger.LogInformation("Applying forum groups for single patron ({Username})", forumUser.Username);

            // See the TODO comment below as to why this is not needed currently

            /*
             * var forumUserFull = await DiscourseAPI.UserInfoByName(forumUser.Username, cancellationToken);
             *
             * if (forumUserFull?.User == null)
             *  throw new Exception("Failed to find user group info after finding user object on the forums");
             */

            // TODO: this is highly inefficient, but there doesn't seem to be an API to get just group owners.
            // So we can use the existing code
            await LoadDiscourseGroupMembers(cancellationToken);

            // Bit of a hack, but the rest of the code doesn't need changes this way
            // When a patron is deleted, this job runs with just an email but no patron object so make one here
            patron ??= new Patron()
            {
                Username = forumUser.Username,
                Email    = forumEmail,
                RewardId = "none",
            };

            HandlePatron(patron, forumUser, logger);

            await ApplyGroupMemberChanges(logger, cancellationToken);
        }
예제 #2
0
        private async Task HandlePatrons(CancellationToken cancellationToken)
        {
            if (Settings == null)
            {
                throw new InvalidOperationException("Patreon settings haven't been loaded");
            }

            // TODO: might need to change this to working in batches
            var allPatrons = await Database.Patrons.ToListAsync(cancellationToken);

            foreach (var patron in allPatrons)
            {
                // Skip patrons who shouldn't have a forum group, check_unmarked will find them
                if (PatreonGroupHandler.ShouldBeInGroupForPatron(patron, Settings) ==
                    PatreonGroupHandler.RewardGroup.None)
                {
                    continue;
                }

                // Also skip suspended who should have their groups revoked as long as they are suspended
                if (patron.Suspended == true)
                {
                    continue;
                }

                // TODO: alias implementation
                var forumUser = await DiscourseAPI.FindUserByEmail(patron.Email, cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (forumUser == null)
                {
                    logger.LogTrace("Patron ({Username}) is missing a forum account, can't apply groups",
                                    patron.Username);
                    patron.HasForumAccount = false;
                }
                else
                {
                    patron.HasForumAccount = true;
                    HandlePatron(patron, forumUser, logger);
                }
            }
        }