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-05-12 14:00:25 +00:00
|
|
|
use CodeIgniter\Database\BaseResult;
|
2021-05-06 14:00:48 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
|
|
|
use DateTimeInterface;
|
2023-08-26 13:03:01 +00:00
|
|
|
use Michalsn\Uuid\UuidModel;
|
2023-08-27 13:26:06 +00:00
|
|
|
use Modules\Fediverse\Config\Fediverse;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Fediverse\Entities\Activity;
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2023-08-26 13:03:01 +00:00
|
|
|
class ActivityModel 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_activities';
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
protected $uuidFields = ['id', 'post_id'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2022-09-08 11:50:23 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $afterInsert = ['notify'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $afterUpdate = ['notify'];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $allowedFields = [
|
|
|
|
'id',
|
|
|
|
'actor_id',
|
|
|
|
'target_actor_id',
|
2021-08-13 11:07:29 +00:00
|
|
|
'post_id',
|
2021-04-02 17:20:02 +00:00
|
|
|
'type',
|
|
|
|
'payload',
|
2021-12-14 16:41:10 +00:00
|
|
|
'status',
|
2021-04-02 17:20:02 +00:00
|
|
|
'scheduled_at',
|
|
|
|
];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Activity::class;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @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-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
protected $updatedField;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function getActivityById(string $activityId): ?Activity
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName =
|
2023-08-27 13:26:06 +00:00
|
|
|
config(Fediverse::class)
|
2021-05-19 16:35:13 +00:00
|
|
|
->cachePrefix . "activity#{$activityId}";
|
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$found = $this->find($activityId);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a new activity record in the database
|
|
|
|
*
|
2022-07-03 16:42:20 +00:00
|
|
|
* @param Time|null $scheduledAt
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
|
|
|
public function newActivity(
|
2021-05-06 14:00:48 +00:00
|
|
|
string $type,
|
|
|
|
int $actorId,
|
|
|
|
?int $targetActorId,
|
2021-08-13 11:07:29 +00:00
|
|
|
?string $postId,
|
2021-05-06 14:00:48 +00:00
|
|
|
string $payload,
|
|
|
|
DateTimeInterface $scheduledAt = null,
|
2021-06-21 10:04:18 +00:00
|
|
|
?string $taskStatus = null
|
2021-05-19 16:35:13 +00:00
|
|
|
): BaseResult | int | string | false {
|
2021-04-02 17:20:02 +00:00
|
|
|
return $this->insert(
|
|
|
|
[
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => $actorId,
|
2021-04-02 17:20:02 +00:00
|
|
|
'target_actor_id' => $targetActorId,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => $postId,
|
|
|
|
'type' => $type === 'Undo' ? $type . '_' . (json_decode(
|
|
|
|
$payload,
|
|
|
|
true
|
|
|
|
))['object']['type'] : $type,
|
|
|
|
'payload' => $payload,
|
2021-04-02 17:20:02 +00:00
|
|
|
'scheduled_at' => $scheduledAt,
|
2023-06-12 14:47:38 +00:00
|
|
|
'status' => $taskStatus,
|
2021-04-02 17:20:02 +00:00
|
|
|
],
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* @return Activity[]
|
2021-05-14 17:59:35 +00:00
|
|
|
*/
|
|
|
|
public function getScheduledActivities(): array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2022-04-14 14:33:53 +00:00
|
|
|
return $this->where('`scheduled_at` <= UTC_TIMESTAMP()', null, false)
|
2021-12-14 16:41:10 +00:00
|
|
|
->where('status', 'queued')
|
2021-04-02 17:20:02 +00:00
|
|
|
->orderBy('scheduled_at', 'ASC')
|
|
|
|
->findAll();
|
|
|
|
}
|
2022-09-08 11:50:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, array<string|int, mixed>> $data
|
|
|
|
* @return array<string, array<string|int, mixed>>
|
|
|
|
*/
|
|
|
|
protected function notify(array $data): array
|
|
|
|
{
|
2022-12-07 14:00:38 +00:00
|
|
|
$activity = (new self())->find(is_array($data['id']) ? $data['id'][0] : $data['id']);
|
2022-09-08 11:50:23 +00:00
|
|
|
|
|
|
|
if (! $activity instanceof Activity) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($activity->target_actor_id === $activity->actor_id) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify only if incoming activity (with status set to NULL) is created
|
|
|
|
if ($activity->status !== null) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($activity->type === 'Follow') {
|
|
|
|
(new NotificationModel())->insert([
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => $activity->actor_id,
|
2022-09-08 11:50:23 +00:00
|
|
|
'target_actor_id' => $activity->target_actor_id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'activity_id' => $activity->id,
|
|
|
|
'type' => 'follow',
|
|
|
|
'created_at' => $activity->created_at,
|
2022-09-08 11:50:23 +00:00
|
|
|
]);
|
|
|
|
} elseif ($activity->type === 'Undo_Follow') {
|
|
|
|
(new NotificationModel())->builder()
|
|
|
|
->delete([
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => $activity->actor_id,
|
2022-09-08 11:50:23 +00:00
|
|
|
'target_actor_id' => $activity->target_actor_id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'follow',
|
2022-09-08 11:50:23 +00:00
|
|
|
]);
|
|
|
|
} elseif (in_array($activity->type, ['Create', 'Like', 'Announce'], true) && $activity->post_id !== null) {
|
|
|
|
(new NotificationModel())->insert([
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => $activity->actor_id,
|
2022-09-08 11:50:23 +00:00
|
|
|
'target_actor_id' => $activity->target_actor_id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => $activity->post_id,
|
|
|
|
'activity_id' => $activity->id,
|
|
|
|
'type' => match ($activity->type) {
|
|
|
|
'Create' => 'reply',
|
|
|
|
'Like' => 'like',
|
2022-09-08 11:50:23 +00:00
|
|
|
'Announce' => 'share',
|
|
|
|
},
|
|
|
|
'created_at' => $activity->created_at,
|
|
|
|
]);
|
|
|
|
} elseif (in_array($activity->type, ['Undo_Like', 'Undo_Announce'], true) && $activity->post_id !== null) {
|
|
|
|
(new NotificationModel())->builder()
|
|
|
|
->delete([
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => $activity->actor_id,
|
2022-09-08 11:50:23 +00:00
|
|
|
'target_actor_id' => $activity->target_actor_id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'post_id' => service('uuid')
|
2022-09-08 11:50:23 +00:00
|
|
|
->fromString($activity->post_id)
|
|
|
|
->getBytes(),
|
|
|
|
'type' => match ($activity->type) {
|
2023-06-12 14:47:38 +00:00
|
|
|
'Undo_Like' => 'like',
|
2022-09-08 11:50:23 +00:00
|
|
|
'Undo_Announce' => 'share',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|