<NME> Version10000Date20200819121721.php
<BEF> ADDFILE
<MSG> new DB table to store bridge configs

Signed-off-by: Julien Veyssier <8e8474783687c4f3f88ebb20cff5cefd8daba41d@posteo.net>

<DFF> @@ -0,0 +1,68 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020, Julien Veyssier <eneiluj@posteo.net>
+ *
+ * @author Julien Veyssier <eneiluj@posteo.net>
+ *
+ * @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\Migration;
+
+use Closure;
+use Doctrine\DBAL\Types\Type;
+use OCP\DB\ISchemaWrapper;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+class Version10000Date20200819121721 extends SimpleMigrationStep {
+	/**
+	 * @param IOutput $output
+	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+	 * @param array $options
+	 * @return null|ISchemaWrapper
+	 */
+	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
+		/** @var ISchemaWrapper $schema */
+		$schema = $schemaClosure();
+
+		if (!$schema->hasTable('talk_bridges')) {
+			$table = $schema->createTable('talk_bridges');
+			$table->addColumn('id', Type::INTEGER, [
+				'autoincrement' => true,
+				'notnull' => true,
+				'length' => 11,
+			]);
+			$table->addColumn('room_id', Type::INTEGER, [
+				'notnull' => true,
+				'length' => 11,
+				'unsigned' => true,
+				'default' => 0,
+			]);
+			$table->addColumn('json_values', Type::STRING, [
+				'notnull' => true,
+				'length' => 10000,
+				'default' => '{}',
+			]);
+			$table->addIndex(['room_id'], 'tbr_room_id');
+		}
+
+		return $schema;
+	}
+}

