2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2021 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ActivityPub\Models;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use ActivityPub\Entities\Actor;
|
2021-05-12 14:00:25 +00:00
|
|
|
use CodeIgniter\Database\Exceptions\DataException;
|
2021-04-22 17:20:28 +00:00
|
|
|
use CodeIgniter\Events\Events;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class ActorModel extends Model
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $table = 'activitypub_actors';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $allowedFields = [
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'username',
|
|
|
|
'domain',
|
|
|
|
'display_name',
|
|
|
|
'summary',
|
|
|
|
'private_key',
|
|
|
|
'public_key',
|
|
|
|
'avatar_image_url',
|
|
|
|
'avatar_image_mimetype',
|
|
|
|
'cover_image_url',
|
|
|
|
'cover_image_mimetype',
|
|
|
|
'inbox_url',
|
|
|
|
'outbox_url',
|
|
|
|
'followers_url',
|
|
|
|
'followers_count',
|
|
|
|
'notes_count',
|
|
|
|
'is_blocked',
|
|
|
|
];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Actor::class;
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function getActorById($id): Actor
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName = config('ActivityPub')->cachePrefix . "actor#{$id}";
|
|
|
|
if (!($found = cache($cacheName))) {
|
|
|
|
$found = $this->find($id);
|
|
|
|
|
|
|
|
cache()->save($cacheName, $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks for actor with username and domain,
|
|
|
|
* if no domain has been specified, the current host will be used
|
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getActorByUsername(
|
|
|
|
string $username,
|
|
|
|
?string $domain = null
|
|
|
|
): ?Actor {
|
2021-04-02 17:20:02 +00:00
|
|
|
// TODO: is there a better way?
|
|
|
|
helper('activitypub');
|
|
|
|
|
|
|
|
if (!$domain) {
|
|
|
|
$domain = get_current_domain();
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
$cacheName = "actor-{$username}-{$domain}";
|
|
|
|
if (!($found = cache($cacheName))) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$found = $this->where([
|
|
|
|
'username' => $username,
|
|
|
|
'domain' => $domain,
|
|
|
|
])->first();
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
cache()->save($cacheName, $found, DECADE);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getActorByUri($actorUri)
|
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$hashedActorUri = md5($actorUri);
|
|
|
|
$cacheName =
|
2021-05-06 14:00:48 +00:00
|
|
|
config('ActivityPub')->cachePrefix . "actor-{$hashedActorUri}";
|
2021-04-22 17:20:28 +00:00
|
|
|
if (!($found = cache($cacheName))) {
|
|
|
|
$found = $this->where('uri', $actorUri)->first();
|
|
|
|
|
|
|
|
cache()->save($cacheName, $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFollowers($actorId)
|
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName =
|
|
|
|
config('ActivityPub')->cachePrefix . "actor#{$actorId}_followers";
|
|
|
|
if (!($found = cache($cacheName))) {
|
|
|
|
$found = $this->join(
|
|
|
|
'activitypub_follows',
|
|
|
|
'activitypub_follows.actor_id = id',
|
|
|
|
'inner',
|
|
|
|
)
|
|
|
|
->where('activitypub_follows.target_actor_id', $actorId)
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save($cacheName, $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-22 17:20:28 +00:00
|
|
|
* Check if an existing actor is blocked using its uri.
|
|
|
|
* Returns FALSE if the actor doesn't exist
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function isActorBlocked(string $actorUri): bool
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
if ($actor = $this->getActorByUri($actorUri)) {
|
|
|
|
return $actor->is_blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves all blocked actors.
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return Actor[]
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function getBlockedActors(): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName = config('ActivityPub')->cachePrefix . 'blocked_actors';
|
|
|
|
if (!($found = cache($cacheName))) {
|
|
|
|
$found = $this->where('is_blocked', 1)->findAll();
|
|
|
|
|
|
|
|
cache()->save($cacheName, $found, DECADE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function blockActor($actorId): void
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$prefix = config('ActivityPub')->cachePrefix;
|
|
|
|
cache()->delete($prefix . 'blocked_actors');
|
|
|
|
cache()->deleteMatching($prefix . '*replies');
|
|
|
|
|
|
|
|
Events::trigger('on_block_actor', $actorId);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->update($actorId, ['is_blocked' => 1]);
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
public function unblockActor($actorId): void
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$prefix = config('ActivityPub')->cachePrefix;
|
|
|
|
cache()->delete($prefix . 'blocked_actors');
|
|
|
|
cache()->deleteMatching($prefix . '*replies');
|
|
|
|
|
|
|
|
Events::trigger('on_unblock_actor', $actorId);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->update($actorId, ['is_blocked' => 0]);
|
|
|
|
}
|
|
|
|
}
|