date-poll-api/src/Controller/api/v1/CommentController.php

181 lines
4.7 KiB
PHP
Raw Normal View History

2020-01-30 11:28:24 +01:00
<?php
2021-05-03 12:38:30 +02:00
namespace App\Controller\api\v1;
2020-01-30 11:28:24 +01:00
use App\Controller\EmailsController;
2020-01-30 11:28:24 +01:00
use App\Entity\Comment;
use App\Entity\Owner;
use App\Entity\Poll;
2020-04-16 17:11:01 +02:00
use DateTime;
2020-01-30 11:31:23 +01:00
use FOS\RestBundle\Controller\Annotations\Delete;
2020-01-30 11:28:24 +01:00
use FOS\RestBundle\Controller\Annotations\Get;
2020-01-30 11:31:23 +01:00
use FOS\RestBundle\Controller\Annotations\Post;
2020-01-30 11:28:24 +01:00
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
2020-01-30 11:28:24 +01:00
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
2020-01-30 11:28:24 +01:00
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1/comment",name="api_")
2020-01-30 11:28:24 +01:00
*/
class CommentController extends EmailsController {
2020-01-30 11:28:24 +01:00
/**
* @Get(
2022-02-11 08:59:50 +01:00
* path = "/poll/{id}",
2020-01-30 11:28:24 +01:00
* name = "get_poll_comment",
* requirements = {"id"="\d+"}
2020-01-30 11:28:24 +01:00
* )
*/
public
function getPollCommentsAction(
SerializerInterface $serializer,
Poll $poll
2020-01-30 11:28:24 +01:00
) {
2021-04-27 10:22:16 +02:00
$jsonResponse = $serializer->serialize( [
'message' => 'here are your comments of the poll',
2021-04-27 10:22:16 +02:00
'data' => $poll->getComments(),
],
'json' );
2021-04-27 10:22:16 +02:00
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response;
2020-01-30 11:28:24 +01:00
}
/**
* add a comment on a poll
* @Post(
2022-02-11 08:59:50 +01:00
* path = "/poll/{id}",
2020-01-30 11:28:24 +01:00
* name = "new_comment",
* requirements = {"content"="\w+", "id"="\d+"}
2020-01-30 11:28:24 +01:00
* )
*/
public
function newCommentAction(
Poll $poll,
Request $request
) {
if ( ! $poll ) {
return $this->json( [ 'message' => 'poll not found' ], 404 );
}
$data = $request->getContent();
$serializer = SerializerBuilder::create()->build();
$comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' );
$em = $this->getDoctrine()->getRepository( Owner::class );
$data = json_decode( $data, true );
2021-04-27 10:22:16 +02:00
if ( ! isset( $data[ 'email' ] ) ) {
return $this->json( [ "message" => "Incorrect JSON in request" ], 400 );
}
2020-01-30 11:28:24 +01:00
$foundOwner = $em->findOneByEmail( $data[ 'email' ] );
// manage existing or new Owner
if ( ! $foundOwner ) {
$foundOwner = new Owner();
$foundOwner->setPseudo( $data[ 'email' ] )
2021-04-27 10:22:16 +02:00
->setEmail( $data[ 'email' ] )
->setModifierToken( uniqid( '', true ) );
2020-01-30 11:28:24 +01:00
}
// anti flood
$seconds_limit_lastpost = 5;
$emComment = $this->getDoctrine()->getRepository( Comment::class );
$lastCommentOfOwner = $emComment->findBy( [ 'owner' => $foundOwner ], [ 'id' => 'desc' ] );
// TODO anti flood by session / IP
if ( $lastCommentOfOwner ) {
// check time of last comment
2020-04-16 17:11:01 +02:00
$now = new DateTime();
2020-01-30 11:28:24 +01:00
$now = $now->format( 'Y-m-d H:i:s' );
$date_first = strtotime( $lastCommentOfOwner[ 0 ]->getCreatedAt()->format( 'Y-m-d H:i:s' ) );
$date_second = strtotime( $now );
if ( ( $date_second - $date_first ) < $seconds_limit_lastpost ) {
return $this->json( [
'message' => 'anti flood déclenché',
'details' => 'votre deriner commentaire a été envoyé il y a moins de ' . $seconds_limit_lastpost . ' secondes',
],
403 );
}
// check similar text content
if ( $lastCommentOfOwner[ 0 ]->getText() == $comment->getText() ) {
return $this->json( [
'message' => 'anti flood déclenché',
'details' => 'votre deriner commentaire a exactement le même contenu que celui ci, il n\'a donc pas été créé',
],
403 );
}
}
$comment->setOwner( $foundOwner )
2021-04-27 10:22:16 +02:00
->setCreatedAt( new DateTime() )
->setPoll( $poll );
2020-01-30 11:28:24 +01:00
$foundOwner->addComment( $comment );
$em = $this->getDoctrine()->getManager();
$em->persist( $foundOwner );
$em->persist( $comment );
$em->flush();
2021-04-27 10:22:16 +02:00
if ( $poll->getMailOnComment() ) {
$this->sendCommentNotificationAction( $foundOwner, $comment );
2020-04-16 18:14:59 +02:00
}
2020-04-16 17:11:01 +02:00
2020-01-30 11:28:24 +01:00
return $this->json( [
'message' => 'you created a comment',
'data' => [
'your_comment' => $comment->display(),
],
],
201 );
}
/**
* Erase all comments of a poll
* @Delete(
2022-02-11 08:59:50 +01:00
* path = "/poll/{id}",
2020-01-30 11:28:24 +01:00
* name = "poll_comments_delete",
* requirements = {"accessToken"="\w+", "id"="\d+"}
2020-01-30 11:28:24 +01:00
* )
*
* @param Poll $poll
* @param $accessToken
*
* @return JsonResponse
*/
public
function deletePollCommentsAction(
Poll $poll,
$accessToken
) {
if ( $accessToken == $poll->getAdminKey() ) {
$em = $this->getDoctrine()->getManager();
$length = count( $poll->getComments() );
$em->remove( $poll->getComments() );
$em->flush();
return $this->json( [
'message' => 'boom! les ' . $length . ' commentaires du sondage ont été supprimés',
] );
} else {
return $this->json( [
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
] );
}
}
}