1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

Compare commits

..

4 Commits

6 changed files with 218 additions and 66 deletions

View File

@ -2,6 +2,39 @@
REST backend in symfony 5 for Funky framadate frontend.
https://framagit.org/framasoft/framadate/funky-framadate-front
------------------------------------ -------------- -------- ------ ----------------------------------------------------
Name Method Scheme Host Path
------------------------------------ -------------- -------- ------ ----------------------------------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
admin_homepage_get_default GET ANY ANY /admin/
admin_homepage_clean_expired_polls GET ANY ANY /admin/polls/clean/{token}
admin_homepage_migrate_framadate GET ANY ANY /admin/polls/migrate
home_sweet_home GET ANY ANY /
admin_homepage_migrate_from_v1 GET ANY ANY /migration-from-v1/{unique_key}
user_homepageget_default GET ANY ANY /user/
user_homepage_polls_send_by_email GET ANY ANY /user/{email}/polls/send-by-email
poll_index GET ANY ANY /poll/
poll_new GET|POST ANY ANY /poll/new
poll_show GET ANY ANY /poll/{id}
poll_edit GET|POST ANY ANY /poll/{id}/edit
poll_delete DELETE ANY ANY /poll/{id}
api_get_poll_comment GET ANY ANY /api/v1/comment/poll/{id}/comments
api_new_comment POST ANY ANY /api/v1/comment/poll/{id}/comment
api_poll_comments_delete DELETE ANY ANY /api/v1/comment/poll/{id}/comments
api_get_all_polls GET ANY ANY /api/v1/poll/
api_get_poll GET ANY ANY /api/v1/poll/{customUrl}
api_get_protected_poll GET ANY ANY /api/v1/poll/{customUrl}/pass/{md5}
api_get_admin_poll GET ANY ANY /api/v1/polladmin/{admin_key}
api_update_poll PUT ANY ANY /api/v1/poll/{customUrl}/{token}
api_new_poll POST ANY ANY /api/v1/poll/
api_test-mail-poll GET ANY ANY /api/v1/poll/mail/test-mail-poll/{emailChoice}
api_poll_delete DELETE ANY ANY /api/v1/poll/{customUrl}
api_check_slug_is_unique GET ANY ANY /api/v1/poll/slug/{customUrl}
api_get_admin_config GET ANY ANY /api/v1/poll/admin/{token}
api_new_vote_stack POST|OPTIONS ANY ANY /api/v1/vote/poll/{custom_url}/answer
api_update_vote_stack PATCH ANY ANY /api/v1/vote/vote-stack/{id}/token/{modifierToken}
api_poll_votes_delete DELETE ANY ANY /api/v1/vote/poll/{id}/votes/{accessToken}
------------------------------------ -------------- -------- ------ ----------------------------------------------------
## TODO:

View File

