public SpamProbability GetSpamProbability(ServerMessagesCacheService serverMessagesCacheService, DiscordRequest request, Contexts contexts)
        {
            var userId          = contexts.User.Id;
            var serverId        = contexts.Server.Id;
            var lastFewMessages = serverMessagesCacheService.GetLastUserMessages(userId, serverId)
                                  .TakeLast(4)
                                  .ToList();

            if (lastFewMessages.Count < 2)
            {
                return(SpamProbability.None);
            }

            var content = request.OriginalMessage;
            var percentOfSimilarityToSuspectSpam = this._configurationService.GetConfigurationItem <PercentOfSimilarityBetweenMessagesToSuspectSpam>(serverId).Value;
            var similarMessagesCount             = lastFewMessages.Count(x => this.GetDifferencePercent(x.Content, content) < percentOfSimilarityToSuspectSpam);
            var isUserSafe = this.UserSafetyChecker.IsUserSafe(userId, serverId);

            return(similarMessagesCount switch
            {
                0 => SpamProbability.None,
                1 => SpamProbability.Low,
                2 when isUserSafe => SpamProbability.Low,
                2 => SpamProbability.Medium,
                _ when isUserSafe => SpamProbability.Medium,
                _ => SpamProbability.Sure
            });
Пример #2
0
        public void OverallSpamDetectorStrategy_ShouldNotDetectSpam(string messageContent1, string messageContent2, string messageContent3, string messageContent4, bool isUserSafe)
        {
            // Arrange
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);

            var contexts       = this._antiSpamTestsService.GetDefaultContexts();
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(new List <SmallMessage>
            {
                new SmallMessage(messageContent1, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-20), GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent2, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-16), GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent3, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-15), GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent4, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-10), GetMessagesQuery.GET_ALL_SERVERS)
            });
            var overallSpamDetector = OverallSpamDetectorStrategy.GetStrategyWithDefaultDetectors(serverMessages, userSafetyChecker.Object, this._configurationService);

            // Act
            var overallSpamProbability = overallSpamDetector.GetOverallSpamProbability(contexts);

            // Assert
            Assert.That(overallSpamProbability, Is.EqualTo(SpamProbability.None));
        }
Пример #3
0
        public SpamProbability GetSpamProbability(bool isUserSafe, params string[] messagesContent)
        {
            // Arrange
            var spamTestsService  = new AntiSpamTestsService();
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);
            var configurationService = new Mock <IConfigurationService>();

            configurationService
            .Setup(x => x.GetConfigurationItem <PercentOfSimilarityBetweenMessagesToSuspectSpam>(It.IsAny <ulong>()))
            .Returns(new PercentOfSimilarityBetweenMessagesToSuspectSpam(GetMessagesQuery.GET_ALL_SERVERS));

            var(request, contexts) = spamTestsService.CreateRequestAndContexts(messagesContent.Last());
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(messagesContent.SkipLast(1).Select(x => new SmallMessage(x, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS)));

            var needsConfiguration = typeof(T).GetConstructors().Any(x => x.GetParameters().Any(x => x.ParameterType == typeof(IConfigurationService)));
            var spamDetector       = needsConfiguration
                ? (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object, configurationService.Object)
                : (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object);

            // Act
            return(spamDetector !.GetSpamProbability(serverMessages, request, contexts));
        }
Пример #4
0
        public SpamProbability GetSpamProbability(ServerMessagesCacheService serverMessagesCacheService, Contexts contexts)
        {
            var userId          = contexts.User.Id;
            var serverId        = contexts.Server.Id;
            var lastFewMessages = serverMessagesCacheService.GetLastUserMessages(userId, serverId)
                                  .TakeLast(5)
                                  .ToList();

            if (lastFewMessages.Count < 2)
            {
                return(SpamProbability.None);
            }
            var percentOfSimilarityToSuspectSpam = this._configurationService.GetConfigurationItem <PercentOfSimilarityBetweenMessagesToSuspectSpam>(serverId).Value;
            var content = lastFewMessages.Last().Content;
            var similarMessagesCount = lastFewMessages
                                       .SkipLast(1) // because I'm comparing all the other messages to the last message
                                       .Count(x => this.GetDifferencePercent(x.Content, content) < percentOfSimilarityToSuspectSpam);

            var isUserSafe = this._userSafetyChecker.IsUserSafe(userId, serverId);

            return(similarMessagesCount switch
            {
                0 => SpamProbability.None,
                1 => SpamProbability.Low,
                2 when isUserSafe => SpamProbability.Low,
                2 => SpamProbability.Medium,
                _ when isUserSafe => SpamProbability.Medium,
                _ => SpamProbability.Sure
            });
        public void OverallSpamDetectorStrategy_ShouldNotDetectSpam(string messageContent1, string messageContent2, string messageContent3, string messageContent4, bool isUserSafe)
        {
            // Arrange
            var spamTestsService  = new AntiSpamTestsService();
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);

            var(request, contexts) = spamTestsService.CreateRequestAndContexts(messageContent4);
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(new List <SmallMessage>
            {
                new SmallMessage(messageContent1, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent2, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent3, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS)
            });
            var configurationService = new Mock <IConfigurationService>();

            configurationService
            .Setup(x => x.GetConfigurationItem <PercentOfSimilarityBetweenMessagesToSuspectSpam>(It.IsAny <ulong>()))
            .Returns(new PercentOfSimilarityBetweenMessagesToSuspectSpam(GetMessagesQuery.GET_ALL_SERVERS));
            var overallSpamDetector = OverallSpamDetectorStrategy.GetStrategyWithDefaultDetectors(serverMessages, userSafetyChecker.Object, configurationService.Object);

            // Act
            var overallSpamProbability = overallSpamDetector.GetOverallSpamProbability(request, contexts);

            // Assert
            Assert.That(overallSpamProbability, Is.EqualTo(SpamProbability.None));
        }
