Skip to content

Commit

Permalink
Merge branch 'main' into feat/two-factor-authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
franbarbalopez authored May 14, 2024
2 parents f45c6a7 + 0f435e0 commit 63d8a0e
Show file tree
Hide file tree
Showing 41 changed files with 1,000 additions and 359 deletions.
19 changes: 17 additions & 2 deletions app/Livewire/Followers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
namespace App\Livewire\Followers;

use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\View\View;
use Livewire\Attributes\Locked;
use Livewire\Component;
use Livewire\WithoutUrlPagination;
use Livewire\WithPagination;

final class Index extends Component
{
use WithPagination;
use WithoutUrlPagination, WithPagination;

/**
* The component's user ID.
*/
#[Locked]
public int $userId;

/**
* Indicates if the modal is opened.
*/
public bool $isOpened = false;

/**
* Renders the user's followers.
*/
Expand All @@ -29,7 +37,14 @@ public function render(): View

return view('livewire.followers.index', [
'user' => $user,
'followers' => $user->followers()->orderBy('created_at', 'desc')->simplePaginate(10),
'followers' => $this->isOpened ? $user->followers()
->when(auth()->user()?->isNot($user), function (Builder|BelongsToMany $query): void {
$query->withExists([
'following as is_follower' => function (Builder $query): void {
$query->where('user_id', auth()->id());
},
]);
})->orderBy('created_at', 'desc')->simplePaginate(10) : collect(),
]);
}
}
15 changes: 13 additions & 2 deletions app/Livewire/Following/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
namespace App\Livewire\Following;

use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\View\View;
use Livewire\Attributes\Locked;
use Livewire\Component;
use Livewire\WithoutUrlPagination;
use Livewire\WithPagination;

final class Index extends Component
{
use WithPagination;
use WithoutUrlPagination, WithPagination;

/**
* The component's user ID.
*/
#[Locked]
public int $userId;

/**
* Indicates if the modal is opened.
*/
public bool $isOpened = false;

/**
* Renders the user's followers.
*/
Expand All @@ -29,7 +36,11 @@ public function render(): View

return view('livewire.following.index', [
'user' => $user,
'following' => $user->following()->orderBy('created_at', 'desc')->simplePaginate(10),
'following' => $this->isOpened ? $user->following()->withExists([
'following as is_follower' => function (Builder $query): void {
$query->where('user_id', auth()->id());
},
])->orderBy('created_at', 'desc')->simplePaginate(10) : collect(),
]);
}
}
41 changes: 33 additions & 8 deletions app/Livewire/Questions/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,65 @@ final class Edit extends Component
*/
public string $answer = '';

/**
* Mount the component.
*/
public function mount(string $questionId): void
{
$this->questionId = $questionId;
$question = Question::findOrFail($questionId);
$rawAnswer = $question->getRawOriginal('answer');
$this->answer = is_string($rawAnswer) ? $rawAnswer : '';
}

/**
* Updates the question with the given answer.
*/
public function update(Request $request): void
{
$this->validate([
$validated = $this->validate([
'answer' => ['required', 'string', 'max:1000', new NoBlankCharacters],
]);

$user = type($request->user())->as(User::class);

$question = Question::query()
->whereNull('answer')
->where('is_reported', false)
->where('is_ignored', false)
->find($this->questionId);

$originalAnswer = $question->answer ?? null;

if (is_null($question)) {
$this->dispatch('notification.created', message: 'Sorry, something unexpected happened. Please try again.');
$this->redirectRoute('profile.show', ['username' => $user->username], navigate: true);

return;
}

if ($question->answered_at !== null && $question->answered_at->diffInHours(now()) > 24) {
$this->dispatch('notification.created', message: 'Answer cannot be edited after 24 hours.');

return;
}

$this->authorize('update', $question);

$question->update([
'answer' => $this->answer,
'answered_at' => now(),
]);
if ($originalAnswer === null) {
$validated['answered_at'] = now();
} else {
$validated['answer_updated_at'] = now();
}

$question->update($validated);

$this->answer = '';
if ($originalAnswer !== null) {
$question->likes()->delete();

$this->dispatch('close-modal', "question.edit.answer.{$question->id}");
}

$this->dispatch('notification.created', message: 'Question answered.');
$this->dispatch('notification.created', message: $originalAnswer === null ? 'Question answered.' : 'Answer updated.');
$this->dispatch('question.updated');
}

Expand Down
3 changes: 2 additions & 1 deletion app/Livewire/Questions/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Question;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Livewire\Attributes\Locked;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function render(Request $request): View
->questionsReceived()
->where('is_ignored', false)
->where('is_reported', false)
->when($user->isNot($request->user()), function (Builder $query, bool $_): void { // @phpstan-ignore-line
->when($user->isNot($request->user()), function (Builder|HasMany $query): void {
$query->whereNotNull('answer');
})
->orderByDesc('pinned')
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @property bool $anonymously
* @property string|null $answer
* @property Carbon|null $answered_at
* @property Carbon|null $answer_updated_at
* @property bool $is_reported
* @property bool $is_ignored
* @property int $views
Expand Down Expand Up @@ -84,6 +85,7 @@ public function casts(): array
'is_reported' => 'boolean',
'anonymously' => 'boolean',
'answered_at' => 'datetime',
'answer_updated_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'pinned' => 'bool',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public function parse(string $content): string
{
return (string) preg_replace_callback(
'/(<a\s+[^>]*>.*?<\/a>)|@([^\s,.?!\/@<]+)/i',
'/(<a\s+[^>]*>.*?<\/a>)|@([a-z0-9_]+)/i',
fn (array $matches): string => $matches[1] !== ''
? $matches[1]
: '<a href="/@'.$matches[2].'" class="text-blue-500 hover:underline hover:text-blue-700 cursor-pointer" wire-navigate>@'.$matches[2].'</a>',
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"type": "project",
"require": {
"php": "^8.3",
"filament/filament": "^3.2.73",
"laravel/fortify": "^1.21",
"intervention/image": "^3.6.3",
"laravel/framework": "dev-feat/exception-page as 11.5.0",
"laravel/pennant": "^1.7",
"laravel/pulse": "^1.0@beta",
"laravel/socialite": "^5.13.2",
"filament/filament": "^3.2.76",
"intervention/image": "^3.6.4",
"laravel/framework": "^11.7.0",
"laravel/pennant": "^1.7.1",
"laravel/pulse": "^1.1.0",
"laravel/socialite": "^5.14.0",
"laravel/tinker": "^2.9.0",
"livewire/livewire": "^3.4.12",
"matomo/device-detector": "^6.3.1",
Expand All @@ -21,15 +21,15 @@
"require-dev": {
"barryvdh/laravel-debugbar": "^3.13.5",
"fakerphp/faker": "^1.23.1",
"larastan/larastan": "^2.9.5",
"larastan/larastan": "^2.9.6",
"laravel/pint": "^1.15.3",
"laravel/sail": "^1.29.1",
"mockery/mockery": "^1.6.11",
"nunomaduro/collision": "^8.1.1",
"pestphp/pest": "^3.0.0",
"pestphp/pest-plugin-laravel": "^3.0.0",
"pestphp/pest-plugin-type-coverage": "^3.0.0",
"rector/rector": "^1.0.4"
"rector/rector": "^1.0.5"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 63d8a0e

Please sign in to comment.