public async Task Handle(DeleteMessageNotification notification, CancellationToken cancellationToken)
        {
            var messageId = notification.MessageId;
            var context   = _contextFactory.CreateDbContext();

            // remove message from database
            var message = await context.Messages.FirstOrDefaultAsync(m => m.Id == messageId, cancellationToken);

            context.Messages.Remove(message);
            await context.SaveChangesAsync(cancellationToken);

            // also refresh that channel's Markov chain
            await _chainService.AddChainForChannelAsync(message.ChannelId, cancellationToken);
        }
Пример #2
0
        public async Task Handle(PurgeWordNotification notification, CancellationToken cancellationToken)
        {
            var(channelId, word) = notification;
            var context     = _contextFactory.CreateDbContext();
            var badMessages = context.Messages.Where(m => m.ChannelId == channelId && m.Message.ToLower().Contains(word.ToLower()));

            // ditch the bad messages
            context.Messages.RemoveRange(badMessages);
            var count = await context.SaveChangesAsync(cancellationToken);

            _logger.LogInformation("Purged {Count} messages from channel {ChannelId}.", count, channelId);

            // re-create the pool for the given channel
            await _chainService.AddChainForChannelAsync(channelId, cancellationToken);
        }
        public async Task <Unit> Handle(BanUserRequest request, CancellationToken cancellationToken)
        {
            var(channelName, username) = request;
            var channelId = await _userService.GetIdByUsername(channelName);

            var userId = await _userService.GetIdByUsername(username);

            var context = _contextFactory.CreateDbContext();

            // remove all messages from that user in that channel from database
            var messages = context.Messages.Where(m => m.ChannelId == channelId && m.UserId == userId);

            context.Messages.RemoveRange(messages);
            await context.SaveChangesAsync(cancellationToken);

            // also refresh that channel's Markov chain
            await _chainService.AddChainForChannelAsync(channelId, cancellationToken);

            return(Unit.Value);
        }
Пример #4
0
        public async Task <Unit> Handle(JoinRequest request, CancellationToken cancellationToken)
        {
            var channelId = request.ChannelId;
            var context   = _contextFactory.CreateDbContext();

            if (context.Channels.Any(c => c.Id == channelId))
            {
                return(Unit.Value);
            }

            // make sure a user exists on Twitch first
            string channelName;

            try
            {
                channelName = await _userService.GetUsernameById(channelId);
            }
            catch (HttpRequestException ex)
            {
                _logger.LogWarning(ex, "Could not obtain channel name from API, response did not indicate success");
                throw;
            }

            // add channel to database
            context.Channels.Add(new TwitchChannel
            {
                Id = channelId
            });
            await context.SaveChangesAsync(cancellationToken);

            // connect to channel on Twitch client
            await _twitchClient.JoinChannelAsync(channelName, cancellationToken);

            // also create a Markov chain
            await _chainService.AddChainForChannelAsync(channelId, cancellationToken);

            return(Unit.Value);
        }
Пример #5
0
        public async Task Handle(IgnoreUserNotification notification, CancellationToken cancellationToken)
        {
            var(channelId, userId) = notification;

            var context     = _contextFactory.CreateDbContext();
            var badMessages = context.Messages.Where(m => m.ChannelId == channelId && m.UserId == userId);

            // ditch the bad messages and add the user
            context.Messages.RemoveRange(badMessages);
            context.IgnoredUsers.Add(new IgnoredTwitchUser
            {
                Id        = userId,
                ChannelId = channelId,
                BannedOn  = DateTime.UtcNow
            });
            await context.SaveChangesAsync(cancellationToken);

            _logger.LogInformation("Now ignoring user {UserId} for channel {ChannelId}.", userId, channelId);
            await _mediator.Send(new SendMessageRequest(channelId, "Alrighty, I won't listen to them anymore! Also, I'll forget all the things they've said up till now."), cancellationToken);

            // re-create the chain for the given channel
            await _chainService.AddChainForChannelAsync(channelId, cancellationToken);
        }