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,16 +12,31 @@
|
||||
namespace IPS\core\api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Api\Controller;
|
||||
use IPS\Api\Exception;
|
||||
use IPS\Api\Response;
|
||||
use IPS\core\Messenger\Conversation;
|
||||
use IPS\core\Messenger\Message;
|
||||
use IPS\DateTime;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Text\Parser;
|
||||
use OutOfRangeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function is_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 Personal Conversations API
|
||||
*/
|
||||
class _messages extends \IPS\Api\Controller
|
||||
class messages extends Controller
|
||||
{
|
||||
/**
|
||||
* POST /core/messages
|
||||
@@ -36,60 +51,62 @@ class _messages extends \IPS\Api\Controller
|
||||
* @throws 1C374/3 INVALID_RECIPIENT No recipients were supplied
|
||||
* @throws 1C374/4 INVALID_RECIPIENT One or more recipients are invalid
|
||||
* @throws 1C374/5 MISSING_TITLE_OR_BODY The title and/or body of the conversation were not supplied
|
||||
* @return int Conversation ID
|
||||
* @apireturn int Conversation ID
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTindex()
|
||||
public function POSTindex(): Response
|
||||
{
|
||||
/* Make sure there is a valid sender */
|
||||
if ( !isset( \IPS\Request::i()->from ) OR !\IPS\Member::load( (int) \IPS\Request::i()->from )->member_id )
|
||||
if ( !isset( Request::i()->from ) OR !Member::load( (int) Request::i()->from )->member_id )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_SENDER', '1C374/2', 404 );
|
||||
throw new Exception( 'INVALID_SENDER', '1C374/2', 404 );
|
||||
}
|
||||
|
||||
/* Verify there are recipients and all the recipients are valid */
|
||||
if( !isset( \IPS\Request::i()->to ) OR !\is_array( \IPS\Request::i()->to ) OR !\count( \IPS\Request::i()->to ) )
|
||||
if( !isset( Request::i()->to ) OR !is_array( Request::i()->to ) OR !count( Request::i()->to ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_RECIPIENT', '1C374/3', 404 );
|
||||
throw new Exception( 'INVALID_RECIPIENT', '1C374/3', 404 );
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach( \IPS\Request::i()->to as $to )
|
||||
foreach( Request::i()->to as $to )
|
||||
{
|
||||
if( !\IPS\Member::load( (int) $to )->member_id )
|
||||
if( !Member::load( (int) $to )->member_id )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_RECIPIENT', '1C374/4', 404 );
|
||||
throw new Exception( 'INVALID_RECIPIENT', '1C374/4', 404 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Make sure we have a title and body */
|
||||
if( !isset( \IPS\Request::i()->title ) OR !isset( \IPS\Request::i()->body ) )
|
||||
if( !isset( Request::i()->title ) OR !isset( Request::i()->body ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'MISSING_TITLE_OR_BODY', '1C374/5', 404 );
|
||||
throw new Exception( 'MISSING_TITLE_OR_BODY', '1C374/5', 404 );
|
||||
}
|
||||
|
||||
/* Create the conversation */
|
||||
$item = \IPS\core\Messenger\Conversation::createItem( \IPS\Member::load( (int) \IPS\Request::i()->from ), \IPS\Request::i()->ipAddress(), \IPS\DateTime::create(), NULL );
|
||||
$item->title = \IPS\Request::i()->title;
|
||||
$item->to_count = \count( \IPS\Request::i()->to );
|
||||
$item = Conversation::createItem( Member::load( (int) Request::i()->from ), Request::i()->ipAddress(), DateTime::create() );
|
||||
$item->title = Request::i()->title;
|
||||
$item->to_count = count( Request::i()->to );
|
||||
$item->save();
|
||||
|
||||
/* Create the first message */
|
||||
$postContents = \IPS\Text\Parser::parseStatic( \IPS\Request::i()->body, TRUE, NULL, \IPS\Member::load( (int) \IPS\Request::i()->from ), 'core_Messaging' );
|
||||
$postContents = Parser::parseStatic( Request::i()->body, NULL, Member::load( (int)Request::i()->from ), 'core_Messaging' );
|
||||
|
||||
/** @var Message $commentClass */
|
||||
$commentClass = $item::$commentClass;
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, NULL, NULL, \IPS\Member::load( (int) \IPS\Request::i()->from ), \IPS\DateTime::create() );
|
||||
$post = $commentClass::create( $item, $postContents, TRUE, NULL, NULL, Member::load( (int) Request::i()->from ), DateTime::create() );
|
||||
|
||||
$item->first_msg_id = $post->id;
|
||||
$item->save();
|
||||
|
||||
/* Authorize sender and recipients */
|
||||
$item->authorize( array_map( function( $member ) { return (int) $member; }, array_merge( array( \IPS\Request::i()->from ), \IPS\Request::i()->to ) ) );
|
||||
$item->authorize( array_map( function( $member ) { return (int) $member; }, array_merge( array( Request::i()->from ), Request::i()->to ) ) );
|
||||
|
||||
/* Send notifications */
|
||||
$post->sendNotifications();
|
||||
|
||||
return new \IPS\Api\Response( 201, $item->id );
|
||||
return new Response( 201, $item->id );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,39 +120,40 @@ class _messages extends \IPS\Api\Controller
|
||||
* @throws 1C374/6 INVALID_ID The personal conversation ID does not exist
|
||||
* @throws 1C374/7 INVALID_SENDER The sender ID supplied was not valid
|
||||
* @throws 1C374/8 SENDER_NO_PERMISSON The sender supplied does not have permmission to reply to the conversation
|
||||
* @return bool
|
||||
* @apireturn bool
|
||||
* @return Response
|
||||
*/
|
||||
public function POSTitem( $id )
|
||||
public function POSTitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$message = \IPS\core\Messenger\Conversation::load( $id );
|
||||
$message = Conversation::load( $id );
|
||||
|
||||
/* Make sure we have a member, and the member is authorized to reply */
|
||||
if( !isset( \IPS\Request::i()->from ) OR !\IPS\Member::load( (int) \IPS\Request::i()->from )->member_id )
|
||||
if( !isset( Request::i()->from ) OR !Member::load( (int) Request::i()->from )->member_id )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_SENDER', '1C374/7', 404 );
|
||||
throw new Exception( 'INVALID_SENDER', '1C374/7', 404 );
|
||||
}
|
||||
|
||||
if( !$message->canView( \IPS\Member::load( (int) \IPS\Request::i()->from ) ) )
|
||||
if( !$message->canView( Member::load( (int) Request::i()->from ) ) )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'SENDER_NO_PERMISSON', '1C374/8', 403 );
|
||||
throw new Exception( 'SENDER_NO_PERMISSON', '1C374/8', 403 );
|
||||
}
|
||||
|
||||
/* Create the reply */
|
||||
$postContents = \IPS\Text\Parser::parseStatic( \IPS\Request::i()->body, TRUE, NULL, \IPS\Member::load( (int) \IPS\Request::i()->from ), 'core_Messaging' );
|
||||
$postContents = Parser::parseStatic( Request::i()->body, NULL, Member::load( (int)Request::i()->from ), 'core_Messaging' );
|
||||
|
||||
$commentClass = $message::$commentClass;
|
||||
$post = $commentClass::create( $message, $postContents, TRUE, NULL, NULL, \IPS\Member::load( (int) \IPS\Request::i()->from ), \IPS\DateTime::create() );
|
||||
$post = $commentClass::create( $message, $postContents, TRUE, NULL, NULL, Member::load( (int) Request::i()->from ), DateTime::create() );
|
||||
|
||||
/* Send notifications */
|
||||
$post->sendNotifications();
|
||||
|
||||
return new \IPS\Api\Response( 200, TRUE );
|
||||
return new Response( 200, TRUE );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\Exception( 'INVALID_ID', '1C374/6', 404 );
|
||||
throw new Exception( 'INVALID_ID', '1C374/6', 404 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,20 +164,21 @@ class _messages extends \IPS\Api\Controller
|
||||
* @apiclientonly
|
||||
* @param int $id ID Number
|
||||
* @throws 1C292/2 INVALID_ID The personal conversation ID does not exist
|
||||
* @return void
|
||||
* @apireturn void
|
||||
* @return Response
|
||||
*/
|
||||
public function DELETEitem( $id )
|
||||
public function DELETEitem( int $id ): Response
|
||||
{
|
||||
try
|
||||
{
|
||||
$message = \IPS\core\Messenger\Conversation::load( $id );
|
||||
$message = Conversation::load( $id );
|
||||
$message->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', '1C374/1', 404 );
|
||||
throw new Exception( 'INVALID_ID', '1C374/1', 404 );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user