From 9c3ae1a469e61e6a028c5757dea05be08880c90c Mon Sep 17 00:00:00 2001 From: Tippin Date: Wed, 30 Jun 2021 23:02:21 -0400 Subject: [PATCH] bug fix. Updated bot model to adhere to UUID config, since we store bots as owners on tables mixed with end users providers. --- .../2019_10_01_000033_create_bots_table.php | 6 ++- ..._10_01_000034_create_bot_actions_table.php | 6 ++- src/MessengerBots.php | 7 ++++ src/MessengerServiceProvider.php | 4 ++ src/Models/Bot.php | 41 ++++++++++++++----- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/database/migrations/2019_10_01_000033_create_bots_table.php b/database/migrations/2019_10_01_000033_create_bots_table.php index 5cae0142..734685f7 100644 --- a/database/migrations/2019_10_01_000033_create_bots_table.php +++ b/database/migrations/2019_10_01_000033_create_bots_table.php @@ -15,7 +15,11 @@ class CreateBotsTable extends Migration public function up() { Schema::create('bots', function (Blueprint $table) { - $table->uuid('id')->primary(); + if (config('messenger.provider_uuids')) { + $table->uuid('id')->primary(); + } else { + $table->id(); + } $table->uuid('thread_id'); Helpers::SchemaMorphType('owner', $table); $table->string('name'); diff --git a/database/migrations/2019_10_01_000034_create_bot_actions_table.php b/database/migrations/2019_10_01_000034_create_bot_actions_table.php index f109766f..2071c356 100644 --- a/database/migrations/2019_10_01_000034_create_bot_actions_table.php +++ b/database/migrations/2019_10_01_000034_create_bot_actions_table.php @@ -16,7 +16,11 @@ public function up() { Schema::create('bot_actions', function (Blueprint $table) { $table->uuid('id')->primary(); - $table->uuid('bot_id'); + if (config('messenger.provider_uuids')) { + $table->uuid('bot_id'); + } else { + $table->unsignedBigInteger('bot_id'); + } Helpers::SchemaMorphType('owner', $table); $table->string('handler'); $table->string('triggers')->nullable()->index(); diff --git a/src/MessengerBots.php b/src/MessengerBots.php index c551f808..9f1306b1 100644 --- a/src/MessengerBots.php +++ b/src/MessengerBots.php @@ -30,6 +30,13 @@ final class MessengerBots 'starts:with:caseless' => 'Same as "starts with", but is case insensitive.', ]; + /** + * This determines if we use UUID or BigInt on the bot model. + * + * @var bool + */ + public static bool $useUuid = false; + /** * @var Collection */ diff --git a/src/MessengerServiceProvider.php b/src/MessengerServiceProvider.php index 0c3218eb..72d1834f 100644 --- a/src/MessengerServiceProvider.php +++ b/src/MessengerServiceProvider.php @@ -80,6 +80,10 @@ public function boot(): void 'bots' => Bot::class, ]); + if (config('messenger.provider_uuids')) { + MessengerBots::$useUuid = true; + } + Collection::macro('forProvider', function (MessengerProvider $provider, string $morph = 'owner'): Collection { /** @var Collection $this */ return $this->where("{$morph}_id", '=', $provider->getKey()) diff --git a/src/Models/Bot.php b/src/Models/Bot.php index dbd24f5c..e7ab89ac 100644 --- a/src/Models/Bot.php +++ b/src/Models/Bot.php @@ -11,16 +11,17 @@ use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Str; +use RTippin\Messenger\MessengerBots as Bots; use RTippin\Messenger\Contracts\MessengerProvider; use RTippin\Messenger\Database\Factories\BotFactory; use RTippin\Messenger\Facades\Messenger; use RTippin\Messenger\Facades\MessengerBots; use RTippin\Messenger\Support\Helpers; use RTippin\Messenger\Traits\ScopesProvider; -use RTippin\Messenger\Traits\Uuids; /** - * @property string $id + * @property string|int $id * @property string $thread_id * @property string|int $owner_id * @property string $owner_type @@ -40,26 +41,30 @@ class Bot extends Model implements MessengerProvider { use HasFactory; - use Uuids; use SoftDeletes; use ScopesProvider; /** - * The database table used by the model. + * Create a new Eloquent model instance. * - * @var string + * @param array $attributes + * @return void */ - protected $table = 'bots'; + public function __construct(array $attributes = []) + { + $this->keyType = Bots::$useUuid ? 'string' : 'int'; - /** - * @var bool - */ - public $incrementing = false; + $this->incrementing = ! Bots::$useUuid; + + parent::__construct($attributes); + } /** + * The database table used by the model. + * * @var string */ - public $keyType = 'string'; + protected $table = 'bots'; /** * The attributes that can be set with Mass Assignment. @@ -76,6 +81,20 @@ class Bot extends Model implements MessengerProvider 'hide_actions' => 'boolean', ]; + /** + * On creating, set primary key as UUID if enabled. + */ + public static function boot() + { + parent::boot(); + + static::creating(function (Model $model) { + if (Bots::$useUuid) { + $model->id = Str::orderedUuid()->toString(); + } + }); + } + /** * @return MorphTo|MessengerProvider */