Skip to content

Commit

Permalink
bug fix. Updated bot model to adhere to UUID config, since we store b…
Browse files Browse the repository at this point in the history
…ots as owners on tables mixed with end users providers.
  • Loading branch information
RTippin committed Jul 1, 2021
1 parent 664b991 commit 9c3ae1a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
6 changes: 5 additions & 1 deletion database/migrations/2019_10_01_000033_create_bots_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/MessengerBots.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/MessengerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
41 changes: 30 additions & 11 deletions src/Models/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
*/
Expand Down

0 comments on commit 9c3ae1a

Please sign in to comment.