Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+92 -58
View File
@@ -11,43 +11,72 @@
namespace IPS\Content\Api\GraphQL;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use Exception;
use IPS\Api\GraphQL\SafeException;
use IPS\Content\Comment;
use IPS\Content\Item;
use IPS\Content\Reaction;
use IPS\Content\Search\Index;
use IPS\Content\Search\SearchContent;
use IPS\core\Reports\Report;
use IPS\core\Reports\Types;
use IPS\DateTime;
use IPS\Db;
use IPS\File;
use IPS\IPS;
use IPS\Member;
use IPS\Request;
use IPS\Settings;
use IPS\Text\Parser;
use OutOfRangeException;
use UnderflowException;
use function defined;
use function get_class;
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 Base mutator class for comments
*/
abstract class _CommentMutator extends ContentMutator
abstract class CommentMutator extends ContentMutator
{
/**
* Report comment
* Revoke Comment Report
*
* @param array $args Arguments
* @param Comment $comment Comment
*
* @return Comment
*/
protected function _revokeCommentReport( array $args, \IPS\Content\Comment $comment )
protected function _revokeCommentReport( array $args, Comment $comment ): Comment
{
try
{
$report = \IPS\Db::i()->select( '*', 'core_rc_reports', array( 'id=? AND report_by=? AND date_reported > ?', $args['reportID'], \IPS\Member::loggedIn()->member_id, time() - ( \IPS\Settings::i()->automoderation_report_again_mins * 60 ) ) )->first();
$report = Db::i()->select( '*', 'core_rc_reports', array( 'id=? AND report_by=? AND date_reported > ?', $args['reportID'], Member::loggedIn()->member_id, time() - ( Settings::i()->automoderation_report_again_mins * 60 ) ) )->first();
}
catch( \UnderflowException $e )
catch( UnderflowException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_REPORT', '1F295/1', 403 );
throw new SafeException( 'NO_REPORT', '1F295/1', 403 );
}
try
{
$index = \IPS\core\Reports\Report::load( $report['rid'] );
$index = Report::load( $report['rid'] );
}
catch( \OutofRangeException $e )
catch( OutofRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_REPORT', '1F295/1', 403 );
throw new SafeException( 'NO_REPORT', '1F295/1', 403 );
}
\IPS\Db::i()->delete( 'core_rc_reports', array( 'id=?', $args['reportID'] ) );
Db::i()->delete( 'core_rc_reports', array( 'id=?', $args['reportID'] ) );
/* Recalculate, we may have dropped below the threshold needed to hide a thing */
$index->runAutomaticModeration();
@@ -61,42 +90,46 @@ abstract class _CommentMutator extends ContentMutator
/**
* Report comment
*
* @param array $args Arguments
* @param Comment $comment Comment
*
* @return Comment
*/
protected function _reportComment( array $args, \IPS\Content\Comment $comment )
protected function _reportComment( array $args, Comment $comment ): Comment
{
$class = $this->class;
$canReport = $comment->canReport();
if ( $canReport !== TRUE AND !( $canReport == 'report_err_already_reported' AND \IPS\Settings::i()->automoderation_enabled ) )
if ( $canReport !== TRUE AND !( $canReport == 'report_err_already_reported' AND Settings::i()->automoderation_enabled ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'CANNOT_REPORT', '1F295/1', 403 );
throw new SafeException( 'CANNOT_REPORT', '1F295/1', 403 );
}
$itemIdColumn = $class::$databaseColumnId;
$idColumn = $comment::$databaseColumnId;
if ( \IPS\Member::loggedIn()->member_id and \IPS\Settings::i()->automoderation_enabled )
if ( Member::loggedIn()->member_id and Settings::i()->automoderation_enabled )
{
/* Has this member already reported this in the past 24 hours */
try {
$index = \IPS\core\Reports\Report::loadByClassAndId( \get_class( $comment ), $comment->$idColumn );
$report = \IPS\Db::i()->select( '*', 'core_rc_reports', array( 'rid=? and report_by=? and date_reported > ?', $index->id, \IPS\Member::loggedIn()->member_id, time() - ( \IPS\Settings::i()->automoderation_report_again_mins * 60 ) ) );
$index = Report::loadByClassAndId( get_class( $comment ), $comment->$idColumn );
$report = Db::i()->select( '*', 'core_rc_reports', array( 'rid=? and report_by=? and date_reported > ?', $index->id, Member::loggedIn()->member_id, time() - ( Settings::i()->automoderation_report_again_mins * 60 ) ) );
throw new \IPS\Api\GraphQL\SafeException( 'ALREADY_REPORTED', '1F295/1', 403 );
throw new SafeException( 'ALREADY_REPORTED', '1F295/1', 403 );
}
catch( \Exception $e ) {
if( $e instanceof \IPS\Api\GraphQL\SafeException ){
catch( Exception $e ) {
if( $e instanceof SafeException ){
throw $e;
}
}
if( !\in_array( $args['reason'], array_keys( \IPS\core\Reports\Types::roots() ) ) && $args['reason'] !== 0 )
if( !in_array( $args['reason'], array_keys( Types::roots() ) ) && $args['reason'] !== 0 )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_REASON', '1F295/1', 403 );
throw new SafeException( 'INVALID_REASON', '1F295/1', 403 );
}
}
if( !\IPS\Settings::i()->automoderation_enabled )
if( !Settings::i()->automoderation_enabled )
{
$args['reason'] = 0;
}
@@ -107,9 +140,9 @@ abstract class _CommentMutator extends ContentMutator
{
$comment->report( $args['additionalInfo'], $args['reason'] );
}
catch( \Exception $e )
catch( Exception $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'REPORT_FAILED', '1F295/1', 403 );
throw new SafeException( 'REPORT_FAILED', '1F295/1', 403 );
}
@@ -120,18 +153,18 @@ abstract class _CommentMutator extends ContentMutator
* Comment reactions
*
* @param int $reactionID ID of reaction to add
* @param \IPS\Content\Comment $comment The comment to add a reaction on
* @param Comment $comment The comment to add a reaction on
* @return void
*/
protected function _reactComment( $reactionID, \IPS\Content\Comment $comment )
protected function _reactComment( int $reactionID, Comment $comment ): void
{
try
{
$reaction = \IPS\Content\Reaction::load( $reactionID );
$reaction = Reaction::load( $reactionID );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_REACTION', '1F295/1', 403 );
throw new SafeException( 'INVALID_REACTION', '1F295/1', 403 );
}
$comment->react( $reaction );
@@ -140,43 +173,44 @@ abstract class _CommentMutator extends ContentMutator
/**
* Remove comment reaction
*
* @param \IPS\Content\Comment $comment The comment to remove the reaction on
* @param Comment $comment The comment to remove the reaction on
* @return void
*/
protected function _unreactComment( \IPS\Content\Comment $comment )
protected function _unreactComment( Comment $comment ): void
{
try {
$comment->removeReaction();
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_REACTIONS', '1F295/1', 403 );
throw new SafeException( 'NO_REACTIONS', '1F295/1', 403 );
}
}
/**
* Create
*
* @param array $commentData Array of data used to generate comment
* @param \IPS\Content\Item $item Content Item
* @param string $postKey Post key
* @return \IPS\Content\Comment
* @param array $commentData Array of data used to generate comment
* @param Item $item Content Item
* @param string|null $postKey Post key
* @param Comment|null $replyTo Reply To
* @return Comment
*/
protected function _createComment( array $commentData, \IPS\Content\Item $item, string $postKey = NULL, $replyTo = NULL )
protected function _createComment( array $commentData, Item $item, string|null $postKey = NULL, Comment|null $replyTo = NULL ): Comment
{
/* Work out the date */
$date = \IPS\DateTime::create();
$date = DateTime::create();
$hidden = false;
$quoteHtml = '';
if( $replyTo !== NULL && $replyTo instanceof \IPS\Content\Comment ){
if( $replyTo instanceof Comment ){
try {
$idField = $replyTo::$databaseColumnId;
$app = $replyTo::$application;
$replyToItem = $replyTo->item();
$contentType = $replyToItem::$module;
$itemClassSafe = str_replace( '\\', '_', mb_substr( $replyTo::$itemClass, 4 ) );
$citationLang = \IPS\Member::loggedIn()->language()->get('_date_just_now_c') . ', ' . $replyTo->mapped('author_name');
$citationLang = Member::loggedIn()->language()->get('_date_just_now_c') . ', ' . $replyTo->mapped('author_name');
$quoteHtml = <<<HTML
<blockquote class="ipsQuote" data-ipsquote="" data-ipsquote-contentapp="{$app}" data-ipsquote-contentclass="{$itemClassSafe}" data-ipsquote-contentcommentid="{$replyTo->mapped('id')}" data-ipsquote-contentid="{$replyTo->item()->mapped('id')}" data-ipsquote-contenttype="{$contentType}" data-ipsquote-timestamp="{$replyTo->mapped('date')}" data-ipsquote-userid="{$replyTo->author()->member_id}" data-ipsquote-username="{$replyTo->mapped('author_name')}">
@@ -186,7 +220,7 @@ abstract class _CommentMutator extends ContentMutator
<div class='ipsQuote_contents'>{$replyTo->mapped('content')}</div>
</blockquote>
HTML;
} catch ( \Exception $err ) {
} catch ( Exception $err ) {
// If something goes wrong here, it isn't a big deal - just continue without the quote
}
}
@@ -199,14 +233,14 @@ HTML;
{
$this->_addAttachmentsToContent( $postKey, $commentData['content'] );
}
catch ( \DomainException $e )
catch ( DomainException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'ATTACHMENTS_TOO_LARGE', '2S400/2', 403 );
throw new SafeException( 'ATTACHMENTS_TOO_LARGE', '2S400/2', 403 );
}
}
/* Parse */
$content = $quoteHtml . \IPS\Text\Parser::parseStatic( $commentData['content'], TRUE, md5( $postKey . ':' ), \IPS\Member::loggedIn(), $item::$application . '_' . mb_ucfirst( $item::$module ) );
$content = $quoteHtml . Parser::parseStatic( $commentData['content'], array( md5( $postKey . ':' ) ), Member::loggedIn(), $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
/* Create post */
$class = $this->class;
@@ -216,40 +250,40 @@ HTML;
}
else
{*/
$comment = $class::create( $item, $content, FALSE, \IPS\Member::loggedIn()->member_id ? NULL : \IPS\Member::loggedIn()->real_name, NULL, \IPS\Member::loggedIn(), $date, \IPS\Request::i()->ipAddress() );
$comment = $class::create( $item, $content, FALSE, Member::loggedIn()->member_id ? NULL : Member::loggedIn()->real_name, NULL, Member::loggedIn(), $date, Request::i()->ipAddress() );
/*}*/
$itemIdColumn = $item::$databaseColumnId;
$commentIdColumn = $comment::$databaseColumnId;
\IPS\File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $comment->$commentIdColumn );
File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $comment->$commentIdColumn );
/* Index */
if ( $item instanceof \IPS\Content\Searchable )
if( SearchContent::isSearchable( $item ) )
{
if ( $item::$firstCommentRequired and !$comment->isFirst() )
{
if ( \in_array( 'IPS\Content\Searchable', class_implements( $class ) ) )
if( SearchContent::isSearchable( $class ) )
{
\IPS\Content\Search\Index::i()->index( $item->firstComment() );
Index::i()->index( $item->firstComment() );
}
}
else
{
\IPS\Content\Search\Index::i()->index( $item );
Index::i()->index( $item );
}
}
if ( $comment instanceof \IPS\Content\Searchable )
if( SearchContent::isSearchable( $comment ) )
{
\IPS\Content\Search\Index::i()->index( $comment );
Index::i()->index( $comment );
}
/* Hide */
if ( isset( $commentData['hidden'] ) and \IPS\Member::loggedIn()->member_id and $comment->canHide() )
if ( isset( $commentData['hidden'] ) and Member::loggedIn()->member_id and $comment->canHide() )
{
$comment->hide( \IPS\Member::loggedIn() );
$comment->hide( Member::loggedIn() );
}
/* Mark it as read */
if( $item instanceof \IPS\Content\ReadMarkers )
if( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
{
$item->markRead();
}
+29 -21
View File
@@ -11,33 +11,38 @@
namespace IPS\Content\Api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Comment;
use IPS\Content\Item;
use IPS\Member;
use function defined;
use function get_class;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
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 Base mutator class for Content Comments
*/
class _CommentType extends ObjectType
class CommentType extends ObjectType
{
/*
* @brief The item classname we use for this type
*/
protected static $commentClass = '\IPS\Content\Comment';
protected static string $commentClass = '\IPS\Content\Comment';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'core_Comment';
protected static string $typeName = 'core_Comment';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A generic content comment item';
protected static string $typeDescription = 'A generic content comment item';
public function __construct()
{
@@ -57,7 +62,7 @@ class _CommentType extends ObjectType
*
* @return array
*/
public function fields()
public function fields(): array
{
return array(
'id' => [
@@ -161,13 +166,14 @@ class _CommentType extends ObjectType
],
],
'resolveField' => function ($item, $args, $context, $info) {
$className = \get_class( $item );
$className = get_class( $item );
/* @var Item $className */
switch( $info->fieldName )
{
case 'indefinite':
return $className::_indefiniteArticle( NULL );
break;
return $className::_indefiniteArticle();
case 'definite':
if( $args['withItem'] === FALSE )
{
@@ -177,9 +183,9 @@ class _CommentType extends ObjectType
}
// Return normal definite article string handled by \IPS\Content
return $className::_definiteArticle( NULL, NULL, $args['uppercase'] ? array( 'ucfirst' => TRUE ) : array() );
break;
return $className::_definiteArticle( NULL, NULL, $args['uppercase'] ? array( 'ucfirst' => TRUE ) : array() );
}
return '';
}
]),
'resolve' => function ($item) {
@@ -209,28 +215,28 @@ class _CommentType extends ObjectType
*
* @return array
*/
public static function getCommentPermissionFields()
public static function getCommentPermissionFields(): array
{
return array(
'canShare' => [
'type' => TypeRegistry::boolean(),
'description' => 'Can the user share this item?',
'resolve' => function ($comment) {
return $comment->canShare( \IPS\Member::loggedIn() );
return $comment->canShare( Member::loggedIn() );
}
],
'canReport' => [
'type' => TypeRegistry::boolean(),
'description' => 'Can the user report this item?',
'resolve' => function ($comment) {
return $comment->canReport( \IPS\Member::loggedIn() ) === TRUE;
return $comment->canReport( Member::loggedIn() ) === TRUE;
}
],
'canReportOrRevoke' => [
'type' => TypeRegistry::boolean(),
'description' => 'Can the user report (or revoke a report) on this comment?',
'resolve' => function ($comment) {
return $comment->canReportOrRevoke( \IPS\Member::loggedIn() ) === TRUE;
return $comment->canReportOrRevoke( Member::loggedIn() ) === TRUE;
}
]
);
@@ -241,7 +247,7 @@ class _CommentType extends ObjectType
*
* @return array
*/
public static function args()
public static function args(): array
{
return array(
'offsetPosition' => [
@@ -301,7 +307,7 @@ class _CommentType extends ObjectType
*
* @return ObjectType
*/
public static function getItemType()
public static function getItemType(): ObjectType
{
return \IPS\Content\Api\GraphQL\TypeRegistry::item();
}
@@ -309,10 +315,12 @@ class _CommentType extends ObjectType
/**
* Return the definite article, but without the item type
*
* @return string
* @param Comment $post
* @param array $options
* @return string
*/
public static function definiteArticleNoItem($post, $options = array())
public static function definiteArticleNoItem( Comment $post, array $options = array()): string
{
return \IPS\Member::loggedIn()->language()->addToStack( '__defart_comment', FALSE );
return Member::loggedIn()->language()->addToStack( '__defart_comment', FALSE );
}
}
+26 -14
View File
@@ -11,16 +11,28 @@
namespace IPS\Content\Api\GraphQL;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use IPS\Db;
use IPS\File;
use IPS\Helpers\Form\Editor;
use IPS\Http\Url;
use IPS\Member;
use IPS\Theme;
use function count;
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 Base mutator class for comments
*/
abstract class _ContentMutator
abstract class ContentMutator
{
/**
* Add attachments to content
@@ -28,29 +40,29 @@ abstract class _ContentMutator
* @param string $postKey Post key
* @param string $content Post content
* @return void
* @throws \DomainException Size is too large
* @throws DomainException Size is too large
*/
protected function _addAttachmentsToContent( $postKey, &$content )
protected function _addAttachmentsToContent( string $postKey, string &$content ): void
{
$maxTotalSize = \IPS\Helpers\Form\Editor::maxTotalAttachmentSize( \IPS\Member::loggedIn(), 0 ); // @todo Currently set to 0 because editing is not supported. Once editing is supported by GraphQL, that will need to set the correct value
$maxTotalSize = Editor::maxTotalAttachmentSize( Member::loggedIn(), 0 ); // @todo Currently set to 0 because editing is not supported. Once editing is supported by GraphQL, that will need to set the correct value
$fileAttachments = array();
$totalSize = 0;
foreach ( \IPS\Db::i()->select( '*', 'core_attachments', array( 'attach_post_key=?', $postKey ) ) as $attachment )
foreach ( Db::i()->select( '*', 'core_attachments', array( 'attach_post_key=?', $postKey ) ) as $attachment )
{
if ( $maxTotalSize !== NULL )
{
$totalSize += $attachment['attach_filesize'];
if ( $totalSize > $maxTotalSize )
{
throw new \DomainException;
throw new DomainException;
}
}
$ext = mb_substr( $attachment['attach_file'], mb_strrpos( $attachment['attach_file'], '.' ) + 1 );
if ( \in_array( mb_strtolower( $ext ), \IPS\File::$videoExtensions ) )
if ( in_array( mb_strtolower( $ext ), File::$videoExtensions ) )
{
$content .= \IPS\Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedVideo( $attachment['attach_location'], \IPS\Http\Url::baseUrl( \IPS\Http\Url::PROTOCOL_RELATIVE ) . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'], $attachment['attach_file'], \IPS\File::getMimeType( $attachment['attach_file'] ), $attachment['attach_id'] );
$content .= Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedVideo( $attachment['attach_location'], Url::baseUrl( Url::PROTOCOL_RELATIVE ) . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'], $attachment['attach_file'], File::getMimeType( $attachment['attach_file'] ), $attachment['attach_id'] );
}
elseif ( $attachment['attach_is_image'] )
{
@@ -65,19 +77,19 @@ abstract class _ContentMutator
$width = $attachment['attach_img_width'];
}
$content .= str_replace( '<fileStore.core_Attachment>', \IPS\File::getClass('core_Attachment')->baseUrl(), \IPS\Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedImage( $attachment['attach_location'], $attachment['attach_thumb_location'] ? $attachment['attach_thumb_location'] : $attachment['attach_location'], $attachment['attach_file'], $attachment['attach_id'], $width, $ratio ) );
$content .= str_replace( '<fileStore.core_Attachment>', File::getClass('core_Attachment')->baseUrl(), Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedImage( $attachment['attach_location'], $attachment['attach_thumb_location'] ?: $attachment['attach_location'], $attachment['attach_file'], $attachment['attach_id'], $width, $ratio ) );
}
else
{
$fileAttachments[] = \IPS\Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedFile( \IPS\Http\Url::baseUrl() . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'] . ( $attachment['attach_security_key'] ? "&key={$attachment['attach_security_key']}" : '' ), $attachment['attach_file'], FALSE, $attachment['attach_ext'], $attachment['attach_id'], $attachment['attach_security_key'] );
$fileAttachments[] = Theme::i()->getTemplate( 'editor', 'core', 'global' )->attachedFile( Url::baseUrl() . "applications/core/interface/file/attachment.php?id=" . $attachment['attach_id'] . ( $attachment['attach_security_key'] ? "&key={$attachment['attach_security_key']}" : '' ), $attachment['attach_file'], FALSE, $attachment['attach_ext'], $attachment['attach_id'], $attachment['attach_security_key'] );
}
}
if( \count( $fileAttachments ) )
if( count( $fileAttachments ) )
{
$content .= "<p>" . implode( ' ', $fileAttachments ) . "</p>";
}
\IPS\Db::i()->update( 'core_attachments', array( 'attach_post_key' => '' ), array( 'attach_post_key=?', $postKey ) );
Db::i()->update( 'core_attachments', array( 'attach_post_key' => '' ), array( 'attach_post_key=?', $postKey ) );
}
}
+13 -10
View File
@@ -10,19 +10,21 @@
namespace IPS\Content\Api\GraphQL;
use GraphQL\Type\Definition\UnionType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Comment;
use IPS\Content\Item;
use function defined;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
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 Base mutator class for Content Items
*/
class _ContentType extends UnionType
class ContentType extends UnionType
{
public function __construct()
{
@@ -30,18 +32,19 @@ class _ContentType extends UnionType
'name' => 'core_Content',
'description' => 'A union type that returns a CommentType, ItemType or @todo ReviewType depending on the object passed to it',
'types' => [
\IPS\Content\Api\GraphQL\TypeRegistry::comment(),
\IPS\Content\Api\GraphQL\TypeRegistry::item()
TypeRegistry::comment(),
TypeRegistry::item()
],
'resolveType' => function ($content) {
if ( $content instanceof \IPS\Content\Comment )
if ( $content instanceof Comment )
{
return \IPS\Content\Api\GraphQL\TypeRegistry::comment();
return TypeRegistry::comment();
}
elseif( $content instanceof \IPS\Content\Item )
elseif( $content instanceof Item )
{
return \IPS\Content\Api\GraphQL\TypeRegistry::item();
return TypeRegistry::item();
}
return null;
}
];
+84 -78
View File
@@ -10,18 +10,41 @@
namespace IPS\Content\Api\GraphQL;
use \IPS\Api\GraphQL\TypeRegistry;
use DomainException;
use IPS\Api\Exception;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Comment;
use IPS\Content\Item;
use IPS\Content\Search\Index;
use IPS\Content\Search\SearchContent;
use IPS\DateTime;
use IPS\File;
use IPS\IPS;
use IPS\Member;
use IPS\Node\Model;
use IPS\Request;
use IPS\Settings;
use IPS\Text\Parser;
use OutOfRangeException;
use function count;
use function defined;
use function in_array;
use function intval;
use function strlen;
use function strpos;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
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 Base mutator class for Content Items
*/
abstract class _ItemMutator extends ContentMutator
abstract class ItemMutator extends ContentMutator
{
public function args(): array
{
@@ -38,10 +61,10 @@ abstract class _ItemMutator extends ContentMutator
/**
* Mark as read
*
* @param \IPS\Content\Item $item Item to mark as read
* @return void
* @param Item $item Item to mark as read
* @return Item
*/
protected function _markRead( $item )
protected function _markRead( Item $item ): Item
{
$item->markRead();
return $item;
@@ -50,21 +73,21 @@ abstract class _ItemMutator extends ContentMutator
/**
* Create
*
* @param \IPS\Node\Model $container Container
* @param \IPS\Member $author Author
* @param string $postKey Post key
* @return \IPS\Content\Item
* @param array $itemData Item Data
* @param Model|null $container Container
* @param string|null $postKey Post key
* @return Item
*/
protected function _create( $itemData, \IPS\Node\Model $container = NULL, string $postKey = NULL )
protected function _create( array $itemData, Model|null $container = NULL, string|null $postKey = NULL ): Item
{
$class = $this->class;
/* Work out the date */
$date = \IPS\DateTime::create();
$date = DateTime::create();
/* Create item */
$item = $class::createItem( \IPS\Member::loggedIn(), \IPS\Request::i()->ipAddress(), $date, $container );
$this->_createOrUpdate( $item, $itemData, 'add' );
$item = $class::createItem( Member::loggedIn(), Request::i()->ipAddress(), $date, $container );
$this->_createOrUpdate( $item, $itemData );
$item->save();
/* Create post */
@@ -77,19 +100,19 @@ abstract class _ItemMutator extends ContentMutator
{
$this->_addAttachmentsToContent( $postKey, $itemData['content'] );
}
catch ( \DomainException $e )
catch ( DomainException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/1', 403 );
throw new SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/1', 403 );
}
}
$postContents = \IPS\Text\Parser::parseStatic( $itemData['content'], TRUE, md5( $postKey . ':' ), \IPS\Member::loggedIn(), $class::$application . '_' . mb_ucfirst( $class::$module ) );
$postContents = Parser::parseStatic( $itemData['content'], array( md5( $postKey . ':' ) ), Member::loggedIn(), $class::$application . '_' . IPS::mb_ucfirst( $class::$module ) );
$commentClass = $item::$commentClass;
$post = $commentClass::create( $item, $postContents, TRUE, \IPS\Member::loggedIn()->member_id ? NULL : \IPS\Member::loggedIn()->real_name, NULL, \IPS\Member::loggedIn(), $date );
$post = $commentClass::create( $item, $postContents, TRUE, Member::loggedIn()->member_id ? NULL : Member::loggedIn()->real_name, NULL, Member::loggedIn(), $date );
$itemIdColumn = $item::$databaseColumnId;
$postIdColumn = $commentClass::$databaseColumnId;
\IPS\File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $post->$postIdColumn );
File::claimAttachments( "{$postKey}:", $item->$itemIdColumn, $post->$postIdColumn );
if ( isset( $class::$databaseColumnMap['first_comment_id'] ) )
{
@@ -101,13 +124,13 @@ abstract class _ItemMutator extends ContentMutator
}
/* Index */
if ( $item instanceof \IPS\Content\Searchable )
if( SearchContent::isSearchable( $item ) )
{
\IPS\Content\Search\Index::i()->index( $item );
Index::i()->index( $item );
}
/* Mark it as read */
if( $item instanceof \IPS\Content\ReadMarkers )
if( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
{
$item->markRead();
}
@@ -116,7 +139,7 @@ abstract class _ItemMutator extends ContentMutator
if ( !$item->hidden() )
{
$item->sendNotifications();
\IPS\Member::loggedIn()->achievementAction( 'core', 'NewContentItem', $item );
Member::loggedIn()->achievementAction( 'core', 'NewContentItem', $item );
}
elseif( $item->hidden() !== -1 )
{
@@ -130,13 +153,13 @@ abstract class _ItemMutator extends ContentMutator
/**
* Create or update item
*
* @param \IPS\Content\Item $item The item
* @param array $itemData Item data
* @param string $type add or edit
* @param string $postKey Post key
* @return \IPS\Content\Item
* @param Item $item The item
* @param array $itemData Item data
* @param string $type add or edit
* @param string|null $postKey Post key
* @return Item
*/
protected function _createOrUpdate( \IPS\Content\Item $item, array $itemData=array(), $type='add', $postKey = NULL )
protected function _createOrUpdate( Item $item, array $itemData=array(), string $type='add', string|null $postKey = NULL ): Item
{
$class = $this->class;
@@ -148,40 +171,21 @@ abstract class _ItemMutator extends ContentMutator
}
/* Tags */
if ( ( isset( $itemData['prefix'] ) or isset( $itemData['tags'] ) ) and \in_array( 'IPS\Content\Tags', class_implements( \get_class( $item ) ) ) )
if ( ( isset( $itemData['prefix'] ) or isset( $itemData['tags'] ) ) and IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
{
if ( \IPS\Member::loggedIn()->member_id && $item::canTag( NULL, $item->containerWrapper() ) )
{
$source = array();
if( !\IPS\Settings::i()->tags_open_system )
{
/* And get the defined tags */
if( $class::definedTags( $item->containerWrapper() ) )
{
$source = array_map( 'trim', array_unique( array_merge( $source, $class::definedTags( $item->containerWrapper() ) ) ) );
}
}
if ( Member::loggedIn()->member_id && $item::canTag( NULL, $item->containerWrapper() ) )
{
$source = array_map( 'trim', array_unique( $class::definedTags() ) );
/* Filter our provided tags and exclude any that are invalid */
$validTags = array_filter( array_map( 'trim', array_unique( $itemData['tags'] ) ), function($tag) use ($source) {
if( !\IPS\Settings::i()->tags_open_system && !\in_array($tag, $source) )
if( !in_array($tag, $source) )
{
return FALSE;
}
if( \IPS\Settings::i()->tags_len_min && \strlen($tag) < \IPS\Settings::i()->tags_len_min )
{
return FALSE;
}
if( \IPS\Settings::i()->tags_len_max && \strlen($tag) < \IPS\Settings::i()->tags_len_max )
{
return FALSE;
}
if( \strpos( $tag, '#' ) !== FALSE )
if( strpos( $tag, '#' ) !== FALSE )
{
return FALSE;
}
@@ -189,14 +193,14 @@ abstract class _ItemMutator extends ContentMutator
return TRUE;
});
if( \IPS\Settings::i()->tags_min && \count( $validTags ) < \IPS\Settings::i()->tags_min )
if( Settings::i()->tags_min && count( $validTags ) < Settings::i()->tags_min )
{
throw new \IPS\Api\GraphQL\SafeException( 'TOO_FEW_TAGS', 'GQL/0011/1', 400 );
throw new SafeException( 'TOO_FEW_TAGS', 'GQL/0011/1', 400 );
}
if( \IPS\Settings::i()->tags_max && \count( $validTags ) > \IPS\Settings::i()->tags_max )
if( Settings::i()->tags_max && count( $validTags ) > Settings::i()->tags_max )
{
throw new \IPS\Api\GraphQL\SafeException( 'TOO_MANY_TAGS', 'GQL/0011/2', 400 );
throw new SafeException( 'TOO_MANY_TAGS', 'GQL/0011/2', 400 );
}
/* we need to save the item before we set the tags because setTags requires that the item exists */
@@ -211,14 +215,15 @@ abstract class _ItemMutator extends ContentMutator
}
/* Open/closed */
if ( isset( $itemData['state']['locked'] ) and \in_array( 'IPS\Content\Lockable', class_implements( \get_class( $item ) ) ) )
/* @var array $databaseColumnMap */
if ( isset( $itemData['state']['locked'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) )
{
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['locked'] and $item->canLock() ) or ( !$itemData['state']['locked'] and $item->canUnlock() ) )
if ( Member::loggedIn()->member_id && ( $itemData['state']['locked'] and $item->canLock() ) or ( !$itemData['state']['locked'] and $item->canUnlock() ) )
{
if ( isset( $item::$databaseColumnMap['locked'] ) )
{
$lockedColumn = $item::$databaseColumnMap['locked'];
$item->$lockedColumn = \intval( $itemData['state']['locked'] );
$item->$lockedColumn = intval( $itemData['state']['locked'] );
}
else
{
@@ -229,9 +234,9 @@ abstract class _ItemMutator extends ContentMutator
}
/* Hidden */
if ( isset( $itemData['state']['hidden'] ) and \in_array( 'IPS\Content\Hideable', class_implements( \get_class( $item ) ) ) )
if ( isset( $itemData['state']['hidden'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Hideable' ) )
{
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['hidden'] and $item->canHide() ) or ( !$itemData['state']['hidden'] and $item->canUnhide() ) )
if ( Member::loggedIn()->member_id && ( $itemData['state']['hidden'] and $item->canHide() ) or ( !$itemData['state']['hidden'] and $item->canUnhide() ) )
{
$idColumn = $item::$databaseColumnId;
if ( $itemData['state']['hidden'] )
@@ -278,27 +283,27 @@ abstract class _ItemMutator extends ContentMutator
}
/* Pinned */
if ( isset( $itemData['state']['pinned'] ) and \in_array( 'IPS\Content\Pinnable', class_implements( \get_class( $item ) ) ) )
if ( isset( $itemData['state']['pinned'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Pinnable' ) )
{
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['pinned'] and $item->canPin() ) or ( !$itemData['state']['pinned'] and $item->canUnpin() ) )
if ( Member::loggedIn()->member_id && ( $itemData['state']['pinned'] and $item->canPin() ) or ( !$itemData['state']['pinned'] and $item->canUnpin() ) )
{
$pinnedColumn = $item::$databaseColumnMap['pinned'];
$item->$pinnedColumn = \intval( $itemData['state']['pinned'] );
$item->$pinnedColumn = intval( $itemData['state']['pinned'] );
}
}
/* Featured */
if ( isset( $itemData['state']['featured'] ) and \in_array( 'IPS\Content\Featurable', class_implements( \get_class( $item ) ) ) )
if ( isset( $itemData['state']['featured'] ) and IPS::classUsesTrait( $item, 'IPS\Content\Featurable' ) )
{
if ( \IPS\Member::loggedIn()->member_id && ( $itemData['state']['featured'] and $item->canFeature() ) or ( !$itemData['state']['featured'] and $item->canUnfeature() ) )
if ( Member::loggedIn()->member_id && $item->canFeature() )
{
$featuredColumn = $item::$databaseColumnMap['featured'];
$item->$featuredColumn = \intval( $itemData['state']['featured'] );
$item->$featuredColumn = intval( $itemData['state']['featured'] );
}
}
/* Update first comment if required, and it's not a new item */
$field = isset( $item::$databaseColumnMap['first_comment_id'] ) ? $item::$databaseColumnMap['first_comment_id'] : NULL;
$field = $item::$databaseColumnMap['first_comment_id'] ?? NULL;
$commentClass = $item::$commentClass;
$contentField = $commentClass::$databaseColumnMap['content'];
if ( $item::$firstCommentRequired AND isset( $item->$field ) AND isset( $itemData[ $contentField ] ) AND $type == 'edit' )
@@ -310,30 +315,31 @@ abstract class _ItemMutator extends ContentMutator
{
$this->_addAttachmentsToContent( $postKey, $itemData[ $contentField ] );
}
catch ( \DomainException $e )
catch ( DomainException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/2', 403 );
throw new SafeException( 'ATTACHMENTS_TOO_LARGE', '2S401/2', 403 );
}
}
$content = \IPS\Text\Parser::parseStatic( $itemData[ $contentField ], TRUE, array( $item->_id, $item->$field ), \IPS\Member::loggedIn(), $item::$application . '_' . mb_ucfirst( $item::$module ) );
$content = Parser::parseStatic( $itemData[$contentField], array( $item->_id, $item->$field ), Member::loggedIn(), $item::$application . '_' . IPS::mb_ucfirst( $item::$module ) );
try
{
/* @var Comment $commentClass */
$comment = $commentClass::load( $item->$field );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\Exception( 'NO_FIRST_POST', '1S377/1', 400 );
throw new Exception( 'NO_FIRST_POST', '1S377/1', 400 );
}
$comment->$contentField = $content;
$comment->save();
/* Update Search Index of the first item */
if ( $item instanceof \IPS\Content\Searchable )
if( SearchContent::isSearchable( $item ) )
{
\IPS\Content\Search\Index::i()->index( $comment );
Index::i()->index( $comment );
}
}
+152 -104
View File
@@ -9,35 +9,52 @@
*/
namespace IPS\Content\Api\GraphQL;
use BadMethodCallException;
use DateTimeZone;
use Exception;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Comment;
use IPS\Content\Item;
use IPS\DateTime;
use IPS\Db;
use IPS\File;
use IPS\IPS;
use IPS\Http\Url\Friendly;
use IPS\Member;
use IPS\Settings;
use OutOfRangeException;
use function defined;
use function get_class;
use function in_array;
use function is_array;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
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 Base mutator class for Content Items
*/
class _ItemType extends ObjectType
class ItemType extends ObjectType
{
/*
* @brief The item classname we use for this type
*/
protected static $itemClass = '\IPS\Content\Item';
protected static string $itemClass = '\IPS\Content\Item';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'core_Item';
protected static string $typeName = 'core_Item';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A generic content item';
protected static string $typeDescription = 'A generic content item';
public function __construct()
@@ -58,7 +75,7 @@ class _ItemType extends ObjectType
*
* @return array
*/
public function fields()
public function fields(): array
{
return array(
'id' => [
@@ -83,13 +100,13 @@ class _ItemType extends ObjectType
'seoTitle' => [
'type' => TypeRegistry::string(),
'resolve' => function ($item) {
return \IPS\Http\Url\Friendly::seoTitle( $item->mapped('title') );
return Friendly::seoTitle( $item->mapped('title') );
}
],
'views' => [
'type' => TypeRegistry::int(),
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\Views', class_implements( $item ) ) )
if ( in_array( 'IPS\Content\ViewUpdates', class_uses( $item ) ) )
{
return $item->mapped('views');
}
@@ -117,7 +134,7 @@ class _ItemType extends ObjectType
'isLocked' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\Lockable', class_implements( $item ) ) )
if ( IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) )
{
return (bool) $item->locked();
}
@@ -128,7 +145,7 @@ class _ItemType extends ObjectType
'isPinned' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\Pinnable', class_implements( $item ) ) )
if ( IPS::classUsesTrait( $item, 'IPS\Content\Pinnable' ) )
{
return (bool) $item->mapped('pinned');
}
@@ -139,7 +156,7 @@ class _ItemType extends ObjectType
'isFeatured' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\Featurable', class_implements( $item ) ) )
if ( IPS::classUsesTrait( $item, 'IPS\Content\Featurable' ) )
{
return (bool) $item->mapped('featured');
}
@@ -153,7 +170,7 @@ class _ItemType extends ObjectType
'values' => ['HIDDEN', 'PENDING', 'DELETED']
]),
'resolve' => function ($item) {
if ( !\in_array( 'IPS\Content\Hideable', class_implements( $item ) ) )
if ( !IPS::classUsesTrait( $item, 'IPS\Content\Hideable' ) )
{
switch( $item->hidden() ){
case -2:
@@ -166,6 +183,7 @@ class _ItemType extends ObjectType
return NULL;
}
}
return null;
}
],
'updated' => [
@@ -183,7 +201,7 @@ class _ItemType extends ObjectType
'isUnread' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\ReadMarkers', class_implements( $item ) ) )
if ( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
{
return $item->unread();
}
@@ -201,7 +219,7 @@ class _ItemType extends ObjectType
'type' => TypeRegistry::int(),
'description' => 'Returns the position of the comment that is the first unread in this topic',
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\ReadMarkers', class_implements( $item ) ) )
if ( IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) )
{
return static::getUnreadPosition($item);
}
@@ -222,7 +240,8 @@ class _ItemType extends ObjectType
'follow' => [
'type' => TypeRegistry::follow(),
'resolve' => function ($item) {
if( \in_array( 'IPS\Content\Followable', class_implements( $item ) ) && isset( static::$followData ) && \is_array( static::$followData ) ){
if( IPS::classUsesTrait( $item, 'IPS\Content\Followable' ) && isset( static::$followData ) && is_array( static::$followData ) )
{
$idColumn = static::getIdColumn($item);
return array_merge( static::$followData, array(
'id' => $item->$idColumn,
@@ -237,7 +256,7 @@ class _ItemType extends ObjectType
'tags' => [
'type' => TypeRegistry::listOf( \IPS\core\api\GraphQL\TypeRegistry::tag() ),
'resolve' => function ($item) {
if ( \in_array( 'IPS\Content\Tags', class_implements( $item ) ) )
if ( IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
{
return static::tags($item);
}
@@ -273,13 +292,13 @@ class _ItemType extends ObjectType
'hasPoll' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($item) {
return $item instanceof \IPS\Content\Polls && $item->poll_state;
return IPS::classUsesTrait( $item, 'IPS\Content\Polls' ) AND $item->poll_state;
}
],
'poll' => [
'type' => \IPS\core\api\GraphQL\TypeRegistry::poll(),
'resolve' => function ($item) {
if( $item instanceof \IPS\Content\Polls && $item->poll_state )
if( IPS::classUsesTrait( $item, 'IPS\Content\Polls' ) AND $item->poll_state )
{
return $item->getPoll();
}
@@ -290,7 +309,9 @@ class _ItemType extends ObjectType
'firstCommentRequired' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($item) {
return static::$itemClass::$firstCommentRequired;
/* @var Item $className */
$className = get_class( $item );
return $className::$firstCommentRequired;
}
],
'articleLang' => [
@@ -309,17 +330,19 @@ class _ItemType extends ObjectType
]
],
'resolveField' => function ($item, $args, $context, $info) {
$className = \get_class( $item );
/* @var Item $className */
$className = get_class( $item );
switch( $info->fieldName )
{
case 'indefinite':
return $className::_indefiniteArticle( NULL );
break;
return $className::_indefiniteArticle();
case 'definite':
return $className::_definiteArticle( NULL, NULL, $args['uppercase'] ? array( 'ucfirst' => TRUE ) : array() );
break;
}
return '';
}
]),
'resolve' => function ($item) {
@@ -376,11 +399,12 @@ class _ItemType extends ObjectType
/**
* Return the ID column for the provided item
*
* @return array
* @param Item $item
* @return string
*/
protected function getIdColumn($item)
protected function getIdColumn(Item $item): string
{
$className = \get_class( $item );
$className = get_class( $item );
return $className::$databaseColumnId;
}
@@ -389,7 +413,7 @@ class _ItemType extends ObjectType
*
* @return array
*/
public static function args()
public static function args(): array
{
return array(
'offset' => [
@@ -428,7 +452,7 @@ class _ItemType extends ObjectType
*
* @return array
*/
public static function getOrderByOptions()
public static function getOrderByOptions(): array
{
return array('title', 'author_name', 'last_comment_name');
}
@@ -438,42 +462,42 @@ class _ItemType extends ObjectType
*
* @return array
*/
public static function getItemPermissionFields()
public static function getItemPermissionFields(): array
{
return array(
'canComment' => [
'type' => TypeRegistry::boolean(),
'description' => 'Can the logged in user comment on this item?',
'resolve' => function ($item, $args, $context) {
return $item->canComment( \IPS\Member::loggedIn(), FALSE );
return $item->canComment( Member::loggedIn(), FALSE );
}
],
'commentInformation' => [
'type' => TypeRegistry::string(),
'description' => 'A message providing some information about the comment form availability',
'resolve' => function ($item, $args, $context) {
if( $item->canComment( \IPS\Member::loggedIn(), FALSE ) )
if( $item->canComment( Member::loggedIn(), FALSE ) )
{
if( $item instanceof \IPS\Content\Lockable && $item->locked() )
if( IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) AND $item->locked() )
{
return 'locked_can_comment';
}
}
else
{
if( $item instanceof \IPS\Content\Lockable && $item->locked() )
if( IPS::classUsesTrait( $item, 'IPS\Content\Lockable' ) AND $item->locked() )
{
return 'locked_cannot_comment';
}
elseif( \IPS\Member::loggedIn()->restrict_post )
elseif( Member::loggedIn()->restrict_post )
{
return 'restricted_cannot_comment';
}
elseif( \IPS\Member::loggedIn()->members_bitoptions['unacknowledged_warnings'] )
elseif( Member::loggedIn()->members_bitoptions['unacknowledged_warnings'] )
{
return 'unacknowledged_warning_cannot_post';
}
elseif( !\IPS\Member::loggedIn()->checkPostsPerDay() )
elseif( !Member::loggedIn()->checkPostsPerDay() )
{
return 'member_exceeded_posts_per_day';
}
@@ -485,11 +509,11 @@ class _ItemType extends ObjectType
'canCommentIfSignedIn' => [
'type' => TypeRegistry::boolean(),
'description' => 'Returns boolean indicating whether a guest who signs in would be able to comment on this item. Returns NULL if already signed in.',
'resolve' => function ($item, $args, $context) {
if ( !\IPS\Member::loggedIn()->member_id )
'resolve' => function ($item) {
if ( !Member::loggedIn()->member_id )
{
$testUser = new \IPS\Member;
$testUser->member_group_id = \IPS\Settings::i()->member_group;
$testUser = new Member;
$testUser->member_group_id = Settings::i()->member_group;
return $item->canComment( $testUser, FALSE );
}
@@ -501,21 +525,21 @@ class _ItemType extends ObjectType
'type' => TypeRegistry::boolean(),
'description' => 'Boolean indicating whether this item supports read markers, and if so, if the user can mark as read',
'resolve' => function ($item) {
return $item instanceof \IPS\Content\ReadMarkers && \IPS\Member::loggedIn()->member_id;
return IPS::classUsesTrait( $item, 'IPS\Content\ReadMarkers' ) && Member::loggedIn()->member_id;
}
],
'canReport' => [
'type' => TypeRegistry::boolean(),
'description' => 'Can the user report this item?',
'resolve' => function ($item) {
return $item->canReport( \IPS\Member::loggedIn() ) === TRUE;
return $item->canReport( Member::loggedIn() ) === TRUE;
}
],
'canReportOrRevoke' => [
'type' => TypeRegistry::boolean(),
'description' => 'Can the user report (or revoke a report) on this item?',
'resolve' => function ($item) {
return $item->canReportOrRevoke( \IPS\Member::loggedIn() ) === TRUE;
return $item->canReportOrRevoke( Member::loggedIn() ) === TRUE;
}
],
'canShare' => [
@@ -533,7 +557,7 @@ class _ItemType extends ObjectType
*
* @return ObjectType
*/
protected static function getCommentType()
protected static function getCommentType(): ObjectType
{
return \IPS\Content\Api\GraphQL\TypeRegistry::comment();
}
@@ -541,25 +565,27 @@ class _ItemType extends ObjectType
/**
* Return content images for the provided item
*
* @return array|null
* @param Item $item
* @return array|null
*/
protected static function contentImages($item)
protected static function contentImages(Item $item): array|NULL
{
try
{
if ( $images = $item->contentImages( 20 ) )
{
$toReturn = [];
foreach( $images as $image )
{
foreach( $image as $extension => $file )
{
$toReturn[] = (string) \IPS\File::get( $extension, $file )->url;
$toReturn[] = (string) File::get( $extension, $file )->url;
}
}
return $toReturn;
}
}
catch( \BadMethodCallException $e ) { }
catch( BadMethodCallException $e ) { }
return NULL;
}
@@ -567,20 +593,25 @@ class _ItemType extends ObjectType
/**
* Resolve the findCommentPosition field
*
* @param \IPS\forums\Topic
* @param array Arguments passed to this resolver
* @return array
* @param Item $item
* @param array $args Arguments passed to this resolver
* @return int|NULL
*/
protected static function findCommentPosition($item, $args)
protected static function findCommentPosition(Item $item, array $args): int|NULL
{
if( $args['findComment'] === NULL )
{
return NULL;
}
/* @var Item $itemClass */
$itemClass = get_class( $item );
try
{
$comment = static::$itemClass::$commentClass::load( $args['findComment'] );
/* @var Comment $commentClass */
$commentClass = $itemClass::$commentClass;
$comment = $commentClass::load( $args['findComment'] );
// Check this comment belongs to this topic
if( $comment->item() !== $item )
@@ -588,7 +619,7 @@ class _ItemType extends ObjectType
return NULL;
}
}
catch (\Exception $e)
catch (Exception $e)
{
return NULL;
}
@@ -599,12 +630,15 @@ class _ItemType extends ObjectType
/**
* Resolve the comments field
*
* @param \IPS\forums\Topic
* @param array Arguments passed to this resolver
* @return array
* @param Item $item
* @param array $args Arguments passed to this resolver
* @return array
*/
protected static function comments($item, $args)
protected static function comments(Item $item, array $args): array
{
/* @var Item $itemClass */
$itemClass = get_class( $item );
$offset = 0;
$limit = 25;
@@ -621,7 +655,7 @@ class _ItemType extends ObjectType
case 'ID':
if( !isset( $args['findComment'] ) )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
$offset = static::getCommentPosition($item, $args['findComment']) + $args['offsetAdjust'];
@@ -637,7 +671,10 @@ class _ItemType extends ObjectType
if( $args['orderBy'] == 'DATE' )
{
$args['orderBy'] = static::$itemClass::$commentClass::$databaseColumnMap['date'];
/* @var Comment $commentClass */
/* @var array $databaseColumnMap */
$commentClass = $itemClass::$commentClass;
$args['orderBy'] = $commentClass::$databaseColumnMap['date'];
}
@@ -653,27 +690,35 @@ class _ItemType extends ObjectType
/**
* Get the position of a specific comment
*
* @param \IPS\Content\Item
* @return int
* @param Item $item
* @param int $commentID
* @return int|null
*/
protected static function getCommentPosition($item, $commentID)
protected static function getCommentPosition(Item $item, int $commentID): ?int
{
$itemClass = get_class( $item );
/* @var Comment $commentClass */
$commentClass = $itemClass::$commentClass;
try
{
$comment = static::$itemClass::$commentClass::load($commentID);
$comment = $commentClass::load($commentID);
return static::findComment($comment, $item);
}
catch(\Exception $error)
catch(Exception $error)
{}
return null;
}
/**
* Get the position of the last comment
*
* @param \IPS\Content\Item
* @param Item $item
* @return int
*/
protected static function getEndPosition($item)
protected static function getEndPosition(Item $item): int
{
$comment = $item->comments( 1, NULL, 'date', 'desc' );
return static::findComment($comment, $item);
@@ -682,20 +727,21 @@ class _ItemType extends ObjectType
/**
* Get the position of the first unread comment
*
* @param \IPS\Content\Item
* @param Item $item
* @return int
*/
protected static function getUnreadPosition($item)
protected static function getUnreadPosition(Item $item): int
{
try
{
{
/* @var Item $class */
$class = static::$itemClass;
$timeLastRead = $item->timeLastRead();
if ( $timeLastRead instanceof \IPS\DateTime )
if ( $timeLastRead instanceof DateTime )
{
$comment = NULL;
if( \IPS\DateTime::ts( $item->mapped('date') ) < $timeLastRead )
if( DateTime::ts( $item->mapped('date') ) < $timeLastRead )
{
$comment = $item->comments( 1, NULL, 'date', 'asc', NULL, NULL, $timeLastRead );
}
@@ -724,11 +770,11 @@ class _ItemType extends ObjectType
if ( $item->unread() )
{
/* If we do not have a time last read set for this content, fallback to the reset time */
$resetTimes = \IPS\Member::loggedIn()->markersResetTimes( $class::$application );
$resetTimes = Member::loggedIn()->markersResetTimes( $class::$application );
if ( array_key_exists( $item->container()->_id, $resetTimes ) and $item->mapped('date') < $resetTimes[ $item->container()->_id ] )
{
$comment = $item->comments( 1, NULL, 'date', 'asc', NULL, NULL, \IPS\DateTime::ts( $resetTimes[ $item->container()->_id ] ) );
$comment = $item->comments( 1, NULL, 'date', 'asc', NULL, NULL, DateTime::ts( $resetTimes[ $item->container()->_id ] ) );
if ( !$comment || $class::$firstCommentRequired and $comment->isFirst() )
{
@@ -748,7 +794,7 @@ class _ItemType extends ObjectType
return static::findComment($comment, $item);
}
catch( \Exception $e )
catch( Exception $e )
{
return 0;
}
@@ -757,20 +803,21 @@ class _ItemType extends ObjectType
/**
* Find the position of a comment
*
* @param \IPS\Content\Comment
* @param \IPS\Content\Item
* @param Comment $comment
* @param Item $item
* @return int
*/
protected static function findComment($comment, $item)
protected static function findComment(Comment $comment, Item $item): int
{
try
{
$commentClass = \get_class( $comment );
/* @var array $databaseColumnMap */
$commentClass = get_class( $comment );
$idColumn = $commentClass::$databaseColumnId;
$itemColumn = $commentClass::$databaseColumnMap['item'];
/* Work out where the comment is in the item */
$directional = ( \in_array( 'IPS\Content\Review', class_parents( $commentClass ) ) ) ? '>=?' : '<=?';
$directional = ( in_array( 'IPS\Content\Review', class_parents( $commentClass ) ) ) ? '>=?' : '<=?';
$where = array(
array( $commentClass::$databasePrefix . $itemColumn . '=?', $comment->$itemColumn ),
array( $commentClass::$databasePrefix . $idColumn . $directional, $comment->$idColumn )
@@ -804,15 +851,17 @@ class _ItemType extends ObjectType
}
}
}
$commentPosition = \IPS\Db::i()->select( 'COUNT(*) AS position', $commentClass::$databaseTable, $where )->first();
$commentPosition = Db::i()->select( 'COUNT(*) AS position', $commentClass::$databaseTable, $where )->first();
if( static::$itemClass::$firstCommentRequired ){
/* @var Item $itemClass */
$itemClass = get_class( $item );
if( $itemClass::$firstCommentRequired ){
$commentPosition = $commentPosition - 1;
}
return $commentPosition;
}
catch( \Exception $e )
catch( Exception $e )
{
return 0;
}
@@ -821,15 +870,15 @@ class _ItemType extends ObjectType
/**
* Resolve the tags field
*
* @param \IPS\Content\Item
* @return int
* @param Item $item
* @return int|null
*/
protected static function timeLastRead($item)
protected static function timeLastRead(Item $item): ?int
{
$time = $item->timeLastRead();
if( $time instanceof \IPS\DateTime )
if( $time instanceof DateTime )
{
return (int) $time->setTimezone( new \DateTimeZone( "UTC" ) )->format('U');
return (int) $time->setTimezone( new DateTimeZone( "UTC" ) )->format('U');
}
return NULL;
}
@@ -837,23 +886,22 @@ class _ItemType extends ObjectType
/**
* Resolve the tags field
*
* @param \IPS\Content\Item
* @param Item $item
* @return array
*/
protected static function tags($item)
protected static function tags(Item $item): array
{
$tags = $item->tags();
return \is_array( $tags ) ? $tags : array();
return $item->tags();
}
/**
* Resolve the author field
*
* @param null
* @param array Arguments passed to this resolver
* @return \IPS\forums\Forum
* @param Item $item
* @param array $args Arguments passed to this resolver
* @return Member
*/
protected static function author($item, $args)
protected static function author(Item $item, array $args): Member
{
return $item->author();
}
@@ -861,11 +909,11 @@ class _ItemType extends ObjectType
/**
* Resolve the last comment author field
*
* @param \IPS\forums\Topic
* @param array Arguments passed to this resolver
* @return \IPS\Member
* @param Item $item
* @param array $args Arguments passed to this resolver
* @return Member
*/
protected static function lastCommentAuthor($item, $args)
protected static function lastCommentAuthor(Item $item, array $args): Member
{
if( $item->mapped('num_comments') )
{
@@ -878,11 +926,11 @@ class _ItemType extends ObjectType
/**
* Resolve the last comment date field
*
* @param \IPS\forums\Topic
* @param array Arguments passed to this resolver
* @param Item $item
* @param array $args Arguments passed to this resolver
* @return string
*/
protected static function lastCommentDate($item, $args)
protected static function lastCommentDate(Item $item, array $args): string
{
if( $item->mapped('last_comment') )
{
+13 -13
View File
@@ -10,23 +10,23 @@
*/
namespace IPS\Content\Api\GraphQL;
use GraphQL\Type\Definition\Type;
use function defined;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
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;
}
/**
* \IPS\Content base types
*/
class _TypeRegistry
class TypeRegistry
{
protected static $comment;
protected static $content;
protected static $item;
protected static CommentType $comment;
protected static ContentType $content;
protected static ItemType $item;
/**
* Constructor
@@ -39,24 +39,24 @@ class _TypeRegistry
/**
* @return CommentType
*/
public static function comment()
public static function comment(): CommentType
{
return self::$comment ?: (self::$comment = new \IPS\Content\Api\GraphQL\CommentType());
return self::$comment ?? (self::$comment = new CommentType());
}
/**
* @return ItemType
*/
public static function item()
public static function item(): ItemType
{
return self::$item ?: (self::$item = new \IPS\Content\Api\GraphQL\ItemType());
return self::$item ?? (self::$item = new ItemType());
}
/**
* @return ContentType
*/
public static function content()
public static function content(): ContentType
{
return self::$content ?: (self::$content = new \IPS\Content\Api\GraphQL\ContentType());
return self::$content ?? (self::$content = new ContentType());
}
}