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,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;
|
||||
|
||||
Reference in new issue
Block a user