notification: update comment and api endpoint for marking as read/unread

This commit is contained in:
2021-12-29 17:36:14 +01:00
parent 9d638fe897
commit 8fe94bd117
7 changed files with 302 additions and 16 deletions

View File

@@ -125,7 +125,7 @@ class NotificationController extends AbstractController
*/
public function editAction(Notification $notification, Request $request): Response
{
$this->denyAccessUnlessGranted(NotificationVoter::UPDATE, $notification);
$this->denyAccessUnlessGranted(NotificationVoter::NOTIFICATION_UPDATE, $notification);
$form = $this->createForm(NotificationType::class, $notification);
@@ -207,27 +207,61 @@ class NotificationController extends AbstractController
*/
public function showAction(Notification $notification, Request $request): Response
{
$this->denyAccessUnlessGranted(NotificationVoter::SEE, $notification);
$this->denyAccessUnlessGranted(NotificationVoter::NOTIFICATION_SEE, $notification);
$notification->addComment($appendComment = new NotificationComment());
$appendComment = new NotificationComment();
$appendCommentForm = $this->createForm(NotificationCommentType::class, $appendComment);
$appendCommentForm->handleRequest($request);
if ($appendCommentForm->isSubmitted() && $appendCommentForm->isValid()) {
$this->em->persist($appendComment);
$this->em->flush();
if ($request->query->has('edit')) {
$commentId = $request->query->getInt('edit');
$editedComment = $notification->getComments()->filter(static function (NotificationComment $c) use ($commentId) {
return $c->getId() === $commentId;
})->first();
$this->addFlash('success', $this->translator->trans('notification.comment_appended'));
if (false === $editedComment) {
throw $this->createNotFoundException("Comment with id {$commentId} does not exists nor belong to this notification");
}
return $this->redirectToRoute('chill_main_notification_show', [
'id' => $notification->getId(),
]);
$editedCommentForm = $this->createForm(NotificationCommentType::class, $editedComment);
if (Request::METHOD_POST === $request->getMethod() && 'edit' === $request->request->get('form')) {
$editedCommentForm->handleRequest($request);
if ($editedCommentForm->isSubmitted() && $editedCommentForm->isValid()) {
$this->em->flush();
$this->addFlash('success', $this->translator->trans('notification.comment_updated'));
return $this->redirectToRoute('chill_main_notification_show', [
'id' => $notification->getId(),
'_fragment' => 'comment-' . $commentId,
]);
}
}
}
if (Request::METHOD_POST === $request->getMethod() && 'append' === $request->request->get('form')) {
$appendCommentForm->handleRequest($request);
if ($appendCommentForm->isSubmitted() && $appendCommentForm->isValid()) {
$notification->addComment($appendComment);
$this->em->persist($appendComment);
$this->em->flush();
$this->addFlash('success', $this->translator->trans('notification.comment_appended'));
return $this->redirectToRoute('chill_main_notification_show', [
'id' => $notification->getId(),
]);
}
}
$response = $this->render('@ChillMain/Notification/show.html.twig', [
'notification' => $notification,
'handler' => $this->notificationHandlerManager->getHandler($notification),
'appendCommentForm' => $appendCommentForm->createView(),
'editedCommentForm' => isset($editedCommentForm) ? $editedCommentForm->createView() : null,
'editedCommentId' => $commentId ?? null,
]);
// we mark the notification as read after having computed the response