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,35 @@
|
||||
namespace IPS\forums\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\forums\Topic;
|
||||
use IPS\forums\Topic\Post;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
|
||||
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 Posts API
|
||||
*/
|
||||
class _posts extends \IPS\Content\Api\CommentController
|
||||
class posts extends CommentController
|
||||
{
|
||||
/**
|
||||
* Class
|
||||
*/
|
||||
protected $class = 'IPS\forums\Topic\Post';
|
||||
protected string $class = 'IPS\forums\Topic\Post';
|
||||
|
||||
/**
|
||||
* GET /forums/posts
|
||||
@@ -46,17 +60,18 @@ class _posts 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\forums\Topic\Post>
|
||||
* @apireturn PaginatedResponse<IPS\forums\Topic\Post>
|
||||
* @return PaginatedResponse<Post>
|
||||
*/
|
||||
public function GETindex()
|
||||
public function GETindex(): PaginatedResponse
|
||||
{
|
||||
/* Init */
|
||||
$where = array();
|
||||
|
||||
/* Has best answer */
|
||||
if ( isset( \IPS\Request::i()->hasBestAnswer ) )
|
||||
if ( isset( Request::i()->hasBestAnswer ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->hasBestAnswer )
|
||||
if ( Request::i()->hasBestAnswer )
|
||||
{
|
||||
$where[] = array( "topic_answered_pid>0" );
|
||||
}
|
||||
@@ -67,15 +82,15 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
}
|
||||
|
||||
/* Archived */
|
||||
if ( isset( \IPS\Request::i()->archived ) )
|
||||
if ( isset( Request::i()->archived ) )
|
||||
{
|
||||
if ( \IPS\Request::i()->archived )
|
||||
if ( Request::i()->archived )
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( 'topic_archive_status', array( \IPS\forums\Topic::ARCHIVE_DONE, \IPS\forums\Topic::ARCHIVE_WORKING, \IPS\forums\Topic::ARCHIVE_RESTORE ) ) );
|
||||
$where[] = array( Db::i()->in( 'topic_archive_status', array( Topic::ARCHIVE_DONE, Topic::ARCHIVE_WORKING, Topic::ARCHIVE_RESTORE ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( \IPS\Db::i()->in( 'topic_archive_status', array( \IPS\forums\Topic::ARCHIVE_NOT, \IPS\forums\Topic::ARCHIVE_EXCLUDE ) ) );
|
||||
$where[] = array( Db::i()->in( 'topic_archive_status', array( Topic::ARCHIVE_NOT, Topic::ARCHIVE_EXCLUDE ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,12 +104,14 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @throws 1F295/4 INVALID_ID The post ID does not exist or the authorized user does not have permission to view it
|
||||
* @return \IPS\forums\Topic\Post
|
||||
* @apireturn \IPS\forums\Topic\Post
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem( $id )
|
||||
public function GETitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
/* @var Post $class */
|
||||
$class = $this->class;
|
||||
if ( $this->member )
|
||||
{
|
||||
@@ -105,11 +122,11 @@ class _posts 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', '1F295/4', 404 );
|
||||
throw new Exception( 'INVALID_ID', '1F295/4', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,14 +137,15 @@ class _posts 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\forums\Topic\Post
|
||||
* @apireturn \IPS\forums\Topic\Post
|
||||
* @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 );
|
||||
}
|
||||
@@ -138,13 +156,14 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
*
|
||||
* @param int $id ID Number
|
||||
* @apiparam int author ID of the member who reacted
|
||||
* @return \IPS\forums\Topic\Post
|
||||
* @apireturn \IPS\forums\Topic\Post
|
||||
* @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 );
|
||||
}
|
||||
@@ -167,18 +186,19 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
* @throws 1F295/3 NO_POST No post was supplied
|
||||
* @throws 2F294/A NO_PERMISSION The authorized user does not have permission to reply to that topic
|
||||
* @throws 3F295/C NO_ANON_PERMISSION The topic is set for anonymous posting, but the author does not have permission to post anonymously
|
||||
* @return \IPS\forums\Topic\Post
|
||||
* @apireturn \IPS\forums\Topic\Post
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTindex()
|
||||
public function POSTindex(): Response
|
||||
{
|
||||
/* Get topic */
|
||||
try
|
||||
{
|
||||
$topic = \IPS\forums\Topic::load( \IPS\Request::i()->topic );
|
||||
$topic = Topic::load( Request::i()->topic );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_TOPIC', '1F295/1', 403 );
|
||||
throw new Exception( 'NO_TOPIC', '1F295/1', 403 );
|
||||
}
|
||||
|
||||
/* Get author */
|
||||
@@ -186,47 +206,47 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
if ( !$topic->canComment( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F294/A', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2F294/A', 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', '1F295/2', 403 );
|
||||
throw new Exception( 'NO_AUTHOR', '1F295/2', 403 );
|
||||
}
|
||||
}
|
||||
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', '1F295/2', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '1F295/2', 400 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check anonymous posting */
|
||||
if ( isset( \IPS\Request::i()->anonymous ) and $author->member_id )
|
||||
if ( isset( Request::i()->anonymous ) and $author->member_id )
|
||||
{
|
||||
if ( ! $topic->container()->canPostAnonymously( 0, $author ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_ANON_PERMISSION', '3F295/C', 403 );
|
||||
throw new Exception( 'NO_ANON_PERMISSION', '3F295/C', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
/* Check we have a post */
|
||||
if ( !\IPS\Request::i()->post )
|
||||
if ( !Request::i()->post )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_POST', '1F295/3', 403 );
|
||||
throw new Exception( 'NO_POST', '1F295/3', 403 );
|
||||
}
|
||||
|
||||
/* Do it */
|
||||
@@ -249,33 +269,34 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
* @throws 1F295/8 CANNOT_HIDE_FIRST_POST You cannot hide or unhide the first post in a topic. Hide/unhide the topic itself instead.
|
||||
* @throws 1F295/9 CANNOT_AUTHOR_FIRST_POST You cannot change the author for the first post in a topic. Change the author on the topic itself instead.
|
||||
* @throws 2F295/A NO_PERMISSION The authorized user does not have permission to edit the post
|
||||
* @return \IPS\forums\Topic\Post
|
||||
* @apireturn \IPS\forums\Topic\Post
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $id )
|
||||
public function POSTitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
/* Load */
|
||||
$post = \IPS\forums\Topic\Post::load( $id );
|
||||
$post = Post::load( $id );
|
||||
if ( $this->member and !$post->canView( $this->member ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
if ( $this->member and !$post->canEdit( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F295/A', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2F295/A', 403 );
|
||||
}
|
||||
|
||||
/* Check */
|
||||
if ( $post->isFirst() )
|
||||
{
|
||||
if ( isset( \IPS\Request::i()->hidden ) )
|
||||
if ( isset( Request::i()->hidden ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'CANNOT_HIDE_FIRST_POST', '1F295/8', 403 );
|
||||
throw new Exception( 'CANNOT_HIDE_FIRST_POST', '1F295/8', 403 );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->author ) )
|
||||
if ( isset( Request::i()->author ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'CANNOT_AUTHOR_FIRST_POST', '1F295/9', 403 );
|
||||
throw new Exception( 'CANNOT_AUTHOR_FIRST_POST', '1F295/9', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,14 +305,14 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
{
|
||||
return $this->_edit( $post, 'post' );
|
||||
}
|
||||
catch ( \InvalidArgumentException $e )
|
||||
catch ( InvalidArgumentException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_AUTHOR', '2F295/7', 400 );
|
||||
throw new Exception( 'NO_AUTHOR', '2F295/7', 400 );
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '2F295/6', 404 );
|
||||
throw new Exception( 'INVALID_ID', '2F295/6', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,29 +324,31 @@ class _posts extends \IPS\Content\Api\CommentController
|
||||
* @throws 1F295/5 INVALID_ID The post ID does not exist
|
||||
* @throws 1F295/B CANNOT_DELETE_FIRST_POST You cannot delete the first post in a topic. Delete the topic itself instead.
|
||||
* @throws 2F295/B NO_PERMISSION The authorized user does not have permission to delete the post
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem( $id )
|
||||
public function DELETEitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
/* @var Post $class */
|
||||
$class = $this->class;
|
||||
$object = $class::load( $id );
|
||||
if ( $object->isFirst() )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'CANNOT_DELETE_FIRST_POST', '1F295/B', 403 );
|
||||
throw new Exception( 'CANNOT_DELETE_FIRST_POST', '1F295/B', 403 );
|
||||
}
|
||||
if ( $this->member and !$object->canDelete( $this->member ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'NO_PERMISSION', '2F295/B', 403 );
|
||||
throw new Exception( 'NO_PERMISSION', '2F295/B', 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', '1F295/5', 404 );
|
||||
throw new Exception( 'INVALID_ID', '1F295/5', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,9 +362,10 @@ class _posts 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\forums\Topic\Post
|
||||
* @apireturn \IPS\forums\Topic\Post
|
||||
* @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