-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from medilies/from-direct-messages-to-conversat…
…ions From direct messages to conversations
- Loading branch information
Showing
45 changed files
with
738 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Models\Message; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class MessageEvent implements ShouldBroadcast | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct( | ||
protected array $message | ||
) { | ||
} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return \Illuminate\Broadcasting\Channel|array | ||
*/ | ||
public function broadcastOn() | ||
{ | ||
return array_map( | ||
fn ($user) => new PrivateChannel("chat.{$user['id']}"), | ||
$this->message['conversation']['other_users'] | ||
); | ||
} | ||
|
||
public function broadcastWith(): array | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
app/Http/Controllers/UserConversations/ConversationController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\UserConversations; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\UserConversationResource; | ||
use App\Models\Conversation; | ||
use App\Models\User; | ||
use App\Services\MessageService; | ||
use Illuminate\Http\Request; | ||
|
||
class ConversationController extends Controller | ||
{ | ||
public function list() | ||
{ | ||
/** @var User */ | ||
$current_user = auth()->user(); | ||
|
||
return UserConversationResource::collection( | ||
$current_user->conversations()->latest('updated_at') | ||
->get() | ||
->load('otherUsers') | ||
); | ||
} | ||
|
||
public function getConversationMessages(Conversation $conversation) | ||
{ | ||
return $conversation->messages->load('user'); | ||
} | ||
|
||
public function newConversationMessage(Request $request, Conversation $conversation, MessageService $message_service): array | ||
{ | ||
$conversation->load('users'); | ||
|
||
return $message_service->consume($request, $conversation); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/Http/Controllers/UserConversations/DirectConversationController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\UserConversations; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use App\Repositories\ConversationRepository; | ||
|
||
class DirectConversationController extends Controller | ||
{ | ||
public function getConversation( | ||
ConversationRepository $conversation_repository, | ||
User $user | ||
) { | ||
return $conversation_repository->findOrCreateDirectConversation(auth()->user(), $user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class NewMessageResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
"content" => $this->content, | ||
"user_id" => $this->user_id, | ||
"conversation_id" => $this->conversation_id, | ||
"created_at" => $this->created_at, | ||
"id" => $this->id, | ||
"user" => [ | ||
"id" => $this->user->id, | ||
"name" => $this->user->name, | ||
"email" => $this->user->email, | ||
], | ||
"conversation" => new UserConversationResource($this->whenLoaded('conversation')) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class OtherUsersResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
'email' => $this->email, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class UserConversationResource extends JsonResource | ||
{ | ||
public function toArray($request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'created_at' => $this->created_at, | ||
'updated_at' => $this->updated_at, | ||
'type' => $this->type, | ||
'name' => $this->name, | ||
'visibility' => $this->visibility, | ||
'other_users' => OtherUsersResource::collection($this->whenLoaded('otherUsers')), | ||
] + | ||
($this->type === 'direct' ? | ||
[] : | ||
['pivot' => [ | ||
'created_at' => $this->created_at, | ||
'updated_at' => $this->updated_at, | ||
]]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
use Illuminate\Database\Query\Builder; | ||
|
||
class Conversation extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = ['id']; | ||
|
||
// -------------------------------------------- | ||
// Relations | ||
// -------------------------------------------- | ||
|
||
public function users(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(User::class)->withTimestamps(); | ||
} | ||
|
||
public function otherUsers(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(User::class)->whereNot('user_id', auth()->id())->withTimestamps(); | ||
} | ||
|
||
public function messages() | ||
{ | ||
return $this->hasMany(Message::class); | ||
} | ||
|
||
// -------------------------------------------- | ||
// Scopes | ||
// -------------------------------------------- | ||
|
||
public function scopeDirect(Builder|EloquentBuilder $query): void | ||
{ | ||
$query->where('type', 'direct'); | ||
} | ||
} |
Oops, something went wrong.