Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ jobs:
options: --health-cmd="mysqladmin ping" --health-interval 5s --health-timeout 2s --health-retries 5

steps:
- name: Enable ONLY_FULL_GROUP_BY MySQL option
run: |
echo "SET GLOBAL sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'));" | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword
echo "SELECT @@sql_mode;" | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword

- name: Checkout server
uses: actions/checkout@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion lib/Chat/MessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function setActor(Message $message): void {
$displayName = $this->guestNames[$comment->getActorId()];
} else {
try {
$participant = $message->getRoom()->getParticipantByActor(Attendee::ACTOR_GUESTS, $comment->getActorId(), false);
$participant = $message->getRoom()->getParticipantByActor(Attendee::ACTOR_GUESTS, $comment->getActorId());
$displayName = $participant->getAttendee()->getDisplayName();
} catch (ParticipantNotFoundException $e) {
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ protected function getGuest(Room $room, string $actorId): array {

protected function getGuestName(Room $room, string $actorId): string {
try {
$participant = $room->getParticipantByActor(Attendee::ACTOR_GUESTS, $actorId, false);
$participant = $room->getParticipantByActor(Attendee::ACTOR_GUESTS, $actorId);
$name = $participant->getAttendee()->getDisplayName();
if ($name === '') {
return $this->l->t('Guest');
Expand Down
2 changes: 1 addition & 1 deletion lib/Chat/Parser/UserMention.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function parseMessage(Message $chatMessage): void {
];
} elseif ($mention['type'] === 'guest') {
try {
$participant = $chatMessage->getRoom()->getParticipantByActor(Attendee::ACTOR_GUESTS, substr($mention['id'], strlen('guest/')), false);
$participant = $chatMessage->getRoom()->getParticipantByActor(Attendee::ACTOR_GUESTS, substr($mention['id'], strlen('guest/')));
$displayName = $participant->getAttendee()->getDisplayName() ?: $this->l->t('Guest');
} catch (ParticipantNotFoundException $e) {
$displayName = $this->l->t('Guest');
Expand Down
11 changes: 2 additions & 9 deletions lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1508,15 +1508,8 @@ protected function changeParticipantType(int $attendeeId, bool $promote): DataRe
}

// Prevent users/moderators modifying themselves
if ($attendee->getActorType() === Attendee::ACTOR_USERS) {
if ($attendee->getActorId() === $this->userId) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
} elseif ($attendee->getActorType() === Attendee::ACTOR_GUESTS) {
$session = $targetParticipant->getSession();
$currentSessionId = $this->session->getSessionForRoom($this->room->getToken());

if ($session instanceof Session && $currentSessionId === $session->getSessionId()) {
if ($attendee->getActorType() === $this->participant->getAttendee()->getActorType()) {
if ($attendee->getActorId() === $this->participant->getAttendee()->getActorId()) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
} elseif ($attendee->getActorType() === Attendee::ACTOR_GROUPS) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/SignalingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,14 @@ private function backendRoom(array $roomRequest): DataResponse {
// If the user joins the session might not be known to the server yet.
// In this case we load by actor information and use the session id as new session.
try {
$participant = $room->getParticipantByActor($actorType, $actorId, false);
$participant = $room->getParticipantByActor($actorType, $actorId);
} catch (ParticipantNotFoundException $e) {
}
}
}
} else {
try {
$participant = $room->getParticipantByActor($actorType, $actorId, false);
$participant = $room->getParticipantByActor($actorType, $actorId);
} catch (ParticipantNotFoundException $e) {
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
* @throws ParticipantNotFoundException
*/
protected function getGuestParameter(Room $room, string $actorId): array {
$participant = $room->getParticipantByActor(Attendee::ACTOR_GUESTS, $actorId, false);
$participant = $room->getParticipantByActor(Attendee::ACTOR_GUESTS, $actorId);
$name = $participant->getAttendee()->getDisplayName();
if (trim($name) === '') {
throw new ParticipantNotFoundException('Empty name');
Expand Down
40 changes: 3 additions & 37 deletions lib/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,10 @@ public function getParticipantByPin(string $pin): Participant {

/**
* @param int $attendeeId
* @param string|null|false $sessionId Set to false if you don't want to load a session (and save resources),
* string to try loading a specific session
* null to try loading "any"
* @return Participant
* @throws ParticipantNotFoundException When the pin is not valid (has no participant assigned)
*/
public function getParticipantByAttendeeId(int $attendeeId, $sessionId = null): Participant {
public function getParticipantByAttendeeId(int $attendeeId): Participant {
$query = $this->db->getQueryBuilder();
$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
Expand All @@ -582,20 +579,6 @@ public function getParticipantByAttendeeId(int $attendeeId, $sessionId = null):
->andWhere($query->expr()->eq('a.room_id', $query->createNamedParameter($this->getId())))
->setMaxResults(1);

if ($sessionId !== false) {
if ($sessionId !== null) {
$helper->selectSessionsTable($query);
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->andX(
$query->expr()->eq('s.session_id', $query->createNamedParameter($sessionId)),
$query->expr()->eq('a.id', 's.attendee_id')
));
} else {
$helper->selectSessionsTableMax($query);
$query->groupBy('a.id');
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->eq('a.id', 's.attendee_id'));
}
}

$result = $query->executeQuery();
$row = $result->fetch();
$result->closeCursor();
Expand All @@ -610,15 +593,12 @@ public function getParticipantByAttendeeId(int $attendeeId, $sessionId = null):
/**
* @param string $actorType
* @param string $actorId
* @param string|null|false $sessionId Set to false if you don't want to load a session (and save resources),
* string to try loading a specific session
* null to try loading "any"
* @return Participant
* @throws ParticipantNotFoundException When the pin is not valid (has no participant assigned)
*/
public function getParticipantByActor(string $actorType, string $actorId, $sessionId = null): Participant {
public function getParticipantByActor(string $actorType, string $actorId): Participant {
if ($actorType === Attendee::ACTOR_USERS) {
return $this->getParticipant($actorId, $sessionId);
return $this->getParticipant($actorId, false);
}

$query = $this->db->getQueryBuilder();
Expand All @@ -630,20 +610,6 @@ public function getParticipantByActor(string $actorType, string $actorId, $sessi
->andWhere($query->expr()->eq('a.room_id', $query->createNamedParameter($this->getId())))
->setMaxResults(1);

if ($sessionId !== false) {
if ($sessionId !== null) {
$helper->selectSessionsTable($query);
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->andX(
$query->expr()->eq('s.session_id', $query->createNamedParameter($sessionId)),
$query->expr()->eq('a.id', 's.attendee_id')
));
} else {
$helper->selectSessionsTableMax($query);
$query->groupBy('a.id');
$query->leftJoin('a', 'talk_sessions', 's', $query->expr()->eq('a.id', 's.attendee_id'));
}
}

$result = $query->executeQuery();
$row = $result->fetch();
$result->closeCursor();
Expand Down
14 changes: 10 additions & 4 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,18 +1226,24 @@ public function getParticipantsByNotificationLevel(Room $room, int $notification

$helper = new SelectHelper();
$helper->selectAttendeesTable($query);
$helper->selectSessionsTableMax($query);
$helper->selectSessionsTable($query);
$query->from('talk_attendees', 'a')
// Currently we only care if the user has a session at all, so we can select any: #ThisIsFine
->leftJoin(
'a', 'talk_sessions', 's',
$query->expr()->eq('s.attendee_id', 'a.id')
)
->where($query->expr()->eq('a.room_id', $query->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('a.notification_level', $query->createNamedParameter($notificationLevel, IQueryBuilder::PARAM_INT)))
->groupBy('a.id');
->andWhere($query->expr()->eq('a.notification_level', $query->createNamedParameter($notificationLevel, IQueryBuilder::PARAM_INT)));

return $this->getParticipantsFromQuery($query, $room);
$participants = $this->getParticipantsFromQuery($query, $room);

$uniqueAttendees = [];
foreach ($participants as $participant) {
$uniqueAttendees[$participant->getAttendee()->getId()] = $participant;
}

return array_values($uniqueAttendees);
}

/**
Expand Down
152 changes: 152 additions & 0 deletions tests/php/Service/ParticipantServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Tests\php\Service;

use OCA\Talk\Config;
use OCA\Talk\Federation\Notifications;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\AttendeeMapper;
use OCA\Talk\Model\Session;
use OCA\Talk\Model\SessionMapper;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\MembershipService;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\SessionService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* @group DB
*/
class ParticipantServiceTest extends TestCase {

/** @var IConfig|MockObject */
protected $serverConfig;
/** @var Config|MockObject */
protected $talkConfig;
/** @var AttendeeMapper */
protected $attendeeMapper;
/** @var SessionMapper */
protected $sessionMapper;
/** @var SessionService|MockObject */
protected $sessionService;
/** @var ISecureRandom|MockObject */
protected $secureRandom;
/** @var IEventDispatcher|MockObject */
protected $dispatcher;
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IGroupManager|MockObject */
protected $groupManager;
/** @var MembershipService|MockObject */
protected $membershipService;
/** @var Notifications|MockObject */
protected $federationNotifications;
/** @var ITimeFactory|MockObject */
protected $time;
/** @var ICacheFactory|MockObject */
protected $cacheFactory;
/** @var ParticipantService */
private $service;


public function setUp(): void {
parent::setUp();

$this->serverConfig = $this->createMock(IConfig::class);
$this->talkConfig = $this->createMock(Config::class);
$this->attendeeMapper = new AttendeeMapper(\OC::$server->getDatabaseConnection());
$this->sessionMapper = new SessionMapper(\OC::$server->getDatabaseConnection());
$this->sessionService = $this->createMock(SessionService::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->membershipService = $this->createMock(MembershipService::class);
$this->federationNotifications = $this->createMock(Notifications::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->service = new ParticipantService(
$this->serverConfig,
$this->talkConfig,
$this->attendeeMapper,
$this->sessionMapper,
$this->sessionService,
$this->secureRandom,
\OC::$server->getDatabaseConnection(),
$this->dispatcher,
$this->userManager,
$this->groupManager,
$this->membershipService,
$this->federationNotifications,
$this->time,
$this->cacheFactory
);
}

public function tearDown(): void {
try {
$attendee = $this->attendeeMapper->findByActor(123456789, Attendee::ACTOR_USERS, 'test');
$this->sessionMapper->deleteByAttendeeId($attendee->getId());
$this->attendeeMapper->delete($attendee);
} catch (DoesNotExistException $exception) {
}

parent::tearDown();
}

public function testGetParticipantsByNotificationLevel(): void {
$attendee = new Attendee();
$attendee->setActorType(Attendee::ACTOR_USERS);
$attendee->setActorId('test');
$attendee->setRoomId(123456789);
$attendee->setNotificationLevel(Participant::NOTIFY_MENTION);
$this->attendeeMapper->insert($attendee);

$session1 = new Session();
$session1->setAttendeeId($attendee->getId());
$session1->setSessionId(self::getUniqueID('session1'));
$this->sessionMapper->insert($session1);

$session2 = new Session();
$session2->setAttendeeId($attendee->getId());
$session2->setSessionId(self::getUniqueID('session2'));
$this->sessionMapper->insert($session2);

$room = $this->createMock(Room::class);
$room->method('getId')
->willReturn(123456789);
$participants = $this->service->getParticipantsByNotificationLevel($room, Participant::NOTIFY_MENTION);
self::assertCount(1, $participants);
}
}