Пример #6
0
 public AntiSpamController(ServerMessagesCacheService serverMessagesCacheService, CheckUserSafetyService checkUserSafetyService, PunishmentsCachingService punishmentsCachingService, AntiSpamService antiSpamService, ConfigurationService configurationService)
 {
     this._serverMessagesCacheService = serverMessagesCacheService;
     this._punishmentsCachingService  = punishmentsCachingService;
     this._antiSpamService            = antiSpamService;
     this._overallSpamDetector        = OverallSpamDetectorStrategy.GetStrategyWithDefaultDetectors(serverMessagesCacheService, checkUserSafetyService, configurationService);
     this._spamPunishmentStrategy     = new SpamPunishmentStrategy(punishmentsCachingService);
 }
Пример #7
0
 public static OverallSpamDetectorStrategy GetStrategyWithDefaultDetectors(ServerMessagesCacheService serverMessagesCacheService, IUserSafetyChecker userSafetyChecker, IConfigurationService configurationService)
 {
     return(new OverallSpamDetectorStrategy(serverMessagesCacheService, new List <ISpamDetector>
     {
         new LinksDetectorStrategy(userSafetyChecker),
         new DuplicatedMessagesDetectorStrategy(userSafetyChecker, configurationService)
     }));
 }
Пример #8
0
        public SpamProbability GetSpamProbability(ServerMessagesCacheService serverMessagesCacheService, Contexts contexts)
        {
            var content = serverMessagesCacheService.GetLastUserMessages(contexts.User.Id, contexts.Server.Id).Last().Content;

            if (!this._linkRegex.IsMatch(content))
            {
                return(SpamProbability.None);
            }
            var userId   = contexts.User.Id;
            var serverId = contexts.Server.Id;

            return(this._userSafetyChecker.IsUserSafe(userId, serverId)
                ? SpamProbability.Low
                : SpamProbability.Medium);
        }
Пример #9
0
        public SpamProbability GetSpamProbability(ServerMessagesCacheService serverMessagesCacheService, DiscordRequest request, Contexts contexts)
        {
            var content = request.OriginalMessage;

            if (!this._linkRegex.IsMatch(content))
            {
                return(SpamProbability.None);
            }

            var userId   = contexts.User.Id;
            var serverId = contexts.Server.Id;

            return(this.UserSafetyChecker.IsUserSafe(userId, serverId)
                ? SpamProbability.Low
                : SpamProbability.Medium);
        }
Пример #10
0
        public SpamProbability GetSpamProbability(ServerMessagesCacheService serverMessagesCacheService, Contexts contexts)
        {
            var lastFewMessages = serverMessagesCacheService.GetLastUserMessages(contexts.User.Id, contexts.Server.Id)
                                  .TakeLast(7)
                                  .ToList();

            if (!this.IsMessageWithMuchCapslock(lastFewMessages.Last().Content, contexts.Server.Id))
            {
                return(SpamProbability.None);
            }
            var capslockedMessagesCount = lastFewMessages.Count(x => this.IsMessageWithMuchCapslock(x.Content, contexts.Server.Id));

            return(capslockedMessagesCount switch
            {
                1 => SpamProbability.Low,
                2 => SpamProbability.Medium,
                _ => SpamProbability.Sure
            });
Пример #11
0
        public SpamProbability GetSpamProbability(ServerMessagesCacheService serverMessagesCacheService, Contexts contexts)
        {
            var howManyMessagesCount = this._configurationService.GetConfigurationItem <HowManyMessagesInShortTimeToBeSpam>(contexts.Server.Id).Value;
            var howManySeconds       = this._configurationService.GetConfigurationItem <HowLongIsShortTimeInSeconds>(contexts.Server.Id).Value;
            var minDate       = DateTime.UtcNow.AddSeconds(-howManySeconds);
            var messagesCount = serverMessagesCacheService.GetLastUserMessages(contexts.User.Id, contexts.Server.Id)
                                .TakeLast(howManyMessagesCount)
                                .Count(x => x.SentAt >= minDate);

            if (messagesCount <= howManyMessagesCount / 3)
            {
                return(SpamProbability.None);
            }
            var userIsSafe = this._userSafetyChecker.IsUserSafe(contexts.User.Id, contexts.Server.Id);

            return(userIsSafe switch //todo: rewrite this switch when we'll switch to c# 9
            {
                true when messagesCount < howManyMessagesCount => SpamProbability.None,
                false when messagesCount <= howManyMessagesCount / 2 => SpamProbability.Low,
                false when messagesCount < howManyMessagesCount => SpamProbability.Medium,
                _ => SpamProbability.Sure
            });
Пример #12
0
        public SpamProbability GetSpamProbability(bool isUserSafe, params SmallMessage[] smallMessages)
        {
            // Arrange
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);

            var lastMessage    = smallMessages.Last();
            var contexts       = this._antiSpamTestsService.GetDefaultContexts(lastMessage.UserId, lastMessage.ServerId);
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(smallMessages);

            var needsConfiguration = typeof(T).GetConstructors().Any(x => x.GetParameters().Any(x => x.ParameterType == typeof(IConfigurationService)));
            var spamDetector       = needsConfiguration
                ? (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object, this._configurationService)
                : (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object);

            // Act
            return(spamDetector !.GetSpamProbability(serverMessages, contexts));
        }
Пример #13
0
 public OverallSpamDetectorStrategy(ServerMessagesCacheService serverMessagesCacheService, List <ISpamDetector> spamDetectors)
 {
     this._serverMessagesCacheService = serverMessagesCacheService;
     this._spamDetectors = spamDetectors;
 }