{'commit': 'diff --git a/lib/BridgeManager.php b/lib/BridgeManager.php\nindex 2584c22df..3b1f1f412 100644\n--- a/lib/BridgeManager.php\n+++ b/lib/BridgeManager.php\n@@ -25,6 +25,7 @@ namespace OCA\\Talk;\n \n use OCP\\IConfig;\n use OCP\\IDBConnection;\n+use OCP\\DB\\QueryBuilder\\IQueryBuilder;\n use OCP\\IL10N;\n use OCP\\IUserManager;\n use OCP\\Files\\IAppData;\n@@ -81,10 +82,7 @@ class BridgeManager {\n \t * @return array decoded json bridge information\n \t */\n \tpublic function getBridgeOfRoom(string $token): array {\n-\t\t$defaultParts = \'{"enabled":false,"pid":0,"parts":[]}\';\n-\t\t$bridgeJSON = $this->config->getAppValue(\'spreed\', \'bridge_\' . $token, $defaultParts);\n-\t\t$bridge = json_decode($bridgeJSON, true);\n-\t\treturn $bridge;\n+\t\treturn $this->getBridgeFromDb($token);\n \t}\n \n \t/**\n@@ -111,8 +109,7 @@ class BridgeManager {\n \t\t$newBridge[\'pid\'] = $pid;\n \n \t\t// save config\n-\t\t$newBridgeJSON = json_encode($newBridge);\n-\t\t$this->config->setAppValue(\'spreed\', \'bridge_\' . $token, $newBridgeJSON);\n+\t\t$this->saveBridgeToDb($token, $newBridge);\n \n \t\treturn true;\n \t}\n@@ -157,8 +154,7 @@ class BridgeManager {\n \t\tif ($pid !== $bridge[\'pid\']) {\n \t\t\t// save the new PID if necessary\n \t\t\t$bridge[\'pid\'] = $pid;\n-\t\t\t$bridgeJSON = json_encode($bridge);\n-\t\t\t$this->config->setAppValue(\'spreed\', \'bridge_\' . $token, $bridgeJSON);\n+\t\t\t$this->saveBridgeToDb($token, $bridge);\n \t\t}\n \t}\n \n@@ -557,4 +553,66 @@ class BridgeManager {\n \t\t}\n \t\treturn false;\n \t}\n+\n+\tprivate function getBridgeFromDb(string $token): array {\n+\t\t$room = $this->manager->getRoomByToken($token);\n+\t\t$roomId = $room->getId();\n+\n+\t\t$qb = $this->db->getQueryBuilder();\n+\t\t$qb->select(\'json_values\')\n+\t\t\t->from(\'talk_bridges\', \'b\')\n+\t\t\t->where(\n+\t\t\t\t$qb->expr()->eq(\'room_id\', $qb->createNamedParameter($roomId, IQueryBuilder::PARAM_INT))\n+\t\t\t);\n+\t\t$req = $qb->execute();\n+\t\t$jsonValues = \'{"enabled":false,"pid":0,"parts":[]}\';\n+\t\twhile ($row = $req->fetch()) {\n+\t\t\t$jsonValues = $row[\'json_values\'];\n+\t\t\tbreak;\n+\t\t}\n+\t\t$req->closeCursor();\n+\t\t$qb = $qb->resetQueryParts();\n+\n+\t\treturn json_decode($jsonValues, true);\n+\t}\n+\n+\tprivate function saveBridgeToDb(string $token, array $bridge) {\n+\t\t$room = $this->manager->getRoomByToken($token);\n+\t\t$roomId = $room->getId();\n+\t\t$jsonValues = json_encode($bridge);\n+\n+\t\t$qb = $this->db->getQueryBuilder();\n+\n+\t\t$exists = false;\n+\t\t$qb->select(\'json_values\')\n+\t\t\t->from(\'talk_bridges\', \'b\')\n+\t\t\t->where(\n+\t\t\t\t$qb->expr()->eq(\'room_id\', $qb->createNamedParameter($roomId, IQueryBuilder::PARAM_INT))\n+\t\t\t);\n+\t\t$req = $qb->execute();\n+\t\twhile ($row = $req->fetch()) {\n+\t\t\t$exists = true;\n+\t\t\tbreak;\n+\t\t}\n+\t\t$req->closeCursor();\n+\t\t$qb = $qb->resetQueryParts();\n+\n+\t\tif ($exists) {\n+\t\t\t$qb->update(\'talk_bridges\');\n+\t\t\t$qb->set(\'json_values\', $qb->createNamedParameter($jsonValues, IQueryBuilder::PARAM_STR));\n+\t\t\t$qb->where(\n+\t\t\t\t$qb->expr()->eq(\'room_id\', $qb->createNamedParameter($roomId, IQueryBuilder::PARAM_INT))\n+\t\t\t);\n+\t\t\t$req = $qb->execute();\n+\t\t\t$qb = $qb->resetQueryParts();\n+\t\t} else {\n+\t\t\t$qb->insert(\'talk_bridges\')\n+\t\t\t\t->values([\n+\t\t\t\t\t\'room_id\' => $qb->createNamedParameter($roomId, IQueryBuilder::PARAM_INT),\n+\t\t\t\t\t\'json_values\' => $qb->createNamedParameter($jsonValues, IQueryBuilder::PARAM_STR),\n+\t\t\t\t]);\n+\t\t\t$req = $qb->execute();\n+\t\t\t$qb = $qb->resetQueryParts();\n+\t\t}\n+\t}\n }\ndiff --git a/lib/Migration/Version10000Date20200819121721.php b/lib/Migration/Version10000Date20200819121721.php\nnew file mode 100644\nindex 000000000..8fc49d82e\n--- /dev/null\n+++ b/lib/Migration/Version10000Date20200819121721.php\n@@ -0,0 +1,68 @@\n+<?php\n+\n+declare(strict_types=1);\n+/**\n+ * @copyright Copyright (c) 2020, Julien Veyssier <eneiluj@posteo.net>\n+ *\n+ * @author Julien Veyssier <eneiluj@posteo.net>\n+ *\n+ * @license GNU AGPL version 3 or any later version\n+ *\n+ * This program is free software: you can redistribute it and/or modify\n+ * it under the terms of the GNU Affero General Public License as\n+ * published by the Free Software Foundation, either version 3 of the\n+ * License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU Affero General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU Affero General Public License\n+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+ *\n+ */\n+\n+namespace OCA\\Talk\\Migration;\n+\n+use Closure;\n+use Doctrine\\DBAL\\Types\\Type;\n+use OCP\\DB\\ISchemaWrapper;\n+use OCP\\Migration\\IOutput;\n+use OCP\\Migration\\SimpleMigrationStep;\n+\n+class Version10000Date20200819121721 extends SimpleMigrationStep {\n+\t/**\n+\t * @param IOutput $output\n+\t * @param Closure $schemaClosure The `\\Closure` returns a `ISchemaWrapper`\n+\t * @param array $options\n+\t * @return null|ISchemaWrapper\n+\t */\n+\tpublic function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {\n+\t\t/** @var ISchemaWrapper $schema */\n+\t\t$schema = $schemaClosure();\n+\n+\t\tif (!$schema->hasTable(\'talk_bridges\')) {\n+\t\t\t$table = $schema->createTable(\'talk_bridges\');\n+\t\t\t$table->addColumn(\'id\', Type::INTEGER, [\n+\t\t\t\t\'autoincrement\' => true,\n+\t\t\t\t\'notnull\' => true,\n+\t\t\t\t\'length\' => 11,\n+\t\t\t]);\n+\t\t\t$table->addColumn(\'room_id\', Type::INTEGER, [\n+\t\t\t\t\'notnull\' => true,\n+\t\t\t\t\'length\' => 11,\n+\t\t\t\t\'unsigned\' => true,\n+\t\t\t\t\'default\' => 0,\n+\t\t\t]);\n+\t\t\t$table->addColumn(\'json_values\', Type::STRING, [\n+\t\t\t\t\'notnull\' => true,\n+\t\t\t\t\'length\' => 10000,\n+\t\t\t\t\'default\' => \'{}\',\n+\t\t\t]);\n+\t\t\t$table->addIndex([\'room_id\'], \'tbr_room_id\');\n+\t\t}\n+\n+\t\treturn $schema;\n+\t}\n+}\n', 'commit_subject': 'new DB table to store bridge configs', 'repo_name': 'nextcloud/spreed', 'committer': {'name': 'Joas Schilling', 'email': 'ab43a7c9cb5b2380afc4ddf8b3e2583169b39a02@schilljs.com', 'time_sec': '1598338026', 'tz_offset': '120', 'date': {'seconds': '1598338026'}}, 'commit_tree': '626cd5698e80a208ccd774407aae45e9f3dd5b0c', 'commit_message': 'new DB table to store bridge configs\n\nSigned-off-by: Julien Veyssier <8e8474783687c4f3f88ebb20cff5cefd8daba41d@posteo.net>\n', 'language_name': 'JavaScript', 'license': 'agpl-3.0', 'hunks': "@@ -0,0 +1,68 @@\n+<?php\n+\n+declare(strict_types=1);\n+/**\n+ * @copyright Copyright (c) 2020, Julien Veyssier <eneiluj@posteo.net>\n+ *\n+ * @author Julien Veyssier <eneiluj@posteo.net>\n+ *\n+ * @license GNU AGPL version 3 or any later version\n+ *\n+ * This program is free software: you can redistribute it and/or modify\n+ * it under the terms of the GNU Affero General Public License as\n+ * published by the Free Software Foundation, either version 3 of the\n+ * License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU Affero General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU Affero General Public License\n+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\n+ *\n+ */\n+\n+namespace OCA\\Talk\\Migration;\n+\n+use Closure;\n+use Doctrine\\DBAL\\Types\\Type;\n+use OCP\\DB\\ISchemaWrapper;\n+use OCP\\Migration\\IOutput;\n+use OCP\\Migration\\SimpleMigrationStep;\n+\n+class Version10000Date20200819121721 extends SimpleMigrationStep {\n+\t/**\n+\t * @param IOutput $output\n+\t * @param Closure $schemaClosure The `\\Closure` returns a `ISchemaWrapper`\n+\t * @param array $options\n+\t * @return null|ISchemaWrapper\n+\t */\n+\tpublic function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {\n+\t\t/** @var ISchemaWrapper $schema */\n+\t\t$schema = $schemaClosure();\n+\n+\t\tif (!$schema->hasTable('talk_bridges')) {\n+\t\t\t$table = $schema->createTable('talk_bridges');\n+\t\t\t$table->addColumn('id', Type::INTEGER, [\n+\t\t\t\t'autoincrement' => true,\n+\t\t\t\t'notnull' => true,\n+\t\t\t\t'length' => 11,\n+\t\t\t]);\n+\t\t\t$table->addColumn('room_id', Type::INTEGER, [\n+\t\t\t\t'notnull' => true,\n+\t\t\t\t'length' => 11,\n+\t\t\t\t'unsigned' => true,\n+\t\t\t\t'default' => 0,\n+\t\t\t]);\n+\t\t\t$table->addColumn('json_values', Type::STRING, [\n+\t\t\t\t'notnull' => true,\n+\t\t\t\t'length' => 10000,\n+\t\t\t\t'default' => '{}',\n+\t\t\t]);\n+\t\t\t$table->addIndex(['room_id'], 'tbr_room_id');\n+\t\t}\n+\n+\t\treturn $schema;\n+\t}\n+}\n", 'addition_count': 68, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/lib/Migration/Version10000Date20200819121721.php', 'file_extension': '.php', 'before_file': 'ADDFILE', 'message': 'new DB table to store bridge configs\n\nSigned-off-by: Julien Veyssier <8e8474783687c4f3f88ebb20cff5cefd8daba41d@posteo.net>\n', 'author_name': 'Joas Schilling', 'lang': 'php'}