Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user