Skip to content

Commit

Permalink
Merge pull request #20 from ahmadrosid/share-conversation
Browse files Browse the repository at this point in the history
Shared chat
  • Loading branch information
ahmadrosid authored Nov 28, 2024
2 parents 382fb5b + 9551e35 commit 6da06cb
Show file tree
Hide file tree
Showing 19 changed files with 545 additions and 4 deletions.
35 changes: 35 additions & 0 deletions app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Controllers;

use App\Models\Document;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class DocumentController extends Controller
{
public function copy(Document $document)
{
// Check if document is public
if (!$document->is_public) {
abort(403);
}

// Copy the file to a new location
$newPath = 'documents/' . auth()->id() . '/' . Str::random(40) . '.pdf';
Storage::copy($document->file_path, $newPath);

// Create a new document record
$newDocument = Document::create([
'file_path' => $newPath,
'file_name' => $document->file_name,
'file_size' => $document->file_size,
'user_id' => auth()->id(),
'is_public' => false,
]);

return redirect()->route('documents.show', $newDocument)
->with('success', 'Document copied successfully to your library.');
}
}
31 changes: 31 additions & 0 deletions app/Livewire/ChatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ChatInterface extends Component
public $threadId;
public $openaiThreadId;
public $assistant_id;
public $showShareModal = false;

public function mount()
{
Expand Down Expand Up @@ -260,6 +261,36 @@ public function indexing_progress()
return min(95, round(($secondsPassed / 30) * 100)); // Max 95% until confirmed indexed
}

public function toggleShare()
{
if (!$this->document->sharing_token) {
$this->document->update([
'sharing_token' => Str::random(32),
'is_public' => true,
]);
} else {
$this->document->update([
'is_public' => !$this->document->is_public
]);
}

$this->document->refresh();
}

public function copyShareLink()
{
return route('documents.public', $this->document->sharing_token);
}

#[Computed]
public function shareUrl()
{
if ($this->document->is_public && $this->document->sharing_token) {
return route('documents.public', $this->document->sharing_token);
}
return null;
}

