diff --git a/README.md b/README.md index 284503a..3107af0 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ ___ - `--count=X` flag to set how many messages are sent. - `--delay=X` flag to set delay in seconds between each message. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--silent` flag that will suppress all broadcast and event dispatches. --- @@ -88,6 +89,7 @@ ___ - `--messages=X` flag to set how many latest messages are chosen at random to be reacted to. - `--delay=X` flag to set delay in seconds between each reaction. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--silent` flag that will suppress all broadcast and event dispatches. --- @@ -99,6 +101,7 @@ ___ - `--type=X` flag to set the system message type. `88, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103` - `--delay=X` flag to set delay in seconds between each system message. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--silent` flag that will suppress all broadcast and event dispatches. --- @@ -109,6 +112,7 @@ ___ - `--count=X` flag to set how many images are sent. - `--delay=X` flag to set delay in seconds between each image. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--local` flag will choose a random image from the directory specified for images in the config file. - `--url=X` flag lets you directly specify an image URL to download and emit. - `--silent` flag that will suppress all broadcast and event dispatches. @@ -121,6 +125,7 @@ ___ - `--count=X` flag to set how many documents are sent. - `--delay=X` flag to set delay in seconds between each document. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--url=X` flag lets you directly specify a document URL to download and emit. - `--silent` flag that will suppress all broadcast and event dispatches. @@ -132,6 +137,7 @@ ___ - `--count=X` flag to set how many audio files are sent. - `--delay=X` flag to set delay in seconds between each audio file. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--url=X` flag lets you directly specify an audio URL to download and emit. - `--silent` flag that will suppress all broadcast and event dispatches. @@ -143,6 +149,7 @@ ___ - `--count=X` flag to set how many video files are sent. - `--delay=X` flag to set delay in seconds between each video file. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--url=X` flag lets you directly specify a video URL to download and emit. - `--silent` flag that will suppress all broadcast and event dispatches. @@ -154,6 +161,7 @@ ___ - `--count=X` flag to set how many messages are sent. - `--delay=X` flag to set delay in seconds between each message. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. - `--silent` flag that will suppress all broadcast and event dispatches. --- @@ -169,6 +177,7 @@ ___ - Make participants in the thread type. - `{thread?}` ID of the thread you want to seed. Random if not set. - `--admins` flag will only use admin participants if using a group thread. +- `--bots` flag will only use bots if using a group thread. --- diff --git a/src/Commands/BaseFakerCommand.php b/src/Commands/BaseFakerCommand.php index e39a52f..bae5e13 100644 --- a/src/Commands/BaseFakerCommand.php +++ b/src/Commands/BaseFakerCommand.php @@ -74,6 +74,7 @@ protected function getOptions(): array { $options = [ ['admins', null, InputOption::VALUE_NONE, 'Only use admins from the given thread, if any'], + ['bots', null, InputOption::VALUE_NONE, 'Only use bots from the given thread, if any'], ['silent', null, InputOption::VALUE_NONE, 'Silences all broadcast and events'], ]; @@ -97,7 +98,8 @@ protected function setupFaker(?int $loadMessageCount = null): bool $this->faker ->setThreadWithId( $this->argument('thread') ?: null, - $this->option('admins') + $this->option('admins'), + $this->option('bots') ) ->setDelay( $this->hasOption('delay') diff --git a/src/MessengerFaker.php b/src/MessengerFaker.php index d3fd8b3..2b363c9 100644 --- a/src/MessengerFaker.php +++ b/src/MessengerFaker.php @@ -16,6 +16,8 @@ use RTippin\Messenger\Exceptions\MessengerComposerException; use RTippin\Messenger\Exceptions\ReactionException; use RTippin\Messenger\Messenger; +use RTippin\Messenger\Models\Bot; +use RTippin\Messenger\Models\Message; use RTippin\Messenger\Models\Participant; use RTippin\Messenger\Models\Thread; use RTippin\Messenger\Support\MessengerComposer; @@ -48,9 +50,9 @@ class MessengerFaker private ?Thread $thread = null; /** - * @var DBCollection + * @var DBCollection|null */ - private DBCollection $participants; + private ?DBCollection $participants = null; /** * @var DBCollection|null @@ -72,6 +74,16 @@ class MessengerFaker */ private int $delay = 0; + /** + * @var bool + */ + private bool $useAdmins = false; + + /** + * @var bool + */ + private bool $useBots = false; + /** * @var bool */ @@ -120,17 +132,22 @@ public function getFakerGenerator(): Generator /** * @param string|null $threadId * @param bool $useAdmins + * @param bool $useBots * @return $this * * @throws ModelNotFoundException */ - public function setThreadWithId(?string $threadId = null, bool $useAdmins = false): self + public function setThreadWithId(?string $threadId = null, + bool $useAdmins = false, + bool $useBots = false): self { + $this->useAdmins = $useAdmins; + $this->useBots = $useBots; $this->thread = is_null($threadId) ? Thread::inRandomOrder()->firstOrFail() : Thread::findOrFail($threadId); - $this->setParticipants($useAdmins); + $this->setParticipants(); return $this; } @@ -138,15 +155,20 @@ public function setThreadWithId(?string $threadId = null, bool $useAdmins = fals /** * @param Thread $thread * @param bool $useAdmins + * @param bool $useBots * @return $this * * @throws ModelNotFoundException */ - public function setThread(Thread $thread, bool $useAdmins = false): self + public function setThread(Thread $thread, + bool $useAdmins = false, + bool $useBots = false): self { + $this->useAdmins = $useAdmins; + $this->useBots = $useBots; $this->thread = $thread; - $this->setParticipants($useAdmins); + $this->setParticipants(); return $this; } @@ -220,6 +242,14 @@ public function getThread(): ?Thread return $this->thread; } + /** + * @return DBCollection|null + */ + public function getParticipants(): ?DBCollection + { + return $this->participants; + } + /** * @return Thread */ @@ -253,13 +283,14 @@ public function knock(): self * Mark the given providers as read and send broadcast. * * @param Participant|null $participant + * @param Message|null $message * @return $this * * @throws Throwable */ - public function read(Participant $participant = null): self + public function read(Participant $participant = null, ?Message $message = null): self { - $message = $this->thread->messages()->latest()->first(); + $message = $message ?? $this->thread->messages()->latest()->first(); if (! is_null($message)) { if (! is_null($participant)) { @@ -517,9 +548,13 @@ private function endMessage(bool $isFinal): void return; } - $this->usedParticipants - ->unique('id') - ->each(fn (Participant $participant) => $this->read($participant)); + if (! $this->useBots) { + $message = $this->thread->messages()->latest()->first(); + + $this->usedParticipants + ->unique('id') + ->each(fn (Participant $participant) => $this->read($participant, $message)); + } $this->usedParticipants = new Collection; } @@ -549,16 +584,36 @@ private function composer(): MessengerComposer } /** - * @param bool $useAdmins + * @return void */ - private function setParticipants(bool $useAdmins): void + private function setParticipants(): void { - if ($useAdmins && $this->thread->isGroup()) { - $this->participants = $this->thread->participants()->admins()->with('owner')->get(); + if ($this->thread->isGroup()) { + if ($this->useAdmins) { + $this->participants = $this->thread + ->participants() + ->admins() + ->with('owner') + ->get(); + + return; + } - return; + if ($this->useBots) { + $this->participants = $this->thread + ->bots() + ->get() + ->map( + fn (Bot $bot) => (new Participant)->setRelation('owner', $bot) + ); + + return; + } } - $this->participants = $this->thread->participants()->with('owner')->get(); + $this->participants = $this->thread + ->participants() + ->with('owner') + ->get(); } } diff --git a/tests/MessengerFakerTest.php b/tests/MessengerFakerTest.php index 90a6aa2..518712a 100644 --- a/tests/MessengerFakerTest.php +++ b/tests/MessengerFakerTest.php @@ -21,6 +21,7 @@ use RTippin\Messenger\Events\NewMessageEvent; use RTippin\Messenger\Events\ReactionAddedEvent; use RTippin\Messenger\Facades\Messenger; +use RTippin\Messenger\Models\Bot; use RTippin\Messenger\Models\Message; use RTippin\Messenger\Models\Participant; use RTippin\Messenger\Models\Thread; @@ -92,6 +93,38 @@ public function it_sets_random_thread_when_id_null() $this->assertContains($faker->getThread()->id, [$thread1->id, $thread2->id]); } + /** @test */ + public function it_sets_thread_participants() + { + $faker = app(MessengerFaker::class); + $thread = $this->createGroupThread($this->tippin, $this->doe); + $faker->setThreadWithId($thread->id); + + $this->assertSame(2, $faker->getParticipants()->count()); + } + + /** @test */ + public function it_sets_thread_admin_participants() + { + $faker = app(MessengerFaker::class); + $thread = $this->createGroupThread($this->tippin, $this->doe); + $faker->setThreadWithId($thread->id, true); + + $this->assertSame(1, $faker->getParticipants()->count()); + } + + /** @test */ + public function it_sets_thread_bots_as_participants() + { + $faker = app(MessengerFaker::class); + $thread = $this->createGroupThread($this->tippin, $this->doe); + $bot = Bot::factory()->for($thread)->owner($this->tippin)->create(); + $faker->setThreadWithId($thread->id, false, true); + + $this->assertSame(1, $faker->getParticipants()->count()); + $this->assertSame($bot->id, $faker->getParticipants()->first()->owner->id); + } + /** @test */ public function it_sets_thread_using_thread() {