Initial project

This commit is contained in:
2026-05-01 00:41:02 +03:30
parent d324115341
commit cde71d5761
172 changed files with 22074 additions and 12 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Livewire\Chat;
use App\Models\Conversation;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\On;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Chat')]
class ChatPage extends Component
{
public ?int $selectedConversationId = null;
public bool $detailsPanelOpen = true;
#[On('conversation-selected')]
public function selectConversation(int $conversationId): void
{
$conversation = Conversation::query()
->forUser(Auth::user())
->findOrFail($conversationId);
Gate::authorize('view', $conversation);
$this->selectedConversationId = $conversation->id;
$this->detailsPanelOpen = true;
}
#[On('conversation-closed')]
public function clearConversation(): void
{
$this->selectedConversationId = null;
$this->detailsPanelOpen = false;
}
#[On('conversation-details-toggled')]
public function toggleDetailsPanel(): void
{
$this->detailsPanelOpen = ! $this->detailsPanelOpen;
}
public function render(): View
{
return view('livewire.chat.chat-page');
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Livewire\Chat;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
class ConversationDetailsPanel extends Component
{
#[Locked]
public int $conversationId;
public function mount(int $conversationId): void
{
$this->conversationId = $conversationId;
}
#[Computed]
public function conversation(): Conversation
{
$conversation = Conversation::query()
->forUser(Auth::user())
->with([
'participants' => fn ($query) => $query
->select(['id', 'conversation_id', 'user_id', 'role', 'joined_at'])
->with('user:id,name,email'),
])
->withCount('messages')
->findOrFail($this->conversationId);
Gate::authorize('view', $conversation);
return $conversation;
}
public function title(): string
{
if ($this->conversation->isGroup()) {
return $this->conversation->name ?? __('Untitled group');
}
return $this->conversation->participants
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
?->user?->name ?? __('Direct conversation');
}
public function initials(): string
{
return collect(explode(' ', $this->title()))
->filter()
->take(2)
->map(fn (string $word) => mb_substr($word, 0, 1))
->implode('');
}
public function render(): View
{
return view('livewire.chat.conversation-details-panel');
}
}

View File

@@ -0,0 +1,103 @@
<?php
namespace App\Livewire\Chat;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
class ConversationHeader extends Component
{
#[Locked]
public int $conversationId;
public function mount(int $conversationId): void
{
$this->conversationId = $conversationId;
}
#[Computed]
public function conversation(): Conversation
{
$conversation = Conversation::query()
->forUser(Auth::user())
->with([
'participants' => fn ($query) => $query
->select(['id', 'conversation_id', 'user_id', 'role'])
->with('user:id,name,email'),
])
->findOrFail($this->conversationId);
Gate::authorize('view', $conversation);
return $conversation;
}
public function title(): string
{
if ($this->conversation->isGroup()) {
return $this->conversation->name ?? __('Untitled group');
}
return $this->otherParticipant()?->name ?? __('Direct conversation');
}
public function subtitle(): string
{
if ($this->conversation->isGroup()) {
return trans_choice(':count member|:count members', $this->conversation->participants->count(), [
'count' => $this->conversation->participants->count(),
]);
}
return $this->isOnline() ? __('Online') : __('Recently active');
}
public function initials(): string
{
if (! $this->conversation->isGroup()) {
return $this->otherParticipant()?->initials() ?? 'DC';
}
return collect(explode(' ', $this->title()))
->filter()
->take(2)
->map(fn (string $word) => mb_substr($word, 0, 1))
->implode('');
}
public function isOnline(): bool
{
$participant = $this->otherParticipant();
return $participant instanceof User && $participant->id % 3 !== 0;
}
public function closeConversation(): void
{
$this->dispatch('conversation-closed');
}
public function toggleDetails(): void
{
$this->dispatch('conversation-details-toggled');
}
private function otherParticipant(): ?User
{
return $this->conversation->participants
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
?->user;
}
public function render(): View
{
return view('livewire.chat.conversation-header');
}
}

View File

@@ -0,0 +1,166 @@
<?php
namespace App\Livewire\Chat;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\User;
use Carbon\CarbonInterface;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Computed;
use Livewire\Attributes\On;
use Livewire\Component;
class ConversationList extends Component
{
public ?int $selectedConversationId = null;
public string $search = '';
#[On('message-created')]
public function refreshConversations(): void
{
unset($this->conversations);
}
public function selectConversation(int $conversationId): void
{
$this->dispatch('conversation-selected', conversationId: $conversationId);
}
/**
* @return Collection<int, Conversation>
*/
#[Computed]
public function conversations(): Collection
{
$user = Auth::user();
$search = trim($this->search);
return Conversation::query()
->select(['id', 'created_by_id', 'type', 'name', 'description', 'created_at', 'updated_at'])
->forUser($user)
->with([
'participants' => fn ($query) => $query
->select(['id', 'conversation_id', 'user_id', 'role', 'last_read_at'])
->with('user:id,name,email'),
'latestMessage' => fn ($query) => $query
->select(['messages.id', 'messages.conversation_id', 'messages.user_id', 'messages.body', 'messages.created_at']),
'latestMessage.sender:id,name,email',
])
->withMax('messages', 'created_at')
->withCount([
'messages as unread_messages_count' => fn (Builder $messages) => $messages
->where('user_id', '!=', $user->id)
->whereExists(fn ($participants) => $participants
->selectRaw('1')
->from('conversation_participants')
->whereColumn('conversation_participants.conversation_id', 'messages.conversation_id')
->where('conversation_participants.user_id', $user->id)
->where(fn ($readState) => $readState
->whereNull('conversation_participants.last_read_at')
->orWhereColumn('messages.created_at', '>', 'conversation_participants.last_read_at'))),
])
->when($search !== '', fn (Builder $query) => $query->where(function (Builder $query) use ($search, $user): void {
$query
->where('name', 'like', "%{$search}%")
->orWhereHas('participants.user', fn (Builder $users) => $users
->where('users.id', '!=', $user->id)
->where(fn (Builder $users) => $users
->where('name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")));
}))
->orderByDesc('messages_max_created_at')
->orderByDesc('updated_at')
->limit(40)
->get();
}
public function titleFor(Conversation $conversation): string
{
if ($conversation->isGroup()) {
return $conversation->name ?? __('Untitled group');
}
return $this->otherParticipant($conversation)?->name ?? __('Direct conversation');
}
public function initialsFor(Conversation $conversation): string
{
if ($conversation->isGroup()) {
return collect(explode(' ', $this->titleFor($conversation)))
->filter()
->take(2)
->map(fn (string $word) => mb_substr($word, 0, 1))
->implode('');
}
return $this->otherParticipant($conversation)?->initials() ?? 'DC';
}
public function previewFor(Conversation $conversation): string
{
if (! $conversation->latestMessage) {
return __('No messages yet');
}
$prefix = $conversation->latestMessage->user_id === Auth::id()
? __('You: ')
: ($conversation->isGroup() ? $conversation->latestMessage->sender?->name.': ' : '');
return str($prefix.$conversation->latestMessage->body)
->squish()
->limit(86)
->toString();
}
public function timeFor(?CarbonInterface $timestamp): string
{
if (! $timestamp) {
return '';
}
if ($timestamp->isToday()) {
return $timestamp->format('H:i');
}
if ($timestamp->isCurrentYear()) {
return $timestamp->format('M j');
}
return $timestamp->format('M j, Y');
}
public function participantSummaryFor(Conversation $conversation): string
{
if (! $conversation->isGroup()) {
return $this->isOnline($conversation) ? __('Online') : __('Recently active');
}
return trans_choice(':count member|:count members', $conversation->participants->count(), [
'count' => $conversation->participants->count(),
]);
}
public function isOnline(Conversation $conversation): bool
{
$participant = $this->otherParticipant($conversation);
return $participant instanceof User && $participant->id % 3 !== 0;
}
private function otherParticipant(Conversation $conversation): ?User
{
return $conversation->participants
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
?->user;
}
public function render(): View
{
return view('livewire.chat.conversation-list');
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace App\Livewire\Chat;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\Message;
use Carbon\CarbonInterface;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\On;
use Livewire\Component;
class ConversationView extends Component
{
public int $conversationId;
public int $messageLimit = 40;
public function mount(int $conversationId): void
{
$this->conversationId = $conversationId;
$this->authorizeConversation();
$this->markAsRead();
}
public function loadEarlier(): void
{
$this->messageLimit += 25;
}
#[On('message-created')]
public function refreshMessages(int $conversationId): void
{
if ($conversationId !== $this->conversationId) {
return;
}
unset($this->messages, $this->hasMoreMessages);
$this->markAsRead();
}
#[Computed]
public function conversation(): Conversation
{
$conversation = Conversation::query()
->forUser(Auth::user())
->with([
'participants' => fn ($query) => $query
->select(['id', 'conversation_id', 'user_id', 'role', 'last_read_at'])
->with('user:id,name,email'),
])
->findOrFail($this->conversationId);
Gate::authorize('view', $conversation);
return $conversation;
}
/**
* @return Collection<int, Message>
*/
#[Computed]
public function messages(): Collection
{
return Message::query()
->where('conversation_id', $this->conversationId)
->with('sender:id,name,email')
->latest()
->limit($this->messageLimit)
->get()
->reverse()
->values();
}
#[Computed]
public function hasMoreMessages(): bool
{
return Message::query()
->where('conversation_id', $this->conversationId)
->count() > $this->messageLimit;
}
public function dateLabel(CarbonInterface $timestamp): string
{
if ($timestamp->isToday()) {
return __('Today');
}
if ($timestamp->isYesterday()) {
return __('Yesterday');
}
return $timestamp->format('F j, Y');
}
private function authorizeConversation(): void
{
$conversation = Conversation::query()
->forUser(Auth::user())
->findOrFail($this->conversationId);
Gate::authorize('view', $conversation);
}
private function markAsRead(): void
{
ConversationParticipant::query()
->where('conversation_id', $this->conversationId)
->where('user_id', Auth::id())
->update(['last_read_at' => now()]);
}
public function render(): View
{
return view('livewire.chat.conversation-view');
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Livewire\Chat;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\Message;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Locked;
use Livewire\Component;
class MessageComposer extends Component
{
#[Locked]
public int $conversationId;
public string $body = '';
public function mount(int $conversationId): void
{
$this->conversationId = $conversationId;
$this->conversation();
}
public function sendMessage(): void
{
$this->body = trim($this->body);
$validated = $this->validate([
'body' => ['required', 'string', 'max:4000'],
], [
'body.required' => __('Write a message before sending.'),
]);
$conversation = $this->conversation();
Gate::authorize('sendMessage', $conversation);
$message = Message::query()->create([
'conversation_id' => $conversation->id,
'user_id' => Auth::id(),
'type' => Message::TypeText,
'body' => $validated['body'],
]);
$conversation->touch();
ConversationParticipant::query()
->where('conversation_id', $conversation->id)
->where('user_id', Auth::id())
->update(['last_read_at' => now()]);
$this->reset('body');
$this->resetValidation();
$this->dispatch('message-created', conversationId: $conversation->id, messageId: $message->id);
}
private function conversation(): Conversation
{
$conversation = Conversation::query()
->forUser(Auth::user())
->findOrFail($this->conversationId);
Gate::authorize('sendMessage', $conversation);
return $conversation;
}
public function render(): View
{
return view('livewire.chat.message-composer');
}
}