From b19d7b0fe5340ff4685e35665c9a367c87087534 Mon Sep 17 00:00:00 2001 From: ticktackk Date: Tue, 9 Mar 2021 23:42:58 +0530 Subject: [PATCH] closes #47 --- CHANGELOG.md | 1 + Cli/Command/Seed/SeedSpamCleanerLog.php | 18 ++++++ Seed/SpamCleanerLog.php | 83 +++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 Cli/Command/Seed/SeedSpamCleanerLog.php create mode 100644 Seed/SpamCleanerLog.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f1425a..ea2b842 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ CHANGELOG - **New:** Seed for rejected users (#42) - **New:** Seed for server error logs (#45) - **New:** Seed for spam trigger log (#46) +- **New:** Seed for spam cleaner log (#47) - **Fix:** Threads cannot be seeded (#36) ## 1.1.0 Alpha 3 (`1010013`) diff --git a/Cli/Command/Seed/SeedSpamCleanerLog.php b/Cli/Command/Seed/SeedSpamCleanerLog.php new file mode 100644 index 0000000..f60548b --- /dev/null +++ b/Cli/Command/Seed/SeedSpamCleanerLog.php @@ -0,0 +1,18 @@ +where('is_admin', false) + ->where('is_moderator', false) + ->where('is_staff', false) + ->where('is_banned', false); + } + + protected function findRandomModOrAdmin() : UserEntity + { + return $this->finderWithRandomOrder('XF:User') + ->whereOr(['is_admin', true], ['is_moderator', true]) + ->fetchOne(); + } + + /** + * @param array $params + * + * @return bool + * + * @throws \XF\PrintableException + */ + protected function seed(array $params = []): bool + { + $randomContent = $this->findRandomContent(); + if (!$randomContent) + { + return false; + } + + $faker = $this->faker(); + + /** @var SpamCleanerLogEntity $spamCleanerLog */ + $spamCleanerLog = $this->em()->create('XF:SpamCleanerLog'); + $spamCleanerLog->data = $faker->randomElements(); + $spamCleanerLog->application_date = $faker->dateTime->getTimestamp(); + $spamCleanerLog->restored_date = $faker->boolean ? $faker->dateTime->getTimestamp() : 0; + + $visitor = \XF::visitor(); + $spamCleanerLog->user_id = $visitor->user_id; + $spamCleanerLog->username = $visitor->username; + + $applyingUser = $this->findRandomModOrAdmin(); + $spamCleanerLog->applying_user_id = $applyingUser->user_id; + $spamCleanerLog->applying_username = $applyingUser->username; + + try + { + return $spamCleanerLog->save(); + } + catch (DuplicateKeyException $exception) + { + return false; + } + } + + protected function findRandomContent() :? Entity + { + $contentTypes = $this->app()->getContentTypeField('spam_handler_class'); + $contentType = \array_rand($contentTypes); + $identifier = $this->app()->getContentTypeFieldValue($contentType, 'entity'); + if ($identifier === null) + { + return null; + } + + return $this->finderWithRandomOrder($identifier)->fetchOne(); + } +} \ No newline at end of file