Skip to content

Commit

Permalink
Refactor seeders.
Browse files Browse the repository at this point in the history
  • Loading branch information
RTippin committed Feb 6, 2022
1 parent fd5d2e1 commit 6d7eb49
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 33 deletions.
20 changes: 14 additions & 6 deletions database/seeders/BotSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use App\Bots\RecursionBot;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use RTippin\Messenger\Actions\Bots\InstallPackagedBot;
use RTippin\Messenger\Facades\Messenger;
use RTippin\Messenger\MessengerBots;
use RTippin\Messenger\Models\Bot;
use RTippin\Messenger\Models\BotAction;
use RTippin\Messenger\Models\Thread;
use RTippin\MessengerBots\Bots\ReactionBombBot;
use RTippin\MessengerBots\Bots\ReplyBot;
use RTippin\MessengerBots\Packages\GamesPackage;
use RTippin\MessengerBots\Packages\JokesterPackage;
Expand All @@ -29,14 +31,16 @@ class BotSeeder extends Seeder
*/
public function run(): void
{
$admin = User::where('email', '=', DatabaseSeeder::Admin['email'])->first();
$group = Thread::group()->first();
DB::transaction(function () {
$admin = User::where('email', '=', DatabaseSeeder::Admin['email'])->first();
$group = Thread::group()->first();

Messenger::setProvider($admin);
Messenger::setProvider($admin);

$this->installMessengerBot($group, $admin);
$this->installMessengerBot($group, $admin);

$this->installPackagedBots($group);
$this->installPackagedBots($group);
});
}

/**
Expand Down Expand Up @@ -108,12 +112,16 @@ private function actions(): array
'Why hello there!',
],
];
$bomb = [
'reactions' => ['🤖', '👋'],
];

return [
[ReplyBot::class, MessengerBots::MATCH_CONTAINS_CASELESS, 'help|git|package|error', $about, 300],
[ReplyBot::class, MessengerBots::MATCH_CONTAINS_CASELESS, 'hi|hello|test|testing|hallo', $hello, 15],
[ReplyBot::class, MessengerBots::MATCH_CONTAINS_CASELESS, 'hi|hello|test|testing|hallo', $hello, 45],
[RecursionBot::class, MessengerBots::MATCH_EXACT_CASELESS, '!recursion'],
[FakerBot::class, MessengerBots::MATCH_STARTS_WITH_CASELESS, '!faker'],
[ReactionBombBot::class, MessengerBots::MATCH_CONTAINS_CASELESS, 'hi|hello|test|testing|hallo', $bomb, 45],
];
}
}
24 changes: 15 additions & 9 deletions database/seeders/FriendsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use RTippin\Messenger\Models\Friend;
use Throwable;

class FriendsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*
* @throws Throwable
*/
public function run(): void
{
$users = User::all();
DB::transaction(function () {
$users = User::all();

// Make ALL users friends with one another
foreach ($users as $user) {
$others = $users->where('email', '!=', $user->email)->all();
// Make ALL users friends with one another
foreach ($users as $user) {
$others = $users->where('email', '!=', $user->email)->all();

foreach ($others as $other) {
if (Friend::forProvider($user)->forProvider($other, 'party')->doesntExist()) {
Friend::factory()->providers($user, $other)->create();
Friend::factory()->providers($other, $user)->create();
foreach ($others as $other) {
if (Friend::forProvider($user)->forProvider($other, 'party')->doesntExist()) {
Friend::factory()->providers($user, $other)->create();
Friend::factory()->providers($other, $user)->create();
}
}
}
}
});
}
}
22 changes: 14 additions & 8 deletions database/seeders/MessagesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use RTippin\Messenger\Models\Message;
use RTippin\Messenger\Models\Thread;
use Throwable;

class MessagesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*
* @throws Throwable
*/
public function run(): void
{
// Seed ALL threads with messages
Thread::with('participants.owner')->get()->each(function (Thread $thread) {
for ($x = 0; $x < rand(5, 20); $x++) {
Message::factory()
->for($thread)
->owner($thread->participants->random()->owner)
->create();
}
DB::transaction(function () {
// Seed ALL threads with messages
Thread::with('participants.owner')->get()->each(function (Thread $thread) {
for ($x = 0; $x < rand(5, 20); $x++) {
Message::factory()
->for($thread)
->owner($thread->participants->random()->owner)
->create();
}
});
});
}
}
12 changes: 9 additions & 3 deletions database/seeders/ThreadsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use RTippin\Messenger\Models\Participant;
use RTippin\Messenger\Models\Thread;
use Throwable;

class ThreadsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*
* @throws Throwable
*/
public function run(): void
{
$users = User::all();
DB::transaction(function () {
$users = User::all();

$this->makePrivates($users);
$this->makePrivates($users);

$this->makeGroupThread($users);
$this->makeGroupThread($users);
});
}

/**
Expand Down
20 changes: 13 additions & 7 deletions database/seeders/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Throwable;

class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*
* @throws Throwable
*/
public function run(): void
{
// Main admin account
User::factory()->admin()->create([
'name' => DatabaseSeeder::Admin['name'],
'email' => DatabaseSeeder::Admin['email'],
]);
DB::transaction(function () {
// Main admin account
User::factory()->admin()->create([
'name' => DatabaseSeeder::Admin['name'],
'email' => DatabaseSeeder::Admin['email'],
]);

// Random accounts
User::factory()->count(15)->create();
// Random accounts
User::factory()->count(15)->create();
});
}
}

0 comments on commit 6d7eb49

Please sign in to comment.