Move counting functions to participant service

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-10-22 14:36:21 +02:00
Родитель b7c7838a8b
Коммит cc93851b17
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7076EA9751AACDDA
6 изменённых файлов: 72 добавлений и 58 удалений

Просмотреть файл

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace OCA\Talk\BackgroundJob;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCA\Talk\Manager;
@ -39,6 +40,9 @@ class RemoveEmptyRooms extends TimedJob {
/** @var Manager */
protected $manager;
/** @var ParticipantService */
protected $participantService;
/** @var LoggerInterface */
protected $logger;
@ -46,6 +50,7 @@ class RemoveEmptyRooms extends TimedJob {
public function __construct(ITimeFactory $timeFactory,
Manager $manager,
ParticipantService $participantService,
LoggerInterface $logger) {
parent::__construct($timeFactory);
@ -53,6 +58,7 @@ class RemoveEmptyRooms extends TimedJob {
$this->setInterval(60 * 5);
$this->manager = $manager;
$this->participantService = $participantService;
$this->logger = $logger;
}
@ -71,7 +77,7 @@ class RemoveEmptyRooms extends TimedJob {
return;
}
if ($room->getNumberOfParticipants(false) === 0 && $room->getObjectType() !== 'file') {
if ($this->participantService->getNumberOfActors($room) === 0 && $room->getObjectType() !== 'file') {
$room->deleteRoom();
$this->numDeletedRooms++;
}

Просмотреть файл

@ -1249,15 +1249,15 @@ class RoomController extends AEnvironmentAwareController {
protected function removeSelfFromRoomLogic(Room $room, Participant $participant): DataResponse {
if ($room->getType() !== Room::ONE_TO_ONE_CALL) {
if ($participant->hasModeratorPermissions(false)
&& $room->getNumberOfParticipants() > 1
&& $room->getNumberOfModerators() === 1) {
&& $this->participantService->getNumberOfUsers($room) > 1
&& $this->participantService->getNumberOfModerators($room) === 1) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
if ($room->getType() !== Room::CHANGELOG_CONVERSATION &&
$room->getObjectType() !== 'file' &&
$room->getNumberOfParticipants() === 1) {
$this->participantService->getNumberOfUsers($room) === 1) {
$room->deleteRoom();
return new DataResponse();
}

Просмотреть файл

@ -25,6 +25,7 @@ namespace OCA\Talk\Listener;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserDeletedEvent;
@ -33,9 +34,13 @@ class UserDeletedListener implements IEventListener {
/** @var Manager */
private $manager;
/** @var ParticipantService */
private $participantService;
public function __construct(Manager $manager) {
public function __construct(Manager $manager,
ParticipantService $participantService) {
$this->manager = $manager;
$this->participantService = $participantService;
}
public function handle(Event $event): void {
@ -49,7 +54,7 @@ class UserDeletedListener implements IEventListener {
$rooms = $this->manager->getRoomsForUser($user->getUID());
foreach ($rooms as $room) {
if ($room->getNumberOfParticipants() === 1) {
if ($this->participantService->getNumberOfUsers($room) === 1) {
$room->deleteRoom();
} else {
$room->removeUser($user, Room::PARTICIPANT_REMOVED);

Просмотреть файл

@ -77,6 +77,30 @@ class AttendeeMapper extends QBMapper {
return $this->findEntities($query);
}
/**
* @param int $roomId
* @param int[] $participantType
* @return int
*/
public function countActorsByParticipantType(int $roomId, array $participantType): int {
$query = $this->db->getQueryBuilder();
$query->select($query->func()->count('*', 'num_actors'))
->from($this->getTableName())
->where($query->expr()->eq('room_id', $query->createNamedParameter($roomId, IQueryBuilder::PARAM_INT)));
// TODO Should exclude groups and circles when we add them
if (!empty($participantType)) {
$query->andWhere($query->expr()->in('participant_type', $query->createNamedParameter($participantType, IQueryBuilder::PARAM_INT_ARRAY)));
}
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
return (int) ($row['num_actors'] ?? 0);
}
public function createAttendeeFromRow(array $row): Attendee {
return $this->mapRowToEntity([
'id' => $row['a_id'],

Просмотреть файл

@ -1285,58 +1285,6 @@ class Room {
return (bool) $row;
}
public function getNumberOfModerators(bool $ignoreGuests = true): int {
$types = [
Participant::OWNER,
Participant::MODERATOR,
];
if (!$ignoreGuests) {
$types[] = Participant::GUEST_MODERATOR;
}
$query = $this->db->getQueryBuilder();
$query->select($query->func()->count('*', 'num_moderators'))
->from('talk_participants')
->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->in('participant_type', $query->createNamedParameter($types, IQueryBuilder::PARAM_INT_ARRAY)));
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
return (int) ($row['num_moderators'] ?? 0);
}
/**
* @param bool $ignoreGuests
* @param int $lastPing When the last ping is older than the given timestamp, the user is ignored
* @return int
*/
public function getNumberOfParticipants(bool $ignoreGuests = true, int $lastPing = 0): int {
$query = $this->db->getQueryBuilder();
$query->select($query->func()->count('*', 'num_participants'))
->from('talk_participants')
->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)));
if ($lastPing > 0) {
$query->andWhere($query->expr()->gt('last_ping', $query->createNamedParameter($lastPing, IQueryBuilder::PARAM_INT)));
}
if ($ignoreGuests) {
$query->andWhere($query->expr()->notIn('participant_type', $query->createNamedParameter([
Participant::GUEST,
Participant::GUEST_MODERATOR,
Participant::USER_SELF_JOINED,
], IQueryBuilder::PARAM_INT_ARRAY)));
}
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
return (int) ($row['num_participants'] ?? 0);
}
public function markUsersAsMentioned(array $userIds, int $messageId): void {
$query = $this->db->getQueryBuilder();
$query->update('talk_participants')

Просмотреть файл

@ -305,4 +305,35 @@ class ParticipantService {
return $attendee->getActorId();
}, $attendees);
}
/**
* @param Room $room
* @return int
*/
public function getNumberOfUsers(Room $room): int {
return $this->attendeeMapper->countActorsByParticipantType($room->getId(), [
Participant::USER,
Participant::MODERATOR,
Participant::OWNER,
]);
}
/**
* @param Room $room
* @return int
*/
public function getNumberOfModerators(Room $room): int {
return $this->attendeeMapper->countActorsByParticipantType($room->getId(), [
Participant::MODERATOR,
Participant::OWNER,
]);
}
/**
* @param Room $room
* @return int
*/
public function getNumberOfActors(Room $room): int {
return $this->attendeeMapper->countActorsByParticipantType($room->getId(), []);
}
}