diff --git a/config/musonza_chat.php b/config/musonza_chat.php index c1e660e..f17eb2c 100755 --- a/config/musonza_chat.php +++ b/config/musonza_chat.php @@ -1,6 +1,10 @@ config('database.default'), /* * This will allow you to broadcast an event when a message is sent * Example: diff --git a/database/migrations/create_chat_tables.php b/database/migrations/create_chat_tables.php index e10e954..6d57f76 100644 --- a/database/migrations/create_chat_tables.php +++ b/database/migrations/create_chat_tables.php @@ -14,7 +14,7 @@ class CreateChatTables extends Migration */ public function up() { - Schema::create(ConfigurationManager::CONVERSATIONS_TABLE, function (Blueprint $table) { + Schema::connection(ConfigurationManager::defaultConnection())->create(ConfigurationManager::CONVERSATIONS_TABLE, function (Blueprint $table) { $table->bigIncrements('id'); $table->boolean('private')->default(true); $table->boolean('direct_message')->default(false); @@ -22,10 +22,10 @@ public function up() $table->timestamps(); }); - Schema::create(ConfigurationManager::PARTICIPATION_TABLE, function (Blueprint $table) { + Schema::connection(ConfigurationManager::defaultConnection())->create(ConfigurationManager::PARTICIPATION_TABLE, function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('conversation_id')->unsigned(); - $table->bigInteger('messageable_id')->unsigned(); + $table->uuid('messageable_id'); $table->string('messageable_type'); $table->text('settings')->nullable(); $table->timestamps(); @@ -38,7 +38,7 @@ public function up() ->onDelete('cascade'); }); - Schema::create(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) { + Schema::connection(ConfigurationManager::defaultConnection())->create(ConfigurationManager::MESSAGES_TABLE, function (Blueprint $table) { $table->bigIncrements('id'); $table->text('body'); $table->bigInteger('conversation_id')->unsigned(); @@ -58,10 +58,10 @@ public function up() ->onDelete('cascade'); }); - Schema::create(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE, function (Blueprint $table) { + Schema::connection(ConfigurationManager::defaultConnection())->create(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE, function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('message_id')->unsigned(); - $table->bigInteger('messageable_id')->unsigned(); + $table->uuid('messageable_id'); $table->string('messageable_type'); $table->bigInteger('conversation_id')->unsigned(); $table->bigInteger('participation_id')->unsigned(); @@ -97,9 +97,9 @@ public function up() */ public function down() { - Schema::dropIfExists(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE); - Schema::dropIfExists(ConfigurationManager::MESSAGES_TABLE); - Schema::dropIfExists(ConfigurationManager::PARTICIPATION_TABLE); - Schema::dropIfExists(ConfigurationManager::CONVERSATIONS_TABLE); + Schema::connection(ConfigurationManager::defaultConnection())->dropIfExists(ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE); + Schema::connection(ConfigurationManager::defaultConnection())->dropIfExists(ConfigurationManager::MESSAGES_TABLE); + Schema::connection(ConfigurationManager::defaultConnection())->dropIfExists(ConfigurationManager::PARTICIPATION_TABLE); + Schema::connection(ConfigurationManager::defaultConnection())->dropIfExists(ConfigurationManager::CONVERSATIONS_TABLE); } } diff --git a/src/BaseModel.php b/src/BaseModel.php index f11ce4a..b653279 100644 --- a/src/BaseModel.php +++ b/src/BaseModel.php @@ -6,5 +6,11 @@ class BaseModel extends Model { + /* protected $connection = ConfigurationManager::$DB_CONNECTION; */ protected $tablePrefix = 'chat_'; + + public function getConnectionName() + { + return ConfigurationManager::defaultConnection(); + } } diff --git a/src/ConfigurationManager.php b/src/ConfigurationManager.php index 2f7ca74..8bb3904 100644 --- a/src/ConfigurationManager.php +++ b/src/ConfigurationManager.php @@ -9,6 +9,11 @@ class ConfigurationManager const MESSAGE_NOTIFICATIONS_TABLE = 'chat_message_notifications'; const PARTICIPATION_TABLE = 'chat_participation'; + public static function defaultConnection() + { + return config('musonza_chat.db_connection'); + } + public static function paginationDefaultParameters() { $pagination = config('musonza_chat.pagination', []); diff --git a/src/Models/MessageNotification.php b/src/Models/MessageNotification.php index ef4d931..9873f53 100644 --- a/src/Models/MessageNotification.php +++ b/src/Models/MessageNotification.php @@ -12,9 +12,28 @@ class MessageNotification extends BaseModel use SoftDeletes; protected $table = ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE; - protected $fillable = ['messageable_id', 'messageable_type', 'message_id', 'conversation_id']; + protected $fillable = [ + 'messageable_id', + 'messageable_type', + 'message_id', + 'participation_id', + 'conversation_id', + 'is_seen', + 'is_sender', + 'created_at', + ]; protected $dates = ['deleted_at']; + public function messageable() + { + return $this->morphTo(); + } + + public function message() + { + return $this->belongsTo(Message::class, 'message_id'); + } + /** * Creates a new notification. * @@ -37,12 +56,10 @@ public function unReadNotifications(Model $participant) public static function createCustomNotifications($message, $conversation) { - $notification = []; - $i = 0; foreach ($conversation->participants as $participation) { $is_sender = ($message->participation_id == $participation->id) ? 1 : 0; - $notification[] = [ + $notification = [ 'messageable_id' => $participation->messageable_id, 'messageable_type' => $participation->messageable_type, 'message_id' => $message->id, @@ -52,14 +69,9 @@ public static function createCustomNotifications($message, $conversation) 'is_sender' => $is_sender, 'created_at' => $message->created_at, ]; - $i++; - if ($i > 1000) { - self::insert($notification); - $i = 0; - $notification = []; - } + + MessageNotification::create($notification); } - self::insert($notification); } public function markAsRead()