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;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Fediverse\Entities\Activity;
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-27 10:58:22 +00:00
|
|
|
class ActivityModel extends BaseUuidModel
|
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 = '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
|
|
|
|
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 =
|
2021-08-23 11:05:16 +00:00
|
|
|
config('Fediverse')
|
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
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @param Time $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(
|
|
|
|
[
|
|
|
|
'actor_id' => $actorId,
|
|
|
|
'target_actor_id' => $targetActorId,
|
2021-08-13 11:07:29 +00:00
|
|
|
'post_id' => $postId,
|
2021-04-02 17:20:02 +00:00
|
|
|
'type' => $type,
|
|
|
|
'payload' => $payload,
|
|
|
|
'scheduled_at' => $scheduledAt,
|
2021-12-14 16:41:10 +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
|
|
|
{
|
|
|
|
return $this->where('`scheduled_at` <= NOW()', 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();
|
|
|
|
}
|
|
|
|
}
|