Repair: copy legacy joinDate into new user's created_at when creating users from legacy wallz
This commit is contained in:
@@ -301,7 +301,8 @@ class ArtworkService
|
||||
{
|
||||
$query = Artwork::where('user_id', $userId)
|
||||
->with([
|
||||
'user:id,name,username',
|
||||
'user:id,name,username,level,rank',
|
||||
'stats:artwork_id,views,downloads,favorites',
|
||||
'categories' => function ($q) {
|
||||
$q->select('categories.id', 'categories.content_type_id', 'categories.parent_id', 'categories.name', 'categories.slug', 'categories.sort_order')
|
||||
->with(['parent:id,parent_id,content_type_id,name,slug', 'contentType:id,slug,name']);
|
||||
|
||||
@@ -72,6 +72,20 @@ class ContentSanitizer
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize previously rendered HTML for display-time policy changes.
|
||||
* This is useful when stored HTML predates current link attributes or
|
||||
* when display rules depend on the author rather than the raw content.
|
||||
*/
|
||||
public static function sanitizeRenderedHtml(?string $html, bool $allowLinks = true): string
|
||||
{
|
||||
if ($html === null || trim($html) === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return static::sanitizeHtml($html, $allowLinks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip ALL HTML from input, returning plain text with newlines preserved.
|
||||
*/
|
||||
@@ -190,7 +204,7 @@ class ContentSanitizer
|
||||
* Whitelist-based HTML sanitizer.
|
||||
* Removes all tags not in ALLOWED_TAGS, and strips disallowed attributes.
|
||||
*/
|
||||
private static function sanitizeHtml(string $html): string
|
||||
private static function sanitizeHtml(string $html, bool $allowLinks = true): string
|
||||
{
|
||||
// Parse with DOMDocument
|
||||
$doc = new \DOMDocument('1.0', 'UTF-8');
|
||||
@@ -202,7 +216,7 @@ class ContentSanitizer
|
||||
);
|
||||
libxml_clear_errors();
|
||||
|
||||
static::cleanNode($doc->getElementsByTagName('body')->item(0));
|
||||
static::cleanNode($doc->getElementsByTagName('body')->item(0), $allowLinks);
|
||||
|
||||
// Serialize back, removing the wrapping html/body
|
||||
$body = $doc->getElementsByTagName('body')->item(0);
|
||||
@@ -218,13 +232,17 @@ class ContentSanitizer
|
||||
/**
|
||||
* Recursively clean a DOMNode — strip forbidden tags/attributes.
|
||||
*/
|
||||
private static function cleanNode(\DOMNode $node): void
|
||||
private static function cleanNode(\DOMNode $node, bool $allowLinks = true): void
|
||||
{
|
||||
$toRemove = [];
|
||||
$toUnwrap = [];
|
||||
|
||||
foreach ($node->childNodes as $child) {
|
||||
if ($child->nodeType === XML_ELEMENT_NODE) {
|
||||
if (! $child instanceof \DOMElement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tag = strtolower($child->nodeName);
|
||||
|
||||
if (! in_array($tag, self::ALLOWED_TAGS, true)) {
|
||||
@@ -245,17 +263,22 @@ class ContentSanitizer
|
||||
|
||||
// Force external links to be safe
|
||||
if ($tag === 'a') {
|
||||
if (! $allowLinks) {
|
||||
$toUnwrap[] = $child;
|
||||
continue;
|
||||
}
|
||||
|
||||
$href = $child->getAttribute('href');
|
||||
if ($href && ! static::isSafeUrl($href)) {
|
||||
$toUnwrap[] = $child;
|
||||
continue;
|
||||
}
|
||||
$child->setAttribute('rel', 'noopener noreferrer nofollow');
|
||||
$child->setAttribute('rel', 'noopener noreferrer nofollow ugc');
|
||||
$child->setAttribute('target', '_blank');
|
||||
}
|
||||
|
||||
// Recurse
|
||||
static::cleanNode($child);
|
||||
static::cleanNode($child, $allowLinks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
app/Services/Messaging/ConversationDeltaService.php
Normal file
31
app/Services/Messaging/ConversationDeltaService.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Messaging;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ConversationDeltaService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MessagingPayloadFactory $payloadFactory,
|
||||
) {}
|
||||
|
||||
public function messagesAfter(Conversation $conversation, User $viewer, int $afterMessageId, ?int $limit = null): Collection
|
||||
{
|
||||
$maxMessages = max(1, (int) config('messaging.recovery.max_messages', 100));
|
||||
$effectiveLimit = min($limit ?? $maxMessages, $maxMessages);
|
||||
|
||||
return Message::withTrashed()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('id', '>', $afterMessageId)
|
||||
->with(['sender:id,username,name', 'reactions', 'attachments'])
|
||||
->orderBy('id')
|
||||
->limit($effectiveLimit)
|
||||
->get()
|
||||
->map(fn (Message $message) => $this->payloadFactory->message($message, (int) $viewer->id))
|
||||
->values();
|
||||
}
|
||||
}
|
||||
76
app/Services/Messaging/ConversationReadService.php
Normal file
76
app/Services/Messaging/ConversationReadService.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Messaging;
|
||||
|
||||
use App\Events\ConversationUpdated;
|
||||
use App\Events\MessageRead;
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ConversationReadService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ConversationStateService $conversationState,
|
||||
) {}
|
||||
|
||||
public function markConversationRead(Conversation $conversation, User $user, ?int $messageId = null): ConversationParticipant
|
||||
{
|
||||
/** @var ConversationParticipant $participant */
|
||||
$participant = ConversationParticipant::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('user_id', $user->id)
|
||||
->whereNull('left_at')
|
||||
->firstOrFail();
|
||||
|
||||
$lastReadableMessage = Message::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->whereNull('deleted_at')
|
||||
->where('sender_id', '!=', $user->id)
|
||||
->when($messageId, fn ($query) => $query->where('id', '<=', $messageId))
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
$readAt = now();
|
||||
|
||||
$participant->forceFill([
|
||||
'last_read_at' => $readAt,
|
||||
'last_read_message_id' => $lastReadableMessage?->id,
|
||||
])->save();
|
||||
|
||||
if ($lastReadableMessage) {
|
||||
$messageReads = Message::query()
|
||||
->select(['id'])
|
||||
->where('conversation_id', $conversation->id)
|
||||
->whereNull('deleted_at')
|
||||
->where('sender_id', '!=', $user->id)
|
||||
->where('id', '<=', $lastReadableMessage->id)
|
||||
->get()
|
||||
->map(fn (Message $message) => [
|
||||
'message_id' => $message->id,
|
||||
'user_id' => $user->id,
|
||||
'read_at' => $readAt,
|
||||
])
|
||||
->all();
|
||||
|
||||
if (! empty($messageReads)) {
|
||||
DB::table('message_reads')->upsert($messageReads, ['message_id', 'user_id'], ['read_at']);
|
||||
}
|
||||
}
|
||||
|
||||
$participantIds = $this->conversationState->activeParticipantIds($conversation);
|
||||
$this->conversationState->touchConversationCachesForUsers($participantIds);
|
||||
|
||||
DB::afterCommit(function () use ($conversation, $participant, $user, $participantIds): void {
|
||||
event(new MessageRead($conversation, $participant, $user));
|
||||
|
||||
foreach ($participantIds as $participantId) {
|
||||
event(new ConversationUpdated($participantId, $conversation, 'message.read'));
|
||||
}
|
||||
});
|
||||
|
||||
return $participant->fresh(['user']);
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,9 @@
|
||||
|
||||
namespace App\Services\Messaging;
|
||||
|
||||
use App\Events\ConversationUpdated;
|
||||
use App\Events\MessageRead;
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ConversationStateService
|
||||
{
|
||||
@@ -37,62 +32,4 @@ class ConversationStateService
|
||||
Cache::increment($versionKey);
|
||||
}
|
||||
}
|
||||
|
||||
public function markConversationRead(Conversation $conversation, User $user, ?int $messageId = null): ConversationParticipant
|
||||
{
|
||||
/** @var ConversationParticipant $participant */
|
||||
$participant = ConversationParticipant::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('user_id', $user->id)
|
||||
->whereNull('left_at')
|
||||
->firstOrFail();
|
||||
|
||||
$lastReadableMessage = Message::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->whereNull('deleted_at')
|
||||
->where('sender_id', '!=', $user->id)
|
||||
->when($messageId, fn ($query) => $query->where('id', '<=', $messageId))
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
$readAt = now();
|
||||
|
||||
$participant->update([
|
||||
'last_read_at' => $readAt,
|
||||
'last_read_message_id' => $lastReadableMessage?->id,
|
||||
]);
|
||||
|
||||
if ($lastReadableMessage) {
|
||||
$messageReads = Message::query()
|
||||
->select(['id'])
|
||||
->where('conversation_id', $conversation->id)
|
||||
->whereNull('deleted_at')
|
||||
->where('sender_id', '!=', $user->id)
|
||||
->where('id', '<=', $lastReadableMessage->id)
|
||||
->get()
|
||||
->map(fn (Message $message) => [
|
||||
'message_id' => $message->id,
|
||||
'user_id' => $user->id,
|
||||
'read_at' => $readAt,
|
||||
])
|
||||
->all();
|
||||
|
||||
if (! empty($messageReads)) {
|
||||
DB::table('message_reads')->upsert($messageReads, ['message_id', 'user_id'], ['read_at']);
|
||||
}
|
||||
}
|
||||
|
||||
$participantIds = $this->activeParticipantIds($conversation);
|
||||
$this->touchConversationCachesForUsers($participantIds);
|
||||
|
||||
DB::afterCommit(function () use ($conversation, $participant, $user): void {
|
||||
event(new MessageRead($conversation, $participant, $user));
|
||||
|
||||
foreach ($this->activeParticipantIds($conversation) as $participantId) {
|
||||
event(new ConversationUpdated($participantId, $conversation, 'message.read'));
|
||||
}
|
||||
});
|
||||
|
||||
return $participant->fresh(['user']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ use Illuminate\Support\Str;
|
||||
|
||||
class MessageNotificationService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MessagingPresenceService $presence,
|
||||
) {}
|
||||
|
||||
public function notifyNewMessage(Conversation $conversation, Message $message, User $sender): void
|
||||
{
|
||||
if (! DB::getSchemaBuilder()->hasTable('notifications')) {
|
||||
@@ -36,6 +40,13 @@ class MessageNotificationService
|
||||
->whereIn('id', $recipientIds)
|
||||
->get()
|
||||
->filter(fn (User $recipient) => $recipient->allowsMessagesFrom($sender))
|
||||
->filter(function (User $recipient): bool {
|
||||
if (! (bool) config('messaging.notifications.offline_fallback_only', true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ! $this->presence->isUserOnline((int) $recipient->id);
|
||||
})
|
||||
->pluck('id')
|
||||
->map(fn ($id) => (int) $id)
|
||||
->values()
|
||||
|
||||
@@ -56,7 +56,7 @@ class MessagingPayloadFactory
|
||||
'title' => $conversation->title,
|
||||
'is_active' => (bool) ($conversation->is_active ?? true),
|
||||
'last_message_at' => optional($conversation->last_message_at)?->toIso8601String(),
|
||||
'unread_count' => $conversation->unreadCountFor($viewerId),
|
||||
'unread_count' => app(UnreadCounterService::class)->unreadCountForConversation($conversation, $viewerId),
|
||||
'my_participant' => $myParticipant ? $this->participant($myParticipant) : null,
|
||||
'all_participants' => $conversation->allParticipants
|
||||
->whereNull('left_at')
|
||||
@@ -149,4 +149,4 @@ class MessagingPayloadFactory
|
||||
|
||||
return $counts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
69
app/Services/Messaging/MessagingPresenceService.php
Normal file
69
app/Services/Messaging/MessagingPresenceService.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Messaging;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Cache\Repository;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class MessagingPresenceService
|
||||
{
|
||||
public function touch(User|int $user, ?int $conversationId = null): void
|
||||
{
|
||||
$userId = $user instanceof User ? (int) $user->id : (int) $user;
|
||||
$store = $this->store();
|
||||
$onlineKey = $this->onlineKey($userId);
|
||||
$existing = $store->get($onlineKey, []);
|
||||
$previousConversationId = (int) ($existing['conversation_id'] ?? 0) ?: null;
|
||||
$onlineTtl = max(30, (int) config('messaging.presence.ttl_seconds', 90));
|
||||
$conversationTtl = max(15, (int) config('messaging.presence.conversation_ttl_seconds', 45));
|
||||
|
||||
if ($previousConversationId && $previousConversationId !== $conversationId) {
|
||||
$store->forget($this->conversationKey($previousConversationId, $userId));
|
||||
}
|
||||
|
||||
$store->put($onlineKey, [
|
||||
'conversation_id' => $conversationId,
|
||||
'seen_at' => now()->toIso8601String(),
|
||||
], now()->addSeconds($onlineTtl));
|
||||
|
||||
if ($conversationId) {
|
||||
$store->put($this->conversationKey($conversationId, $userId), now()->toIso8601String(), now()->addSeconds($conversationTtl));
|
||||
}
|
||||
}
|
||||
|
||||
public function isUserOnline(int $userId): bool
|
||||
{
|
||||
return $this->store()->has($this->onlineKey($userId));
|
||||
}
|
||||
|
||||
public function isViewingConversation(int $conversationId, int $userId): bool
|
||||
{
|
||||
return $this->store()->has($this->conversationKey($conversationId, $userId));
|
||||
}
|
||||
|
||||
private function onlineKey(int $userId): string
|
||||
{
|
||||
return 'messages:presence:user:' . $userId;
|
||||
}
|
||||
|
||||
private function conversationKey(int $conversationId, int $userId): string
|
||||
{
|
||||
return 'messages:presence:conversation:' . $conversationId . ':user:' . $userId;
|
||||
}
|
||||
|
||||
private function store(): Repository
|
||||
{
|
||||
$store = (string) config('messaging.presence.cache_store', 'redis');
|
||||
|
||||
if ($store === 'redis' && ! class_exists('Redis')) {
|
||||
return Cache::store();
|
||||
}
|
||||
|
||||
try {
|
||||
return Cache::store($store);
|
||||
} catch (\Throwable) {
|
||||
return Cache::store();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,4 +123,4 @@ class SendMessageAction
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
81
app/Services/Messaging/UnreadCounterService.php
Normal file
81
app/Services/Messaging/UnreadCounterService.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Messaging;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class UnreadCounterService
|
||||
{
|
||||
public function applyUnreadCountSelect(Builder $query, User|int $user, string $participantAlias = 'cp_me'): Builder
|
||||
{
|
||||
$userId = $user instanceof User ? (int) $user->id : (int) $user;
|
||||
|
||||
return $query->addSelect([
|
||||
'unread_count' => Message::query()
|
||||
->selectRaw('count(*)')
|
||||
->whereColumn('messages.conversation_id', 'conversations.id')
|
||||
->where('messages.sender_id', '!=', $userId)
|
||||
->whereNull('messages.deleted_at')
|
||||
->where(function ($nested) use ($participantAlias) {
|
||||
$nested->where(function ($group) use ($participantAlias) {
|
||||
$group->whereNull($participantAlias . '.last_read_message_id')
|
||||
->whereNull($participantAlias . '.last_read_at');
|
||||
})->orWhereColumn('messages.id', '>', $participantAlias . '.last_read_message_id')
|
||||
->orWhereColumn('messages.created_at', '>', $participantAlias . '.last_read_at');
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
public function unreadCountForConversation(Conversation $conversation, User|int $user): int
|
||||
{
|
||||
$userId = $user instanceof User ? (int) $user->id : (int) $user;
|
||||
|
||||
$participant = ConversationParticipant::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('user_id', $userId)
|
||||
->whereNull('left_at')
|
||||
->first();
|
||||
|
||||
if (! $participant) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->unreadCountForParticipant($participant);
|
||||
}
|
||||
|
||||
public function unreadCountForParticipant(ConversationParticipant $participant): int
|
||||
{
|
||||
$query = Message::query()
|
||||
->where('conversation_id', $participant->conversation_id)
|
||||
->where('sender_id', '!=', $participant->user_id)
|
||||
->whereNull('deleted_at');
|
||||
|
||||
if ($participant->last_read_message_id) {
|
||||
$query->where('id', '>', $participant->last_read_message_id);
|
||||
} elseif ($participant->last_read_at) {
|
||||
$query->where('created_at', '>', $participant->last_read_at);
|
||||
}
|
||||
|
||||
return (int) $query->count();
|
||||
}
|
||||
|
||||
public function totalUnreadForUser(User|int $user): int
|
||||
{
|
||||
$userId = $user instanceof User ? (int) $user->id : (int) $user;
|
||||
|
||||
return (int) Conversation::query()
|
||||
->select('conversations.id')
|
||||
->join('conversation_participants as cp_me', function ($join) use ($userId) {
|
||||
$join->on('cp_me.conversation_id', '=', 'conversations.id')
|
||||
->where('cp_me.user_id', '=', $userId)
|
||||
->whereNull('cp_me.left_at');
|
||||
})
|
||||
->where('conversations.is_active', true)
|
||||
->get()
|
||||
->sum(fn (Conversation $conversation) => $this->unreadCountForConversation($conversation, $userId));
|
||||
}
|
||||
}
|
||||
29
app/Services/Vision/ArtworkVisionImageUrl.php
Normal file
29
app/Services/Vision/ArtworkVisionImageUrl.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Vision;
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Services\ThumbnailService;
|
||||
|
||||
final class ArtworkVisionImageUrl
|
||||
{
|
||||
public function fromArtwork(Artwork $artwork): ?string
|
||||
{
|
||||
return $this->fromHash(
|
||||
(string) ($artwork->hash ?? ''),
|
||||
(string) ($artwork->thumb_ext ?: 'webp')
|
||||
);
|
||||
}
|
||||
|
||||
public function fromHash(?string $hash, ?string $ext = 'webp', string $size = 'md'): ?string
|
||||
{
|
||||
$clean = strtolower((string) preg_replace('/[^a-z0-9]/', '', (string) $hash));
|
||||
if ($clean === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ThumbnailService::fromHash($clean, $ext, $size);
|
||||
}
|
||||
}
|
||||
213
app/Services/Vision/VectorGatewayClient.php
Normal file
213
app/Services/Vision/VectorGatewayClient.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Vision;
|
||||
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use RuntimeException;
|
||||
|
||||
final class VectorGatewayClient
|
||||
{
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return (bool) config('vision.vector_gateway.enabled', true)
|
||||
&& $this->baseUrl() !== ''
|
||||
&& $this->apiKey() !== '';
|
||||
}
|
||||
|
||||
public function upsertByUrl(string $imageUrl, int|string $id, array $metadata = []): array
|
||||
{
|
||||
$response = $this->postJson(
|
||||
$this->url((string) config('vision.vector_gateway.upsert_endpoint', '/vectors/upsert')),
|
||||
[
|
||||
'url' => $imageUrl,
|
||||
'id' => (string) $id,
|
||||
'metadata' => $metadata,
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new RuntimeException($this->failureMessage('Vector upsert', $response));
|
||||
}
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
return is_array($json) ? $json : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{id: int|string, score: float, metadata: array<string, mixed>}>
|
||||
*/
|
||||
public function searchByUrl(string $imageUrl, int $limit = 5): array
|
||||
{
|
||||
$response = $this->postJson(
|
||||
$this->url((string) config('vision.vector_gateway.search_endpoint', '/vectors/search')),
|
||||
[
|
||||
'url' => $imageUrl,
|
||||
'limit' => max(1, $limit),
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new RuntimeException($this->failureMessage('Vector search', $response));
|
||||
}
|
||||
|
||||
return $this->extractMatches($response->json());
|
||||
}
|
||||
|
||||
public function deleteByIds(array $ids): array
|
||||
{
|
||||
$response = $this->postJson(
|
||||
$this->url((string) config('vision.vector_gateway.delete_endpoint', '/vectors/delete')),
|
||||
[
|
||||
'ids' => array_values(array_map(static fn (int|string $id): string => (string) $id, $ids)),
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new RuntimeException($this->failureMessage('Vector delete', $response));
|
||||
}
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
return is_array($json) ? $json : [];
|
||||
}
|
||||
|
||||
private function request(): PendingRequest
|
||||
{
|
||||
if (! $this->isConfigured()) {
|
||||
throw new RuntimeException('Vision vector gateway is not configured. Set VISION_VECTOR_GATEWAY_URL and VISION_VECTOR_GATEWAY_API_KEY.');
|
||||
}
|
||||
|
||||
return Http::acceptJson()
|
||||
->withHeaders([
|
||||
'X-API-Key' => $this->apiKey(),
|
||||
])
|
||||
->connectTimeout(max(1, (int) config('vision.vector_gateway.connect_timeout_seconds', 5)))
|
||||
->timeout(max(1, (int) config('vision.vector_gateway.timeout_seconds', 20)))
|
||||
->retry(
|
||||
max(0, (int) config('vision.vector_gateway.retries', 1)),
|
||||
max(0, (int) config('vision.vector_gateway.retry_delay_ms', 250)),
|
||||
throw: false,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
private function postJson(string $url, array $payload): Response
|
||||
{
|
||||
$response = $this->request()->post($url, $payload);
|
||||
|
||||
if (! $response instanceof Response) {
|
||||
throw new RuntimeException('Vector gateway request did not return an HTTP response.');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function baseUrl(): string
|
||||
{
|
||||
return rtrim((string) config('vision.vector_gateway.base_url', ''), '/');
|
||||
}
|
||||
|
||||
private function apiKey(): string
|
||||
{
|
||||
return trim((string) config('vision.vector_gateway.api_key', ''));
|
||||
}
|
||||
|
||||
private function url(string $path): string
|
||||
{
|
||||
return $this->baseUrl() . '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
private function failureMessage(string $operation, Response $response): string
|
||||
{
|
||||
$body = trim($response->body());
|
||||
|
||||
if ($body === '') {
|
||||
return $operation . ' failed with HTTP ' . $response->status() . '.';
|
||||
}
|
||||
|
||||
return $operation . ' failed with HTTP ' . $response->status() . ': ' . $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $json
|
||||
* @return list<array{id: int|string, score: float, metadata: array<string, mixed>}>
|
||||
*/
|
||||
private function extractMatches(mixed $json): array
|
||||
{
|
||||
$candidates = [];
|
||||
|
||||
if (is_array($json)) {
|
||||
$candidates = $this->extractCandidateRows($json);
|
||||
}
|
||||
|
||||
$results = [];
|
||||
foreach ($candidates as $candidate) {
|
||||
if (! is_array($candidate)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $candidate['id']
|
||||
?? $candidate['point_id']
|
||||
?? $candidate['payload']['id']
|
||||
?? $candidate['metadata']['id']
|
||||
?? null;
|
||||
|
||||
if (! is_int($id) && ! is_string($id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$score = $candidate['score']
|
||||
?? $candidate['similarity']
|
||||
?? $candidate['distance']
|
||||
?? 0.0;
|
||||
|
||||
$metadata = $candidate['metadata'] ?? $candidate['payload'] ?? [];
|
||||
if (! is_array($metadata)) {
|
||||
$metadata = [];
|
||||
}
|
||||
|
||||
$results[] = [
|
||||
'id' => $id,
|
||||
'score' => (float) $score,
|
||||
'metadata' => $metadata,
|
||||
];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<mixed> $json
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
private function extractCandidateRows(array $json): array
|
||||
{
|
||||
$keys = ['results', 'matches', 'points', 'data'];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (! isset($json[$key]) || ! is_array($json[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $json[$key];
|
||||
if (array_is_list($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
foreach (['results', 'matches', 'points', 'items'] as $nestedKey) {
|
||||
if (isset($value[$nestedKey]) && is_array($value[$nestedKey]) && array_is_list($value[$nestedKey])) {
|
||||
return $value[$nestedKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_is_list($json) ? $json : [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user