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

+29 -18
View File
@@ -10,41 +10,52 @@
*/
namespace IPS\forums\api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\Types;
use IPS\forums\api\GraphQL\Mutations\CreateTopic;
use IPS\forums\api\GraphQL\Mutations\MarkForumRead;
use IPS\forums\api\GraphQL\Mutations\MarkTopicRead;
use IPS\forums\api\GraphQL\Mutations\MarkTopicSolved;
use IPS\forums\api\GraphQL\Mutations\PostReaction;
use IPS\forums\api\GraphQL\Mutations\ReplyTopic;
use IPS\forums\api\GraphQL\Mutations\ReportPost;
use IPS\forums\api\GraphQL\Mutations\RevokePostReport;
use IPS\forums\api\GraphQL\Mutations\SetBestAnswer;
use IPS\forums\api\GraphQL\Mutations\VoteAnswer;
use IPS\forums\api\GraphQL\Mutations\VoteInPoll;
use IPS\forums\api\GraphQL\Mutations\VoteQuestion;
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;
}
/**
* Forums mutationss GraphQL API
*/
abstract class _Mutation
abstract class Mutation
{
/**
* Get the supported query types in this app
*
* @return array
*/
public static function mutations()
public static function mutations() : array
{
return [
'createTopic' => new \IPS\forums\api\GraphQL\Mutations\CreateTopic(),
'replyTopic' => new \IPS\forums\api\GraphQL\Mutations\ReplyTopic(),
'postReaction' => new \IPS\forums\api\GraphQL\Mutations\PostReaction(),
'markForumRead' => new \IPS\forums\api\GraphQL\Mutations\MarkForumRead(),
'markTopicRead' => new \IPS\forums\api\GraphQL\Mutations\MarkTopicRead(),
'markTopicSolved' => new \IPS\forums\api\GraphQL\Mutations\MarkTopicSolved(),
'voteInPoll' => new \IPS\forums\api\GraphQL\Mutations\VoteInPoll(),
'voteQuestion' => new \IPS\forums\api\GraphQL\Mutations\VoteQuestion(),
'voteAnswer' => new \IPS\forums\api\GraphQL\Mutations\VoteAnswer(),
'setBestAnswer' => new \IPS\forums\api\GraphQL\Mutations\SetBestAnswer(),
'reportPost' => new \IPS\forums\api\GraphQL\Mutations\ReportPost(),
'revokePostReport' => new \IPS\forums\api\GraphQL\Mutations\RevokePostReport(),
'createTopic' => new CreateTopic(),
'replyTopic' => new ReplyTopic(),
'postReaction' => new PostReaction(),
'markForumRead' => new MarkForumRead(),
'markTopicRead' => new MarkTopicRead(),
'markTopicSolved' => new MarkTopicSolved(),
'voteInPoll' => new VoteInPoll(),
'voteQuestion' => new VoteQuestion(),
'voteAnswer' => new VoteAnswer(),
'setBestAnswer' => new SetBestAnswer(),
'reportPost' => new ReportPost(),
'revokePostReport' => new RevokePostReport(),
];
}
}
@@ -10,30 +10,38 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\ItemMutator;
use IPS\Content\Item;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Forum;
use IPS\forums\Topic;
use IPS\Member;
use OutOfRangeException;
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;
}
/**
* Create topic mutation for GraphQL API
*/
class _CreateTopic extends \IPS\Content\Api\GraphQL\ItemMutator
class CreateTopic extends ItemMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic';
protected string $class = 'IPS\forums\Topic';
/*
* @brief Query description
*/
public static $description = "Create a new topic";
public static string $description = "Create a new topic";
/*
* Mutation arguments
@@ -52,51 +60,52 @@ class _CreateTopic extends \IPS\Content\Api\GraphQL\ItemMutator
/**
* Return the mutation return type
*
* @return TopicType
*/
public function type()
public function type() : TopicType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this mutation
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return Topic
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : Item
{
/* Get forum */
try
{
$forum = \IPS\forums\Forum::loadAndCheckPerms( $args['forumID'] );
$forum = Forum::loadAndCheckPerms( $args['forumID'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_FORUM', '1F294/2_graphl', 400 );
throw new SafeException( 'NO_FORUM', '1F294/2_graphl', 400 );
}
/* Check permission */
if ( !$forum->can( 'add', \IPS\Member::loggedIn() ) )
if ( !$forum->can( 'add', Member::loggedIn() ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', '2F294/9_graphl', 403 );
throw new SafeException( 'NO_PERMISSION', '2F294/9_graphl', 403 );
}
/* Check we have a title and a post */
if ( !$args['title'] )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TITLE', '1F294/5_graphl', 400 );
throw new SafeException( 'NO_TITLE', '1F294/5_graphl', 400 );
}
if ( !$args['content'] )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F294/4_graphl', 400 );
throw new SafeException( 'NO_POST', '1F294/4_graphl', 400 );
}
$item = $this->_create( $args, $forum, $args['postKey'] ?? NULL );
return $item;
return $this->_create( $args, $forum, $args['postKey'] ?? NULL );
}
}
@@ -10,25 +10,31 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\ForumType;
use IPS\forums\Forum;
use IPS\Node\Api\GraphQL\NodeMutator;
use OutOfRangeException;
use function defined;
use function intval;
/* 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;
}
/**
* Mark forum read mutation for GraphQL API
*/
class _MarkForumRead extends \IPS\Node\Api\GraphQL\NodeMutator
class MarkForumRead extends NodeMutator
{
/*
* @brief Query description
*/
public static $description = "Mark a forum as read";
public static string $description = "Mark a forum as read";
/*
* Mutation arguments
@@ -42,8 +48,10 @@ class _MarkForumRead extends \IPS\Node\Api\GraphQL\NodeMutator
/**
* Return the mutation return type
*
* @return ForumType
*/
public function type()
public function type() : ForumType
{
return \IPS\forums\api\GraphQL\TypeRegistry::forum();
}
@@ -51,20 +59,19 @@ class _MarkForumRead extends \IPS\Node\Api\GraphQL\NodeMutator
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Forum
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Forum
{
try
{
$forum = \IPS\forums\Forum::loadAndCheckPerms( \intval( $args['id'] ) );
$forum = Forum::loadAndCheckPerms( intval( $args['id'] ) );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_NODE', 'GQL/0004/2', 403 );
throw new SafeException( 'INVALID_NODE', 'GQL/0004/2', 403 );
}
$this->_markRead( $forum );
@@ -10,25 +10,30 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\ItemMutator;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Topic;
use OutOfRangeException;
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;
}
/**
* Mark topic read mutation for GraphQL API
*/
class _MarkTopicRead extends \IPS\Content\Api\GraphQL\ItemMutator
class MarkTopicRead extends ItemMutator
{
/*
* @brief Query description
*/
public static $description = "Mark a topic as read";
public static string $description = "Mark a topic as read";
/*
* Mutation arguments
@@ -42,8 +47,10 @@ class _MarkTopicRead extends \IPS\Content\Api\GraphQL\ItemMutator
/**
* Return the mutation return type
*
* @reutrn TopicType
*/
public function type()
public function type() : TopicType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -51,25 +58,24 @@ class _MarkTopicRead extends \IPS\Content\Api\GraphQL\ItemMutator
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Topic
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Topic
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['id'] );
$topic = Topic::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', 'GQL/0005/1', 400 );
throw new SafeException( 'NO_TOPIC', 'GQL/0005/1', 400 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', 'GQL/0005/2', 403 );
throw new SafeException( 'INVALID_ID', 'GQL/0005/2', 403 );
}
$this->_markRead( $topic );
@@ -10,25 +10,35 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Db;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Topic;
use IPS\forums\Topic\Post;
use IPS\Member;
use IPS\Node\Api\GraphQL\NodeMutator;
use IPS\Request;
use OutOfRangeException;
use function defined;
use function intval;
/* 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;
}
/**
* Mark topic solved mutation for GraphQL API
*/
class _MarkTopicSolved extends \IPS\Node\Api\GraphQL\NodeMutator
class MarkTopicSolved extends NodeMutator
{
/*
* @brief Query description
*/
public static $description = "Mark a topic solved";
public static string $description = "Mark a topic solved";
/*
* Mutation arguments
@@ -47,8 +57,10 @@ class _MarkTopicSolved extends \IPS\Node\Api\GraphQL\NodeMutator
/**
* Return the mutation return type
*
* @return TopicType
*/
public function type()
public function type() : TopicType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -56,34 +68,34 @@ class _MarkTopicSolved extends \IPS\Node\Api\GraphQL\NodeMutator
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @return \IPS\forums\Topic
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Topic
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Topic
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( \intval( $args['id'] ) );
$topic = Topic::loadAndCheckPerms( intval( $args['id'] ) );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', 'GQL', 403 );
throw new SafeException( 'NO_TOPIC', 'GQL', 403 );
}
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( \intval( $args['answer'] ) );
$post = Post::loadAndCheckPerms( intval( $args['answer'] ) );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', 'GQL', 403 );
throw new SafeException( 'NO_POST', 'GQL', 403 );
}
/* This is "solved" mode */
if ( !$topic->canSolve() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', 'GQL', 403 );
throw new SafeException( 'NO_PERMISSION', 'GQL', 403 );
}
@@ -96,12 +108,12 @@ class _MarkTopicSolved extends \IPS\Node\Api\GraphQL\NodeMutator
$topic->toggleSolveComment( $post->pid, FALSE );
}
\IPS\Db::i()->insert( 'core_moderator_logs', array(
'member_id' => \IPS\Member::loggedIn()->member_id,
'member_name' => \IPS\Member::loggedIn()->name,
Db::i()->insert( 'core_moderator_logs', array(
'member_id' => Member::loggedIn()->member_id,
'member_name' => Member::loggedIn()->name,
'ctime' => time(),
'note' => json_encode( array( $post->pid => $args['solved'] ) ),
'ip_address' => \IPS\Request::i()->ipAddress(),
'ip_address' => Request::i()->ipAddress(),
'appcomponent' => 'forums',
'module' => 'forums',
'controller' => 'topic',
@@ -10,30 +10,35 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\CommentMutator;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic\Post;
use OutOfRangeException;
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;
}
/**
* React to post mutation for GraphQL API
*/
class _PostReaction extends \IPS\Content\Api\GraphQL\CommentMutator
class PostReaction extends CommentMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic\Post';
protected string $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "React to a post";
public static string $description = "React to a post";
/*
* Mutation arguments
@@ -49,30 +54,33 @@ class _PostReaction extends \IPS\Content\Api\GraphQL\CommentMutator
/**
* Return the mutation return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return Post
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : Post
{
/* Get topic */
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['postID'] );
$post = Post::loadAndCheckPerms( $args['postID'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/1_graphl', 403 );
throw new SafeException( 'NO_POST', '1F295/1_graphl', 403 );
}
/* Do it */
@@ -10,30 +10,38 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\CommentMutator;
use IPS\Content\Comment;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic;
use IPS\forums\Topic\Post;
use IPS\Member;
use OutOfRangeException;
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;
}
/**
* Reply to topic mutation for GraphQL API
*/
class _ReplyTopic extends \IPS\Content\Api\GraphQL\CommentMutator
class ReplyTopic extends CommentMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic\Post';
protected string $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "Create a new post";
public static string $description = "Create a new post";
/*
* Mutation arguments
@@ -50,42 +58,45 @@ class _ReplyTopic extends \IPS\Content\Api\GraphQL\CommentMutator
/**
* Return the mutation return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return Post
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : Comment
{
/* Get topic */
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['topicID'] );
$topic = Topic::loadAndCheckPerms( $args['topicID'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', '1F295/1_graphl', 403 );
throw new SafeException( 'NO_TOPIC', '1F295/1_graphl', 403 );
}
/* Get author */
if ( !$topic->canComment( \IPS\Member::loggedIn() ) )
if ( !$topic->canComment( Member::loggedIn() ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', '2F294/A_graphl', 403 );
throw new SafeException( 'NO_PERMISSION', '2F294/A_graphl', 403 );
}
/* Check we have a post */
if ( !$args['content'] )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/3_graphl', 403 );
throw new SafeException( 'NO_POST', '1F295/3_graphl', 403 );
}
$originalPost = NULL;
@@ -94,9 +105,9 @@ class _ReplyTopic extends \IPS\Content\Api\GraphQL\CommentMutator
{
try
{
$originalPost = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['replyingTo'] );
$originalPost = Post::loadAndCheckPerms( $args['replyingTo'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
// Just ignore it
}
@@ -10,30 +10,36 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\CommentMutator;
use IPS\Content\Comment;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic\Post;
use OutOfRangeException;
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;
}
/**
* Report post mutation for GraphQL API
*/
class _ReportPost extends \IPS\Content\Api\GraphQL\CommentMutator
class ReportPost extends CommentMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic\Post';
protected string $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "Report a post";
public static string $description = "Report a post";
/*
* Mutation arguments
@@ -52,30 +58,33 @@ class _ReportPost extends \IPS\Content\Api\GraphQL\CommentMutator
/**
* Return the mutation return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return Post
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : Comment
{
/* Get post */
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$post = Post::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/1_graphl', 403 );
throw new SafeException( 'NO_POST', '1F295/1_graphl', 403 );
}
/* Do it */
@@ -10,30 +10,36 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\CommentMutator;
use IPS\Content\Comment;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic\Post;
use OutOfRangeException;
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;
}
/**
* Revoke post report mutation for GraphQL API
*/
class _RevokePostReport extends \IPS\Content\Api\GraphQL\CommentMutator
class RevokePostReport extends CommentMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic\Post';
protected string $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "Report a post";
public static string $description = "Report a post";
/*
* Mutation arguments
@@ -48,30 +54,33 @@ class _RevokePostReport extends \IPS\Content\Api\GraphQL\CommentMutator
/**
* Return the mutation return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return Post
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : Comment
{
/* Get post */
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$post = Post::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/1_graphl', 403 );
throw new SafeException( 'NO_POST', '1F295/1_graphl', 403 );
}
/* Do it */
@@ -10,25 +10,30 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use Exception;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic\Post;
use OutOfRangeException;
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;
}
/**
* Set a post as best answer mutation for GraphQL API
*/
class _SetBestAnswer
class SetBestAnswer
{
/*
* @brief Query description
*/
public static $description = "Set a post as best answer";
public static string $description = "Set a post as best answer";
/*
* Mutation arguments
@@ -42,8 +47,10 @@ class _SetBestAnswer
/**
* Return the mutation return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
@@ -51,36 +58,30 @@ class _SetBestAnswer
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Post
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Post
{
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$post = Post::loadAndCheckPerms( $args['id'] );
$topic = $post->item();
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', 'GQL/0010/1', 403 );
throw new SafeException( 'NO_POST', 'GQL/0010/1', 403 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', 'GQL/0010/2', 403 );
}
if( !$topic->isQuestion() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NON_QUESTION', 'GQL/0010/3', 403 );
throw new SafeException( 'INVALID_ID', 'GQL/0010/2', 403 );
}
if( !$topic->canSetBestAnswer() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', 'GQL/0010/4', 403 );
throw new SafeException( 'NO_PERMISSION', 'GQL/0010/4', 403 );
}
// Do we have an existing best answer
@@ -88,11 +89,11 @@ class _SetBestAnswer
{
try
{
$oldBestAnswer = \IPS\forums\Topic\Post::load( $topic->topic_answered_pid );
$oldBestAnswer = Post::load( $topic->topic_answered_pid );
$oldBestAnswer->post_bwoptions['best_answer'] = FALSE;
$oldBestAnswer->save();
}
catch ( \Exception $e ) {}
catch ( Exception $e ) {}
}
$post->post_bwoptions['best_answer'] = TRUE;
@@ -10,25 +10,32 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Db;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic\Post;
use IPS\Member;
use OutOfRangeException;
use UnderflowException;
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;
}
/**
* Vote on an answer mutation for GraphQL API
*/
class _VoteAnswer
class VoteAnswer
{
/*
* @brief Query description
*/
public static $description = "Vote on an answer";
public static string $description = "Vote on an answer";
/*
* Mutation arguments
@@ -43,8 +50,10 @@ class _VoteAnswer
/**
* Return the mutation return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
@@ -52,36 +61,30 @@ class _VoteAnswer
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Post
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Post
{
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$post = Post::loadAndCheckPerms( $args['id'] );
$topic = $post->item();
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', 'GQL/0009/1', 403 );
throw new SafeException( 'NO_POST', 'GQL/0009/1', 403 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', 'GQL/0009/2', 403 );
}
if( !$topic->isQuestion() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NON_QUESTION', 'GQL/0009/3', 403 );
throw new SafeException( 'INVALID_ID', 'GQL/0009/2', 403 );
}
if( !$post->canVote() )
{
throw new \IPS\Api\GraphQL\SafeException( 'CANNOT_VOTE', 'GQL/0009/4', 403 );
throw new SafeException( 'CANNOT_VOTE', 'GQL/0009/4', 403 );
}
$rating = $args['vote'] == 'UP' ? 1 : -1;
@@ -89,20 +92,19 @@ class _VoteAnswer
/* Have we already rated ? */
try
{
\IPS\Db::i()->delete( 'forums_answer_ratings', array( 'topic=? AND post=? AND `member`=?', $topic->tid, $post->pid, \IPS\Member::loggedIn()->member_id ) );
Db::i()->delete( 'forums_answer_ratings', array( 'topic=? AND post=? AND `member`=?', $topic->tid, $post->pid, Member::loggedIn()->member_id ) );
}
catch ( \UnderflowException $e ){}
catch ( UnderflowException $e ){}
\IPS\Db::i()->insert( 'forums_answer_ratings', array(
Db::i()->insert( 'forums_answer_ratings', array(
'post' => $post->pid,
'topic' => $topic->tid,
'member' => \IPS\Member::loggedIn()->member_id,
'member' => Member::loggedIn()->member_id,
'rating' => $rating,
'date' => time()
), TRUE );
/* @note SELECT_FROM_WRITE_SERVER race condition */
$post->post_field_int = (int) \IPS\Db::i()->select( 'SUM(rating)', 'forums_answer_ratings', array( 'post=?', $post->pid ), NULL, NULL, NULL, NULL, \IPS\Db::SELECT_FROM_WRITE_SERVER )->first();
$post->post_field_int = (int) Db::i()->select( 'SUM(rating)', 'forums_answer_ratings', array( 'post=?', $post->pid ), NULL, NULL, NULL, NULL, Db::SELECT_FROM_WRITE_SERVER )->first();
$post->save();
return $post;
@@ -10,31 +10,36 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\InputObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Topic;
use IPS\Poll\Api\GraphQL\PollMutator;
use OutOfRangeException;
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;
}
/**
* Vote in poll mutation for GraphQL API
*/
class _VoteInPoll extends \IPS\Poll\Api\GraphQL\PollMutator
class VoteInPoll extends PollMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic';
protected string $class = 'IPS\forums\Topic';
/*
* @brief Query description
*/
public static $description = "Vote in a poll in a topic";
public static string $description = "Vote in a poll in a topic";
/*
* Mutation arguments
@@ -57,8 +62,10 @@ class _VoteInPoll extends \IPS\Poll\Api\GraphQL\PollMutator
/**
* Return the mutation return type
*
* @return TopicType
*/
public function type()
public function type() : TopicType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -66,27 +73,26 @@ class _VoteInPoll extends \IPS\Poll\Api\GraphQL\PollMutator
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Topic
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Topic
{
/* Get topic */
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['itemID'] );
$topic = Topic::loadAndCheckPerms( $args['itemID'] );
$poll = $topic->getPoll();
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_TOPIC', 'GQL/0006/1', 403 );
throw new SafeException( 'INVALID_TOPIC', 'GQL/0006/1', 403 );
}
if( !$topic->can('read') || $poll === NULL )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POLL', 'GQL/0006/2', 403 );
throw new SafeException( 'NO_POLL', 'GQL/0006/2', 403 );
}
$this->_vote( $poll, $args['poll'] );
@@ -10,25 +10,31 @@
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Db;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Topic;
use IPS\Member;
use OutOfRangeException;
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;
}
/**
* Vote on a question mutation for GraphQL API
*/
class _VoteQuestion
class VoteQuestion
{
/*
* @brief Query description
*/
public static $description = "Vote on a question";
public static string $description = "Vote on a question";
/*
* Mutation arguments
@@ -43,8 +49,10 @@ class _VoteQuestion
/**
* Return the mutation return type
*
* @return TopicType
*/
public function type()
public function type() : TopicType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -52,57 +60,50 @@ class _VoteQuestion
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @return Topic
*/
public function resolve($val, $args)
public function resolve( mixed $val, array $args ) : Topic
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['id'] );
$topic = Topic::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', 'GQL/0008/1', 400 );
throw new SafeException( 'NO_TOPIC', 'GQL/0008/1', 400 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', 'GQL/0008/2', 403 );
}
if( !$topic->isQuestion() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NON_QUESTION', 'GQL/0008/3', 403 );
throw new SafeException( 'INVALID_ID', 'GQL/0008/2', 403 );
}
if( !$topic->canVote() )
{
throw new \IPS\Api\GraphQL\SafeException( 'CANNOT_VOTE', 'GQL/0008/4', 403 );
throw new SafeException( 'CANNOT_VOTE', 'GQL/0008/4', 403 );
}
$rating = $args['vote'] == 'UP' ? 1 : -1;
// If we have an existing vote, undo that
$ratings = $topic->votes();
if ( isset( $ratings[ \IPS\Member::loggedIn()->member_id ] ) )
if ( isset( $ratings[ Member::loggedIn()->member_id ] ) )
{
\IPS\Db::i()->delete( 'forums_question_ratings', array( 'topic=? AND member=?', $topic->tid, \IPS\Member::loggedIn()->member_id ) );
Db::i()->delete( 'forums_question_ratings', array( 'topic=? AND member=?', $topic->tid, Member::loggedIn()->member_id ) );
}
\IPS\Db::i()->insert( 'forums_question_ratings', array(
Db::i()->insert( 'forums_question_ratings', array(
'topic' => $topic->tid,
'forum' => $topic->forum_id,
'member' => \IPS\Member::loggedIn()->member_id,
'member' => Member::loggedIn()->member_id,
'rating' => $rating,
'date' => time()
), TRUE );
/* Rebuild count */
/* @note SELECT_FROM_WRITE_SERVER race condition */
$topic->question_rating = \IPS\Db::i()->select( 'SUM(rating)', 'forums_question_ratings', array( 'topic=?', $topic->tid ), NULL, NULL, NULL, NULL, \IPS\Db::SELECT_FROM_WRITE_SERVER )->first();
$topic->question_rating = Db::i()->select( 'SUM(rating)', 'forums_question_ratings', array( 'topic=?', $topic->tid ), NULL, NULL, NULL, NULL, Db::SELECT_FROM_WRITE_SERVER )->first();
$topic->save();
return $topic;
@@ -10,25 +10,28 @@
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\ForumType;
use IPS\forums\Forum as ForumClass;
use OutOfRangeException;
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;
}
/**
* Forum query for GraphQL API
*/
class _Forum
class Forum
{
/*
* @brief Query description
*/
public static $description = "Returns a forum";
public static string $description = "Returns a forum";
/*
* Query arguments
@@ -43,8 +46,10 @@ class _Forum
/**
* Return the query return type
*
* @return ForumType
*/
public function type()
public function type() : ForumType
{
return \IPS\forums\api\GraphQL\TypeRegistry::forum();
}
@@ -52,18 +57,19 @@ class _Forum
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return ForumClass
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : ForumClass
{
$forum = \IPS\forums\Forum::load( $args['id'] );
$forum = ForumClass::load( $args['id'] );
if( !$forum->can( 'view', $context['member'] ) )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
return $forum;
}
@@ -10,25 +10,28 @@
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ListOfType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\ForumType;
use IPS\forums\Forum;
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;
}
/**
* Forums query for GraphQL API
*/
class _Forums
class Forums
{
/*
* @brief Query description
*/
public static $description = "Returns a list of forums";
public static string $description = "Returns a list of forums";
/*
* Query arguments
@@ -42,8 +45,10 @@ class _Forums
/**
* Return the query return type
*
* @return ListOfType<ForumType>
*/
public function type()
public function type() : ListOfType
{
return TypeRegistry::listOf( \IPS\forums\api\GraphQL\TypeRegistry::forum() );
}
@@ -51,14 +56,15 @@ class _Forums
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return array
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : array
{
\IPS\forums\Forum::loadIntoMemory('view', $context['member']);
return \IPS\forums\Forum::roots();
Forum::loadIntoMemory('view', $context['member']);
return Forum::roots();
}
}
@@ -10,25 +10,28 @@
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\Topic\Post as PostClass;
use OutOfRangeException;
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;
}
/**
* Post query for GraphQL API
*/
class _Post
class Post
{
/*
* @brief Query description
*/
public static $description = "Returns a post";
public static string $description = "Returns a post";
/*
* Query arguments
@@ -42,8 +45,10 @@ class _Post
/**
* Return the query return type
*
* @return PostType
*/
public function type()
public function type() : PostType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
@@ -51,18 +56,19 @@ class _Post
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return PostClass
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : PostClass
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$post = PostClass::loadAndCheckPerms( $args['id'] );
if( !$post->item()->canView() )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
return $post;
}
@@ -10,25 +10,29 @@
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Topic as TopicClass;
use OutOfRangeException;
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;
}
/**
* Topic query for GraphQL API
*/
class _Topic
class Topic
{
/*
* @brief Query description
*/
public static $description = "Returns a topic";
public static string $description = "Returns a topic";
/*
* Query arguments
@@ -42,8 +46,10 @@ class _Topic
/**
* Return the query return type
*
* @return TopicType
*/
public function type()
public function type() : TopicType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -51,25 +57,26 @@ class _Topic
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return TopicClass
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : TopicClass
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['id'] );
$topic = TopicClass::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
catch ( OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', '1F294/2_graphql', 400 );
throw new SafeException( 'NO_TOPIC', '1F294/2_graphql', 400 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', '2F294/9_graphql', 403 );
throw new SafeException( 'INVALID_ID', '2F294/9_graphql', 403 );
}
return $topic;
@@ -10,25 +10,35 @@
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use Exception;
use GraphQL\Type\Definition\ListOfType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Db;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\Forum;
use IPS\forums\Topic;
use IPS\Member;
use IPS\Patterns\ActiveRecordIterator;
use function count;
use function defined;
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;
}
/**
* Topics query for GraphQL API
*/
class _Topics
class Topics
{
/*
* @brief Query description
*/
public static $description = "Returns a list of topics";
public static string $description = "Returns a list of topics";
/*
* Query arguments
@@ -49,7 +59,7 @@ class _Topics
'type' => TypeRegistry::eNum([
'name' => 'forums_fluid_order_by',
'description' => 'Fields on which topics can be sorted',
'values' => \IPS\forums\api\GraphQL\Types\TopicType::getOrderByOptions()
'values' => TopicType::getOrderByOptions()
]),
'defaultValue' => NULL // will use default sort option
],
@@ -70,8 +80,10 @@ class _Topics
/**
* Return the query return type
*
* @return ListOfType<TopicType>
*/
public function type()
public function type() : ListOfType
{
return TypeRegistry::listOf( \IPS\forums\api\GraphQL\TypeRegistry::topic() );
}
@@ -79,30 +91,31 @@ class _Topics
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return ActiveRecordIterator
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : ActiveRecordIterator
{
\IPS\forums\Forum::loadIntoMemory('view', \IPS\Member::loggedIn() );
Forum::loadIntoMemory('view', Member::loggedIn() );
$where = array( 'container' => array( array( 'forums_forums.password IS NULL' ) ) );
$forumIDs = [];
/* Are we filtering by forums? */
if( isset( $args['forums'] ) && \count( $args['forums'] ) )
if( isset( $args['forums'] ) && count( $args['forums'] ) )
{
foreach( $args['forums'] as $id )
{
$forum = \IPS\forums\Forum::loadAndCheckPerms( $id );
$forum = Forum::loadAndCheckPerms( $id );
$forumIDs[] = $forum->id;
}
if( \count( $forumIDs ) )
if( count( $forumIDs ) )
{
$where['container'][] = array( \IPS\Db::i()->in( 'forums_forums.id', array_filter( $forumIDs ) ) );
$where['container'][] = array( Db::i()->in( 'forums_forums.id', array_filter( $forumIDs ) ) );
}
}
@@ -115,30 +128,30 @@ class _Topics
}
else
{
$orderBy = \IPS\forums\Topic::$databaseColumnMap[ $args['orderBy'] ];
$orderBy = Topic::$databaseColumnMap[ $args['orderBy'] ];
}
if( $args['orderBy'] === 'last_comment' )
{
$orderBy = \is_array( $orderBy ) ? array_pop( $orderBy ) : $orderBy;
$orderBy = is_array( $orderBy ) ? array_pop( $orderBy ) : $orderBy;
}
}
catch (\Exception $e)
catch (Exception $e)
{
$orderBy = 'last_post';
}
$sortBy = \IPS\forums\Topic::$databaseTable . '.' . \IPS\forums\Topic::$databasePrefix . "{$orderBy} {$args['orderDir']}";
$sortBy = Topic::$databaseTable . '.' . Topic::$databasePrefix . "{$orderBy} {$args['orderDir']}";
$offset = max( $args['offset'], 0 );
$limit = min( $args['limit'], 50 );
/* Figure out pinned status */
if ( $args['honorPinned'] )
{
$column = \IPS\forums\Topic::$databaseTable . '.' . \IPS\forums\Topic::$databasePrefix . \IPS\forums\Topic::$databaseColumnMap['pinned'];
$column = Topic::$databaseTable . '.' . Topic::$databasePrefix . Topic::$databaseColumnMap['pinned'];
$sortBy = "{$column} DESC, {$sortBy}";
}
return \IPS\forums\Topic::getItemsWithPermission( $where, $sortBy, array( $offset, $limit ), 'read' );
return Topic::getItemsWithPermission( $where, $sortBy, array( $offset, $limit ), 'read' );
}
}
+15 -11
View File
@@ -10,34 +10,38 @@
*/
namespace IPS\forums\api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\Types;
use IPS\forums\api\GraphQL\Queries\Forum;
use IPS\forums\api\GraphQL\Queries\Forums;
use IPS\forums\api\GraphQL\Queries\Post;
use IPS\forums\api\GraphQL\Queries\Topic;
use IPS\forums\api\GraphQL\Queries\Topics;
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;
}
/**
* Forums queries for GraphQL API
*/
abstract class _Query
abstract class Query
{
/**
* Get the supported query types in this app
*
* @return array
*/
public static function queries()
public static function queries() : array
{
return [
'forums' => new \IPS\forums\api\GraphQL\Queries\Forums(),
'forum' => new \IPS\forums\api\GraphQL\Queries\Forum(),
'topics' => new \IPS\forums\api\GraphQL\Queries\Topics(),
'topic' => new \IPS\forums\api\GraphQL\Queries\Topic(),
'post' => new \IPS\forums\api\GraphQL\Queries\Post()
'forums' => new Forums(),
'forum' => new Forum(),
'topics' => new Topics(),
'topic' => new Topic(),
'post' => new Post()
];
}
}
@@ -10,13 +10,16 @@
*/
namespace IPS\forums\api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\Types;
use IPS\forums\api\GraphQL\Types\ForumType;
use IPS\forums\api\GraphQL\Types\PostType;
use IPS\forums\api\GraphQL\Types\TopicType;
use IPS\forums\api\GraphQL\Types\VoteType;
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;
}
@@ -25,35 +28,35 @@ if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
* so we'll generate singletons here.
* @todo automate this somehow?
*/
class _TypeRegistry
class TypeRegistry
{
/**
* Returns the forum instance
*
* @var \IPS\forums\api\GraphQL\Types\ForumType
* @var ForumType|null
*/
protected static $forum;
protected static ?ForumType $forum = null;
/**
* Returns the post instance
*
* @var \IPS\forums\api\GraphQL\Types\PostType
* @var PostType|null
*/
protected static $post;
protected static ?PostType $post = null;
/**
* Returns the topic instance
*
* @var \IPS\forums\api\GraphQL\Types\TopicType
* @var TopicType|null
*/
protected static $topic;
protected static ?TopicType $topic = null;
/**
* Returns the vote instance
*
* @var \IPS\forums\api\GraphQL\Types\VoteType
* @var VoteType|null
*/
protected static $vote;
protected static ?VoteType $vote = null;
/**
* Constructor
@@ -66,32 +69,32 @@ class _TypeRegistry
/**
* @return ForumType
*/
public static function forum() : \IPS\forums\api\GraphQL\Types\ForumType
public static function forum() : ForumType
{
return self::$forum ?: (self::$forum = new \IPS\forums\api\GraphQL\Types\ForumType());
return self::$forum ?: (self::$forum = new ForumType());
}
/**
* @return PostType
*/
public static function post() : \IPS\forums\api\GraphQL\Types\PostType
public static function post() : PostType
{
return self::$post ?: (self::$post = new \IPS\forums\api\GraphQL\Types\PostType());
return self::$post ?: (self::$post = new PostType());
}
/**
* @return TopicType
*/
public static function topic() : \IPS\forums\api\GraphQL\Types\TopicType
public static function topic() : TopicType
{
return self::$topic ?: (self::$topic = new \IPS\forums\api\GraphQL\Types\TopicType());
return self::$topic ?: (self::$topic = new TopicType());
}
/**
* @return VoteType
*/
public static function vote() : \IPS\forums\api\GraphQL\Types\VoteType
public static function vote() : VoteType
{
return self::$vote ?: (self::$vote = new \IPS\forums\api\GraphQL\Types\VoteType());
return self::$vote ?: (self::$vote = new VoteType());
}
}
@@ -11,53 +11,62 @@
namespace IPS\forums\api\GraphQL\Types;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\DateTime;
use IPS\forums\Forum;
use IPS\Login;
use IPS\Member;
use IPS\Node\Api\GraphQL\NodeType;
use IPS\Node\Model;
use IPS\Patterns\ActiveRecordIterator;
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;
}
/**
* ForumType for GraphQL API
*/
class _ForumType extends \IPS\Node\Api\GraphQL\NodeType
class ForumType extends NodeType
{
/*
* @brief The item classname we use for this type
*/
protected static $nodeClass = '\IPS\forums\Forum';
protected static string $nodeClass = '\IPS\forums\Forum';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'forums_Forum';
protected static string $typeName = 'forums_Forum';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A forum';
protected static string $typeDescription = 'A forum';
/*
* @brief Follow data passed in to FollowType resolver
*/
protected static $followData = array('app' => 'forums', 'area' => 'forum');
protected static array $followData = array('app' => 'forums', 'area' => 'forum');
/**
* Return the fields available in this type
*
* @return array
*/
public function fields()
public function fields(): array
{
$defaultFields = parent::fields();
$forumFields = array(
'solvedEnabled' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($forum) {
return ( $forum->forums_bitoptions['bw_enable_answers_member'] || $forum->forums_bitoptions['bw_enable_answers_moderator'] );
return ( $forum->forums_bitoptions['bw_solved_set_by_member'] || $forum->forums_bitoptions['bw_solved_set_by_moderator'] );
}
],
'lastPostAuthor' => [
@@ -99,7 +108,7 @@ class _ForumType extends \IPS\Node\Api\GraphQL\NodeType
'passwordRequired' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($forum) {
if( $forum->password === NULL || \IPS\Member::loggedIn()->inGroup( explode( ',', $forum->password_override ) ) )
if( $forum->password === NULL || Member::loggedIn()->inGroup( explode( ',', $forum->password_override ) ) )
{
return FALSE;
}
@@ -129,7 +138,7 @@ class _ForumType extends \IPS\Node\Api\GraphQL\NodeType
*
* @return ObjectType
*/
public static function getItemType()
public static function getItemType(): ObjectType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -137,34 +146,34 @@ class _ForumType extends \IPS\Node\Api\GraphQL\NodeType
/**
* Check for a password before returning items
*
* @param \IPS\forums\Forum
* @param array Arguments passed to this resolver
* @return array
* @param Model $forum
* @param array $args Arguments passed to this resolver
* @return int|ActiveRecordIterator
*/
protected static function items($forum, $args)
protected static function items( Model $forum, array $args): int|ActiveRecordIterator
{
// If there's no password or our group is excluded, just continue
if( $forum->password === NULL || \IPS\Member::loggedIn()->inGroup( explode( ',', $forum->password_override ) ) )
if( $forum->password === NULL || Member::loggedIn()->inGroup( explode( ',', $forum->password_override ) ) )
{
return parent::items($forum, $args);
return parent::items( $forum, $args );
}
if( !isset( $args['password'] ) || !\IPS\Login::compareHashes( md5( $forum->password ), md5( $args['password'] ) ) )
if( !isset( $args['password'] ) || !Login::compareHashes( md5( $forum->password ), md5( $args['password'] ) ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'INCORRECT_PASSWORD', 'GQL/0002/1', 403 );
throw new SafeException( 'INCORRECT_PASSWORD', 'GQL/0002/1', 403 );
}
return parent::items($forum, $args);
return parent::items( $forum, $args );
}
/**
* Resolve last post author field
*
* @param \IPS\forums\Forum
* @param array Arguments passed to this resolver
* @return array
* @param Forum $forum
* @param array $args Arguments passed to this resolver
* @return Member|null
*/
protected static function lastPostAuthor($forum, $args)
protected static function lastPostAuthor( Forum $forum, array $args) : Member|null
{
$lastComment = $forum->lastPost();
@@ -179,11 +188,11 @@ class _ForumType extends \IPS\Node\Api\GraphQL\NodeType
/**
* Resolve last post date field
*
* @param \IPS\forums\Forum
* @param array Arguments passed to this resolver
* @return array
* @param Forum $forum
* @param array $args Arguments passed to this resolver
* @return int|DateTime|null
*/
protected static function lastPostDate($forum, $args)
protected static function lastPostDate( Forum $forum, array $args ) : int|DateTime|null
{
$lastComment = $forum->lastPost();
@@ -12,40 +12,44 @@
namespace IPS\forums\api\GraphQL\Types;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\CommentType;
use IPS\Content\Comment;
use IPS\Member;
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;
}
/**
* PostType for GraphQL API
*/
class _PostType extends \IPS\Content\Api\GraphQL\CommentType
class PostType extends CommentType
{
/*
* @brief The item classname we use for this type
*/
protected static $commentClass = '\IPS\forums\Topic\Post';
protected static string $commentClass = '\IPS\forums\Topic\Post';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'forums_Post';
protected static string $typeName = 'forums_Post';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A post';
protected static string $typeDescription = 'A post';
/**
* Get the item type that goes with this item type
*
* @return ObjectType
*/
public static function getItemType()
public static function getItemType(): ObjectType
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
@@ -55,7 +59,7 @@ class _PostType extends \IPS\Content\Api\GraphQL\CommentType
*
* @return array
*/
public function fields()
public function fields(): array
{
$defaultFields = parent::fields();
$postFields = array(
@@ -65,14 +69,6 @@ class _PostType extends \IPS\Content\Api\GraphQL\CommentType
return $post->item();
}
],
/* Q&A STUFF */
'isQuestion' => [
'type' => TypeRegistry::boolean(),
'description' => "Boolean indicating whether this post is a question, i.e. the first post in a question topic",
'resolve' => function ($post) {
return $post->item()->isQuestion() && $post->new_topic;
}
],
'answerVotes' => [
'type' => TypeRegistry::int(),
'resolve' => function ($post) {
@@ -85,41 +81,6 @@ class _PostType extends \IPS\Content\Api\GraphQL\CommentType
'resolve' => function ($post) {
return $post->post_bwoptions['best_answer'];
}
],
'canVoteUp' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($post) {
if( !$post->item()->isQuestion() )
{
return NULL;
}
return $post->canVote(1);
}
],
'canVoteDown' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($post) {
if( !$post->item()->isQuestion() )
{
return NULL;
}
return $post->canVote(-1) && \IPS\Settings::i()->forums_questions_downvote;
}
],
'vote' => [
'type' => \IPS\forums\api\GraphQL\TypeRegistry::vote(),
'resolve' => function ($post) {
$ratings = $post->item()->answerVotes( \IPS\Member::loggedIn() );
if( !$post->item()->isQuestion() || !isset( $ratings[ $post->pid ] ) )
{
return NULL;
}
return $ratings[ $post->pid ] === -1 ? 'DOWN' : 'UP';
}
]
);
@@ -132,11 +93,13 @@ class _PostType extends \IPS\Content\Api\GraphQL\CommentType
/**
* 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
{
$type = $post->item()->isQuestion() ? 'answer_lc' : 'post_lc';
return \IPS\Member::loggedIn()->language()->addToStack($type, FALSE, $options);
$type = 'post_lc';
return Member::loggedIn()->language()->addToStack( $type, FALSE, $options);
}
}
@@ -10,48 +10,53 @@
*/
namespace IPS\forums\api\GraphQL\Types;
use Exception;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\_TypeRegistry;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\Content\Api\GraphQL\ItemType;
use IPS\forums\Topic\Post;
use IPS\Member;
use function defined;
use function in_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;
}
/**
* TopicType for GraphQL API
*/
class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
class TopicType extends ItemType
{
/*
* @brief The item classname we use for this type
*/
protected static $itemClass = '\IPS\forums\Topic';
protected static string $itemClass = '\IPS\forums\Topic';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'forums_Topic';
protected static string $typeName = 'forums_Topic';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A topic';
protected static string $typeDescription = 'A topic';
/*
* @brief Follow data passed in to FollowType resolver
*/
protected static $followData = array('app' => 'forums', 'area' => 'topic');
protected static array $followData = array('app' => 'forums', 'area' => 'topic');
/**
* Return the fields available in this type
*
* @return array
*/
public function fields()
public function fields(): array
{
// Extend our fields with topic-specific stuff
$defaultFields = parent::fields();
@@ -73,7 +78,7 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
'resolve' => function ($topic) {
foreach( $topic->stats(FALSE) as $k => $v )
{
if( \in_array( $k, $topic->hotStats ) )
if( in_array( $k, $topic->hotStats ) )
{
return TRUE;
}
@@ -96,6 +101,7 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
if( $topic->isSolved() ){
return $topic->mapped('solved_comment_id');
}
return 0;
}
],
'canMarkSolved' => [
@@ -114,69 +120,14 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
try
{
$solved = \IPS\forums\Topic\Post::load( $topic->mapped('solved_comment_id') );
return $solved;
return Post::load( $topic->mapped('solved_comment_id') );
}
catch (\Exception $err)
catch (Exception $err)
{
return NULL;
}
}
],
/* Q&A STUFF */
'isQuestion' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
return $topic->isQuestion();
}
],
'questionVotes' => [
'type' => TypeRegistry::int(),
'resolve' => function ($topic) {
if( !$topic->isQuestion() )
{
return NULL;
}
return \intval( $topic->question_rating );
}
],
'canVoteUp' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
if( !$topic->isQuestion() )
{
return NULL;
}
return $topic->canVote(1);
}
],
'canVoteDown' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
if( !$topic->isQuestion() )
{
return NULL;
}
return $topic->canVote(-1) && \IPS\Settings::i()->forums_questions_downvote;
}
],
'vote' => [
'type' => \IPS\forums\api\GraphQL\TypeRegistry::vote(),
'resolve' => function ($topic) {
$topicVotes = $topic->votes();
if( !$topic->isQuestion() || !isset( $topicVotes[ \IPS\Member::loggedIn()->member_id ] ) )
{
return NULL;
}
return $topicVotes[ \IPS\Member::loggedIn()->member_id ] === -1 ? 'DOWN' : 'UP';
}
],
'hasBestAnswer' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
@@ -186,11 +137,6 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
'bestAnswerID' => [
'type' => TypeRegistry::id(),
'resolve' => function ($topic) {
if( !$topic->isQuestion() )
{
return NULL;
}
return $topic->topic_answered_pid;
}
],
@@ -208,7 +154,7 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
$topicFields['postCount']['resolve'] = function ($topic, $args) {
if( $args['includeHidden'] )
{
return $topic->commentCount() + ( $topic->isQuestion() ? 1 : 0 );
return $topic->commentCount();
}
return $topic->mapped('num_comments');
@@ -230,7 +176,7 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
return array_merge( $defaultFields, $topicFields );
}
public static function args()
public static function args(): array
{
return array_merge( parent::args(), array(
'password' => [
@@ -244,15 +190,15 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
* Here we adjust the resolver for the commentInformation field to check whether this is
* a poll-only topic.
*
* @return string|null
* @return array
*/
public static function getItemPermissionFields()
public static function getItemPermissionFields(): array
{
$defaultFields = parent::getItemPermissionFields();
$existingResolver = $defaultFields['commentInformation']['resolve'];
$defaultFields['commentInformation']['resolve'] = function ($topic, $args, $context) use ( $existingResolver ) {
if( $topic->canComment( \IPS\Member::loggedIn(), FALSE ) && ( $topic->getPoll() and $topic->getPoll()->poll_only ) )
if( $topic->canComment( Member::loggedIn(), FALSE ) && ( $topic->getPoll() and $topic->getPoll()->poll_only ) )
{
return 'topic_poll_can_comment';
}
@@ -270,7 +216,7 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
*
* @return array
*/
public static function getOrderByOptions()
public static function getOrderByOptions(): array
{
$defaultArgs = parent::getOrderByOptions();
return array_merge( $defaultArgs, array('last_comment', 'num_comments', 'views', 'author_name', 'last_comment_name', 'date', 'votes') );
@@ -281,39 +227,8 @@ class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
*
* @return ObjectType
*/
protected static function getCommentType()
protected static function getCommentType(): ObjectType
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolve the comments field - overridden from ItemType
* If this is a question and order isn't date, then force order by votes
*
* @param \IPS\forums\Topic
* @param array Arguments passed to this resolver
* @return array
*/
protected static function comments($topic, $args)
{
if( $topic->isQuestion() && $args['orderBy'] !== 'date' )
{
if( $topic->isArchived() )
{
$args['orderBy'] = "archive_is_first desc, archive_bwoptions";
$args['orderDir'] = "DESC";
}
else
{
$args['orderBy'] = "new_topic DESC, post_bwoptions DESC, post_field_int DESC, post_date";
$args['orderDir'] = "ASC";
}
}
elseif( !$topic->isQuestion() && $args['orderBy'] === 'votes' )
{
$args['orderBy'] = 'date'; // Only Q&A can order by votes
}
return parent::comments($topic, $args);
}
}
@@ -10,24 +10,24 @@
*/
namespace IPS\forums\api\GraphQL\Types;
use IPS\Api\GraphQL\TypeRegistry;
use GraphQL\Type\Definition\EnumType;
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;
}
/**
* VoteType for GraphQL API
*/
class _VoteType extends \GraphQL\Type\Definition\EnumType
class VoteType extends EnumType
{
/**
* Get object type
*
* @return ObjectType
*/
public function __construct()
{