Version 4.5.0

This commit is contained in:
Neo committed 2025-12-19 05:51:58 -08:00
1 parent 1a0c7fd3c2
commit f79dcf067a
3791 files changed
+248032 -76372

No files matched your search

@@ -0,0 +1,50 @@
<?php
/**
* @brief GraphQL: Forums mutations
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\Types;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Forums mutationss GraphQL API
*/
abstract class _Mutation
{
/**
* Get the supported query types in this app
*
* @return array
*/
public static function mutations()
{
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(),
];
}
}
@@ -0,0 +1,102 @@
<?php
/**
* @brief GraphQL: Create topic mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Create topic mutation for GraphQL API
*/
class _CreateTopic extends \IPS\Content\Api\GraphQL\ItemMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic';
/*
* @brief Query description
*/
public static $description = "Create a new topic";
/*
* Mutation arguments
*/
public function args()
{
return [
'forumID' => TypeRegistry::nonNull( TypeRegistry::id() ),
'title' => TypeRegistry::nonNull( TypeRegistry::string() ),
'content' => TypeRegistry::nonNull( TypeRegistry::string() ),
'tags' => TypeRegistry::listOf( TypeRegistry::string() ),
'state' => TypeRegistry::itemState(),
'postKey' => TypeRegistry::string()
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args, $context, $info)
{
/* Get forum */
try
{
$forum = \IPS\forums\Forum::loadAndCheckPerms( $args['forumID'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_FORUM', '1F294/2', 400 );
}
/* Check permission */
if ( !$forum->can( 'add', \IPS\Member::loggedIn() ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', '2F294/9', 403 );
}
/* Check we have a title and a post */
if ( !$args['title'] )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TITLE', '1F294/5', 400 );
}
if ( !$args['content'] )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F294/4', 400 );
}
$item = $this->_create( $args, $forum, $args['postKey'] ?? NULL );
return $item;
}
}
@@ -0,0 +1,73 @@
<?php
/**
* @brief GraphQL: Mark a forum read mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 9 Nov 2018
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Mark forum read mutation for GraphQL API
*/
class _MarkForumRead extends \IPS\Node\Api\GraphQL\NodeMutator
{
/*
* @brief Query description
*/
public static $description = "Mark a forum as read";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() )
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::forum();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args)
{
try
{
$forum = \IPS\forums\Forum::loadAndCheckPerms( \intval( $args['id'] ) );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_NODE', 'GQL/0004/2', 403 );
}
$this->_markRead( $forum );
return $forum;
}
}
@@ -0,0 +1,78 @@
<?php
/**
* @brief GraphQL: Mark a topic read mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 28 Nov 2018
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Mark topic read mutation for GraphQL API
*/
class _MarkTopicRead extends \IPS\Content\Api\GraphQL\ItemMutator
{
/*
* @brief Query description
*/
public static $description = "Mark a topic as read";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() )
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args)
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', 'GQL/0005/1', 400 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', 'GQL/0005/2', 403 );
}
$this->_markRead( $topic );
return $topic;
}
}
@@ -0,0 +1,123 @@
<?php
/**
* @brief GraphQL: Mark a topic solved mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 1 Jul 2020
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Mark topic solved mutation for GraphQL API
*/
class _MarkTopicSolved extends \IPS\Node\Api\GraphQL\NodeMutator
{
/*
* @brief Query description
*/
public static $description = "Mark a topic solved";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() ),
'answer' => TypeRegistry::nonNull( TypeRegistry::id() ),
'solved' => [
'type' => TypeRegistry::boolean(),
'defaultValue' => TRUE
]
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @return \IPS\forums\Topic
*/
public function resolve($val, $args)
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( \intval( $args['id'] ) );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', 'GQL', 403 );
}
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( \intval( $args['answer'] ) );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', 'GQL', 403 );
}
/* This is "solved" mode */
if ( !$topic->canSolve() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', 'GQL', 403 );
}
if( $args['solved'] )
{
$topic->toggleSolveComment( $post->pid, TRUE );
if( $post->author()->member_id != \IPS\Member::loggedIn()->member_id )
{
$notification = new \IPS\Notification( \IPS\Application::load( 'core' ), 'best_answer', $topic, array( $post ), array( $post->author()->member_id ) );
$notification->recipients->attach( $post->author() );
$notification->send();
}
}
else if ( $topic->mapped('solved_comment_id') )
{
$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,
'ctime' => time(),
'note' => json_encode( array( $post->pid => $args['solved'] ) ),
'ip_address' => \IPS\Request::i()->ipAddress(),
'appcomponent' => 'forums',
'module' => 'forums',
'controller' => 'topic',
'do' => $args['solved'] ? 'solve' : 'unsolve',
'lang_key' => $args['solved'] ? 'modlog__best_answer_set' : 'modlog__best_answer_unset',
'class' => "IPS\forums\Topic",
'item_id' => $args['id'],
) );
return $topic;
}
}
@@ -0,0 +1,90 @@
<?php
/**
* @brief GraphQL: React to a post mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_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
*/
protected $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "React to a post";
/*
* Mutation arguments
*/
public function args()
{
return [
'postID' => TypeRegistry::nonNull( TypeRegistry::id() ),
'reactionID' => TypeRegistry::int(),
'removeReaction' => TypeRegistry::boolean()
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args, $context, $info)
{
/* Get topic */
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['postID'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/1', 403 );
}
/* Do it */
if( isset( $args['removeReaction'] ) && $args['removeReaction'] )
{
$this->_unreactComment( $post );
}
else
{
$this->_reactComment( $args['reactionID'], $post );
}
return $post;
}
}
@@ -0,0 +1,108 @@
<?php
/**
* @brief GraphQL: Reply to topic mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_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
*/
protected $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "Create a new post";
/*
* Mutation arguments
*/
public function args()
{
return [
'topicID' => TypeRegistry::nonNull( TypeRegistry::id() ),
'content' => TypeRegistry::nonNull( TypeRegistry::string() ),
'replyingTo' => TypeRegistry::id(),
'postKey' => TypeRegistry::string()
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args, $context, $info)
{
/* Get topic */
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['topicID'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', '1F295/1', 403 );
}
/* Get author */
if ( !$topic->canComment( \IPS\Member::loggedIn() ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', '2F294/A', 403 );
}
/* Check we have a post */
if ( !$args['content'] )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/3', 403 );
}
$originalPost = NULL;
if( isset( $args['replyingTo'] ) )
{
try
{
$originalPost = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['replyingTo'] );
}
catch ( \OutOfRangeException $e )
{
// Just ignore it
}
}
/* Do it */
return $this->_createComment( $args, $topic, $args['postKey'] ?? NULL, $originalPost );
}
}
@@ -0,0 +1,84 @@
<?php
/**
* @brief GraphQL: Report a post mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 11 Jun 2019
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Report post mutation for GraphQL API
*/
class _ReportPost extends \IPS\Content\Api\GraphQL\CommentMutator
{
/**
* Class
*/
protected $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "Report a post";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() ),
'reason' => [
'type' => TypeRegistry::int(),
'defaultValue' => 0
],
'additionalInfo' => TypeRegistry::string()
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
*/
public function resolve($val, $args, $context, $info)
{
/* Get post */
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/1', 403 );
}
/* Do it */
return $this->_reportComment( $args, $post );
}
}
@@ -0,0 +1,80 @@
<?php
/**
* @brief GraphQL: Revoke a post report mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 15 Jun 2019
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_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
*/
protected $class = 'IPS\forums\Topic\Post';
/*
* @brief Query description
*/
public static $description = "Report a post";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() ),
'reportID'=> TypeRegistry::nonNull( TypeRegistry::id() )
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
*/
public function resolve($val, $args, $context, $info)
{
/* Get post */
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_POST', '1F295/1', 403 );
}
/* Do it */
return $this->_revokeCommentReport( $args, $post );
}
}
@@ -0,0 +1,106 @@
<?php
/**
* @brief GraphQL: Set a post as best answer
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 08 Jan 2019
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Set a post as best answer mutation for GraphQL API
*/
class _SetBestAnswer
{
/*
* @brief Query description
*/
public static $description = "Set a post as best answer";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() )
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args)
{
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$topic = $post->item();
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\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 );
}
if( !$topic->canSetBestAnswer() )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_PERMISSION', 'GQL/0010/4', 403 );
}
// Do we have an existing best answer
if ( $topic->topic_answered_pid )
{
try
{
$oldBestAnswer = \IPS\forums\Topic\Post::load( $topic->topic_answered_pid );
$oldBestAnswer->post_bwoptions['best_answer'] = FALSE;
$oldBestAnswer->save();
}
catch ( \Exception $e ) {}
}
$post->post_bwoptions['best_answer'] = TRUE;
$post->save();
$topic->topic_answered_pid = $post->pid;
$topic->save();
return $post;
}
}
@@ -0,0 +1,109 @@
<?php
/**
* @brief GraphQL: Vote on an answer
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 02 Jan 2019
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Vote on an answer mutation for GraphQL API
*/
class _VoteAnswer
{
/*
* @brief Query description
*/
public static $description = "Vote on an answer";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() ),
'vote' => TypeRegistry::nonNull( \IPS\forums\api\GraphQL\TypeRegistry::vote() ),
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args)
{
try
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
$topic = $post->item();
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\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 );
}
if( !$post->canVote() )
{
throw new \IPS\Api\GraphQL\SafeException( 'CANNOT_VOTE', 'GQL/0009/4', 403 );
}
$rating = $args['vote'] == 'UP' ? 1 : -1;
/* 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 ) );
}
catch ( \UnderflowException $e ){}
\IPS\Db::i()->insert( 'forums_answer_ratings', array(
'post' => $post->pid,
'topic' => $topic->tid,
'member' => \IPS\Member::loggedIn()->member_id,
'rating' => $rating,
'date' => time()
), TRUE );
$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->save();
return $post;
}
}
@@ -0,0 +1,96 @@
<?php
/**
* @brief GraphQL: Vote in poll mutation
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 5 Dec 2018
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\InputObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_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
*/
protected $class = 'IPS\forums\Topic';
/*
* @brief Query description
*/
public static $description = "Vote in a poll in a topic";
/*
* Mutation arguments
*/
public function args()
{
return [
'itemID' => TypeRegistry::nonNull( TypeRegistry::id() ),
'poll' => TypeRegistry::listOf(
new InputObjectType([
'name' => 'core_PollQuestionInput',
'fields' => [
'id' => TypeRegistry::id(),
'choices' => TypeRegistry::listOf( TypeRegistry::int() )
]
])
)
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args)
{
/* Get topic */
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['itemID'] );
$poll = $topic->getPoll();
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\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 );
}
$this->_vote( $poll, $args['poll'] );
return $topic;
}
}
@@ -0,0 +1,109 @@
<?php
/**
* @brief GraphQL: Vote on a question
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 02 Jan 2019
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Vote on a question mutation for GraphQL API
*/
class _VoteQuestion
{
/*
* @brief Query description
*/
public static $description = "Vote on a question";
/*
* Mutation arguments
*/
public function args()
{
return [
'id' => TypeRegistry::nonNull( TypeRegistry::id() ),
'vote' => TypeRegistry::nonNull( \IPS\forums\api\GraphQL\TypeRegistry::vote() ),
];
}
/**
* Return the mutation return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args)
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\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 );
}
if( !$topic->canVote() )
{
throw new \IPS\Api\GraphQL\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 ] ) )
{
\IPS\Db::i()->delete( 'forums_question_ratings', array( 'topic=? AND member=?', $topic->tid, \IPS\Member::loggedIn()->member_id ) );
}
\IPS\Db::i()->insert( 'forums_question_ratings', array(
'topic' => $topic->tid,
'forum' => $topic->forum_id,
'member' => \IPS\Member::loggedIn()->member_id,
'rating' => $rating,
'date' => time()
), TRUE );
/* Rebuild count */
$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->save();
return $topic;
}
}
@@ -0,0 +1,70 @@
<?php
/**
* @brief GraphQL: Forum query
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Forum query for GraphQL API
*/
class _Forum
{
/*
* @brief Query description
*/
public static $description = "Returns a forum";
/*
* Query arguments
*/
public function args()
{
return array(
'id' => TypeRegistry::nonNull( TypeRegistry::id() ),
'password' => TypeRegistry::string()
);
}
/**
* Return the query return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::forum();
}
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Forum
*/
public function resolve($val, $args, $context, $info)
{
$forum = \IPS\forums\Forum::load( $args['id'] );
if( !$forum->can( 'view', $context['member'] ) )
{
throw new \OutOfRangeException;
}
return $forum;
}
}
@@ -0,0 +1,64 @@
<?php
/**
* @brief GraphQL: Forums query
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Forums query for GraphQL API
*/
class _Forums
{
/*
* @brief Query description
*/
public static $description = "Returns a list of forums";
/*
* Query arguments
*/
public function args()
{
return array(
'id' => TypeRegistry::id()
);
}
/**
* Return the query return type
*/
public function type()
{
return TypeRegistry::listOf( \IPS\forums\api\GraphQL\TypeRegistry::forum() );
}
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return array
*/
public function resolve($val, $args, $context, $info)
{
\IPS\forums\Forum::loadIntoMemory('view', $context['member']);
return \IPS\forums\Forum::roots();
}
}
@@ -0,0 +1,69 @@
<?php
/**
* @brief GraphQL: Post query
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Post query for GraphQL API
*/
class _Post
{
/*
* @brief Query description
*/
public static $description = "Returns a post";
/*
* Query arguments
*/
public function args()
{
return array(
'id' => TypeRegistry::nonNull( TypeRegistry::id() )
);
}
/**
* Return the query return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::post();
}
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
*/
public function resolve($val, $args, $context, $info)
{
$post = \IPS\forums\Topic\Post::loadAndCheckPerms( $args['id'] );
if( !$post->item()->canView() )
{
throw new \OutOfRangeException;
}
return $post;
}
}
@@ -0,0 +1,77 @@
<?php
/**
* @brief GraphQL: Topic query
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Topic query for GraphQL API
*/
class _Topic
{
/*
* @brief Query description
*/
public static $description = "Returns a topic";
/*
* Query arguments
*/
public function args()
{
return array(
'id' => TypeRegistry::nonNull( TypeRegistry::id() )
);
}
/**
* Return the query return type
*/
public function type()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
*/
public function resolve($val, $args, $context, $info)
{
try
{
$topic = \IPS\forums\Topic::loadAndCheckPerms( $args['id'] );
}
catch ( \OutOfRangeException $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NO_TOPIC', '1F294/2', 400 );
}
if( !$topic->can('read') )
{
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_ID', '2F294/9', 403 );
}
return $topic;
}
}
@@ -0,0 +1,144 @@
<?php
/**
* @brief GraphQL: Topics query
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Queries;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Topics query for GraphQL API
*/
class _Topics
{
/*
* @brief Query description
*/
public static $description = "Returns a list of topics";
/*
* Query arguments
*/
public function args()
{
return array(
'forums' => TypeRegistry::listOf( TypeRegistry::int() ),
'offset' => [
'type' => TypeRegistry::int(),
'defaultValue' => 0
],
'limit' => [
'type' => TypeRegistry::int(),
'defaultValue' => 25
],
'orderBy' => [
'type' => TypeRegistry::eNum([
'name' => 'forums_fluid_order_by',
'description' => 'Fields on which topics can be sorted',
'values' => \IPS\forums\api\GraphQL\Types\TopicType::getOrderByOptions()
]),
'defaultValue' => NULL // will use default sort option
],
'orderDir' => [
'type' => TypeRegistry::eNum([
'name' => 'forums_fluid_order_dir',
'description' => 'Directions in which items can be sorted',
'values' => [ 'ASC', 'DESC' ]
]),
'defaultValue' => 'DESC'
],
'honorPinned' => [
'type' => TypeRegistry::boolean(),
'defaultValue' => true
]
);
}
/**
* Return the query return type
*/
public function type()
{
return TypeRegistry::listOf( \IPS\forums\api\GraphQL\TypeRegistry::topic() );
}
/**
* Resolves this query
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @return \IPS\forums\Topic\Post
*/
public function resolve($val, $args, $context, $info)
{
\IPS\forums\Forum::loadIntoMemory('view', \IPS\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'] ) )
{
foreach( $args['forums'] as $id )
{
$forum = \IPS\forums\Forum::loadAndCheckPerms( $id );
$forumIDs[] = $forum->id;
}
if( \count( $forumIDs ) )
{
$where['container'][] = array( \IPS\Db::i()->in( 'forums_forums.id', array_filter( $forumIDs ) ) );
}
}
/* Get sorting */
try
{
if( $args['orderBy'] === NULL )
{
$orderBy = 'last_post';
}
else
{
$orderBy = \IPS\forums\Topic::$databaseColumnMap[ $args['orderBy'] ];
}
if( $args['orderBy'] === 'last_comment' )
{
$orderBy = \is_array( $orderBy ) ? array_pop( $orderBy ) : $orderBy;
}
}
catch (\Exception $e)
{
$orderBy = 'last_post';
}
$sortBy = "{$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'];
$sortBy = "{$column} DESC, {$sortBy}";
}
return \IPS\forums\Topic::getItemsWithPermission( $where, $sortBy, array( $offset, $limit ), 'read' );
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
* @brief GraphQL: Forums queries
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\Types;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Forums queries for GraphQL API
*/
abstract class _Query
{
/**
* Get the supported query types in this app
*
* @return array
*/
public static function queries()
{
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()
];
}
}
@@ -0,0 +1,74 @@
<?php
/**
* @brief GraphQL: Types registry
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 7 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\Types;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Forum type registry. GraphQL requires exactly one instance of each type,
* so we'll generate singletons here.
* @todo automate this somehow?
*/
class _TypeRegistry
{
protected static $forum;
protected static $post;
protected static $topic;
protected static $vote;
/**
* Constructor
*/
public function __construct()
{
// Defined to suppress static warnings
}
/**
* @return ForumType
*/
public static function forum()
{
return self::$forum ?: (self::$forum = new \IPS\forums\api\GraphQL\Types\ForumType());
}
/**
* @return PostType
*/
public static function post()
{
return self::$post ?: (self::$post = new \IPS\forums\api\GraphQL\Types\PostType());
}
/**
* @return TopicType
*/
public static function topic()
{
return self::$topic ?: (self::$topic = new \IPS\forums\api\GraphQL\Types\TopicType());
}
/**
* @return VoteType
*/
public static function vote()
{
return self::$vote ?: (self::$vote = new \IPS\forums\api\GraphQL\Types\VoteType());
}
}
@@ -0,0 +1,197 @@
<?php
/**
* @brief GraphQL: Forum Type
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Types;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* ForumType for GraphQL API
*/
class _ForumType extends \IPS\Node\Api\GraphQL\NodeType
{
/*
* @brief The item classname we use for this type
*/
protected static $nodeClass = '\IPS\forums\Forum';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'forums_Forum';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A forum';
/*
* @brief Follow data passed in to FollowType resolver
*/
protected static $followData = array('app' => 'forums', 'area' => 'forum');
/**
* Return the fields available in this type
*
* @return array
*/
public function fields()
{
$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'] );
}
],
'lastPostAuthor' => [
'type' => \IPS\core\api\GraphQL\TypeRegistry::member(),
'resolve' => function ($forum, $args) {
return self::lastPostAuthor( $forum, $args );
}
],
'lastPostDate' => [
'type' => TypeRegistry::string(),
'resolve' => function ($forum, $args) {
return self::lastPostDate( $forum, $args );
}
],
'featureColor' => [
'type' => TypeRegistry::string(),
'resolve' => function ($forum) {
return $forum->feature_color;
}
],
'isRedirectForum' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($forum) {
return (bool) $forum->redirect_on;
}
],
'redirectHits' => [
'type' => TypeRegistry::int(),
'resolve' => function ($forum) {
return $forum->redirect_on ? (int) $forum->redirect_hits : NULL;
}
],
'passwordProtected' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($forum) {
return $forum->password !== NULL;
}
],
'passwordRequired' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($forum) {
if( $forum->password === NULL || \IPS\Member::loggedIn()->inGroup( explode( ',', $forum->password_override ) ) )
{
return FALSE;
}
return TRUE;
}
]
);
// Duplicate fields that have different names in forums
$forumFields['topicCount'] = $defaultFields['itemCount'];
$forumFields['postCount'] = $defaultFields['commentCount'];
$forumFields['subforums'] = $defaultFields['children'];
$forumFields['topics'] = $defaultFields['items'];
// Remove duplicated fields
unset( $defaultFields['itemCount'] );
unset( $defaultFields['commentCount'] );
unset( $defaultFields['children'] );
unset( $defaultFields['items'] );
return array_merge( $defaultFields, $forumFields );
}
/**
* Get the item type that goes with this node type
*
* @return ObjectType
*/
public static function getItemType()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Check for a password before returning items
*
* @param \IPS\forums\Forum
* @param array Arguments passed to this resolver
* @return array
*/
protected static function items($forum, $args)
{
// If there's no password or our group is excluded, just continue
if( $forum->password === NULL || \IPS\Member::loggedIn()->inGroup( explode( ',', $forum->password_override ) ) )
{
return parent::items($forum, $args);
}
if( !isset( $args['password'] ) || !\IPS\Login::compareHashes( md5( $forum->password ), md5( $args['password'] ) ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'INCORRECT_PASSWORD', 'GQL/0002/1', 403 );
}
return parent::items($forum, $args);
}
/**
* Resolve last post author field
*
* @param \IPS\forums\Forum
* @param array Arguments passed to this resolver
* @return array
*/
protected static function lastPostAuthor($forum, $args)
{
$lastComment = $forum->lastPost();
if( $lastComment )
{
return $lastComment['author'];
}
return NULL;
}
/**
* Resolve last post date field
*
* @param \IPS\forums\Forum
* @param array Arguments passed to this resolver
* @return array
*/
protected static function lastPostDate($forum, $args)
{
$lastComment = $forum->lastPost();
if( $lastComment )
{
return $lastComment['date'];
}
return NULL;
}
}
@@ -0,0 +1,145 @@
<?php
/**
* @brief GraphQL: Post Type
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Types;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* PostType for GraphQL API
*/
class _PostType extends \IPS\Content\Api\GraphQL\CommentType
{
/*
* @brief The item classname we use for this type
*/
protected static $commentClass = '\IPS\forums\Topic\Post';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'forums_Post';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A post';
/**
* Get the item type that goes with this item type
*
* @return ObjectType
*/
public static function getItemType()
{
return \IPS\forums\api\GraphQL\TypeRegistry::topic();
}
/**
* Return the fields available in this type
*
* @return array
*/
public function fields()
{
$defaultFields = parent::fields();
$postFields = array(
'topic' => [
'type' => \IPS\forums\api\GraphQL\TypeRegistry::topic(),
'resolve' => function ($post) {
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) {
return $post->post_field_int;
}
],
'isBestAnswer' => [
'type' => TypeRegistry::boolean(),
'description' => "Whether this post is the best answer in a question",
'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';
}
]
);
// Duplicate fields that have different names in posts
//$defaultFields['topic'] = $defaultFields['item'];
// Remove duplicated fields
unset( $defaultFields['item'] );
return array_merge( $defaultFields, $postFields );
}
/**
* Return the definite article, but without the item type
*
* @return string
*/
public static function definiteArticleNoItem($post, $options = array())
{
$type = $post->item()->isQuestion() ? 'answer_lc' : 'post_lc';
return \IPS\Member::loggedIn()->language()->addToStack($type, FALSE, $options);
}
}
@@ -0,0 +1,319 @@
<?php
/**
* @brief GraphQL: Topic Type
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 10 May 2017
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Types;
use GraphQL\Type\Definition\ObjectType;
use IPS\Api\GraphQL\_TypeRegistry;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* TopicType for GraphQL API
*/
class _TopicType extends \IPS\Content\Api\GraphQL\ItemType
{
/*
* @brief The item classname we use for this type
*/
protected static $itemClass = '\IPS\forums\Topic';
/*
* @brief GraphQL type name
*/
protected static $typeName = 'forums_Topic';
/*
* @brief GraphQL type description
*/
protected static $typeDescription = 'A topic';
/*
* @brief Follow data passed in to FollowType resolver
*/
protected static $followData = array('app' => 'forums', 'area' => 'topic');
/**
* Return the fields available in this type
*
* @return array
*/
public function fields()
{
// Extend our fields with topic-specific stuff
$defaultFields = parent::fields();
$topicFields = array(
'forum' => [
'type' => \IPS\forums\api\GraphQL\TypeRegistry::forum(),
'resolve' => function ($item) {
return $item->container();
}
],
'isArchived' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
return (bool) $topic->isArchived();
}
],
'isHot' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
foreach( $topic->stats(FALSE) as $k => $v )
{
if( \in_array( $k, $topic->hotStats ) )
{
return TRUE;
}
}
return FALSE;
}
],
/* SOLVED STUFF */
'isSolved' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
return (bool) $topic->isSolved();
}
],
'solvedId' => [
'type' => TypeRegistry::int(),
'resolve' => function ($topic) {
if( $topic->isSolved() ){
return $topic->mapped('solved_comment_id');
}
}
],
'canMarkSolved' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
return $topic->canSolve();
}
],
'solvedComment' => [
'type' => \IPS\forums\api\GraphQL\TypeRegistry::post(),
'resolve' => function ($topic) {
if( !$topic->isSolved() )
{
return NULL;
}
try
{
$solved = \IPS\forums\Topic\Post::load( $topic->mapped('solved_comment_id') );
return $solved;
}
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) {
return !!$topic->topic_answered_pid;
}
],
'bestAnswerID' => [
'type' => TypeRegistry::id(),
'resolve' => function ($topic) {
if( !$topic->isQuestion() )
{
return NULL;
}
return $topic->topic_answered_pid;
}
],
'canSetBestAnswer' => [
'type' => TypeRegistry::boolean(),
'resolve' => function ($topic) {
return $topic->canSetBestAnswer();
}
]
);
// Questions get their comment count reduced by one in the model, but for the API
// we'll reverse that change to keep topics & questions consistent.
$topicFields['postCount'] = $defaultFields['commentCount'];
$topicFields['postCount']['resolve'] = function ($topic, $args) {
if( $args['includeHidden'] )
{
return $topic->commentCount() + ( $topic->isQuestion() ? 1 : 0 );
}
return $topic->mapped('num_comments');
};
// Duplicate fields that have different names in topics
//$topicFields['forum'] = $defaultFields['container'];
$topicFields['posts'] = $defaultFields['comments'];
$topicFields['lastPostAuthor'] = $defaultFields['lastCommentAuthor'];
$topicFields['lastPostDate'] = $defaultFields['lastCommentDate'];
// Remove duplicated fields
unset( $defaultFields['container'] );
unset( $defaultFields['comments'] );
unset( $defaultFields['commentCount'] );
unset( $defaultFields['lastCommentAuthor'] );
unset( $defaultFields['lastCommentDate'] );
return array_merge( $defaultFields, $topicFields );
}
public static function args()
{
return array_merge( parent::args(), array(
'password' => [
'type' => TypeRegistry::string()
]
));
}
/**
* Return item permission fields.
* Here we adjust the resolver for the commentInformation field to check whether this is
* a poll-only topic.
*
* @return string|null
*/
public static function getItemPermissionFields()
{
$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 ) )
{
return 'topic_poll_can_comment';
}
else
{
return $existingResolver($topic, $args, $context);
}
};
return $defaultFields;
}
/**
* Return the available sorting options
*
* @return array
*/
public static function getOrderByOptions()
{
$defaultArgs = parent::getOrderByOptions();
return array_merge( $defaultArgs, array('last_comment', 'num_comments', 'views', 'author_name', 'last_comment_name', 'date', 'votes') );
}
/**
* Get the comment type that goes with this item type
*
* @return ObjectType
*/
protected static function getCommentType()
{
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);
}
}
@@ -0,0 +1,41 @@
<?php
/**
* @brief GraphQL: Vote Type
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Community Suite
* @since 25 Jan 2019
* @version SVN_VERSION_NUMBER
*/
namespace IPS\forums\api\GraphQL\Types;
use IPS\Api\GraphQL\TypeRegistry;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* VoteType for GraphQL API
*/
class _VoteType extends \GraphQL\Type\Definition\EnumType
{
/**
* Get object type
*
* @return ObjectType
*/
public function __construct()
{
$config = [
'name' => 'forums_VoteType',
'values' => ['UP', 'DOWN']
];
parent::__construct( $config );
}
}