Entrar
PT
apps/caramail-server / src / Chat / User.php · commit 070afda
<?phpnamespace App\Chat;/** A connected member (what the table chat_connected held). */final class User{    public const REFUSES_PRIVATE = 2;    public const INVISIBLE = 4;    public const BUSY = 64;    public ?Room $room = null;    /** the "catch phrase" of the applet (84) */    public string $motto = '';    /** 26: 2 refuses new private talks, 4 invisible (moderators), 64 busy */    public int $flags = 0;    /** @var array<string, true> "nickname:origin" keys this member ignores (18) */    public array $ignores = [];    /** @var array<string, true> ids this member had a private talk with (they hear 74 when he leaves) */    public array $partners = [];    public float $connectedAt;    public function __construct(        public readonly string $id,        public readonly string $nickname,        public readonly int $age,        public readonly int $sex,        public readonly string $city,        public readonly int $admin,        public readonly int $origin,        public readonly Session $session,    ) {        $this->connectedAt = microtime(true);    }    public function isAdmin(): bool    {        return $this->admin === 1 || $this->admin === 2;    }    /** "nickname:origin": how the applet names a member in its ignore, friend and ejection lists. */    public function key(): string    {        return $this->nickname . ':' . $this->origin;    }    /** An empty field vanishes in the applet's tokenizer (it shifts the others): "?" is its "nothing". */    public function cityField(): string    {        return $this->city === '' ? '?' : $this->city;    }    /** The member as the room's list shows it: id, admin, nickname, flags, age, sex, city. */    public function roomEntry(): array    {        return [$this->id, (string)$this->admin, $this->nickname, (string)$this->flags, (string)$this->age, (string)$this->sex, $this->cityField()];    }    /** The member as a search shows it (with the sex). */    public function searchEntry(bool $withSex): array    {        $row = [$this->id, $this->nickname, (string)($this->flags & (self::REFUSES_PRIVATE | self::BUSY)), (string)$this->age];        if ($withSex) {            $row[] = (string)$this->sex;        }        // where: 0 / 4 in a public room (its name), 1 / 5 in a private one, else nowhere ("?")        $where = $this->room === null ? '2' : ($this->room->type === 0 ? '0' : '1');        return [...$row, $this->cityField(), $this->room === null ? '?' : $this->room->name, $where];    }}