public void Constructor_Always_PropertiesAreGiven()
        {
            var mockOldMessage = new Mock <ICacheable <IMessage, ulong> >();
            var mockNewMessage = new Mock <ISocketMessage>();
            var mockChannel    = new Mock <IISocketMessageChannel>();

            var uut = new MessageUpdatedNotification(mockOldMessage.Object, mockNewMessage.Object, mockChannel.Object);

            uut.OldMessage.ShouldBeSameAs(mockOldMessage.Object);
            uut.NewMessage.ShouldBeSameAs(mockNewMessage.Object);
            uut.Channel.ShouldBeSameAs(mockChannel.Object);
        }
        public async Task HandleNotificationAsync_MessageUpdatedNotification_InviteLinkIsForMessageGuild_DoesNotDeleteMessage(string messageContent)
        {
            (var autoMocker, var uut) = BuildTestContext();

            var notification = new MessageUpdatedNotification(
                oldMessage: autoMocker.Get <ICacheable <IMessage, ulong> >(),
                newMessage: BuildTestMessage(autoMocker, messageContent),
                channel: autoMocker.Get <IISocketMessageChannel>());

            await uut.HandleNotificationAsync(notification);

            autoMocker.MessageShouldNotHaveBeenDeleted();
        }
Exemplo n.º 3
0
        public async Task Handle(MessageUpdatedNotification notification, CancellationToken cancellationToken)
        {
            var cachedMessage = await notification.OldMessage.GetOrDownloadAsync();

            if (cachedMessage is null)
            {
                return;
            }

            if (Pattern.IsMatch(cachedMessage.Content))
            {
                return;
            }

            await OnMessageReceivedAsync(notification.NewMessage);
        }
        public async Task HandleNotificationAsync_MessageUpdatedNotification_MessageAuthorIsSelfUser_DoesNotDeleteMessage()
        {
            (var autoMocker, var uut) = BuildTestContext();

            var notification = new MessageUpdatedNotification(
                oldMessage: autoMocker.Get <ICacheable <IMessage, ulong> >(),
                newMessage: BuildTestMessage(autoMocker, DefaultInviteLink),
                channel: autoMocker.Get <IISocketMessageChannel>());

            autoMocker.GetMock <ISocketSelfUser>()
            .Setup(x => x.Id)
            .Returns(autoMocker.Get <IGuildUser>().Id);

            await uut.HandleNotificationAsync(notification);

            autoMocker.MessageShouldNotHaveBeenDeleted();
        }
        public async Task HandleNotificationAsync(
            MessageUpdatedNotification notification,
            CancellationToken cancellationToken)
        {
            var guild = (notification.Channel as ISocketGuildChannel)?.Guild;

            if (notification.NewMessage.Author.Id == default)
            {
                // update caused by new thread created or deleted
                return;
            }

            using var logScope = MessageLoggingLogMessages.BeginMessageNotificationScope(_logger, guild?.Id, notification.Channel.Id, notification.NewMessage.Id);

            MessageLoggingLogMessages.MessageUpdatedHandling(_logger);

            await TryLogAsync(
                guild,
                notification.OldMessage.HasValue?notification.OldMessage.Value : null,
                notification.NewMessage,
                notification.Channel,
                () => new EmbedBuilder()
                .WithUserAsAuthor(notification.NewMessage.Author, notification.NewMessage.Author.Id.ToString())
                .WithDescription($"\\📝 **Message edited in {notification.NewMessage.GetJumpUrlForEmbed()}**")
                .WithCurrentTimestamp()
                .WithFields(Enumerable.Empty <EmbedFieldBuilder>()
                            .Concat(FormatMessageContent(notification.OldMessage.HasValue
                                ? notification.OldMessage.Value.Content
                                : null)
                                    .EnumerateLongTextAsFieldBuilders("**Original**"))
                            .Concat(FormatMessageContent(notification.NewMessage.Content)
                                    .EnumerateLongTextAsFieldBuilders("**Updated**"))
                            .Append(new EmbedFieldBuilder()
                                    .WithName("Channel ID")
                                    .WithValue(notification.Channel.Id)
                                    .WithIsInline(true))
                            .Append(new EmbedFieldBuilder()
                                    .WithName("Message ID")
                                    .WithValue(notification.NewMessage.Id)
                                    .WithIsInline(true)))
                .Build(),
                cancellationToken);

            MessageLoggingLogMessages.MessageUpdatedHandled(_logger);
        }
