Skip to content

Commit

Permalink
Implement CloudFederationProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Kim <[email protected]>
  • Loading branch information
gary-kim committed Jun 15, 2021
1 parent 8f3e737 commit 0cb0c07
Show file tree
Hide file tree
Showing 5 changed files with 389 additions and 4 deletions.
21 changes: 21 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,27 @@
],
],

/**
* Remote
*/

[
'name' => 'Remote#acceptShare',
'url' => 'api/{apiVersion}/remote/pending/{id}',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v1',
],
],
[
'name' => 'Remote#rejectShare',
'url' => 'api/{apiVersion}/remote/pending/{id}',
'verb' => 'DELETE',
'requirements' => [
'apiVersion' => 'v1',
],
],

/**
* PublicShareAuth
*/
Expand Down
52 changes: 52 additions & 0 deletions lib/Controller/RemoteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021, Gary Kim <[email protected]>
*
* @author Gary Kim <[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\Controller;

use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;

class RemoteController extends OCSController {
public function __construct(string $appName, IRequest $request) {
parent::__construct($appName, $request);
}

/**
* @param string $id
* @return DataResponse
*/
public function acceptShare(string $id): DataResponse {

}

/**
* @param string $id
* @return DataResponse
*/
public function rejectShare(string $id): DataResponse {

}
}
137 changes: 137 additions & 0 deletions lib/Federation/FederationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021, Gary Kim <[email protected]>
*
* @author Gary Kim <[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\Federation;

use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\Room;
use OCP\DB\Exception as DBException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUser;

class FederationManager {
/** @var IDBConnection */
private $db;
/** @var IConfig */
private $config;

public function __construct (
IDBConnection $db,
IConfig $config
) {
$this->db = $db;
$this->config = $config;
}

/**
* Determine if Talk federation is enabled on this instance
* @return bool
*/
public function isEnabled(): bool {
// TODO: Set to true once implementation is complete
return $this->config->getSystemValueBool('talk_federation_enabled', false);
}

/**
* @param IUser $user
* @param string $roomToken
* @param string $remoteUrl
* @param string $sharedSecret
* @return int share id for this specific remote room share
* @throws DBException
*/
public function addRemoteRoom(IUser $user, string $roomToken, string $remoteUrl, string $sharedSecret): int {
$qb = $this->db->getQueryBuilder();

$query = $qb->insert('talk_rooms_external')
->values([
'user_id' => $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR),
'room_id' => $qb->createNamedParameter($roomToken, IQueryBuilder::PARAM_STR),
'remote_url' => $qb->createNamedParameter($remoteUrl, IQueryBuilder::PARAM_STR),
'shareToken' => $qb->createNamedParameter($sharedSecret, IQueryBuilder::PARAM_STR),
])
->executeQuery();

$row = $query->fetch();
return (int)($row['id']);
}

/**
* @throws DBException
* @throws UnauthorizedException
*/
public function acceptRemoteRoomShare(IUser $user, int $shareId) {
$share = this->$this->getShare($shareId);
if ($share['user_id'] !== $user->getUID()) {
throw new UnauthorizedException();
}

$qb = $this->db->getQueryBuilder();

$qb->update('talk_rooms_external')
->set('accepted', true)
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))
)
->executeQuery();
}

/**
* @throws DBException
* @throws UnauthorizedException
*/
public function rejectRemoteRoomShare(IUser $user, int $shareId) {
$share = this->$this->getShare($shareId);
if ($share['user_id'] !== $user->getUID()) {
throw new UnauthorizedException();
}

$qb = $this->db->getQueryBuilder();

$qb->delete('talk_rooms_external')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))
)
->executeQuery();
}

/**
* @throws DBException
*/
public function getShare(int $shareId): array {
$qb = $this->db->getQueryBuilder();

$query = $qb->select('*')
->from('talk_rooms_external')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))
)
->executeQuery();

return $query->fetch();
}
}
41 changes: 37 additions & 4 deletions lib/Migration/Version12000Date20210610232111.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\Talk\Migration;

use Closure;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Types\Types;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
Expand All @@ -38,13 +39,14 @@ class Version12000Date20210610232111 extends SimpleMigrationStep {
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
* @throws SchemaException
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if (!$schema->hasTable('talk_federated_rooms')) {
$table = $schema->createTable('talk_federated_rooms');
if (!$schema->hasTable('talk_federated_servers')) {
$table = $schema->createTable('talk_federated_servers');
$table->addColumn('id', Types::BIGINT, [
'autoincrement' => true,
'notnull' => true,
Expand All @@ -59,7 +61,38 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->addColumn('password', Types::TEXT, [
'notnull' => true,
]);
$table->setPrimaryKey('id');
$table->addColumn('accepted', Types::BOOLEAN, [
'notnull' => true,
'default' => false,
])
$table->setPrimaryKey(['id']);
}

if (!$schema->hasTable('talk_rooms_external')) {
$table = $schema->createTable('talk_rooms_external');
$table->addColumn('id', Types::BIGINT, [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('room_id', Types::BIGINT, [
'notnull' => true,
'unsigned' => true,
]);
$table->addColumn('remote_url', Types::TEXT, [
'notnull' => true,
]);
$table->addColumn('accepted', Types::BOOLEAN, [
'notnull' => true,
'default' => false,
]);
$table->addColumn('shareToken', Types::STRING, [
'notnull' => true,
]);
$table->addColumn('userId', Types::STRING, [
'notnull' => false,
'length' => 255,
]);
$table->setPrimaryKey(['id']);
}

return $schema;
Expand Down
Loading

0 comments on commit 0cb0c07

Please sign in to comment.