Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -12,21 +12,37 @@
|
||||
namespace IPS\gallery\api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use InvalidArgumentException;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\PaginatedResponse;
|
||||
use IPS\Api\Response;
|
||||
use IPS\Content\Api\CommentController;
|
||||
use IPS\Db;
|
||||
use IPS\gallery\Image;
|
||||
use IPS\gallery\Image\Review;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gallery Reviews API
|
||||
*/
|
||||
class _reviews extends \IPS\Content\Api\CommentController
|
||||
class reviews extends CommentController
|
||||
{
|
||||
/**
|
||||
* Class
|
||||
*/
|
||||
protected $class = 'IPS\gallery\Image\Review';
|
||||
protected string $class = 'IPS\gallery\Image\Review';
|
||||
|
||||
/**
|
||||
* GET /gallery/reviews
|
||||
@@ -43,20 +59,21 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @apiparam string sortDir Sort direction. Can be 'asc' or 'desc' - defaults to 'asc'
|
||||
* @apiparam int page Page number
|
||||
* @apiparam int perPage Number of results per page - defaults to 25
|
||||
* @return \IPS\Api\PaginatedResponse<IPS\gallery\Image\Review>
|
||||
* @apireturn PaginatedResponse<IPS\gallery\Image\Review>
|
||||
* @return PaginatedResponse<Review>
|
||||
*/
|
||||
public function GETindex()
|
||||
public function GETindex(): PaginatedResponse
|
||||
{
|
||||
/* Where clause */
|
||||
$where = array();
|
||||
|
||||
/* Albums */
|
||||
if ( isset( \IPS\Request::i()->albums ) )
|
||||
if ( isset( Request::i()->albums ) )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( 'image_album_id', array_filter( explode( ',', \IPS\Request::i()->albums ) ) ) );
|
||||
$where[] = array( Db::i()->in( 'image_album_id', array_filter( explode( ',', Request::i()->albums ) ) ) );
|
||||
}
|
||||
|
||||
return $this->_list( $where, 'categories' );
|
||||
return $this->_list( $where );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,12 +82,14 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @throws 2G318/1 INVALID_ID The review ID does not exist or the authorized user does not have permission to view it
|
||||
* @return \IPS\gallery\Image\Review
|
||||
* @apireturn \IPS\gallery\Image\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem( $id )
|
||||
public function GETitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
/* @var Review $class */
|
||||
$class = $this->class;
|
||||
if ( $this->member )
|
||||
{
|
||||
@@ -81,11 +100,11 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
$object = $class::load( $id );
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, $object->apiOutput( $this->member ) );
|
||||
return new Response( 200, $object->apiOutput( $this->member ) );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2G318/1', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2G318/1', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,18 +127,19 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @throws 1L298/4 NO_CONTENT No content was supplied
|
||||
* @throws 1G318/5 INVALID_RATING The rating is not a valid number up to the maximum rating
|
||||
* @throws 2G318/D NO_PERMISSION The authorized user does not have permission to review that image
|
||||
* @return \IPS\gallery\Image\Review
|
||||
* @apireturn \IPS\gallery\Image\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTindex()
|
||||
public function POSTindex(): Response
|
||||
{
|
||||
/* Get image */
|
||||
try
|
||||
{
|
||||
$image = \IPS\gallery\Image::load( \IPS\Request::i()->image );
|
||||
$image = Image::load( Request::i()->image );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2G318/2', 403 );
|
||||
throw new Exception( 'INVALID_ID', '2G318/2', 403 );
|
||||
}
|
||||
|
||||
/* Get author */
|
||||
@@ -127,44 +147,44 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
if ( !$image->canReview( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2G318/D', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2G318/D', 403 );
|
||||
}
|
||||
$author = $this->member;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( \IPS\Request::i()->author )
|
||||
if ( Request::i()->author )
|
||||
{
|
||||
$author = \IPS\Member::load( \IPS\Request::i()->author );
|
||||
$author = Member::load( Request::i()->author );
|
||||
if ( !$author->member_id )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1G318/3', 404 );
|
||||
throw new Exception( 'NO_AUTHOR', '1G318/3', 404 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( \IPS\Request::i()->author === 0 )
|
||||
if ( Request::i()->author === 0 )
|
||||
{
|
||||
$author = new \IPS\Member;
|
||||
$author->name = \IPS\Request::i()->author_name;
|
||||
$author = new Member;
|
||||
$author->name = Request::i()->author_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1G318/3', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1G318/3', 400 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check we have a post */
|
||||
if ( !\IPS\Request::i()->content )
|
||||
if ( !Request::i()->content )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_CONTENT', '1G318/4', 403 );
|
||||
throw new Exception( 'NO_CONTENT', '1G318/4', 403 );
|
||||
}
|
||||
|
||||
/* Check we have a rating */
|
||||
if ( !\IPS\Request::i()->rating or !\in_array( (int) \IPS\Request::i()->rating, range( 1, \IPS\Settings::i()->reviews_rating_out_of ) ) )
|
||||
if ( !Request::i()->rating or !in_array( (int) Request::i()->rating, range( 1, Settings::i()->reviews_rating_out_of ) ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_RATING', '2G318/8', 403 );
|
||||
throw new Exception( 'INVALID_RATING', '2G318/8', 403 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
@@ -187,41 +207,42 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @throws 1G318/B NO_AUTHOR The author ID does not exist
|
||||
* @throws 1G318/A INVALID_RATING The rating is not a valid number up to the maximum rating
|
||||
* @throws 2G318/E NO_PERMISSION The authorized user does not have permission to edit the review
|
||||
* @return \IPS\gallery\Image\Review
|
||||
* @apireturn \IPS\gallery\Image\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $id )
|
||||
public function POSTitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
/* Load */
|
||||
$comment = \IPS\gallery\Image\Review::load( $id );
|
||||
$comment = Review::load( $id );
|
||||
if ( $this->member and !$comment->canView( $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
if ( $this->member and !$comment->canEdit( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2G318/E', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2G318/E', 403 );
|
||||
}
|
||||
|
||||
/* Check */
|
||||
if ( isset( \IPS\Request::i()->rating ) and !\in_array( (int) \IPS\Request::i()->rating, range( 1, \IPS\Settings::i()->reviews_rating_out_of ) ) )
|
||||
if ( isset( Request::i()->rating ) and !in_array( (int) Request::i()->rating, range( 1, Settings::i()->reviews_rating_out_of ) ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_RATING', '1G318/A', 403 );
|
||||
throw new Exception( 'INVALID_RATING', '1G318/A', 403 );
|
||||
}
|
||||
/* Do it */
|
||||
try
|
||||
{
|
||||
return $this->_edit( $comment );
|
||||
}
|
||||
catch ( \InvalidArgumentException $e )
|
||||
catch ( InvalidArgumentException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '1G318/B', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1G318/B', 400 );
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2G318/9', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2G318/9', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,25 +253,27 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @param int $id ID Number
|
||||
* @throws 2G318/C INVALID_ID The review ID does not exist
|
||||
* @throws 2G318/F NO_PERMISSION The authorized user does not have permission to delete the review
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem( $id )
|
||||
public function DELETEitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
{
|
||||
/* @var Review $class */
|
||||
$class = $this->class;
|
||||
$object = $class::load( $id );
|
||||
if ( $this->member and !$object->canDelete( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2G318/F', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2G318/F', 403 );
|
||||
}
|
||||
$object->delete();
|
||||
|
||||
return new \IPS\Api\Response( 200, NULL );
|
||||
return new Response( 200, NULL );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2G318/C', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2G318/C', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,14 +284,15 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @param int $id ID Number
|
||||
* @apiparam int id ID of the reaction to add
|
||||
* @apiparam int author ID of the member reacting
|
||||
* @return \IPS\gallery\Image\Review
|
||||
* @apireturn \IPS\gallery\Image\Review
|
||||
* @throws 1S425/2 NO_REACTION The reaction ID does not exist
|
||||
* @throws 1S425/3 NO_AUTHOR The author ID does not exist
|
||||
* @throws 1S425/4 REACT_ERROR Error adding the reaction
|
||||
* @throws 1S425/5 INVALID_ID Object ID does not exist
|
||||
* @note If the author has already reacted to this content, any existing reaction will be removed first
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem_react( $id )
|
||||
public function POSTitem_react( int $id ): Response
|
||||
{
|
||||
return $this->_reactAdd( $id );
|
||||
}
|
||||
@@ -279,13 +303,14 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @apiparam int author ID of the member who reacted
|
||||
* @return \IPS\gallery\Image\Review
|
||||
* @apireturn Review
|
||||
* @throws 1S425/6 NO_AUTHOR The author ID does not exist
|
||||
* @throws 1S425/7 REACT_ERROR Error adding the reaction
|
||||
* @throws 1S425/8 INVALID_ID Object ID does not exist
|
||||
* @note If the author has already reacted to this content, any existing reaction will be removed first
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem_react( $id )
|
||||
public function DELETEitem_react( int $id ): Response
|
||||
{
|
||||
return $this->_reactRemove( $id );
|
||||
}
|
||||
@@ -300,9 +325,10 @@ class _reviews extends \IPS\Content\Api\CommentController
|
||||
* @apiparam string message Optional message
|
||||
* @throws 1S425/B NO_AUTHOR The author ID does not exist
|
||||
* @throws 1S425/C REPORTED_ALREADY The member has reported this item in the past 24 hours
|
||||
* @return \IPS\gallery\Image\Review
|
||||
* @apireturn \IPS\gallery\Image\Review
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem_report( $id )
|
||||
public function POSTitem_report( int $id ): Response
|
||||
{
|
||||
return $this->_report( $id );
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user