Skip to content
Snippets Groups Projects
ActivityModel.php 2.5 KiB
Newer Older
<?php

/**
 * @copyright  2021 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

namespace ActivityPub\Models;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Exceptions\DataException;
class ActivityModel extends UuidModel
{
    protected $table = 'activitypub_activities';
    protected $uuidFields = ['id', 'note_id'];

    protected $allowedFields = [
        'id',
        'actor_id',
        'target_actor_id',
        'note_id',
        'type',
        'payload',
        'status',
        'scheduled_at',
    ];

    /**
     * @var string
     */
    protected $returnType = Activity::class;
    /**
     * @var bool
     */
    public function getActivityById(string $activityId): ?Activity
        $cacheName =
            config('ActivityPub')->cachePrefix . "activity#{$activityId}";
        if (!($found = cache($cacheName))) {
            $found = $this->find($activityId);

            cache()->save($cacheName, $found, DECADE);
        }

        return $found;
    }

    /**
     * Inserts a new activity record in the database
     *
        string $type,
        int $actorId,
        ?int $targetActorId,
        ?string $noteId,
        string $payload,
        DateTimeInterface $scheduledAt = null,
        ?string $status = null
    ): BaseResult|int|string|false {
        return $this->insert(
            [
                'actor_id' => $actorId,
                'target_actor_id' => $targetActorId,
                'note_id' => $noteId,
                'type' => $type,
                'payload' => $payload,
                'scheduled_at' => $scheduledAt,
                'status' => $status,
            ],
            true,
        );
    }

    /**
     * @return Activity[] 
     */
    public function getScheduledActivities(): array
    {
        return $this->where('`scheduled_at` <= NOW()', null, false)
            ->where('status', 'queued')
            ->orderBy('scheduled_at', 'ASC')
            ->findAll();
    }
}