Exemplo n.º 6
0
        public async Task HandleNotificationAsync_MessageUpdatedNotification_Otherwise_DeletesMessage(string messageContent)
        {
            (var autoMocker, var uut) = BuildTestContext();

            var notification = new MessageUpdatedNotification(
                oldMessage: autoMocker.Get <ICacheable <IMessage, ulong> >(),
                newMessage: BuildTestMessage(autoMocker, messageContent),
                channel: autoMocker.Get <IISocketMessageChannel>());

            await uut.HandleNotificationAsync(notification);

            autoMocker.GetMock <IModerationService>()
            .ShouldHaveReceived(x => x.
                                DeleteMessageAsync(
                                    notification.NewMessage,
                                    It.Is <string>(y => y.Contains("invite", StringComparison.OrdinalIgnoreCase)),
                                    autoMocker.Get <ISocketSelfUser>().Id));
        }
        public async Task HandleNotificationAsync_MessageUpdatedNotification_MessageAuthorHasPostInviteLink_DoesNotDeleteMessage()
        {
            (var autoMocker, var uut) = BuildTestContext();

            var notification = new MessageUpdatedNotification(
                oldMessage: autoMocker.Get <ICacheable <IMessage, ulong> >(),
                newMessage: BuildTestMessage(autoMocker, DefaultInviteLink),
                channel: autoMocker.Get <IISocketMessageChannel>());

            autoMocker.GetMock <IAuthorizationService>()
            .Setup(x => x.HasClaimsAsync(
                       autoMocker.Get <IGuildUser>(),
                       AuthorizationClaim.PostInviteLink))
            .ReturnsAsync(true);

            await uut.HandleNotificationAsync(notification);

            autoMocker.MessageShouldNotHaveBeenDeleted();
        }
Exemplo n.º 8
0
        public async Task HandleNotificationAsync_MessageUpdatedNotification_Otherwise_SendsResponse()
        {
            (var autoMocker, var uut) = BuildTestContext();

            var notification = new MessageUpdatedNotification(
                oldMessage: autoMocker.Get <ICacheable <IMessage, ulong> >(),
                newMessage: BuildTestMessage(autoMocker, DefaultInviteLink),
                channel: autoMocker.Get <IISocketMessageChannel>());

            await uut.HandleNotificationAsync(notification);

            autoMocker.GetMock <IMessageChannel>()
            .ShouldHaveReceived(x => x.
                                SendMessageAsync(
                                    It.Is <string>(y => y.Contains("invite", StringComparison.OrdinalIgnoreCase)),
                                    It.IsAny <bool>(),
                                    It.IsAny <Embed>(),
                                    It.IsAny <RequestOptions>()));
        }
Exemplo n.º 9
0
        public async Task HandleNotificationAsync_MessageUpdatedNotification_MessageChannelIsUnmoderated_DoesNotDeleteMessage()
        {
            (var autoMocker, var uut) = BuildTestContext();

            var notification = new MessageUpdatedNotification(
                oldMessage: autoMocker.Get <ICacheable <IMessage, ulong> >(),
                newMessage: BuildTestMessage(autoMocker, DefaultInviteLink),
                channel: autoMocker.Get <IISocketMessageChannel>());

            autoMocker.GetMock <IDesignatedChannelService>()
            .Setup(x => x.ChannelHasDesignationAsync(
                       autoMocker.Get <IGuild>(),
                       autoMocker.Get <IMessageChannel>(),
                       DesignatedChannelType.Unmoderated))
            .ReturnsAsync(true);

            await uut.HandleNotificationAsync(notification);

            autoMocker.MessageShouldNotHaveBeenDeleted();
        }
Exemplo n.º 10
0
 /// <inheritdoc />
 public Task HandleNotificationAsync(MessageUpdatedNotification notification, CancellationToken cancellationToken = default)
 => TryPurgeInviteLinkAsync(notification.NewMessage);
Exemplo n.º 11
0
 public Task HandleNotificationAsync(MessageUpdatedNotification notification,
                                     CancellationToken cancellationToken = default)
 => TryCheckMessageAsync(notification.NewMessage);