Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store data in another connection #308

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/musonza_chat.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

return [
/**
* this will set default database connection.
*/
'db_connection' => config('database.default'),
/*
* This will allow you to broadcast an event when a message is sent
* Example:
Expand Down
20 changes: 10 additions & 10 deletions database/migrations/create_chat_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ 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);
$table->text('data')->nullable();
$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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

class BaseModel extends Model
{
/* protected $connection = ConfigurationManager::$DB_CONNECTION; */
protected $tablePrefix = 'chat_';

public function getConnectionName()
{
return ConfigurationManager::defaultConnection();
}
}
5 changes: 5 additions & 0 deletions src/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []);
Expand Down
34 changes: 23 additions & 11 deletions src/Models/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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,
Expand All @@ -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()
Expand Down