Implement chat support functionality: update .env.example for Reverb integration, enhance ChatController with support conversation methods, and modify Conversation model for improved chat handling. Add middleware for chat operator checks and update broadcasting configuration for Reverb.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Models\Chat;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
@@ -14,15 +15,23 @@ class Conversation extends Model
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* Find or create a 1:1 conversation between two users.
|
||||
* Find a 1:1 conversation between two users.
|
||||
*/
|
||||
public static function findOrCreateOneToOne(int $userId, int $otherUserId): self
|
||||
public static function findOneToOne(int $userId, int $otherUserId): ?self
|
||||
{
|
||||
$conversation = static::query()
|
||||
return static::query()
|
||||
->whereHas('users', fn ($query) => $query->where('users.id', $userId))
|
||||
->whereHas('users', fn ($query) => $query->where('users.id', $otherUserId))
|
||||
->has('users', '=', 2)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find or create a 1:1 conversation between two users.
|
||||
*/
|
||||
public static function findOrCreateOneToOne(int $userId, int $otherUserId): self
|
||||
{
|
||||
$conversation = static::findOneToOne($userId, $otherUserId);
|
||||
|
||||
if ($conversation) {
|
||||
return $conversation;
|
||||
@@ -34,6 +43,33 @@ class Conversation extends Model
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inbox for a participant, newest activity first.
|
||||
*/
|
||||
public static function inboxFor(User $viewer): Collection
|
||||
{
|
||||
return $viewer->conversations()
|
||||
->with(['users', 'latestMessage.user'])
|
||||
->withCount([
|
||||
'messages as unread_count' => fn ($query) => $query
|
||||
->where('user_id', '!=', $viewer->id)
|
||||
->whereNull('seen_at'),
|
||||
])
|
||||
->withMax('messages', 'created_at')
|
||||
->orderByDesc('messages_max_created_at')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function loadForViewer(User $viewer): self
|
||||
{
|
||||
return $this->load(['users', 'latestMessage.user'])
|
||||
->loadCount([
|
||||
'messages as unread_count' => fn ($query) => $query
|
||||
->where('user_id', '!=', $viewer->id)
|
||||
->whereNull('seen_at'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
|
||||
Reference in New Issue
Block a user