@ -1,6 +1,6 @@
<?php
namespace App\Controller\api;
namespace App\Controller\api\v1;
use App\Controller\EmailsController;
use App\Entity\Comment;
@ -20,7 +20,7 @@ use Symfony\Component\HttpFoundation\Response;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
* @Route("/api/v1/comment",name="api_")
*/
class CommentController extends EmailsController {

View File

@ -1,6 +1,6 @@
<?php
namespace App\Controller\api;
namespace App\Controller\api\v1;
use App\Controller\EmailsController;
use App\Entity\Choice;
@ -148,7 +148,7 @@ class PollController extends EmailsController {
// wrong pass
return $this->json( [
'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad',
'md5' => md5( $md5 ),
// 'md5' => md5( $md5 ),
'data' => null,
],
403 );
@ -157,6 +157,34 @@ class PollController extends EmailsController {
}
/**
* as an administrator of a poll, get a poll config by its custom URL, we do not want polls to be reachable by their numeric id
* @Get(
* path = "admin/{admin_key}",
* name = "get_admin_poll",
* )
*
* @param SerializerInterface $serializer
* @param Request $request
*
* @return JsonResponse|Response
*/
function getAdminPoll( $admin_key, $md5, SerializerInterface $serializer ) {
$repository = $this->getDoctrine()->getRepository( Poll::class );
$poll = $repository->findOneByAdminKey( $admin_key );
if ( ! $poll ) {
return $this->notFoundPoll( $admin_key );
}
// good matching pass
return $this->json( $poll->displayForAdmin() );
}
function returnPollData( $poll, $serializer ) {
$jsonResponse = $serializer->serialize( $poll, 'json' );
@ -216,12 +244,13 @@ class PollController extends EmailsController {
$newpoll
->setModificationPolicy( $data[ 'modification_policy' ] )
->setTitle( $data[ 'title' ] )
->setKind( $data[ 'kind' ] )
->setCustomUrl( $data[ 'custom_url' ] );
if ( count( $data[ 'allowed_answers' ] ) ) {
$newpoll->setAllowedAnswers( $data[ 'allowed_answers' ] );
}
$expiracyCalculated = $newpoll->addDaysToDate( new DateTime(),
$data[ 'default_expiracy_days_from_now' ] ) ;
$expiracyCalculated = $newpoll->addDaysToDate( new DateTime(),
$data[ 'default_expiracy_days_from_now' ] );
$newpoll->setExpiracyDate( $expiracyCalculated );
$emOwner = $this->getDoctrine()->getRepository( Owner::class );
@ -250,16 +279,16 @@ class PollController extends EmailsController {
// emails
$newpoll->setMailOnComment( true );
if(isset($data['isOwnerNotifiedByEmailOnNewVote'])){
if ( isset( $data[ 'isOwnerNotifiedByEmailOnNewVote' ] ) ) {
$newpoll->setMailOnVote( $data['isOwnerNotifiedByEmailOnNewVote'] );
$newpoll->setMailOnVote( $data[ 'isOwnerNotifiedByEmailOnNewVote' ] );
}
$newpoll->setDescription( $data['description'] );
$newpoll->setDescription( $data[ 'description' ] );
$newpoll->setHideResults( false );
// possible answers
$newpoll->setAllowedAnswers( [ 'yes' ] );
$newpoll->setAllowedAnswers( $data[ 'allowed_answers' ] );
$newpoll->setVotesMax( $data[ 'maxCountOfAnswers' ] );
$newpoll->setCommentsAllowed( $data['allowComments'] );
$newpoll->setCommentsAllowed( $data[ 'allowComments' ] );
// setup the password, converting the raw with md5 hash
if ( $data[ 'password' ] ) {
@ -281,7 +310,7 @@ class PollController extends EmailsController {
elseif ( $data[ 'kind' ] == 'date' ) {
$choices = $data[ 'dateChoices' ];
if ( isset($data[ 'hasSeveralHours' ]) && $data[ 'hasSeveralHours' ] == true ) {
if ( isset( $data[ 'hasSeveralHours' ] ) && $data[ 'hasSeveralHours' ] == true ) {
// different hours spans make more choices
foreach ( $choices as $c ) {
@ -378,7 +407,7 @@ class PollController extends EmailsController {
/**
* @Delete(
* path = "/{customUrl}",
* path = "/{admin_key}",
* name = "poll_delete",
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
* )
@ -389,13 +418,16 @@ class PollController extends EmailsController {
*/
public
function deletePollAction(
Poll $poll,
$accessToken
$admin_key
) {
if ( $accessToken == $poll->getAdminKey() ) {
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
$found = $emPoll->findOneByAdminKey( $admin_key );
if ( $found ) {
$em = $this->getDoctrine()->getManager();
$em->remove( $poll );
$em->remove( $found );
$em->flush();
return $this->json( [
@ -429,7 +461,7 @@ class PollController extends EmailsController {
// we should use an other slug
return $this->json( [
'message' => ' NO, this slug is already taken on this Framadate instance ',
'message' => ' NO, this custom_url is already taken on this Framadate instance ',
'data' => [
'slug' => $customUrl,
],
@ -469,11 +501,11 @@ class PollController extends EmailsController {
if ( $pollFound ) {
$poll = $pollFound;
$poll = $pollFound;
$returnedPoll = [
'message' => 'your poll config',
'poll' => $poll->displayForAdmin(),
'message' => 'your poll config',
'poll' => $poll->displayForAdmin(),
];
return $this->json( $returnedPoll,
@ -490,4 +522,78 @@ class PollController extends EmailsController {
}
/**
* Find expired polls and set them as such
* @Get(
* path = "/admin/clean_expired_polls/{token}",
* name = "clean_expired_polls",
* )
*
* @param $token
*
* @return JsonResponse|Response
*/
public function getExpiredPollsCleanup( $token ) {
if ( $token !== 'superCaligistriixpirlidouciousse' ) {
return $this->json( [
'message' => 'not allowed',
'data' => [
'token' => $token,
],
],
403 );
}
$em = $this->getDoctrine()->getManager();
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
$deletablePollsFound = $emPoll->findDeletableExpiredPolls(); // dead by more than 30 days
$expiredPollsFound = $emPoll->findExpiredPolls(); // just dead
$soon_expired_polls = $emPoll->findSoonExpiredPolls(); // will die in 30 days
$deletedTitle = [];
$expiredTitle = [];
$soon_expired_title = [];
$really_delete = false;
foreach ( $soon_expired_polls as $item ) {
$soon_expired_title[] = $item->getTitle();
}
foreach ( $expiredPollsFound as $item ) {
$expiredTitle[] = $item->getTitle();
$item->setVotesAllowed( false );
$em->persist( $item );
}
foreach ( $deletablePollsFound as $item ) {
$deletedTitle[] = $item->getTitle();
if ( $really_delete ) {
$item->setVotesAllowed( false );
$em->remove( $item );
}
}
$em->flush();
return $this->json( [
'message' => 'cleanup report',
'really_delete' => $really_delete,
'deleted' => count( $deletablePollsFound ),
'deleted_titles' => $deletedTitle,
'expired' => count( $expiredPollsFound ),
'expired_titles' => $expiredTitle,
'soon_to_be_expired' => count( $soon_expired_polls ),
'soon_to_be_expired_titles' => $soon_expired_title,
'data' => [
'token' => $token,
],
],
200 );
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Controller\api;
namespace App\Controller\api\v1;
use App\Controller\EmailsController;
use App\Entity\Comment;
@ -21,7 +21,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
* @Route("/api/v1/vote",name="api_")
*/
class VoteController extends EmailsController {

View File

@ -295,21 +295,22 @@ class Poll {
foreach ( $this->getChoices() as $choice ) {
$boom = explode( ' >>> ', $choice->getName() );
if(count($boom) ==2){
if ( count( $boom ) == 2 ) {
if ( ! isset( $grouped_dates[ $boom[ 0 ] ] ) ) {
if ( ! isset( $grouped_dates[ $boom[ 0 ] ] ) ) {
$grouped_dates[ $boom[ 0 ] ] = [
$grouped_dates[ $boom[ 0 ] ] = [
"date_string" => $boom[ 0 ],
"choices" => [],
"date_string" => $boom[ 0 ],
"choices" => [],
];
}
$grouped_dates[ $boom[ 0 ] ][ "choices" ][] = [
"choice_id" => $choice->getId(),
"name" => $boom[ 1 ],
];
}
$grouped_dates[ $boom[ 0 ] ][ "choices" ][] = [
"choice_id" => $choice->getId(),
"name" => $boom[ 1 ] ];
}
}
}
$scoreInfos = [
@ -380,18 +381,18 @@ class Poll {
}
$groupsOfDates = [];
foreach ( $grouped_dates as $group ) {
$ii =0;
foreach ( $group["choices"] as $slice ) {
$slice['score'] = $computedArray[ $slice['choice_id'] ]['score'];
$slice['yes'] = $computedArray[ $slice['choice_id'] ]['yes'];
$slice['maybe'] = $computedArray[ $slice['choice_id'] ]['maybe'];
$slice['no'] = $computedArray[ $slice['choice_id'] ]['no'];
$slice['id'] = $slice['choice_id'];
$group["choices"][$ii] = $slice;
$ii++;
$ii = 0;
foreach ( $group[ "choices" ] as $slice ) {
$slice[ 'score' ] = $computedArray[ $slice[ 'choice_id' ] ][ 'score' ];
$slice[ 'yes' ] = $computedArray[ $slice[ 'choice_id' ] ][ 'yes' ];
$slice[ 'maybe' ] = $computedArray[ $slice[ 'choice_id' ] ][ 'maybe' ];
$slice[ 'no' ] = $computedArray[ $slice[ 'choice_id' ] ][ 'no' ];
$slice[ 'id' ] = $slice[ 'choice_id' ];
$group[ "choices" ][ $ii ] = $slice;
$ii ++;
}
$groupsOfDates[] = $group ;
$groupsOfDates[] = $group;
}
return [
@ -494,7 +495,11 @@ class Poll {
}
public function setAllowedAnswers( array $allowedAnswers ): self {
$this->allowedAnswers = $allowedAnswers;
if ( ! count( $allowedAnswers ) ) {
$this->allowedAnswers = [ 'yes' ];
} else {
$this->allowedAnswers = $allowedAnswers;
}
return $this;
}

View File

@ -22,29 +22,37 @@ class PollRepository extends ServiceEntityRepository {
// /**
// * @return Poll[] Returns an array of Poll objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Poll
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
public function findExpiredPolls() {
return $this->findExpirationPollOfDay( 0 );
}
*/
public function findSoonExpiredPolls() {
return $this->findExpirationPollOfDay( 30 );
}
public function findDeletableExpiredPolls() {
return $this->findExpirationPollOfDay( - 30 );
}
public function findExpirationPollOfDay( $count_of_days){
$today = new \DateTime();
if($count_of_days > -1){
$date_soon = $today->add( new \DateInterval( 'P' . $count_of_days . 'D' ) );
}else{
$date_soon = $today->sub( new \DateInterval( 'P' . abs($count_of_days) . 'D' ) );
}
return $this->createQueryBuilder( 'p' )
->andWhere( 'p.expiracyDate < :date_soon' )
->setParameter( 'date_soon', $date_soon )
->orderBy( 'p.id', 'ASC' )
->getQuery()
->getResult();
}
}