Update StatusTagsPipeline, fix hashtag bug and formatting

This commit is contained in:
Daniel Supernault 2025-01-04 00:27:39 -07:00
parent 5c426dc026
commit d516b7998f
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -2,27 +2,28 @@
namespace App\Jobs\StatusPipeline; namespace App\Jobs\StatusPipeline;
use App\Hashtag;
use App\Jobs\MentionPipeline\MentionPipeline;
use App\Mention;
use App\Services\AccountService;
use App\Services\CustomEmojiService;
use App\Services\StatusService;
use App\Services\TrendingHashtagService;
use App\StatusHashtag;
use App\Util\ActivityPub\Helpers;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use App\Services\AccountService; use Illuminate\Support\Facades\DB;
use App\Services\CustomEmojiService;
use App\Services\StatusService;
use App\Jobs\MentionPipeline\MentionPipeline;
use App\Mention;
use App\Hashtag;
use App\StatusHashtag;
use App\Services\TrendingHashtagService;
use App\Util\ActivityPub\Helpers;
class StatusTagsPipeline implements ShouldQueue class StatusTagsPipeline implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $activity; protected $activity;
protected $status; protected $status;
/** /**
@ -46,82 +47,116 @@ class StatusTagsPipeline implements ShouldQueue
$res = $this->activity; $res = $this->activity;
$status = $this->status; $status = $this->status;
if(isset($res['tag']['type'], $res['tag']['name'])) { if (isset($res['tag']['type'], $res['tag']['name'])) {
$res['tag'] = [$res['tag']]; $res['tag'] = [$res['tag']];
} }
$tags = collect($res['tag']); $tags = collect($res['tag']);
// Emoji // Emoji
$tags->filter(function($tag) { $tags->filter(function ($tag) {
return $tag && isset($tag['id'], $tag['icon'], $tag['name'], $tag['type']) && $tag['type'] == 'Emoji'; return $tag && isset($tag['id'], $tag['icon'], $tag['name'], $tag['type']) && $tag['type'] == 'Emoji';
}) })
->map(function($tag) { ->map(function ($tag) {
CustomEmojiService::import($tag['id'], $this->status->id); CustomEmojiService::import($tag['id'], $this->status->id);
}); });
// Hashtags // Hashtags
$tags->filter(function($tag) { $tags->filter(function ($tag) {
return $tag && $tag['type'] == 'Hashtag' && isset($tag['href'], $tag['name']); return $tag && $tag['type'] == 'Hashtag' && isset($tag['href'], $tag['name']);
}) })
->map(function($tag) use($status) { ->map(function ($tag) use ($status) {
$name = substr($tag['name'], 0, 1) == '#' ? $name = substr($tag['name'], 0, 1) == '#' ?
substr($tag['name'], 1) : $tag['name']; substr($tag['name'], 1) : $tag['name'];
$banned = TrendingHashtagService::getBannedHashtagNames(); $banned = TrendingHashtagService::getBannedHashtagNames();
if(count($banned)) { if (count($banned)) {
if(in_array(strtolower($name), array_map('strtolower', $banned))) { if (in_array(strtolower($name), array_map('strtolower', $banned))) {
return; return;
} }
} }
if(config('database.default') === 'pgsql') { if (config('database.default') === 'pgsql') {
$hashtag = Hashtag::where('name', 'ilike', $name) $hashtag = DB::transaction(function () use ($name) {
->orWhere('slug', 'ilike', str_slug($name, '-', false)) $baseSlug = str_slug($name, '-', false);
$slug = $baseSlug;
$counter = 1;
$existing = Hashtag::where('name', $name)
->lockForUpdate()
->first(); ->first();
if(!$hashtag) { if ($existing) {
$hashtag = Hashtag::updateOrCreate([ if ($existing->slug !== $slug) {
'slug' => str_slug($name, '-', false), while (Hashtag::where('slug', $slug)
'name' => $name ->where('name', '!=', $name)
]); ->exists()) {
$slug = $baseSlug.'-'.$counter++;
} }
} else { $existing->slug = $slug;
$hashtag = Hashtag::updateOrCreate([ $existing->save();
'slug' => str_slug($name, '-', false), }
'name' => $name
return $existing;
}
while (Hashtag::where('slug', $slug)->exists()) {
$slug = $baseSlug.'-'.$counter++;
}
return Hashtag::create([
'name' => $name,
'slug' => $slug,
]); ]);
});
} else {
$hashtag = DB::transaction(function () use ($name) {
$baseSlug = str_slug($name, '-', false);
$slug = $baseSlug;
$counter = 1;
while (Hashtag::where('slug', $slug)
->where('name', '!=', $name)
->exists()) {
$slug = $baseSlug.'-'.$counter++;
}
return Hashtag::updateOrCreate(
['name' => $name],
['slug' => $slug]
);
});
} }
StatusHashtag::firstOrCreate([ StatusHashtag::firstOrCreate([
'status_id' => $status->id, 'status_id' => $status->id,
'hashtag_id' => $hashtag->id, 'hashtag_id' => $hashtag->id,
'profile_id' => $status->profile_id, 'profile_id' => $status->profile_id,
'status_visibility' => $status->scope 'status_visibility' => $status->scope,
]); ]);
}); });
// Mentions // Mentions
$tags->filter(function($tag) { $tags->filter(function ($tag) {
return $tag && return $tag &&
$tag['type'] == 'Mention' && $tag['type'] == 'Mention' &&
isset($tag['href']) && isset($tag['href']) &&
substr($tag['href'], 0, 8) === 'https://'; substr($tag['href'], 0, 8) === 'https://';
}) })
->map(function($tag) use($status) { ->map(function ($tag) use ($status) {
if(Helpers::validateLocalUrl($tag['href'])) { if (Helpers::validateLocalUrl($tag['href'])) {
$parts = explode('/', $tag['href']); $parts = explode('/', $tag['href']);
if(!$parts) { if (! $parts) {
return; return;
} }
$pid = AccountService::usernameToId(end($parts)); $pid = AccountService::usernameToId(end($parts));
if(!$pid) { if (! $pid) {
return; return;
} }
} else { } else {
$acct = Helpers::profileFetch($tag['href']); $acct = Helpers::profileFetch($tag['href']);
if(!$acct) { if (! $acct) {
return; return;
} }
$pid = $acct->id; $pid = $acct->id;