Update note transformers, add custom emoji support

This commit is contained in:
Daniel Supernault 2022-01-19 02:21:26 -07:00
parent 0d78a9aaf8
commit b2016e6c21
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
3 changed files with 42 additions and 14 deletions

View File

@ -14,7 +14,7 @@ class CustomEmoji extends Model
const SCAN_RE = "/(?<=[^[:alnum:]:]|\n|^):([a-zA-Z0-9_]{2,}):(?=[^[:alnum:]:]|$)/x"; const SCAN_RE = "/(?<=[^[:alnum:]:]|\n|^):([a-zA-Z0-9_]{2,}):(?=[^[:alnum:]:]|$)/x";
const CACHE_KEY = "pf:custom_emoji:"; const CACHE_KEY = "pf:custom_emoji:";
public static function scan($text) public static function scan($text, $activitypub = false)
{ {
if(config('federation.custom_emoji.enabled') == false) { if(config('federation.custom_emoji.enabled') == false) {
return []; return [];
@ -22,24 +22,45 @@ class CustomEmoji extends Model
return Str::of($text) return Str::of($text)
->matchAll(self::SCAN_RE) ->matchAll(self::SCAN_RE)
->map(function($match) { ->map(function($match) use($activitypub) {
$tag = Cache::remember(self::CACHE_KEY . $match, 14400, function() use($match) { $tag = Cache::remember(self::CACHE_KEY . $match, 14400, function() use($match) {
return self::whereShortcode(':' . $match . ':')->first(); return self::whereShortcode(':' . $match . ':')->first();
}); });
if($tag) { if($tag) {
$url = url('/storage/' . $tag->media_path); $url = url('/storage/' . $tag->media_path);
return [
'shortcode' => $match, if($activitypub == true) {
'url' => $url, $mediaType = Str::endsWith($url, '.png') ? 'image/png' : 'image/jpeg';
'static_path' => $url, return [
'visible_in_picker' => $tag->disabled == false '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_path' => $url,
'visible_in_picker' => $tag->disabled == false
];
}
} }
}) })
->filter(function($tag) { ->filter(function($tag) use($activitypub) {
return $tag && isset($tag['static_path']); if($activitypub == true) {
return $tag && isset($tag['icon']);
} else {
return $tag && isset($tag['static_path']);
}
}) })
->values(); ->values()
->toArray();
} }
} }

View File

@ -4,13 +4,13 @@ namespace App\Transformer\ActivityPub\Verb;
use App\Status; use App\Status;
use League\Fractal; use League\Fractal;
use App\Models\CustomEmoji;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class CreateNote extends Fractal\TransformerAbstract class CreateNote extends Fractal\TransformerAbstract
{ {
public function transform(Status $status) public function transform(Status $status)
{ {
$mentions = $status->mentions->map(function ($mention) { $mentions = $status->mentions->map(function ($mention) {
$webfinger = $mention->emailUrl(); $webfinger = $mention->emailUrl();
$name = Str::startsWith($webfinger, '@') ? $name = Str::startsWith($webfinger, '@') ?
@ -46,7 +46,10 @@ class CreateNote extends Fractal\TransformerAbstract
'name' => "#{$hashtag->name}", 'name' => "#{$hashtag->name}",
]; ];
})->toArray(); })->toArray();
$tags = array_merge($mentions, $hashtags);
$emojis = CustomEmoji::scan($status->caption, true) ?? [];
$emoji = array_merge($emojis, $mentions);
$tags = array_merge($emoji, $hashtags);
return [ return [
'@context' => [ '@context' => [

View File

@ -4,6 +4,7 @@ namespace App\Transformer\ActivityPub\Verb;
use App\Status; use App\Status;
use League\Fractal; use League\Fractal;
use App\Models\CustomEmoji;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class Note extends Fractal\TransformerAbstract class Note extends Fractal\TransformerAbstract
@ -46,7 +47,10 @@ class Note extends Fractal\TransformerAbstract
'name' => "#{$hashtag->name}", 'name' => "#{$hashtag->name}",
]; ];
})->toArray(); })->toArray();
$tags = array_merge($mentions, $hashtags);
$emojis = CustomEmoji::scan($status->caption, true) ?? [];
$emoji = array_merge($emojis, $mentions);
$tags = array_merge($emoji, $hashtags);
return [ return [
'@context' => [ '@context' => [