34 lines
894 B
PHP
34 lines
894 B
PHP
<?php
|
|
|
|
namespace Modules\Communication\Services;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class PresenceService
|
|
{
|
|
public function heartbeat(int $businessId, int $userId): void
|
|
{
|
|
$ttl = (int) config('communication.presence_ttl_seconds', 120);
|
|
Cache::put($this->key($businessId, $userId), now()->timestamp, $ttl);
|
|
}
|
|
|
|
public function isOnline(int $businessId, int $userId): bool
|
|
{
|
|
return Cache::has($this->key($businessId, $userId));
|
|
}
|
|
|
|
/**
|
|
* @param array<int> $userIds
|
|
* @return array<int>
|
|
*/
|
|
public function onlineUserIds(int $businessId, array $userIds): array
|
|
{
|
|
return array_values(array_filter($userIds, fn ($id) => $this->isOnline($businessId, (int) $id)));
|
|
}
|
|
|
|
private function key(int $businessId, int $userId): string
|
|
{
|
|
return "communication:presence:{$businessId}:{$userId}";
|
|
}
|
|
}
|