mirror of
https://github.com/ad-aures/castopod.git
synced 2026-04-04 15:26:43 +02:00
parent
c29c018c7a
commit
999999e3ef
15 changed files with 707 additions and 29 deletions
107
modules/Admin/Controllers/NotificationController.php
Normal file
107
modules/Admin/Controllers/NotificationController.php
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2020 Ad Aures
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
||||
* @link https://castopod.org/
|
||||
*/
|
||||
|
||||
namespace Modules\Admin\Controllers;
|
||||
|
||||
use App\Entities\Notification;
|
||||
use App\Entities\Podcast;
|
||||
use App\Models\NotificationModel;
|
||||
use App\Models\PodcastModel;
|
||||
use CodeIgniter\Exceptions\PageNotFoundException;
|
||||
use CodeIgniter\HTTP\RedirectResponse;
|
||||
use CodeIgniter\I18n\Time;
|
||||
use Modules\Fediverse\Models\PostModel;
|
||||
|
||||
class NotificationController extends BaseController
|
||||
{
|
||||
protected Podcast $podcast;
|
||||
|
||||
protected Notification $notification;
|
||||
|
||||
public function _remap(string $method, string ...$params): mixed
|
||||
{
|
||||
if (
|
||||
($podcast = (new PodcastModel())->getPodcastById((int) $params[0])) === null
|
||||
) {
|
||||
throw PageNotFoundException::forPageNotFound();
|
||||
}
|
||||
|
||||
$this->podcast = $podcast;
|
||||
|
||||
if (count($params) > 1) {
|
||||
if (
|
||||
! ($notification = (new NotificationModel())
|
||||
->where([
|
||||
'id' => $params[1],
|
||||
])
|
||||
->first())
|
||||
) {
|
||||
throw PageNotFoundException::forPageNotFound();
|
||||
}
|
||||
|
||||
$this->notification = $notification;
|
||||
|
||||
unset($params[1]);
|
||||
unset($params[0]);
|
||||
}
|
||||
|
||||
return $this->{$method}(...$params);
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$notifications = (new NotificationModel())->where('target_actor_id', $this->podcast->actor_id)
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
$data = [
|
||||
'podcast' => $this->podcast,
|
||||
'notifications' => $notifications->paginate(10),
|
||||
'pager' => $notifications->pager,
|
||||
];
|
||||
|
||||
replace_breadcrumb_params([
|
||||
0 => $this->podcast->title,
|
||||
]);
|
||||
|
||||
return view('podcast/notifications', $data);
|
||||
}
|
||||
|
||||
public function markAsRead(): RedirectResponse
|
||||
{
|
||||
$this->notification->read_at = new Time('now');
|
||||
$notificationModel = new NotificationModel();
|
||||
$notificationModel->update($this->notification->id, $this->notification);
|
||||
|
||||
if ($this->notification->post_id === null) {
|
||||
return redirect()->route('podcast-activity', [esc($this->podcast->handle)]);
|
||||
}
|
||||
|
||||
$post = (new PostModel())->getPostById($this->notification->post_id);
|
||||
|
||||
return redirect()->route(
|
||||
'post',
|
||||
[esc((new PodcastModel())->getPodcastByActorId($this->notification->actor_id)->handle), $post->id]
|
||||
);
|
||||
}
|
||||
|
||||
public function markAllAsRead(): RedirectResponse
|
||||
{
|
||||
$notifications = (new NotificationModel())->where('target_actor_id', $this->podcast->actor_id)
|
||||
->where('read_at', null)
|
||||
->findAll();
|
||||
|
||||
foreach ($notifications as $notification) {
|
||||
$notification->read_at = new Time('now');
|
||||
(new NotificationModel())->update($notification->id, $notification);
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue