feat: add Reverb realtime messaging
This commit is contained in:
@@ -23,14 +23,18 @@ class Conversation extends Model
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'type',
|
||||
'title',
|
||||
'created_by',
|
||||
'last_message_id',
|
||||
'last_message_at',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'last_message_at' => 'datetime',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
// ── Relationships ────────────────────────────────────────────────────────
|
||||
@@ -81,6 +85,7 @@ class Conversation extends Model
|
||||
{
|
||||
return self::query()
|
||||
->where('type', 'direct')
|
||||
->where('is_active', true)
|
||||
->whereHas('allParticipants', fn ($q) => $q->where('user_id', $userA)->whereNull('left_at'))
|
||||
->whereHas('allParticipants', fn ($q) => $q->where('user_id', $userB)->whereNull('left_at'))
|
||||
->whereRaw(
|
||||
@@ -108,6 +113,11 @@ class Conversation extends Model
|
||||
->whereNull('deleted_at')
|
||||
->where('sender_id', '!=', $userId);
|
||||
|
||||
if ($participant->last_read_message_id) {
|
||||
$query->where('id', '>', $participant->last_read_message_id);
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
if ($participant->last_read_at) {
|
||||
$query->where('created_at', '>', $participant->last_read_at);
|
||||
}
|
||||
|
||||
@@ -30,9 +30,11 @@ class ConversationParticipant extends Model
|
||||
'user_id',
|
||||
'role',
|
||||
'last_read_at',
|
||||
'last_read_message_id',
|
||||
'is_muted',
|
||||
'is_archived',
|
||||
'is_pinned',
|
||||
'is_hidden',
|
||||
'pinned_at',
|
||||
'joined_at',
|
||||
'left_at',
|
||||
@@ -40,9 +42,11 @@ class ConversationParticipant extends Model
|
||||
|
||||
protected $casts = [
|
||||
'last_read_at' => 'datetime',
|
||||
'last_read_message_id' => 'integer',
|
||||
'is_muted' => 'boolean',
|
||||
'is_archived' => 'boolean',
|
||||
'is_pinned' => 'boolean',
|
||||
'is_hidden' => 'boolean',
|
||||
'pinned_at' => 'datetime',
|
||||
'joined_at' => 'datetime',
|
||||
'left_at' => 'datetime',
|
||||
|
||||
@@ -7,8 +7,11 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Scout\Searchable;
|
||||
|
||||
use App\Models\MessageRead;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $conversation_id
|
||||
@@ -24,16 +27,31 @@ class Message extends Model
|
||||
use HasFactory, SoftDeletes, Searchable;
|
||||
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'client_temp_id',
|
||||
'conversation_id',
|
||||
'sender_id',
|
||||
'message_type',
|
||||
'body',
|
||||
'meta_json',
|
||||
'reply_to_message_id',
|
||||
'edited_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'meta_json' => 'array',
|
||||
'edited_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (self $message): void {
|
||||
if (! $message->uuid) {
|
||||
$message->uuid = (string) Str::uuid();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ── Relationships ────────────────────────────────────────────────────────
|
||||
|
||||
public function conversation(): BelongsTo
|
||||
@@ -56,9 +74,14 @@ class Message extends Model
|
||||
return $this->hasMany(MessageAttachment::class);
|
||||
}
|
||||
|
||||
public function setBodyAttribute(string $value): void
|
||||
public function reads(): HasMany
|
||||
{
|
||||
$sanitized = trim(strip_tags($value));
|
||||
return $this->hasMany(MessageRead::class);
|
||||
}
|
||||
|
||||
public function setBodyAttribute(?string $value): void
|
||||
{
|
||||
$sanitized = trim(strip_tags((string) $value));
|
||||
$this->attributes['body'] = $sanitized;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ class MessageAttachment extends Model
|
||||
|
||||
protected $fillable = [
|
||||
'message_id',
|
||||
'disk',
|
||||
'user_id',
|
||||
'type',
|
||||
'mime',
|
||||
|
||||
34
app/Models/MessageRead.php
Normal file
34
app/Models/MessageRead.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class MessageRead extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'message_id',
|
||||
'user_id',
|
||||
'read_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'read_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function message(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Message::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user