Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RTippin committed Feb 23, 2022
2 parents 3edea55 + fe6349e commit 0e3784b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 18 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---
Expand All @@ -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.

---
Expand All @@ -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.

---
Expand All @@ -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.
Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

---
Expand All @@ -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.

---

Expand Down
4 changes: 3 additions & 1 deletion src/Commands/BaseFakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];

Expand All @@ -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')
Expand Down
89 changes: 72 additions & 17 deletions src/MessengerFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -72,6 +74,16 @@ class MessengerFaker
*/
private int $delay = 0;

/**
* @var bool
*/
private bool $useAdmins = false;

/**
* @var bool
*/
private bool $useBots = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -120,33 +132,43 @@ 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;
}

/**
* @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;
}
Expand Down Expand Up @@ -220,6 +242,14 @@ public function getThread(): ?Thread
return $this->thread;
}

/**
* @return DBCollection|null
*/
public function getParticipants(): ?DBCollection
{
return $this->participants;
}

/**
* @return Thread
*/
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
33 changes: 33 additions & 0 deletions tests/MessengerFakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 0e3784b

Please sign in to comment.