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
|
|
|
|
|
|
|
use CodeIgniter\Events\Events;
|
2023-08-26 13:03:01 +00:00
|
|
|
use Michalsn\Uuid\UuidModel;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Fediverse\Activities\LikeActivity;
|
|
|
|
use Modules\Fediverse\Activities\UndoActivity;
|
|
|
|
use Modules\Fediverse\Entities\Actor;
|
|
|
|
use Modules\Fediverse\Entities\Favourite;
|
|
|
|
use Modules\Fediverse\Entities\Post;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-08-26 13:03:01 +00:00
|
|
|
class FavouriteModel extends UuidModel
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-08-26 13:03:01 +00:00
|
|
|
protected $table = 'fediverse_favourites';
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
protected $uuidFields = ['post_id'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
protected $allowedFields = ['actor_id', 'post_id'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Favourite::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
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
|
|
|
protected $updatedField;
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
public function addFavourite(Actor $actor, Post $post, bool $registerActivity = true): void
|
2021-05-19 16:35:13 +00:00
|
|
|
{
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
$this->insert([
|
|
|
|
'actor_id' => $actor->id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => $post->id,
|
2021-04-02 17:20:02 +00:00
|
|
|
]);
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('PostModel', false)
|
2021-08-13 11:07:29 +00:00
|
|
|
->where('id', service('uuid') ->fromString($post->id) ->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->increment('favourites_count');
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
$likeActivity = new LikeActivity();
|
2021-05-19 16:35:13 +00:00
|
|
|
$likeActivity->set('actor', $actor->uri)
|
2021-08-13 11:07:29 +00:00
|
|
|
->set('object', $post->uri);
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
$activityId = model('ActivityModel', false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->newActivity(
|
|
|
|
'Like',
|
|
|
|
$actor->id,
|
2022-02-02 17:36:03 +00:00
|
|
|
$post->actor_id,
|
2021-08-13 11:07:29 +00:00
|
|
|
$post->id,
|
2021-05-19 16:35:13 +00:00
|
|
|
$likeActivity->toJSON(),
|
2021-08-13 11:07:29 +00:00
|
|
|
$post->published_at,
|
2021-05-19 16:35:13 +00:00
|
|
|
'queued',
|
|
|
|
);
|
|
|
|
|
2022-03-04 14:33:48 +00:00
|
|
|
$likeActivity->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' => $likeActivity->toJSON(),
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::trigger('on_post_favourite', $actor, $post);
|
2021-06-08 09:52:11 +00:00
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('PostModel', false)
|
2021-08-13 11:07:29 +00:00
|
|
|
->clearCache($post);
|
2021-06-08 09:52:11 +00:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
public function removeFavourite(Actor $actor, Post $post, bool $registerActivity = true): void
|
2021-05-19 16:35:13 +00:00
|
|
|
{
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transStart();
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('PostModel', false)
|
2021-08-13 11:07:29 +00:00
|
|
|
->where('id', service('uuid') ->fromString($post->id) ->getBytes())
|
2021-04-02 17:20:02 +00:00
|
|
|
->decrement('favourites_count');
|
|
|
|
|
2021-08-13 16:07:45 +00:00
|
|
|
$this->where([
|
|
|
|
'actor_id' => $actor->id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => service('uuid')
|
2021-08-13 16:07:45 +00:00
|
|
|
->fromString($post->id)
|
|
|
|
->getBytes(),
|
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->delete();
|
|
|
|
|
|
|
|
if ($registerActivity) {
|
|
|
|
$undoActivity = new UndoActivity();
|
|
|
|
// get like activity
|
2022-02-05 11:40:30 +00:00
|
|
|
$activity = model('ActivityModel', false)
|
2021-04-02 17:20:02 +00:00
|
|
|
->where([
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'Like',
|
2021-04-02 17:20:02 +00:00
|
|
|
'actor_id' => $actor->id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => service('uuid')
|
2021-08-13 11:07:29 +00:00
|
|
|
->fromString($post->id)
|
2021-04-02 17:20:02 +00:00
|
|
|
->getBytes(),
|
|
|
|
])
|
|
|
|
->first();
|
|
|
|
|
|
|
|
$likeActivity = new LikeActivity();
|
|
|
|
$likeActivity
|
2022-03-04 14:33:48 +00:00
|
|
|
->set('id', url_to('activity', esc($actor->username), $activity->id))
|
2021-04-02 17:20:02 +00:00
|
|
|
->set('actor', $actor->uri)
|
2021-08-13 11:07:29 +00:00
|
|
|
->set('object', $post->uri);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$undoActivity
|
|
|
|
->set('actor', $actor->uri)
|
|
|
|
->set('object', $likeActivity);
|
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
$activityId = model('ActivityModel', false)
|
2021-05-19 16:35:13 +00:00
|
|
|
->newActivity(
|
|
|
|
'Undo',
|
|
|
|
$actor->id,
|
2022-02-02 17:36:03 +00:00
|
|
|
$post->actor_id,
|
2021-08-13 11:07:29 +00:00
|
|
|
$post->id,
|
2021-05-19 16:35:13 +00:00
|
|
|
$undoActivity->toJSON(),
|
2021-08-13 11:07:29 +00:00
|
|
|
$post->published_at,
|
2021-05-19 16:35:13 +00:00
|
|
|
'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-08-13 11:07:29 +00:00
|
|
|
Events::trigger('on_post_undo_favourite', $actor, $post);
|
2021-06-08 09:52:11 +00:00
|
|
|
|
2022-02-05 11:40:30 +00:00
|
|
|
model('PostModel', false)
|
2021-08-13 11:07:29 +00:00
|
|
|
->clearCache($post);
|
2021-06-08 09:52:11 +00:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->db->transComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-13 16:07:45 +00:00
|
|
|
* Adds or removes favourite from database
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
public function toggleFavourite(Actor $actor, Post $post): void
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
if (
|
|
|
|
$this->where([
|
|
|
|
'actor_id' => $actor->id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => service('uuid')
|
2021-08-13 11:07:29 +00:00
|
|
|
->fromString($post->id)
|
2021-04-02 17:20:02 +00:00
|
|
|
->getBytes(),
|
2023-09-23 14:27:40 +00:00
|
|
|
])->first() instanceof Favourite
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
2021-08-13 11:07:29 +00:00
|
|
|
$this->removeFavourite($actor, $post);
|
2021-04-02 17:20:02 +00:00
|
|
|
} else {
|
2021-08-13 11:07:29 +00:00
|
|
|
$this->addFavourite($actor, $post);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|