public function render()
{
if (!$this->is_indexed) {
Expand Down
33 changes: 33 additions & 0 deletions app/Livewire/PublicChatInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Livewire;

use App\Models\Document;
use App\Models\Thread;
use Livewire\Component;

class PublicChatInterface extends Component
{
public Document $document;
public $threads;
public $currentThread;

public function mount(Document $document)
{
$this->document = $document;
$this->threads = $document->threads;
$this->currentThread = $this->threads->first();
}

public function render()
{
return view('livewire.public-chat-interface', [
'messages' => $this->currentThread?->messages ?? collect(),
]);
}

public function selectThread(Thread $thread)
{
$this->currentThread = $thread;
}
}
42 changes: 42 additions & 0 deletions app/Livewire/SharedDocuments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Livewire;

use App\Models\Document;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Str;

class SharedDocuments extends Component
{
use WithPagination;

public function toggleShare($documentId)
{
$document = Document::where('user_id', auth()->id())
->findOrFail($documentId);

if (!$document->sharing_token) {
$document->update([
'sharing_token' => Str::random(32),
'is_public' => true,
]);
} else {
$document->update([
'is_public' => !$document->is_public
]);
}
}

public function render()
{
$sharedDocuments = Document::where('user_id', auth()->id())
->whereNotNull('sharing_token')
->latest()
->paginate(10);

return view('livewire.shared-documents', [
'documents' => $sharedDocuments
])->layout('layouts.app');
}
}
6 changes: 6 additions & 0 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class Document extends Model
'file_name',
'file_size',
'user_id',
'sharing_token',
'is_public',
];

protected $casts = [
'is_public' => 'boolean',
];

protected static function boot()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->string('sharing_token')->nullable()->unique();
$table->boolean('is_public')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->dropColumn(['sharing_token', 'is_public']);
});
}
};
13 changes: 12 additions & 1 deletion resources/lang/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,16 @@
"Register with Google": "Daftar dengan Google",
"Or log in with your email and password":"Atau masuk dengan email dan password Anda",
"Welcome to Pdfpintar": "Selamat datang di Pdfpintar",
"Register now it's free.": "Daftar sekarang gratis."
"Register now it's free.": "Daftar sekarang gratis.",
"Shared chats":"Chat yang di bagikan",
"Shared Documents": "Dokumen yang dibagikan",
"Shared": "Dibagikan",
"Want to try PDFPintar for yourself?": "Ingin mencoba PDFPintar untuk diri sendiri?",
"Sign up now to chat with your own PDF documents using AI.": "Daftar sekarang untuk chat dengan dokumen PDF Anda menggunakan AI.",
"Sign up for free": "Daftar gratis",
"Log in": "Masuk",
"No credit card required • Start chatting in minutes": "Tidak perlu kartu kredit • Mulai chat secara gratis sekarang",
"This is your document":"Ini adalah dokumen Anda",
"Continue to your document to start chatting.":"Lanjutkan ke dokumen Anda untuk memulai chat.",
"Continue chat with Document":"Lanjutkan chat dengan Dokumen"
}
5 changes: 5 additions & 0 deletions resources/views/components/icon-check.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props(['class' => 'w-5 h-5'])

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes->merge(['class' => $class]) }}>
<path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" />
</svg>
6 changes: 6 additions & 0 deletions resources/views/components/icon-eye.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@props(['class' => 'w-5 h-5'])

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes->merge(['class' => $class]) }}>
<path stroke-linecap="round" stroke-linejoin="round" d="M2.036 12.322a1.012 1.012 0 0 1 0-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178Z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" />
</svg>
5 changes: 5 additions & 0 deletions resources/views/components/icon-lock.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props(['class' => 'w-5 h-5'])

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes->merge(['class' => $class]) }}>
<path stroke-linecap="round" stroke-linejoin="round" d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H6.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z" />
</svg>
5 changes: 5 additions & 0 deletions resources/views/components/icon-share.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props(['class' => 'w-5 h-5'])

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes->merge(['class' => $class]) }}>
<path stroke-linecap="round" stroke-linejoin="round" d="M7.217 10.907a2.25 2.25 0 1 0 0 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186 9.566-5.314m-9.566 7.5 9.566 5.314m0 0a2.25 2.25 0 1 0 3.935 2.186 2.25 2.25 0 0 0-3.935-2.186Zm0-12.814a2.25 2.25 0 1 0 3.933-2.185 2.25 2.25 0 0 0-3.933 2.185Z" />
</svg>
5 changes: 5 additions & 0 deletions resources/views/components/icon-unlock.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props(['class' => 'w-5 h-5'])

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes->merge(['class' => $class]) }}>
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 10.5V6.75a4.5 4.5 0 119 0v3.75M3.75 21.75h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H3.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z" />
</svg>
23 changes: 23 additions & 0 deletions resources/views/documents/public.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<x-full-screen-layout>
<x-slot name="title">
{{ $document->file_name }}
</x-slot>
<div>
<div class="h-[6vh] p-2 py-3 items-center flex justify-between pt-4">
<div class="text-neutral-900 font-semibold dark:text-neutral-200">
{{ $document->file_name }}
</div>
<div class="text-sm text-neutral-500">
{{ __('Shared Document') }}
</div>
</div>
<div class="flex flex-col lg:grid lg:grid-cols-2 gap-2 h-auto sm:h-[94vh] p-2">
<div class="order-2 lg:order-1 flex-grow lg:flex-grow-0 flex flex-col bg-white h-full overflow-hidden dark:bg-neutral-700">
<div id="pdf-viewer" data-url="{{ $pdfUrl }}" class="w-full h-full"></div>
</div>
<div class="order-1 lg:order-2 flex-grow lg:flex-grow-0 h-[900px] sm:h-auto">
<livewire:public-chat-interface :document="$document" />
</div>
</div>
</div>
</x-full-screen-layout>
28 changes: 25 additions & 3 deletions resources/views/documents/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@
<span class="left-arrow">&#x2190;</span> {{ $document->file_name }}
</a>
<div class="flex items-center gap-4">
<a href="#" class="text-neutral-800 hover:text-neutral-500 dark:text-neutral-300 dark:hover:text-neutral-700">
{{__('History')}}
</a>
<form action="{{ route('documents.share', $document) }}" method="POST" class="inline">
@csrf
<button type="submit" class="text-neutral-800 hover:text-neutral-500 dark:text-neutral-300 dark:hover:text-neutral-700">
@if($document->is_public)
{{ __('Make Private') }}
@else
{{ __('Share') }}
@endif
</button>
</form>
@if($document->is_public)
<button onclick="copyShareLink()" class="text-neutral-800 hover:text-neutral-500 dark:text-neutral-300 dark:hover:text-neutral-700">
{{ __('Copy Link') }}
</button>
@endif
<a href="{{ route('documents.index') }}" class="text-neutral-800 hover:text-neutral-500 dark:text-neutral-300 dark:hover:text-neutral-700 pr-3">
{{ __('Back') }}
</a>
Expand All @@ -25,4 +37,14 @@
</div>
</div>
</div>
@if($document->is_public)
<script>
function copyShareLink() {
const shareUrl = '{{ route('documents.public', $document->sharing_token) }}';
navigator.clipboard.writeText(shareUrl).then(() => {
alert('Share link copied to clipboard!');
});
}
</script>
@endif
</x-full-screen-layout>
34 changes: 34 additions & 0 deletions resources/views/livewire/chat.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
<div class="flex flex-col border border-neutral-200 bg-white dark:bg-neutral-800 dark:border-neutral-700 h-full max-h-[92vh]">
<div class="flex justify-between items-center p-2 border-b border-neutral-200 dark:border-neutral-700">
<div class="flex items-center gap-2">
<button wire:click="newChat" class="text-sm text-neutral-600 hover:text-neutral-900 dark:text-neutral-400 dark:hover:text-neutral-200">
{{ __('New Chat') }}
</button>
<button wire:click="clearMessages" class="text-sm text-neutral-600 hover:text-neutral-900 dark:text-neutral-400 dark:hover:text-neutral-200">
{{ __('Clear Chat') }}
</button>
</div>
<div class="flex items-center gap-2">
<button
wire:click="toggleShare"
class="text-sm {{ $this->shareUrl ? 'text-green-600 dark:text-green-400' : 'text-neutral-600 dark:text-neutral-400' }} hover:text-neutral-900 dark:hover:text-neutral-200"
>
@if($this->shareUrl)
{{ $document->is_public ? __('Disable Sharing') : __('Enable Sharing') }}
@else
{{ __('Share Chat') }}
@endif
</button>
@if($this->shareUrl && $document->is_public)
<button
x-data
x-on:click="
navigator.clipboard.writeText('{{ $this->shareUrl }}');
$dispatch('notify', {message: '{{ __('Share link copied!') }}'})
"
class="text-sm text-neutral-600 hover:text-neutral-900 dark:text-neutral-400 dark:hover:text-neutral-200"
>
{{ __('Copy Link') }}
</button>
@endif
</div>
</div>
<div class="flex-1 overflow-y-auto">
@if(count($messages) == 0)
<div class="flex items-center justify-center w-full h-full text-xl dark:text-neutral-300">
Expand Down
6 changes: 6 additions & 0 deletions resources/views/livewire/layout/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<x-nav-link :href="route('documents.index')" :active="request()->routeIs('documents.index')" wire:navigate>
<span class="dark:text-white">{{ __('Documents') }}</span>
</x-nav-link>
<x-nav-link :href="route('documents.shared')" :active="request()->routeIs('documents.shared')" wire:navigate>
<span class="dark:text-white">{{ __('Shared') }}</span>
</x-nav-link>
</div>
</div>

Expand Down Expand Up @@ -78,6 +81,9 @@
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')" wire:navigate>
{{ __('Dashboard') }}
</x-responsive-nav-link>
<x-responsive-nav-link :href="route('documents.shared')" :active="request()->routeIs('documents.shared')" wire:navigate>
{{ __('Shared Documents') }}
</x-responsive-nav-link>
</div>

<!-- Responsive Settings Options -->
Expand Down
Loading

0 comments on commit 6da06cb

Please sign in to comment.