2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2021 Ad Aures
|
2021-04-02 17:20:02 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-08-23 11:05:16 +00:00
|
|
|
namespace Modules\Fediverse\Models;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
use CodeIgniter\Events\Events;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
2021-05-19 16:35:13 +00:00
|
|
|
use Exception;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Fediverse\Activities\FollowActivity;
|
|
|
|
use Modules\Fediverse\Activities\UndoActivity;
|
|
|
|
use Modules\Fediverse\Entities\Actor;
|
|
|
|
use Modules\Fediverse\Entities\Follow;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-08-27 10:58:22 +00:00
|
|
|
class FollowModel extends BaseModel
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-08-27 10:58:22 +00:00
|
|
|
protected $table = 'follows';
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $allowedFields = ['actor_id', 'target_actor_id'];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Follow::class;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $useTimestamps = true;
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
protected $updatedField;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-06 14:00:48 +00:00
|
|
|
* @param Actor $actor Actor that is following
|
|
|
|
* @param Actor $targetActor Actor that is being followed
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
public function addFollower(Actor $actor, Actor $targetActor, bool $registerActivity = true): void
|
|
|
|
{
|
2021-04-02 17:20:02 +00:00
|
|
|
try {
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
$this->insert([
|
|
|
|
'actor_id' => $actor->id,
|
|
|
|
'target_actor_id' => $targetActor->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// increment followers_count for target actor
|
2022-02-05 11:40:30 +00:00
|
|
|
model('ActorModel', false)
|
2021-04-02 17:20:02 +00:00
|
|
|
->where('id', $targetActor->id)
|
|
|
|
->increment('followers_count');
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
$followActivity = new FollowActivity();
|
|
|
|
|
|
|
|
$followActivity
|
|
|
|
->set('actor', $actor->uri)
|
|
|
|
->set('object', $targetActor->uri);
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
$activityId = model('ActivityModel', false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->newActivity(
|
|
|
|
'Follow',
|
|
|
|
$actor->id,
|
|
|
|
$targetActor->id,
|
|
|
|
null,
|
|
|
|
$followActivity->toJSON(),
|
|
|
|
Time::now(),
|
|
|
|
'queued',
|
|
|
|
);
|
|
|
|
|
2022-03-04 14:33:48 +00:00
|
|
|
$followActivity->set('id', url_to('activity', esc($actor->username), $activityId));
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('ActivityModel', false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->update($activityId, [
|
|
|
|
'payload' => $followActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_follow', $actor, $targetActor);
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('ActorModel', false)
|
2021-06-08 09:52:11 +00:00
|
|
|
->clearCache($targetActor);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
2021-05-14 17:59:35 +00:00
|
|
|
} catch (Exception) {
|
2021-04-02 17:20:02 +00:00
|
|
|
// follow already exists, do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-06 14:00:48 +00:00
|
|
|
* @param Actor $actor Actor that is unfollowing
|
|
|
|
* @param Actor $targetActor Actor that is being unfollowed
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
public function removeFollower(Actor $actor, Actor $targetActor, bool $registerActivity = true): void
|
|
|
|
{
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
$this->where([
|
|
|
|
'actor_id' => $actor->id,
|
|
|
|
'target_actor_id' => $targetActor->id,
|
|
|
|
])->delete();
|
|
|
|
|
|
|
|
// decrement followers_count for target actor
|
2022-02-05 11:40:30 +00:00
|
|
|
model('ActorModel', false)
|
2021-04-02 17:20:02 +00:00
|
|
|
->where('id', $targetActor->id)
|
|
|
|
->decrement('followers_count');
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
$undoActivity = new UndoActivity();
|
|
|
|
// get follow activity from database
|
2022-02-05 11:40:30 +00:00
|
|
|
$followActivity = model('ActivityModel', false)
|
2021-04-02 17:20:02 +00:00
|
|
|
->where([
|
|
|
|
'type' => 'Follow',
|
|
|
|
'actor_id' => $actor->id,
|
|
|
|
'target_actor_id' => $targetActor->id,
|
|
|
|
])
|
|
|
|
->first();
|
|
|
|
|
|
|
|
$undoActivity
|
|
|
|
->set('actor', $actor->uri)
|
|
|
|
->set('object', $followActivity->payload);
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
$activityId = model('ActivityModel', false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->newActivity(
|
|
|
|
'Undo',
|
|
|
|
$actor->id,
|
|
|
|
$targetActor->id,
|
|
|
|
null,
|
|
|
|
$undoActivity->toJSON(),
|
|
|
|
Time::now(),
|
|
|
|
'queued',
|
|
|
|
);
|
|
|
|
|
2022-03-04 14:33:48 +00:00
|
|
|
$undoActivity->set('id', url_to('activity', esc($actor->username), $activityId));
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('ActivityModel', false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->update($activityId, [
|
|
|
|
'payload' => $undoActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::trigger('on_undo_follow', $actor, $targetActor);
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('ActorModel', false)
|
2021-06-08 09:52:11 +00:00
|
|
|
->clearCache($targetActor);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
}
|
|
|
|
}
|