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-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Database\BaseResult;
|
2023-08-26 13:03:01 +00:00
|
|
|
use CodeIgniter\Model;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Fediverse\Entities\PreviewCard;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-08-26 13:03:01 +00:00
|
|
|
class PreviewCardModel extends Model
|
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_preview_cards';
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
2024-04-28 16:39:01 +00:00
|
|
|
* @var list<string>
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
2021-04-02 17:20:02 +00:00
|
|
|
protected $allowedFields = [
|
|
|
|
'id',
|
|
|
|
'url',
|
|
|
|
'title',
|
|
|
|
'description',
|
|
|
|
'type',
|
|
|
|
'author_name',
|
|
|
|
'author_url',
|
|
|
|
'provider_name',
|
|
|
|
'provider_url',
|
|
|
|
'image',
|
|
|
|
'html',
|
|
|
|
];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = PreviewCard::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-14 17:59:35 +00:00
|
|
|
public function getPreviewCardFromUrl(string $url): ?PreviewCard
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$hashedPreviewCardUrl = md5($url);
|
|
|
|
$cacheName =
|
2024-04-28 16:39:01 +00:00
|
|
|
config('Fediverse')
|
2021-05-19 16:35:13 +00:00
|
|
|
->cachePrefix .
|
2021-05-06 14:00:48 +00:00
|
|
|
"preview_card-{$hashedPreviewCardUrl}";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
|
|
|
$found = $this->where('url', $url)
|
|
|
|
->first();
|
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
public function getPostPreviewCard(string $postId): ?PreviewCard
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName =
|
2024-04-28 16:39:01 +00:00
|
|
|
config('Fediverse')
|
2021-08-13 11:07:29 +00:00
|
|
|
->cachePrefix . "post#{$postId}_preview_card";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$found = $this->join(
|
2023-08-26 13:03:01 +00:00
|
|
|
'fediverse_posts_preview_cards',
|
|
|
|
'fediverse_posts_preview_cards.preview_card_id = id',
|
2021-04-22 17:20:28 +00:00
|
|
|
'inner',
|
2021-04-02 17:20:02 +00:00
|
|
|
)
|
2021-08-13 11:07:29 +00:00
|
|
|
->where('post_id', service('uuid') ->fromString($postId) ->getBytes())
|
2021-04-22 17:20:28 +00:00
|
|
|
->first();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function deletePreviewCard(int $id, string $url): BaseResult | bool
|
2021-04-22 17:20:28 +00:00
|
|
|
{
|
|
|
|
$hashedPreviewCardUrl = md5($url);
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2024-04-28 16:39:01 +00:00
|
|
|
->delete(config('Fediverse') ->cachePrefix . "preview_card-{$hashedPreviewCardUrl}");
|
2021-04-22 17:20:28 +00:00
|
|
|
|
|
|
|
return $this->delete($id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
}
|