Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the owner update queries when the instance has no one2one rooms #537

Merged
merged 6 commits into from
Jan 4, 2018
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
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m

]]></description>

<version>2.9.0</version>
<version>2.9.1</version>
<licence>agpl</licence>

<author>Ivan Sein</author>
Expand Down
74 changes: 37 additions & 37 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function __construct(IDBConnection $db, IConfig $config, ISecureRandom $s
*/
public function createRoomObject(array $row) {
$activeSince = null;
if (!empty($row['activeSince'])) {
$activeSince = new \DateTime($row['activeSince']);
if (!empty($row['active_since'])) {
$activeSince = new \DateTime($row['active_since']);
}

return new Room($this, $this->db, $this->secureRandom, $this->dispatcher, $this->hasher, (int) $row['id'], (int) $row['type'], $row['token'], $row['name'], $row['password'], (int) $row['activeGuests'], $activeSince);
return new Room($this, $this->db, $this->secureRandom, $this->dispatcher, $this->hasher, (int) $row['id'], (int) $row['type'], $row['token'], $row['name'], $row['password'], (int) $row['active_guests'], $activeSince);
}

/**
Expand All @@ -80,7 +80,7 @@ public function createRoomObject(array $row) {
* @return Participant
*/
public function createParticipantObject(Room $room, array $row) {
return new Participant($this->db, $room, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId'], (bool) $row['inCall']);
return new Participant($this->db, $room, $row['user_id'], (int) $row['participant_type'], (int) $row['last_ping'], $row['session_id'], (bool) $row['in_call']);
}

/**
Expand All @@ -92,17 +92,17 @@ public function getRoomsForParticipant($participant) {
$query->select('*')
->from('talk_rooms', 'r')
->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
$query->expr()->eq('p.user_id', $query->createNamedParameter($participant)),
$query->expr()->eq('p.room_id', 'r.id')
))
->where($query->expr()->isNotNull('p.userId'));
->where($query->expr()->isNotNull('p.user_id'));

$result = $query->execute();
$rooms = [];
while ($row = $result->fetch()) {
$room = $this->createRoomObject($row);
if ($participant !== null && isset($row['userId'])) {
$room->setParticipant($row['userId'], $this->createParticipantObject($room, $row));
if ($participant !== null && isset($row['user_id'])) {
$room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row));
}
$rooms[] = $room;
}
Expand All @@ -128,10 +128,10 @@ public function getRoomForParticipant($roomId, $participant) {
if ($participant !== null) {
// Non guest user
$query->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
$query->expr()->eq('p.user_id', $query->createNamedParameter($participant)),
$query->expr()->eq('p.room_id', 'r.id')
))
->andWhere($query->expr()->isNotNull('p.userId'));
->andWhere($query->expr()->isNotNull('p.user_id'));
}

$result = $query->execute();
Expand All @@ -143,8 +143,8 @@ public function getRoomForParticipant($roomId, $participant) {
}

$room = $this->createRoomObject($row);
if ($participant !== null && isset($row['userId'])) {
$room->setParticipant($row['userId'], $this->createParticipantObject($room, $row));
if ($participant !== null && isset($row['user_id'])) {
$room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row));
}

if ($participant === null && $room->getType() !== Room::PUBLIC_CALL) {
Expand Down Expand Up @@ -173,8 +173,8 @@ public function getRoomForParticipantByToken($token, $participant) {
if ($participant !== null) {
// Non guest user
$query->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
$query->expr()->eq('p.user_id', $query->createNamedParameter($participant)),
$query->expr()->eq('p.room_id', 'r.id')
));
}

Expand All @@ -187,15 +187,15 @@ public function getRoomForParticipantByToken($token, $participant) {
}

$room = $this->createRoomObject($row);
if ($participant !== null && isset($row['userId'])) {
$room->setParticipant($row['userId'], $this->createParticipantObject($room, $row));
if ($participant !== null && isset($row['user_id'])) {
$room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row));
}

if ($room->getType() === Room::PUBLIC_CALL) {
return $room;
}

if ($participant !== null && $row['userId'] === $participant) {
if ($participant !== null && $row['user_id'] === $participant) {
return $room;
}

Expand Down Expand Up @@ -260,8 +260,8 @@ public function getRoomForSession($userId, $sessionId) {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('talk_participants', 'p')
->leftJoin('p', 'talk_rooms', 'r', $query->expr()->eq('p.roomId', 'r.id'))
->where($query->expr()->eq('p.sessionId', $query->createNamedParameter($sessionId)))
->leftJoin('p', 'talk_rooms', 'r', $query->expr()->eq('p.room_id', 'r.id'))
->where($query->expr()->eq('p.session_id', $query->createNamedParameter($sessionId)))
->setMaxResults(1);

$result = $query->execute();
Expand All @@ -272,13 +272,13 @@ public function getRoomForSession($userId, $sessionId) {
throw new RoomNotFoundException();
}

if ((string) $userId !== $row['userId']) {
if ((string) $userId !== $row['user_id']) {
throw new RoomNotFoundException();
}

$room = $this->createRoomObject($row);
$participant = $this->createParticipantObject($room, $row);
$room->setParticipant($row['userId'], $participant);
$room->setParticipant($row['user_id'], $participant);

if ($room->getType() === Room::PUBLIC_CALL || !in_array($participant->getParticipantType(), [Participant::GUEST, Participant::USER_SELF_JOINED], true)) {
return $room;
Expand All @@ -298,16 +298,16 @@ public function getOne2OneRoom($participant1, $participant2) {
$query->select('*')
->from('talk_rooms', 'r1')
->leftJoin('r1', 'talk_participants', 'p1', $query->expr()->andX(
$query->expr()->eq('p1.userId', $query->createNamedParameter($participant1)),
$query->expr()->eq('p1.roomId', 'r1.id')
$query->expr()->eq('p1.user_id', $query->createNamedParameter($participant1)),
$query->expr()->eq('p1.room_id', 'r1.id')
))
->leftJoin('r1', 'talk_participants', 'p2', $query->expr()->andX(
$query->expr()->eq('p2.userId', $query->createNamedParameter($participant2)),
$query->expr()->eq('p2.roomId', 'r1.id')
$query->expr()->eq('p2.user_id', $query->createNamedParameter($participant2)),
$query->expr()->eq('p2.room_id', 'r1.id')
))
->where($query->expr()->eq('r1.type', $query->createNamedParameter(Room::ONE_TO_ONE_CALL, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->isNotNull('p1.userId'))
->andWhere($query->expr()->isNotNull('p2.userId'));
->andWhere($query->expr()->isNotNull('p1.user_id'))
->andWhere($query->expr()->isNotNull('p2.user_id'));

$result = $query->execute();
$row = $result->fetch();
Expand Down Expand Up @@ -377,9 +377,9 @@ public function getCurrentSessionId($userId) {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('talk_participants')
->where($query->expr()->eq('userId', $query->createNamedParameter($userId)))
->andWhere($query->expr()->neq('sessionId', $query->createNamedParameter('0')))
->orderBy('lastPing', 'DESC')
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->neq('session_id', $query->createNamedParameter('0')))
->orderBy('last_ping', 'DESC')
->setMaxResults(1);
$result = $query->execute();
$row = $result->fetch();
Expand All @@ -389,7 +389,7 @@ public function getCurrentSessionId($userId) {
return null;
}

return $row['sessionId'];
return $row['session_id'];
}

/**
Expand All @@ -404,15 +404,15 @@ public function getSessionIdsForUser($userId) {

// Delete all messages from or to the current user
$query = $this->db->getQueryBuilder();
$query->select('sessionId')
$query->select('session_id')
->from('talk_participants')
->where($query->expr()->eq('userId', $query->createNamedParameter($userId)));
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
$result = $query->execute();

$sessionIds = [];
while ($row = $result->fetch()) {
if ($row['sessionId'] !== '0') {
$sessionIds[] = $row['sessionId'];
if ($row['session_id'] !== '0') {
$sessionIds[] = $row['session_id'];
}
}
$result->closeCursor();
Expand Down
3 changes: 0 additions & 3 deletions lib/Migration/Version2000Date20171026140256.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version2000Date20171026140256 extends SimpleMigrationStep {

/** @var IDBConnection */
Expand Down
3 changes: 0 additions & 3 deletions lib/Migration/Version2000Date20171026140257.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
use OCP\Migration\IOutput;
use OCP\Security\ISecureRandom;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version2000Date20171026140257 extends SimpleMigrationStep {

/** @var IDBConnection */
Expand Down
39 changes: 24 additions & 15 deletions lib/Migration/Version2001Date20170707115443.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ public function __construct(IDBConnection $db) {
$this->db = $db;
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
* @param array $options
* @since 13.0.0
*/
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
Expand Down Expand Up @@ -84,6 +75,17 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
$query = $this->db->getQueryBuilder();

$query->selectAlias($query->createFunction('COUNT(*)'), 'num_rooms')
->from('spreedme_rooms');
$result = $query->execute();
$return = (int) $result->fetch();
$result->closeCursor();
$numRooms = (int) $return['num_rooms'];

if ($numRooms === 0) {
return;
}

$query->select('id')
->from('spreedme_rooms')
->where($query->expr()->eq('type', $query->createNamedParameter(Room::ONE_TO_ONE_CALL)));
Expand All @@ -95,11 +97,15 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array
}
$result->closeCursor();

$owners = $this->makeOne2OneParticipantsOwners($one2oneRooms);
$output->info('Made ' . $owners . ' users owner of their one2one calls');
if (!empty($one2oneRooms)) {
$owners = $this->makeOne2OneParticipantsOwners($one2oneRooms);
$output->info('Made ' . $owners . ' users owner of their one2one calls');
}

$moderators = $this->makeGroupParticipantsModerators($one2oneRooms);
$output->info('Made ' . $moderators . ' users moderators in group calls');
if (count($one2oneRooms) !== $numRooms) {
$moderators = $this->makeGroupParticipantsModerators($one2oneRooms);
$output->info('Made ' . $moderators . ' users moderators in group calls');
}
}

/**
Expand All @@ -125,8 +131,11 @@ protected function makeGroupParticipantsModerators(array $one2oneRooms) {

$update->update('spreedme_room_participants')
->set('participantType', $update->createNamedParameter(Participant::MODERATOR))
->where($update->expr()->nonEmptyString('userId'))
->andWhere($update->expr()->notIn('roomId', $update->createNamedParameter($one2oneRooms, IQueryBuilder::PARAM_INT_ARRAY)));
->where($update->expr()->nonEmptyString('userId'));

if (!empty($one2oneRooms)) {
$update->andWhere($update->expr()->notIn('roomId', $update->createNamedParameter($one2oneRooms, IQueryBuilder::PARAM_INT_ARRAY)));
}

return $update->execute();
}
Expand Down
1 change: 0 additions & 1 deletion lib/Migration/Version2001Date20171026141336.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

class Version2001Date20171026141336 extends SimpleMigrationStep {


/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
Expand Down
41 changes: 21 additions & 20 deletions lib/Migration/Version2001Date20171031102049.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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\Spreed\Migration;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version2001Date20171031102049 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
* @param array $options
* @since 13.0.0
*/
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
Expand All @@ -40,12 +49,4 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
return $schema;
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
* @param array $options
* @since 13.0.0
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
}
}
Loading