Enhance chat functionality: add support for vendor-specific chat channels, implement methods for managing conversations with admin and store owners, and improve conversation payload structure. Update middleware and policies for better access control, and refine chat UI to indicate view-only status for store owner replies.
This commit is contained in:
1 parent
c55694ec8c
commit
5c17c35741
23 files changed
+729
-59
No files matched your search
@@ -2,18 +2,39 @@
|
||||
|
||||
namespace App\Models\Chat;
|
||||
|
||||
use App\Models\Ecommerce\Channel\Channel;
|
||||
use App\Models\User;
|
||||
use App\Support\ChatSupport;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Conversation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const THREAD_ADMIN = 'admin';
|
||||
|
||||
/**
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'customer_id',
|
||||
'channel_id',
|
||||
'thread_key',
|
||||
];
|
||||
|
||||
public static function threadKeyForChannel(int $channelId): string
|
||||
{
|
||||
return 'channel:'.$channelId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a 1:1 conversation between two users.
|
||||
*/
|
||||
@@ -43,13 +64,65 @@ public static function findOrCreateOneToOne(int $userId, int $otherUserId): self
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public static function findSupportFor(User $customer): ?self
|
||||
{
|
||||
return static::query()
|
||||
->where('customer_id', $customer->id)
|
||||
->where('thread_key', self::THREAD_ADMIN)
|
||||
->first();
|
||||
}
|
||||
|
||||
public static function findOrCreateSupport(User $customer): self
|
||||
{
|
||||
abort_if(ChatSupport::isSupportUser($customer), 403);
|
||||
|
||||
$support = ChatSupport::user();
|
||||
|
||||
return static::locateOrCreate($customer, $support, self::THREAD_ADMIN, null);
|
||||
}
|
||||
|
||||
public static function findOrCreateForChannel(User $customer, Channel $channel): self
|
||||
{
|
||||
abort_if(ChatSupport::isSupportUser($customer), 403);
|
||||
|
||||
$existing = static::query()
|
||||
->where('customer_id', $customer->id)
|
||||
->where('thread_key', static::threadKeyForChannel($channel->id))
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$channel->loadMissing('user');
|
||||
$owner = $channel->user;
|
||||
|
||||
abort_unless(
|
||||
$channel->is_visible
|
||||
&& $channel->chatting
|
||||
&& $channel->channelables_type === User::class
|
||||
&& $owner,
|
||||
422,
|
||||
'This store is not available for chat.'
|
||||
);
|
||||
abort_if((int) $owner->id === (int) $customer->id, 403, 'You cannot chat with your own store.');
|
||||
abort_if(ChatSupport::isSupportUser($owner), 422, 'This store is not available for chat.');
|
||||
|
||||
return static::locateOrCreate(
|
||||
$customer,
|
||||
$owner,
|
||||
static::threadKeyForChannel($channel->id),
|
||||
$channel
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inbox for a participant, newest activity first.
|
||||
*/
|
||||
public static function inboxFor(User $viewer): Collection
|
||||
{
|
||||
return $viewer->conversations()
|
||||
->with(['users', 'latestMessage.user'])
|
||||
->with(['users', 'latestMessage.user', 'channel'])
|
||||
->withCount([
|
||||
'messages as unread_count' => fn ($query) => $query
|
||||
->where('user_id', '!=', $viewer->id)
|
||||
@@ -60,9 +133,28 @@ public static function inboxFor(User $viewer): Collection
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function inboxForStaff(): Collection
|
||||
{
|
||||
return static::panelInboxQuery()->get();
|
||||
}
|
||||
|
||||
public static function inboxForVendor(User $vendor): Collection
|
||||
{
|
||||
$channelIds = $vendor->channels()->pluck('id');
|
||||
|
||||
if ($channelIds->isEmpty()) {
|
||||
return new Collection;
|
||||
}
|
||||
|
||||
return static::panelInboxQuery()
|
||||
->whereIn('channel_id', $channelIds)
|
||||
->whereHas('users', fn ($query) => $query->where('users.id', $vendor->id))
|
||||
->get();
|
||||
}
|
||||
|
||||
public function loadForViewer(User $viewer): self
|
||||
{
|
||||
return $this->load(['users', 'latestMessage.user'])
|
||||
return $this->load(['users', 'latestMessage.user', 'channel'])
|
||||
->loadCount([
|
||||
'messages as unread_count' => fn ($query) => $query
|
||||
->where('user_id', '!=', $viewer->id)
|
||||
@@ -70,6 +162,21 @@ public function loadForViewer(User $viewer): self
|
||||
]);
|
||||
}
|
||||
|
||||
public function isSupportThread(): bool
|
||||
{
|
||||
return $this->thread_key === self::THREAD_ADMIN;
|
||||
}
|
||||
|
||||
public function customer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'customer_id');
|
||||
}
|
||||
|
||||
public function channel(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Channel::class);
|
||||
}
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
@@ -89,4 +196,69 @@ public function hasParticipant(User $user): bool
|
||||
{
|
||||
return $this->users()->where('users.id', $user->id)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Builder<static>
|
||||
*/
|
||||
private static function panelInboxQuery()
|
||||
{
|
||||
return static::query()
|
||||
->with(['customer', 'channel', 'users', 'latestMessage.user'])
|
||||
->withCount([
|
||||
'messages as unread_count' => fn ($query) => $query
|
||||
->whereColumn('messages.user_id', 'conversations.customer_id')
|
||||
->whereNull('seen_at'),
|
||||
])
|
||||
->withMax('messages', 'created_at')
|
||||
->orderByDesc('messages_max_created_at');
|
||||
}
|
||||
|
||||
private static function locateOrCreate(User $customer, User $counterpart, string $threadKey, ?Channel $channel): self
|
||||
{
|
||||
$existing = static::query()
|
||||
->where('customer_id', $customer->id)
|
||||
->where('thread_key', $threadKey)
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
try {
|
||||
return DB::transaction(function () use ($customer, $counterpart, $threadKey, $channel) {
|
||||
$conversation = static::query()->create([
|
||||
'customer_id' => $customer->id,
|
||||
'channel_id' => $channel?->id,
|
||||
'thread_key' => $threadKey,
|
||||
]);
|
||||
|
||||
$conversation->users()->attach([$customer->id, $counterpart->id]);
|
||||
|
||||
return $conversation;
|
||||
});
|
||||
} catch (QueryException $exception) {
|
||||
if (! static::isUniqueViolation($exception)) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$conversation = static::query()
|
||||
->where('customer_id', $customer->id)
|
||||
->where('thread_key', $threadKey)
|
||||
->first();
|
||||
|
||||
if ($conversation) {
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
private static function isUniqueViolation(QueryException $exception): bool
|
||||
{
|
||||
$sqlState = (string) $exception->errorInfo[0];
|
||||
|
||||
return in_array($sqlState, ['23000', '23505'], true)
|
||||
|| str_contains(strtolower($exception->getMessage()), 'unique');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user