Version 5.0.13

This commit is contained in:
Neo committed 2025-12-19 19:04:57 -08:00
1 parent b89858b2ff
commit 5efe744c5f
1407 files changed
+69690 -30177

No files matched your search

+157 -2
View File
@@ -14,13 +14,19 @@ namespace IPS\core\Followed;
/* To prevent PHP errors (extending class does not exist) revealing path */
use InvalidArgumentException;
use IPS\Api\Webhook;
use IPS\Application;
use IPS\Content\Tag;
use IPS\DateTime;
use IPS\Db;
use IPS\Events\Event;
use IPS\IPS;
use IPS\Member;
use IPS\Member\Club;
use IPS\Node\Model;
use IPS\Patterns\ActiveRecord;
use OutOfRangeException;
use UnderflowException;
use function defined;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
@@ -59,6 +65,17 @@ class Follow extends ActiveRecord
*/
protected static array $multitonMap = array();
/**
* Set Default Values (overriding $defaultValues)
*
* @return void
*/
protected function setDefaultValues() : void
{
$this->notify_meta = '';
$this->visible = 1;
}
/**
* Save
*
@@ -66,16 +83,24 @@ class Follow extends ActiveRecord
*/
public function save(): void
{
$sendWebhook = false;
if( $this->_new )
{
$this->visible = 1;
$this->notify_sent = 0;
$this->added = time();
$sendWebhook = true;
}
$this->id = md5( $this->app . ';' . $this->area . ';' . $this->rel_id . ';' . $this->member_id );
parent::save();
if ( $sendWebhook )
{
Webhook::fire( 'content_followed', $this);
}
$this->clearFollowCache();
}
/**
@@ -93,6 +118,7 @@ class Follow extends ActiveRecord
* @apiresponse datetime|null followSent Date and time the last notification was sent, or NULL if none has been sent
* @apiresponse string followName Textual representation of the content that was followed (title, name, etc.)
* @apiresponse string followUrl URL to the content that was followed
* @apiresponse \IPS\Member followMember Member following the item
*/
public function apiOutput( Member $authorizedMember = NULL ): array|null
{
@@ -186,6 +212,7 @@ class Follow extends ActiveRecord
'followSent' => $this->notify_sent ? DateTime::ts( $this->notify_sent )->rfc3339() : NULL,
'followName' => $name ?? '',
'followUrl' => ( isset( $url ) ? (string) $url : '' ),
'followMember' => Member::load($this->member_id)->apiOutput($authorizedMember)
);
}
@@ -197,9 +224,116 @@ class Follow extends ActiveRecord
'core' => [
'member' => Member::class,
'club' => Club::class,
'tag' => Tag::class
],
];
/**
* Return the app and area parameters
*
* @param object $object
* @return array
*/
public static function getFollowParameters( object $object ) : array
{
$objectClass = get_class( $object );
foreach( static::$specialFollowClasses as $app => $areas )
{
foreach( $areas as $area => $class )
{
if( $class == $objectClass )
{
return [
'app' => $app,
'area' => $area
];
}
}
}
$itemClass = ( $object instanceof Model ) ? $object::$contentItemClass : get_class( $object );
return [
'app' => $itemClass::$application,
'area' => mb_strtolower( mb_substr( $objectClass, mb_strrpos( $objectClass, '\\' ) + 1 ) )
];
}
/**
* Find a record based on an object and a member
*
* @param object $object
* @param Member|null $member
* @return static|null
* @throws OutOfRangeException
*/
public static function loadByObject( object $object, ?Member $member=null ) : static|null
{
$member = $member ?: Member::loggedIn();
/* Guests can never follow anything */
if ( !$member->member_id )
{
throw new OutOfRangeException;
}
$params = static::getFollowParameters( $object );
$idColumn = $object::$databaseColumnId;
try
{
$row = Db::i()->select( '*', 'core_follow', [ 'follow_app=? and follow_area=? and follow_rel_id=? and follow_member_id=?', $params['app'], $params['area'], $object->$idColumn, $member->member_id ] )->first();
return static::constructFromData( $row );
}
catch( UnderflowException )
{
throw new OutOfRangeException;
}
}
/**
* @return string
*/
public function get__class() : string
{
if( isset( static::$specialFollowClasses[ $this->app ][ $this->area ] ) )
{
return static::$specialFollowClasses[ $this->app ][ $this->area ];
}
return 'IPS\\' . $this->app . '\\' . IPS::mb_ucfirst( $this->area );
}
/**
* @return ActiveRecord|null
*/
public function get__item() : ActiveRecord|null
{
$class = $this->_class;
try
{
return $class::load( $this->rel_id );
}
catch( OutOfRangeException )
{
return null;
}
}
/**
* Set as many properties as we can get from the object
*
* @param object $object
* @return void
*/
public function set_object( object $object ) : void
{
$params = static::getFollowParameters( $object );
$this->app = $params['app'];
$this->area = $params['area'];
$idColumn = $object::$databaseColumnId;
$this->rel_id = $object->$idColumn;
}
/**
* Get the class to follow
*
@@ -225,4 +359,25 @@ class Follow extends ActiveRecord
return $classToFollow;
}
/**
* Delete corresponding record from the follow cache
*
* @return void
*/
protected function clearFollowCache() : void
{
Db::i()->delete( 'core_follow_count_cache', array( 'id=? AND class=?', $this->rel_id, $this->_class ) );
}
public function delete(): void
{
Webhook::fire( 'content_unfollowed', $this );
Event::fire( 'onUnfollow', Member::load( $this->member_id ), [ $this->_item ] );
parent::delete();
/* Delete from the cache */
$this->clearFollowCache();
}
}