pixelfed/app/Models/CustomEmoji.php

69 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2022-01-19 06:03:21 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class CustomEmoji extends Model
{
use HasFactory;
const SCAN_RE = "/(?<=[^[:alnum:]:]|\n|^):([a-zA-Z0-9_]{2,}):(?=[^[:alnum:]:]|$)/x";
const CACHE_KEY = "pf:custom_emoji:";
protected $guarded = [];
public static function scan($text, $activitypub = false)
2022-01-19 06:03:21 +00:00
{
2022-01-19 07:46:30 +00:00
if(config('federation.custom_emoji.enabled') == false) {
return [];
}
2022-01-19 06:03:21 +00:00
return Str::of($text)
->matchAll(self::SCAN_RE)
->map(function($match) use($activitypub) {
2022-01-19 06:03:21 +00:00
$tag = Cache::remember(self::CACHE_KEY . $match, 14400, function() use($match) {
2022-01-21 07:27:30 +00:00
return self::orderBy('id')->whereDisabled(false)->whereShortcode(':' . $match . ':')->first();
2022-01-19 06:03:21 +00:00
});
if($tag) {
$url = url('/storage/' . $tag->media_path);
if($activitypub == true) {
2022-01-21 08:55:58 +00:00
$mediaType = Str::endsWith($url, '.png') ? 'image/png' : 'image/jpg';
return [
'id' => url('emojis/' . $tag->id),
'type' => 'Emoji',
'name' => $tag->shortcode,
'updated' => $tag->updated_at->toAtomString(),
'icon' => [
'type' => 'Image',
'mediaType' => $mediaType,
'url' => $url
]
];
} else {
return [
'shortcode' => $match,
'url' => $url,
'static_url' => $url,
'visible_in_picker' => $tag->disabled == false
];
}
2022-01-19 06:03:21 +00:00
}
})
->filter(function($tag) use($activitypub) {
if($activitypub == true) {
return $tag && isset($tag['icon']);
} else {
return $tag && isset($tag['static_url']);
}
2022-01-19 06:03:21 +00:00
})
->values()
->toArray();
2022-01-19 06:03:21 +00:00
}
}