Sign in
EN
apps/caramail-server / src / Chat / Room.php · commit 070afda
<?phpnamespace App\Chat;/** A room (chat_rooms) with its members and its last sentences (chat_sentences). */final class Room{    /** "un salon, quel que soit son type, ne peut accueillir plus de 32 personnes" (the help of the chat, 1999) */    public const MAX_MEMBERS = 32;    public const HISTORY = 15;    /** @var array<string, User> by id */    public array $members = [];    /** @var array<string, float> who signalled a behaviour problem here, and when (550) */    public array $reports = [];    /** @var list<array{id: string, nickname: string, text: string, at: int}> */    public array $history = [];    public function __construct(        public readonly string $name,        public readonly int $type,        public readonly string $creator,        public bool $permanent = false,        public int $createdAt = 0,    ) {        $this->createdAt = $createdAt ?: time();    }    public function key(): string    {        return self::keyOf($this->name, $this->type);    }    public static function keyOf(string $name, int $type): string    {        return $type . '|' . mb_strtolower($name);    }    public function count(): int    {        return count($this->members);    }    public function isFull(): bool    {        return $this->count() >= self::MAX_MEMBERS;    }    public function remember(User $user, string $text): void    {        $this->history[] = ['id' => $user->id, 'nickname' => $user->nickname, 'text' => $text, 'at' => time()];        if (count($this->history) > self::HISTORY) {            $this->history = array_slice($this->history, -self::HISTORY);        }    }    /** The room as a room list shows it: name, "?", creator, members. */    public function listEntry(): array    {        return [$this->name, '?', $this->creator, (string)$this->count()];    }}