Skip to content

Commit

Permalink
feat(comments): Add a meta data column for comments
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Dec 13, 2023
1 parent 11e27ac commit cdc197a
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 31 deletions.
67 changes: 67 additions & 0 deletions core/Migrations/Version29000Date20231213104850.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Joas Schilling <[email protected]>
*
* @author Joas Schilling <[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 OC\Core\Migrations;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version29000Date20231213104850 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('comments');
$modified = false;

if (!$table->hasColumn('reference_id')) {
$modified = true;
$table->addColumn('reference_id', Types::STRING, [
'notnull' => false,
'length' => 64,
]);
}

if (!$table->hasColumn('meta_data')) {
$modified = true;
$table->addColumn('meta_data', Types::TEXT, [
'notnull' => false,
'default' => '',
]);
}

return $modified ? $schema : null;
}
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@
'OC\\Core\\Migrations\\Version28000Date20230906104802' => $baseDir . '/core/Migrations/Version28000Date20230906104802.php',
'OC\\Core\\Migrations\\Version28000Date20231004103301' => $baseDir . '/core/Migrations/Version28000Date20231004103301.php',
'OC\\Core\\Migrations\\Version28000Date20231103104802' => $baseDir . '/core/Migrations/Version28000Date20231103104802.php',
'OC\\Core\\Migrations\\Version29000Date20231213104850' => $baseDir . '/core/Migrations/Version29000Date20231213104850.php',
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Migrations\\Version28000Date20230906104802' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20230906104802.php',
'OC\\Core\\Migrations\\Version28000Date20231004103301' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231004103301.php',
'OC\\Core\\Migrations\\Version28000Date20231103104802' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231103104802.php',
'OC\\Core\\Migrations\\Version29000Date20231213104850' => __DIR__ . '/../../..' . '/core/Migrations/Version29000Date20231213104850.php',
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',
Expand Down
16 changes: 16 additions & 0 deletions lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Comment implements IComment {
'objectType' => '',
'objectId' => '',
'referenceId' => null,
'metaData' => null,
'creationDT' => null,
'latestChildDT' => null,
'reactions' => null,
Expand Down Expand Up @@ -400,6 +401,21 @@ public function setReferenceId(?string $referenceId): IComment {
return $this;
}

/**
* @inheritDoc
*/
public function getMetaData(): ?string {
return $this->data['metaData'];
}

/**
* @inheritDoc
*/
public function setMetaData(?string $metaData): IComment {
$this->data['metaData'] = $metaData;
return $this;
}

/**
* @inheritDoc
*/
Expand Down
40 changes: 10 additions & 30 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ protected function normalizeDatabaseData(array $data): array {
$data['expire_date'] = new \DateTime($data['expire_date']);
}
$data['children_count'] = (int)$data['children_count'];
$data['reference_id'] = $data['reference_id'] ?? null;
$data['reference_id'] = $data['reference_id'];
$data['meta_data'] = $data['meta_data'];
if ($this->supportReactions()) {
if ($data['reactions'] !== null) {
$list = json_decode($data['reactions'], true);
Expand Down Expand Up @@ -1150,22 +1151,6 @@ public function save(IComment $comment) {
* @return bool
*/
protected function insert(IComment $comment): bool {
try {
$result = $this->insertQuery($comment, true);
} catch (InvalidFieldNameException $e) {
// The reference id field was only added in Nextcloud 19.
// In order to not cause too long waiting times on the update,
// it was decided to only add it lazy, as it is also not a critical
// feature, but only helps to have a better experience while commenting.
// So in case the reference_id field is missing,
// we simply save the comment without that field.
$result = $this->insertQuery($comment, false);
}

return $result;
}

protected function insertQuery(IComment $comment, bool $tryWritingReferenceId): bool {
$qb = $this->dbConn->getQueryBuilder();

$values = [
Expand All @@ -1181,12 +1166,10 @@ protected function insertQuery(IComment $comment, bool $tryWritingReferenceId):
'object_type' => $qb->createNamedParameter($comment->getObjectType()),
'object_id' => $qb->createNamedParameter($comment->getObjectId()),
'expire_date' => $qb->createNamedParameter($comment->getExpireDate(), 'datetime'),
'reference_id' => $qb->createNamedParameter($comment->getReferenceId()),
'meta_data' => $qb->createNamedParameter($comment->getMetaData()),
];

if ($tryWritingReferenceId) {
$values['reference_id'] = $qb->createNamedParameter($comment->getReferenceId());
}

$affectedRows = $qb->insert('comments')
->values($values)
->execute();
Expand Down Expand Up @@ -1305,7 +1288,7 @@ protected function update(IComment $comment) {
return $result;
}

protected function updateQuery(IComment $comment, bool $tryWritingReferenceId): bool {
protected function updateQuery(IComment $comment): bool {
$qb = $this->dbConn->getQueryBuilder();
$qb
->update('comments')
Expand All @@ -1320,14 +1303,11 @@ protected function updateQuery(IComment $comment, bool $tryWritingReferenceId):
->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
->set('object_id', $qb->createNamedParameter($comment->getObjectId()))
->set('expire_date', $qb->createNamedParameter($comment->getExpireDate(), 'datetime'));

if ($tryWritingReferenceId) {
$qb->set('reference_id', $qb->createNamedParameter($comment->getReferenceId()));
}

$affectedRows = $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($comment->getId())))
->execute();
->set('expire_date', $qb->createNamedParameter($comment->getExpireDate(), 'datetime'))
->set('reference_id', $qb->createNamedParameter($comment->getReferenceId()))
->set('meta_data', $qb->createNamedParameter($comment->getMetaData()))
->where($qb->expr()->eq('id', $qb->createNamedParameter($comment->getId())));
$affectedRows = $qb->executeStatement();

if ($affectedRows === 0) {
throw new NotFoundException('Comment to update does ceased to exist');
Expand Down
18 changes: 18 additions & 0 deletions lib/public/Comments/IComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ public function getReferenceId(): ?string;
*/
public function setReferenceId(?string $referenceId): IComment;

/**
* returns the meta data of the comment
*
* @return string|null
* @since 29.0.0
*/
public function getMetaData(): ?string;

/**
* Sets (overwrites) the meta data of the comment
* It is recommended to store the data as a json encoded array
*
* @param string|null $metaData
* @return IComment
* @since 29.0.0
*/
public function setMetaData(?string $metaData): IComment;

/**
* Returns the reactions array if exists
*
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patch level
// when updating major/minor version number.

$OC_Version = [29, 0, 0, 2];
$OC_Version = [29, 0, 0, 3];

// The human-readable string
$OC_VersionString = '29.0.0 dev';
Expand Down

0 comments on commit cdc197a

Please sign in to comment.