Skip to content

Commit

Permalink
Adding group created by id
Browse files Browse the repository at this point in the history
  • Loading branch information
abdosaeedelhassan committed Oct 22, 2024
1 parent d35c302 commit aa2772c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return new class extends Migration
Schema::create('filachat_groups', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->unsignedBigInteger('created_by');
$table->softDeletes();
$table->timestamps();
});
Expand Down
34 changes: 19 additions & 15 deletions src/Services/ChatListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ public function createConversation(array $data): void
try {
DB::transaction(function () use ($data) {

$senderableId = auth()->id();
$senderableType = auth()->user()::class;

if ($data['type'] === 'group') {

$group = FilaChatGroup::query()->create([
'created_by' => $senderableId,
'name' => $data['group_name'],
]);

Expand All @@ -194,7 +198,7 @@ public function createConversation(array $data): void
$receiverableId = $group->id;
$receiverableType = FilaChatGroup::class;
$conversation = FilaChatConversation::query()->create([
'senderable_id' => auth()->id(),
'senderable_id' => $senderableId,
'senderable_type' => auth()->user()::class,
'receiverable_id' => $receiverableId,
'receiverable_type' => $receiverableType,
Expand All @@ -212,31 +216,31 @@ public function createConversation(array $data): void
$receiverableId = (int)$matches[1];
}
$foundConversation = FilaChatConversation::query()
->where(function ($query) use ($receiverableId, $receiverableType) {
$query->where(function ($query) {
$query->where('senderable_id', auth()->id())
->where('senderable_type', auth()->user()::class);
->where(function ($query) use ($receiverableId, $receiverableType, $senderableId, $senderableType) {
$query->where(function ($query) use ($senderableId, $senderableType) {
$query->where('senderable_id', $senderableId)
->where('senderable_type', $senderableType);
})
->orWhere(function ($query) use ($receiverableId, $receiverableType) {
$query->where('senderable_id', $receiverableId)
->where('senderable_type', $receiverableType);
});
})
->where(function ($query) use ($receiverableId, $receiverableType) {
->where(function ($query) use ($receiverableId, $receiverableType, $senderableId, $senderableType) {
$query->where(function ($query) use ($receiverableId, $receiverableType) {
$query->where('receiverable_id', $receiverableId)
->where('receiverable_type', $receiverableType);
})
->orWhere(function ($query) {
$query->where('receiverable_id', auth()->id())
->where('receiverable_type', auth()->user()::class);
->orWhere(function ($query) use ($senderableId, $senderableType) {
$query->where('receiverable_id', $senderableId)
->where('receiverable_type', $senderableType);
});
})
->first();
if (!$foundConversation) {
$conversation = FilaChatConversation::query()->create([
'senderable_id' => auth()->id(),
'senderable_type' => auth()->user()::class,
'senderable_id' => $senderableId,
'senderable_type' => $senderableType,
'receiverable_id' => $receiverableId,
'receiverable_type' => $receiverableType,
]);
Expand All @@ -247,8 +251,8 @@ public function createConversation(array $data): void

$message = FilaChatMessage::query()->create([
'filachat_conversation_id' => $conversation->id,
'senderable_id' => auth()->id(),
'senderable_type' => auth()->user()::class,
'senderable_id' => $senderableId,
'senderable_type' => $senderableType,
'receiverable_id' => $receiverableId,
'receiverable_type' => $receiverableType,
'message' => $data['message'],
Expand All @@ -264,15 +268,15 @@ public function createConversation(array $data): void
$conversation->id,
$message->id,
$member->id,
auth()->id(),
$senderableId,
));
}
} else {
broadcast(new FilaChatMessageEvent(
$conversation->id,
$message->id,
$receiverableId,
auth()->id(),
$senderableId,
));
}

Expand Down

0 comments on commit aa2772c

Please sign in to comment.