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,40 @@
|
||||
*/
|
||||
|
||||
namespace IPS\core\api\GraphQL\Mutations;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use InvalidArgumentException;
|
||||
use IPS\Api\GraphQL\SafeException;
|
||||
use IPS\Api\GraphQL\TypeRegistry;
|
||||
use IPS\Api\GraphQL\Types\FollowType;
|
||||
use IPS\Application;
|
||||
use IPS\Content;
|
||||
use IPS\core\Followed\Follow as FollowClass;
|
||||
use IPS\Db;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Notification;
|
||||
use OutOfRangeException;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function get_class;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Follow something mutation for GraphQL API
|
||||
*/
|
||||
class _Follow
|
||||
class Follow
|
||||
{
|
||||
/*
|
||||
* @brief Query description
|
||||
*/
|
||||
public static $description = "Follow a node, item or member";
|
||||
public static string $description = "Follow a node, item or member";
|
||||
|
||||
/*
|
||||
* Mutation arguments
|
||||
@@ -50,8 +65,10 @@ class _Follow
|
||||
|
||||
/**
|
||||
* Return the mutation return type
|
||||
*
|
||||
* @return FollowType
|
||||
*/
|
||||
public function type()
|
||||
public function type() : FollowType
|
||||
{
|
||||
return TypeRegistry::follow();
|
||||
}
|
||||
@@ -61,46 +78,45 @@ class _Follow
|
||||
* @todo this is basically copied and pasted from notifications.php which isn't ideal, so we
|
||||
* might want to consider refactoring to abstract this functionality.
|
||||
*
|
||||
* @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
|
||||
* @return array
|
||||
*/
|
||||
public function resolve($val, $args)
|
||||
public function resolve( mixed $val, array $args ) : array
|
||||
{
|
||||
if( !\IPS\Member::loggedIn()->member_id )
|
||||
if( !Member::loggedIn()->member_id )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'NOT_LOGGED_IN', 'GQL/0001/6', 403 );
|
||||
throw new SafeException( 'NOT_LOGGED_IN', 'GQL/0001/6', 403 );
|
||||
}
|
||||
/* Get class */
|
||||
try
|
||||
{
|
||||
$class = \IPS\core\Followed\Follow::getClassToFollow( $args['app'], $args['area'] );
|
||||
$class = FollowClass::getClassToFollow( $args['app'], $args['area'] );
|
||||
}
|
||||
catch( \InvalidArgumentException $e )
|
||||
catch( InvalidArgumentException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'NOT_FOUND', 'GQL/0001/4', 404 );
|
||||
throw new SafeException( 'NOT_FOUND', 'GQL/0001/4', 404 );
|
||||
}
|
||||
|
||||
if( $args['app'] == 'core' and $args['area'] == 'member' )
|
||||
{
|
||||
/* You can't follow yourself */
|
||||
if( $args['id'] == \IPS\Member::loggedIn()->member_id )
|
||||
if( $args['id'] == Member::loggedIn()->member_id )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'CANT_FOLLOW_SELF', 'GQL/0001/1', 403 );
|
||||
throw new SafeException( 'CANT_FOLLOW_SELF', 'GQL/0001/1', 403 );
|
||||
}
|
||||
|
||||
/* Following disabled */
|
||||
$member = \IPS\Member::load( $args['id'] );
|
||||
$member = Member::load( $args['id'] );
|
||||
|
||||
if( !$member->member_id )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'CANT_FOLLOW_MEMBER', 'GQL/0001/2', 403 );
|
||||
throw new SafeException( 'CANT_FOLLOW_MEMBER', 'GQL/0001/2', 403 );
|
||||
}
|
||||
|
||||
if( $member->members_bitoptions['pp_setting_moderate_followers'] and !\IPS\Member::loggedIn()->following( 'core', 'member', $member->member_id ) )
|
||||
if( $member->members_bitoptions['pp_setting_moderate_followers'] and !Member::loggedIn()->following( 'core', 'member', $member->member_id ) )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'CANT_FOLLOW_MEMBER', 'GQL/0001/3', 403 );
|
||||
throw new SafeException( 'CANT_FOLLOW_MEMBER', 'GQL/0001/3', 403 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,49 +131,54 @@ class _Follow
|
||||
$thing = NULL;
|
||||
try
|
||||
{
|
||||
if ( \in_array( 'IPS\Node\Model', class_parents( $class ) ) )
|
||||
if ( in_array( 'IPS\Node\Model', class_parents( $class ) ) )
|
||||
{
|
||||
/* @var Model $class */
|
||||
$thing = $class::loadAndCheckPerms( (int) $args['id'] );
|
||||
}
|
||||
elseif ( $class == 'IPS\Member\Club' )
|
||||
{
|
||||
/* @var Member\Club $class */
|
||||
$thing = $class::loadAndCheckPerms( (int) $args['id'] );
|
||||
}
|
||||
elseif ( $class != "IPS\Member" )
|
||||
{
|
||||
/* @var Content $class */
|
||||
$thing = $class::loadAndCheckPerms( (int) $args['id'] );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( !$class instanceof \IPS\Content\Followable )
|
||||
if( !IPS::classUsesTrait( $class, 'IPS\Content\Followable' ) )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
/* @var Member $class */
|
||||
$thing = $class::load( (int) $args['id'] );
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'NOT_FOUND', 'GQL/0001/5', 404 );
|
||||
throw new SafeException( 'NOT_FOUND', 'GQL/0001/5', 404 );
|
||||
}
|
||||
|
||||
/* Do we follow it? */
|
||||
try
|
||||
{
|
||||
$current = \IPS\Db::i()->select( '*', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_rel_id=? AND follow_member_id=?', $args['app'], $args['area'], $args['id'], \IPS\Member::loggedIn()->member_id ) )->first();
|
||||
$current = Db::i()->select( '*', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_rel_id=? AND follow_member_id=?', $args['app'], $args['area'], $args['id'], Member::loggedIn()->member_id ) )->first();
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
$current = FALSE;
|
||||
}
|
||||
|
||||
/* Insert */
|
||||
$save = array(
|
||||
'follow_id' => md5( $args['app'] . ';' . $args['area'] . ';' . $args['id'] . ';' . \IPS\Member::loggedIn()->member_id ),
|
||||
'follow_id' => md5( $args['app'] . ';' . $args['area'] . ';' . $args['id'] . ';' . Member::loggedIn()->member_id ),
|
||||
'follow_app' => $args['app'],
|
||||
'follow_area' => $args['area'],
|
||||
'follow_rel_id' => $args['id'],
|
||||
'follow_member_id' => \IPS\Member::loggedIn()->member_id,
|
||||
'follow_member_id' => Member::loggedIn()->member_id,
|
||||
'follow_is_anon' => $args['anonymous'],
|
||||
'follow_added' => time(),
|
||||
'follow_notify_do' => $args['type'] == 'NONE' ? 0 : 1,
|
||||
@@ -168,11 +189,11 @@ class _Follow
|
||||
);
|
||||
if ( $current )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_follow', $save, array( 'follow_id=?', $current['follow_id'] ) );
|
||||
Db::i()->update( 'core_follow', $save, array( 'follow_id=?', $current['follow_id'] ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Db::i()->insert( 'core_follow', $save );
|
||||
Db::i()->insert( 'core_follow', $save );
|
||||
}
|
||||
|
||||
/* Also follow all nodes if following club */
|
||||
@@ -185,11 +206,11 @@ class _Follow
|
||||
$followArea = mb_strtolower( mb_substr( $node['node_class'], mb_strrpos( $node['node_class'], '\\' ) + 1 ) );
|
||||
|
||||
$save = array(
|
||||
'follow_id' => md5( $followApp . ';' . $followArea . ';' . $node['node_id'] . ';' . \IPS\Member::loggedIn()->member_id ),
|
||||
'follow_id' => md5( $followApp . ';' . $followArea . ';' . $node['node_id'] . ';' . Member::loggedIn()->member_id ),
|
||||
'follow_app' => $followApp,
|
||||
'follow_area' => $followArea,
|
||||
'follow_rel_id' => $node['node_id'],
|
||||
'follow_member_id' => \IPS\Member::loggedIn()->member_id,
|
||||
'follow_member_id' => Member::loggedIn()->member_id,
|
||||
'follow_is_anon' => $args['anonymous'],
|
||||
'follow_added' => time(),
|
||||
'follow_notify_do' => $args['type'] == 'NONE' ? 0 : 1,
|
||||
@@ -198,25 +219,25 @@ class _Follow
|
||||
'follow_notify_sent' => 0,
|
||||
'follow_visible' => 1,
|
||||
);
|
||||
\IPS\Db::i()->insert( 'core_follow', $save, TRUE );
|
||||
Db::i()->insert( 'core_follow', $save, TRUE );
|
||||
}
|
||||
}
|
||||
|
||||
/* Send notification if following member */
|
||||
if( $class == "IPS\Member" )
|
||||
{
|
||||
$notification = new \IPS\Notification( \IPS\Application::load( 'core' ), 'member_follow', \IPS\Member::loggedIn(), array( \IPS\Member::loggedIn() ) );
|
||||
$notification = new Notification( Application::load( 'core' ), 'member_follow', Member::loggedIn(), array( Member::loggedIn() ) );
|
||||
$notification->recipients->attach( $thing );
|
||||
$notification->send();
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ( \in_array( 'IPS\Node\Model', class_parents( $class ) ) )
|
||||
if ( in_array( 'IPS\Node\Model', class_parents( $class ) ) )
|
||||
{
|
||||
$return = array_merge($return, array(
|
||||
'node' => $thing,
|
||||
'nodeClass' => \get_class( $thing )
|
||||
'nodeClass' => get_class( $thing )
|
||||
));
|
||||
}
|
||||
else if( $class == 'IPS\Member' )
|
||||
@@ -233,7 +254,7 @@ class _Follow
|
||||
{
|
||||
$return = array_merge($return, array(
|
||||
'item' => $thing,
|
||||
'itemClass' => \get_class( $thing )
|
||||
'itemClass' => get_class( $thing )
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user