jonnybarnes.uk/app/Http/Controllers/NotesController.php

93 lines
2.6 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
declare(strict_types=1);
2016-05-19 15:01:28 +01:00
namespace App\Http\Controllers;
2017-12-19 16:00:42 +00:00
use App\Models\Note;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2019-10-27 19:31:33 +00:00
use Illuminate\Http\JsonResponse;
2019-10-27 16:29:15 +00:00
use Illuminate\Http\RedirectResponse;
2019-10-27 19:31:33 +00:00
use Illuminate\Http\Response;
2019-10-27 16:29:15 +00:00
use Illuminate\View\View;
2016-05-19 15:01:28 +01:00
use Jonnybarnes\IndieWeb\Numbers;
/**
* @todo Need to sort out Twitter and webmentions!
*/
2016-05-19 15:01:28 +01:00
class NotesController extends Controller
{
/**
2017-09-11 15:56:47 +01:00
* Show all the notes. This is also the homepage.
2016-05-19 15:01:28 +01:00
*/
public function index(): View|Response
2016-05-19 15:01:28 +01:00
{
$notes = Note::latest()
->with('place', 'media', 'client')
->withCount(['webmentions AS replies' => function ($query) {
$query->where('type', 'in-reply-to');
}])
->withCount(['webmentions AS likes' => function ($query) {
$query->where('type', 'like-of');
}])
->withCount(['webmentions AS reposts' => function ($query) {
$query->where('type', 'repost-of');
}])->paginate(10);
2016-05-19 15:01:28 +01:00
return view('notes.index', compact('notes'));
2016-05-19 15:01:28 +01:00
}
/**
* Show a single note.
*/
2023-02-18 09:34:57 +00:00
public function show(string $urlId): View|JsonResponse|Response
2016-05-19 15:01:28 +01:00
{
try {
$note = Note::nb60($urlId)->with('place', 'media', 'client')
->withCount(['webmentions AS replies' => function ($query) {
$query->where('type', 'in-reply-to');
}])
->withCount(['webmentions AS likes' => function ($query) {
$query->where('type', 'like-of');
}])
->withCount(['webmentions AS reposts' => function ($query) {
$query->where('type', 'repost-of');
}])->firstOrFail();
} catch (ModelNotFoundException $exception) {
abort(404);
}
2016-05-19 15:01:28 +01:00
return view('notes.show', compact('note'));
2016-05-19 15:01:28 +01:00
}
/**
* Redirect /note/{decID} to /notes/{nb60id}.
*/
public function redirect(int $decId): RedirectResponse
2016-05-19 15:01:28 +01:00
{
return redirect(config('app.url') . '/notes/' . (new Numbers)->numto60($decId));
2016-05-19 15:01:28 +01:00
}
/**
* Show all notes tagged with {tag}.
*/
public function tagged(string $tag): View
2016-05-19 15:01:28 +01:00
{
$notes = Note::whereHas('tags', function ($query) use ($tag) {
$query->where('tag', $tag);
})->get();
2016-05-19 15:01:28 +01:00
2017-02-15 18:39:39 +00:00
return view('notes.tagged', compact('notes', 'tag'));
2016-05-19 15:01:28 +01:00
}
2022-08-13 16:00:06 +01:00
/**
* Page to create a new note.
*
* Dummy page for now.
*/
public function create(): View
{
return view('notes.create');
}
2016-05-19 15:01:28 +01:00
}