|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2021, Gary Kim <gary@garykim.dev> |
| 6 | + * |
| 7 | + * @author Gary Kim <gary@garykim.dev> |
| 8 | + * |
| 9 | + * @license GNU AGPL version 3 or any later version |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\Talk\Federation; |
| 27 | + |
| 28 | +use OCA\Talk\Exceptions\UnauthorizedException; |
| 29 | +use OCA\Talk\Room; |
| 30 | +use OCP\DB\Exception as DBException; |
| 31 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 32 | +use OCP\IConfig; |
| 33 | +use OCP\IDBConnection; |
| 34 | +use OCP\IUser; |
| 35 | + |
| 36 | +class FederationManager { |
| 37 | + /** @var IDBConnection */ |
| 38 | + private $db; |
| 39 | + /** @var IConfig */ |
| 40 | + private $config; |
| 41 | + |
| 42 | + public function __construct ( |
| 43 | + IDBConnection $db, |
| 44 | + IConfig $config |
| 45 | + ) { |
| 46 | + $this->db = $db; |
| 47 | + $this->config = $config; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Determine if Talk federation is enabled on this instance |
| 52 | + * @return bool |
| 53 | + */ |
| 54 | + public function isEnabled(): bool { |
| 55 | + // TODO: Set to true once implementation is complete |
| 56 | + return $this->config->getSystemValueBool('talk_federation_enabled', false); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param IUser $user |
| 61 | + * @param string $roomToken |
| 62 | + * @param string $remoteUrl |
| 63 | + * @param string $sharedSecret |
| 64 | + * @return int share id for this specific remote room share |
| 65 | + * @throws DBException |
| 66 | + */ |
| 67 | + public function addRemoteRoom(IUser $user, string $roomToken, string $remoteUrl, string $sharedSecret): int { |
| 68 | + $qb = $this->db->getQueryBuilder(); |
| 69 | + |
| 70 | + $query = $qb->insert('talk_rooms_external') |
| 71 | + ->values([ |
| 72 | + 'user_id' => $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR), |
| 73 | + 'room_id' => $qb->createNamedParameter($roomToken, IQueryBuilder::PARAM_STR), |
| 74 | + 'remote_url' => $qb->createNamedParameter($remoteUrl, IQueryBuilder::PARAM_STR), |
| 75 | + 'shareToken' => $qb->createNamedParameter($sharedSecret, IQueryBuilder::PARAM_STR), |
| 76 | + ]) |
| 77 | + ->executeQuery(); |
| 78 | + |
| 79 | + $row = $query->fetch(); |
| 80 | + return (int)($row['id']); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @throws DBException |
| 85 | + * @throws UnauthorizedException |
| 86 | + */ |
| 87 | + public function acceptRemoteRoomShare(IUser $user, int $shareId) { |
| 88 | + $share = this->$this->getShare($shareId); |
| 89 | + if ($share['user_id'] !== $user->getUID()) { |
| 90 | + throw new UnauthorizedException(); |
| 91 | + } |
| 92 | + |
| 93 | + $qb = $this->db->getQueryBuilder(); |
| 94 | + |
| 95 | + $qb->update('talk_rooms_external') |
| 96 | + ->set('accepted', true) |
| 97 | + ->where( |
| 98 | + $qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT)) |
| 99 | + ) |
| 100 | + ->executeQuery(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @throws DBException |
| 105 | + * @throws UnauthorizedException |
| 106 | + */ |
| 107 | + public function rejectRemoteRoomShare(IUser $user, int $shareId) { |
| 108 | + $share = this->$this->getShare($shareId); |
| 109 | + if ($share['user_id'] !== $user->getUID()) { |
| 110 | + throw new UnauthorizedException(); |
| 111 | + } |
| 112 | + |
| 113 | + $qb = $this->db->getQueryBuilder(); |
| 114 | + |
| 115 | + $qb->delete('talk_rooms_external') |
| 116 | + ->where( |
| 117 | + $qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT)) |
| 118 | + ) |
| 119 | + ->executeQuery(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @throws DBException |
| 124 | + */ |
| 125 | + public function getShare(int $shareId): array { |
| 126 | + $qb = $this->db->getQueryBuilder(); |
| 127 | + |
| 128 | + $query = $qb->select('*') |
| 129 | + ->from('talk_rooms_external') |
| 130 | + ->where( |
| 131 | + $qb->expr()->eq('id', $qb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT)) |
| 132 | + ) |
| 133 | + ->executeQuery(); |
| 134 | + |
| 135 | + return $query->fetch(); |
| 136 | + } |
| 137 | +} |
0 commit comments