1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00
Tims-Chat/files/lib/data/suspension/Suspension.class.php

120 lines
3.3 KiB
PHP
Raw Normal View History

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
/*
2024-01-13 20:03:36 +00:00
* Copyright (c) 2010-2024 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.
*
2024-01-13 20:03:36 +00:00
* Change Date: 2028-01-13
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 3
* or later of the General Public License.
*/
namespace chat\data\suspension;
2022-03-04 17:10:24 +00:00
use chat\data\room\Room;
use chat\data\room\RoomCache;
use wcf\data\DatabaseObject;
2022-03-04 18:14:03 +00:00
use wcf\data\object\type\ObjectType;
2022-03-04 17:10:24 +00:00
use wcf\data\object\type\ObjectTypeCache;
use wcf\data\user\User;
use wcf\system\cache\runtime\UserRuntimeCache;
2018-08-16 22:30:59 +00:00
/**
* Represents a chat suspension.
*/
2022-03-04 17:10:24 +00:00
class Suspension extends DatabaseObject implements \JsonSerializable
{
/**
* Returns the active suspensions for the given (objectTypeID, Room, User)
* triple.
*
* @return \chat\data\suspension\Suspension[]
*/
2022-03-04 18:14:03 +00:00
public static function getActiveSuspensionsByTriple(int $objectTypeID, User $user, Room $room)
2022-03-04 17:10:24 +00:00
{
$suspensionList = new SuspensionList();
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
$suspensionList->getConditionBuilder()->add('(expires IS NULL OR expires > ?)', [ TIME_NOW ]);
$suspensionList->getConditionBuilder()->add('revoked IS NULL');
$suspensionList->getConditionBuilder()->add('userID = ?', [ $user->userID ]);
$suspensionList->getConditionBuilder()->add('objectTypeID = ?', [ $objectTypeID ]);
$suspensionList->getConditionBuilder()->add('(roomID IS NULL OR roomID = ?)', [ $room->roomID ]);
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
$suspensionList->readObjects();
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
return \array_filter($suspensionList->getObjects(), static function (self $suspension) {
return $suspension->isActive();
});
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
/**
* Returns the suspension object type of this message.
*/
2022-03-04 18:14:03 +00:00
public function getSuspensionType(): ObjectType
2022-03-04 17:10:24 +00:00
{
return ObjectTypeCache::getInstance()->getObjectType($this->objectTypeID);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
/**
* Returns whether this suspension still is in effect.
*/
2022-03-04 18:14:03 +00:00
public function isActive(): bool
2022-03-04 17:10:24 +00:00
{
if ($this->revoked !== null) {
return false;
}
if (!$this->getSuspensionType()->getProcessor()->hasEffect($this)) {
return false;
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
if ($this->expires === null) {
return true;
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
return $this->expires > TIME_NOW;
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
/**
* Returns the chat room this suspension is in effect.
* Returns null if this is a global suspension.
*/
2022-03-04 18:14:03 +00:00
public function getRoom(): ?Room
2022-03-04 17:10:24 +00:00
{
if ($this->roomID === null) {
return null;
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
return RoomCache::getInstance()->getRoom($this->roomID);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
/**
* Returns the user that is affected by this suspension.
*/
2022-03-04 18:14:03 +00:00
public function getUser(): User
2022-03-04 17:10:24 +00:00
{
return UserRuntimeCache::getInstance()->getObject($this->userID);
}
2018-08-16 22:30:59 +00:00
2022-03-04 17:10:24 +00:00
/**
* @inheritDoc
*/
public function jsonSerialize(): array
2022-03-04 17:10:24 +00:00
{
return [
'userID' => $this->userID,
'username' => $this->getUser()->username,
'roomID' => $this->roomID,
'time' => $this->time,
'expires' => $this->expires,
'reason' => $this->reason,
'objectType' => $this->getSuspensionType()->objectType,
'judgeID' => $this->judgeID,
'judge' => $this->judge,
];
}
2018-08-16 22:30:59 +00:00
}