[COMPONENT][Notification] Make logic more generic and robust
Fixed various bugs Some important concepts to bear in mind: * Notification: Associated with activities, won't be reconstructed together with objects, can be thought of as transient * Attention: Associated with objects, will be reconstructed with them, can be thought as persistent * Notifications and Attentions have no direct implications. * Mentions are a specific form of attentions in notes, leads to the creation of Attentions. Finally, Potential PHP issue detected and reported: https://github.com/php/php-src/issues/8199 `static::method()` from a non static context (such as a class method) calls `__call`, rather than the expected `__callStatic`. Can be fixed by using `(static fn() => static::method())()`, but the usage of the magic method is strictly unnecessary in this case.
This commit is contained in:
@@ -24,12 +24,15 @@ declare(strict_types = 1);
|
||||
namespace App\Core;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Entity\Actor;
|
||||
use App\Util\Exception\NotFoundException;
|
||||
use App\Util\Formatting;
|
||||
use BadMethodCallException;
|
||||
use Component\Notification\Entity\Attention;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use Functional as F;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -37,6 +40,14 @@ use InvalidArgumentException;
|
||||
*/
|
||||
abstract class Entity
|
||||
{
|
||||
/**
|
||||
* @return string Returns the name of this entity's DB table
|
||||
*/
|
||||
public static function schemaName(): string
|
||||
{
|
||||
return static::schemaDef()['name'];
|
||||
}
|
||||
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
if (Formatting::startsWith($name, 'has')) {
|
||||
@@ -46,7 +57,7 @@ abstract class Entity
|
||||
$private_property_accessor = $private_property_accessor->bindTo($this, static::class);
|
||||
return $private_property_accessor($prop);
|
||||
}
|
||||
throw new BadMethodCallException('Non existent method ' . static::class . "::{$name} called with arguments: " . print_r($arguments, true));
|
||||
throw new BadMethodCallException('Non existent non-static method ' . static::class . "->{$name} called with arguments: " . var_export($arguments, true));
|
||||
}
|
||||
|
||||
abstract public static function schemaDef(): array;
|
||||
@@ -55,10 +66,8 @@ abstract class Entity
|
||||
* Create an instance of the called class or fill in the
|
||||
* properties of $obj with the associative array $args. Doesn't
|
||||
* persist the result
|
||||
*
|
||||
* @param null|mixed $obj
|
||||
*/
|
||||
public static function create(array $args, $obj = null)
|
||||
public static function create(array $args, mixed $obj = null)
|
||||
{
|
||||
$class = static::class;
|
||||
|
||||
@@ -126,7 +135,7 @@ abstract class Entity
|
||||
* Entity::getByPK([42, 'foo']);
|
||||
* Entity::getByPK(['key1' => 42, 'key2' => 'foo'])
|
||||
*
|
||||
* @return null|static
|
||||
* @throws \App\Util\Exception\DuplicateFoundException
|
||||
*/
|
||||
public static function getByPK(mixed $values): ?self
|
||||
{
|
||||
@@ -175,27 +184,28 @@ abstract class Entity
|
||||
}
|
||||
|
||||
/**
|
||||
* Who should be notified about this object?
|
||||
* Ids of the Actors that should be informed about this object.
|
||||
* BEWARE: If you call this, your object must have a serial integer id!
|
||||
*
|
||||
* @return array of ids of Actors
|
||||
* @return array int[] of Actor's id
|
||||
*/
|
||||
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
|
||||
public function getAttentionTargetIds(): array
|
||||
{
|
||||
// Additional actors that should know about this
|
||||
if (\array_key_exists('additional', $ids_already_known)) {
|
||||
return $ids_already_known['additional'];
|
||||
}
|
||||
return [];
|
||||
$attention = DB::findBy(Attention::class, [
|
||||
'object_type' => static::schemaName(),
|
||||
'object_id' => $this->getId(),
|
||||
]);
|
||||
return F\map($attention, fn ($cc) => $cc->getTargetId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Who should be notified about this object?
|
||||
* To whom should this be brought attention to?
|
||||
*
|
||||
* @return array of Actors
|
||||
* @return array Actor[]
|
||||
*/
|
||||
public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null, bool $include_additional = true): array
|
||||
public function getAttentionTargets(): array
|
||||
{
|
||||
$target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id, $include_additional);
|
||||
return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]);
|
||||
$target_ids = $this->getAttentionTargetIds();
|
||||
return DB::findBy(Actor::class, ['id' => $target_ids]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user