2018-08-16 22:30:59 +00:00
|
|
|
|
<?php
|
2022-03-04 17:10:24 +00:00
|
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
|
/*
|
2022-03-04 17:10:24 +00:00
|
|
|
|
* Copyright (c) 2010-2022 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
|
*
|
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
|
* included in the LICENSE file.
|
|
|
|
|
*
|
2022-03-04 16:52:33 +00:00
|
|
|
|
* Change Date: 2026-03-04
|
2018-08-16 22:30:59 +00:00
|
|
|
|
*
|
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
|
* or later of the General Public License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace chat\data\user;
|
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
use chat\data\room\Room;
|
|
|
|
|
use chat\data\room\RoomCache;
|
|
|
|
|
use wcf\data\DatabaseObjectDecorator;
|
|
|
|
|
use wcf\system\WCF;
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decorates the User object to make it useful in context of Tim’s Chat.
|
|
|
|
|
*/
|
2022-03-04 17:10:24 +00:00
|
|
|
|
class User extends DatabaseObjectDecorator implements \JsonSerializable
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
protected static $baseClass = \wcf\data\user\User::class;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* array of room_to_user rows
|
|
|
|
|
*
|
|
|
|
|
* @var int[][]
|
|
|
|
|
*/
|
|
|
|
|
protected $roomToUser;
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an array of the room_to_user arrays for this user.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed[]
|
|
|
|
|
*/
|
|
|
|
|
public function getRoomAssociations($skipCache = false)
|
|
|
|
|
{
|
|
|
|
|
if ($this->roomToUser === null || $skipCache) {
|
|
|
|
|
$sql = "SELECT *
|
|
|
|
|
FROM chat1_room_to_user
|
|
|
|
|
WHERE userID = ?";
|
|
|
|
|
$statement = WCF::getDB()->prepare($sql);
|
|
|
|
|
$statement->execute([ $this->userID ]);
|
|
|
|
|
$this->roomToUser = [ ];
|
|
|
|
|
while (($row = $statement->fetchArray())) {
|
|
|
|
|
$this->roomToUser[$row['roomID']] = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
return $this->roomToUser;
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an array of Rooms this user is part of.
|
|
|
|
|
*
|
|
|
|
|
* @return \chat\data\room\Room[]
|
|
|
|
|
*/
|
|
|
|
|
public function getRooms($skipCache = false)
|
|
|
|
|
{
|
|
|
|
|
return \array_map(static function ($assoc) {
|
|
|
|
|
return RoomCache::getInstance()->getRoom($assoc['roomID']);
|
|
|
|
|
}, \array_filter($this->getRoomAssociations($skipCache), static function ($assoc) {
|
|
|
|
|
return $assoc['active'] === 1;
|
|
|
|
|
}));
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns whether the user is in the given room.
|
|
|
|
|
*/
|
2022-03-04 18:14:52 +00:00
|
|
|
|
public function isInRoom(Room $room, $skipCache = false): bool
|
2022-03-04 17:10:24 +00:00
|
|
|
|
{
|
|
|
|
|
$assoc = $this->getRoomAssociations($skipCache);
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
if (!isset($assoc[$room->roomID])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
return $assoc[$room->roomID]['active'] === 1;
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns (userID, roomID, sessionID) triples where the client died.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed[][]
|
|
|
|
|
*/
|
|
|
|
|
public static function getDeadSessions()
|
|
|
|
|
{
|
|
|
|
|
$sql = "SELECT userID,
|
|
|
|
|
roomID,
|
|
|
|
|
sessionID
|
|
|
|
|
FROM chat1_session
|
|
|
|
|
WHERE lastRequest < ?";
|
|
|
|
|
$statement = WCF::getDB()->prepare($sql);
|
|
|
|
|
$statement->execute([
|
|
|
|
|
TIME_NOW - 60 * 3,
|
|
|
|
|
]);
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
return $statement->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function jsonSerialize()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'userID' => $this->userID,
|
|
|
|
|
'username' => $this->username,
|
|
|
|
|
'link' => $this->getLink(),
|
|
|
|
|
];
|
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|