Skip to content

Commit

Permalink
Merge pull request #3285 from nextcloud/bugfix/noid/merge-ping-updates
Browse files Browse the repository at this point in the history
Ping all session ids with 1 query from the external signaling
  • Loading branch information
nickvergessen authored Apr 14, 2020
2 parents c1823f1 + c16fec5 commit a2e7c0a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/Controller/SignalingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,17 @@ private function backendPing(array $request): DataResponse {
]);
}

$pingSessionIds = [];
$now = $this->timeFactory->getTime();
foreach ($request['entries'] as $entry) {
if (array_key_exists('userid', $entry)) {
$room->ping($entry['userid'], $entry['sessionid'], $now);
} else {
$room->ping('', $entry['sessionid'], $now);
if ($entry['sessionid'] !== '0') {
$pingSessionIds[] = $entry['sessionid'];
}
}

// Ping all active sessions with one query
$room->pingSessionIds($pingSessionIds, $now);

$response = [
'type' => 'room',
'room' => [
Expand Down
14 changes: 14 additions & 0 deletions lib/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -1240,4 +1240,18 @@ public function ping(?string $userId, string $sessionId, int $timestamp): void {

$query->execute();
}

/**
* @param string[] $sessionIds
* @param int $timestamp
*/
public function pingSessionIds(array $sessionIds, int $timestamp): void {
$query = $this->db->getQueryBuilder();
$query->update('talk_participants')
->set('last_ping', $query->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->in('session_id', $query->createNamedParameter($sessionIds), IQueryBuilder::PARAM_STR_ARRAY));

$query->execute();
}
}
52 changes: 48 additions & 4 deletions tests/php/Controller/SignalingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,8 @@ public function testBackendPingUser() {
->method('getToken')
->willReturn($roomToken);
$room->expects($this->once())
->method('ping')
->with($this->userId, $sessionId);
->method('pingSessionIds')
->with([$sessionId]);

$result = $this->performBackendRequest([
'type' => 'ping',
Expand Down Expand Up @@ -786,8 +786,8 @@ public function testBackendPingAnonymous() {
->method('getToken')
->willReturn($roomToken);
$room->expects($this->once())
->method('ping')
->with('', $sessionId);
->method('pingSessionIds')
->with([$sessionId]);

$result = $this->performBackendRequest([
'type' => 'ping',
Expand All @@ -810,6 +810,50 @@ public function testBackendPingAnonymous() {
], $result->getData());
}

public function testBackendPingMixedAndInactive() {
$roomToken = 'the-room';
$sessionId = 'the-session';
$room = $this->createMock(Room::class);
$this->manager->expects($this->once())
->method('getRoomByToken')
->with($roomToken)
->willReturn($room);
$room->expects($this->once())
->method('getToken')
->willReturn($roomToken);
$room->expects($this->once())
->method('pingSessionIds')
->with([$sessionId . '1', $sessionId . '2']);

$result = $this->performBackendRequest([
'type' => 'ping',
'ping' => [
'roomid' => $roomToken,
'entries' => [
[
'userid' => '',
'sessionid' => $sessionId . '1',
],
[
'userid' => $this->userId,
'sessionid' => $sessionId . '2',
],
[
'userid' => 'inactive',
'sessionid' => '0',
],
],
],
]);
$this->assertSame([
'type' => 'room',
'room' => [
'version' => '1.0',
'roomid' => $roomToken,
],
], $result->getData());
}


public function testLeaveRoomWithOldSession() {
// Make sure that leaving a user with an old session id doesn't remove
Expand Down

0 comments on commit a2e7c0a

Please sign in to comment.