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

Add lastUpdated property to Form #1479

Merged
merged 1 commit into from
Feb 21, 2023
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
28 changes: 26 additions & 2 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public function newForm(): DataResponse {
$form->setShowExpiration(false);
$form->setExpires(0);
$form->setIsAnonymous(false);
$form->setLastUpdated(time());

$this->formMapper->insert($form);

Expand Down Expand Up @@ -315,6 +316,7 @@ public function cloneForm(int $id): DataResponse {
$formData = $oldForm->read();
unset($formData['id']);
$formData['created'] = time();
$formData['lastUpdated'] = time();
$formData['hash'] = $this->formsService->generateFormHash();
// TRANSLATORS Appendix to the form Title of a duplicated/copied form.
$formData['title'] .= ' - ' . $this->l10n->t('Copy');
Expand Down Expand Up @@ -384,9 +386,10 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {
throw new OCSForbiddenException();
}

// Don't allow to change params id, hash, ownerId, created
// Don't allow to change params id, hash, ownerId, created, lastUpdated
if (key_exists('id', $keyValuePairs) || key_exists('hash', $keyValuePairs) ||
key_exists('ownerId', $keyValuePairs) || key_exists('created', $keyValuePairs)) {
key_exists('ownerId', $keyValuePairs) || key_exists('created', $keyValuePairs) ||
key_exists('lastUpdated', $keyValuePairs)) {
$this->logger->info('Not allowed to update id, hash, ownerId or created');
throw new OCSForbiddenException();
}
Expand All @@ -397,6 +400,7 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {

// Update changed Columns in Db.
$this->formMapper->update($form);
$this->formsService->setLastUpdatedTimestamp($id);

return new DataResponse($form->getId());
}
Expand Down Expand Up @@ -501,6 +505,8 @@ public function newQuestion(int $formId, string $type, string $text = ''): DataR
$response = $question->read();
$response['options'] = [];

$this->formsService->setLastUpdatedTimestamp($formId);

return new DataResponse($response);
}

Expand Down Expand Up @@ -594,6 +600,8 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
];
}

$this->formsService->setLastUpdatedTimestamp($formId);

return new DataResponse($response);
}

Expand Down Expand Up @@ -654,6 +662,8 @@ public function updateQuestion(int $id, array $keyValuePairs): DataResponse {
// Update changed Columns in Db.
$this->questionMapper->update($question);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($question->getId());
}

Expand Down Expand Up @@ -703,6 +713,8 @@ public function deleteQuestion(int $id): DataResponse {
}
}

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -744,6 +756,8 @@ public function newOption(int $questionId, string $text): DataResponse {

$option = $this->optionMapper->insert($option);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($option->read());
}

Expand Down Expand Up @@ -798,6 +812,8 @@ public function updateOption(int $id, array $keyValuePairs): DataResponse {
// Update changed Columns in Db.
$this->optionMapper->update($option);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($option->getId());
}

Expand Down Expand Up @@ -833,6 +849,8 @@ public function deleteOption(int $id): DataResponse {

$this->optionMapper->delete($option);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -1013,6 +1031,8 @@ public function insertSubmission(int $formId, array $answers, string $shareHash
}
}

$this->formsService->setLastUpdatedTimestamp($formId);

//Create Activity
$this->activityManager->publishNewSubmission($form, $submission->getUserId());

Expand Down Expand Up @@ -1051,6 +1071,8 @@ public function deleteSubmission(int $id): DataResponse {
// Delete submission (incl. Answers)
$this->submissionMapper->deleteById($id);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -1085,6 +1107,8 @@ public function deleteAllSubmissions(int $formId): DataResponse {
// Delete all submissions (incl. Answers)
$this->submissionMapper->deleteByForm($formId);

$this->formsService->setLastUpdatedTimestamp($formId);

return new DataResponse($formId);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public function newShare(int $formId, int $shareType, string $shareWith = '', ar

// Create share-notifications (activity)
$this->formsService->notifyNewShares($form, $share);

$this->formsService->setLastUpdatedTimestamp($formId);

// Append displayName for Frontend
$shareData = $share->read();
Expand Down Expand Up @@ -250,6 +252,8 @@ public function deleteShare(int $id): DataResponse {

$this->shareMapper->deleteById($id);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($id);
}

Expand Down Expand Up @@ -303,6 +307,8 @@ public function updateShare(int $id, array $keyValuePairs): DataResponse {
$share->setPermissions($keyValuePairs['permissions']);
$share = $this->shareMapper->update($share);

$this->formsService->setLastUpdatedTimestamp($form->getId());

return new DataResponse($share->getId());
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Db/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
* @method void setSubmitMultiple(bool $value)
* @method integer getShowExpiration()
* @method void setShowExpiration(bool $value)
* @method integer getLastUpdated()
* @method void setLastUpdated(integer $value)
*/
class Form extends Entity {
protected $hash;
Expand All @@ -62,6 +64,7 @@ class Form extends Entity {
protected $isAnonymous;
protected $submitMultiple;
protected $showExpiration;
protected $lastUpdated;

/**
* Form constructor.
Expand All @@ -72,6 +75,7 @@ public function __construct() {
$this->addType('isAnonymous', 'bool');
$this->addType('submitMultiple', 'bool');
$this->addType('showExpiration', 'bool');
$this->addType('lastUpdated', 'integer');
}

// JSON-Decoding of access-column.
Expand All @@ -97,7 +101,8 @@ public function read() {
'expires' => (int)$this->getExpires(),
'isAnonymous' => (bool)$this->getIsAnonymous(),
'submitMultiple' => (bool)$this->getSubmitMultiple(),
'showExpiration' => (bool)$this->getShowExpiration()
'showExpiration' => (bool)$this->getShowExpiration(),
'lastUpdated' => (int)$this->getLastUpdated()
];
}
}
10 changes: 6 additions & 4 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ public function findAll(): array {

$qb->select('*')
->from($this->getTableName())
//Newest forms first
->orderBy('created', 'DESC');
//Last updated forms first, then newest forms first
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

return $this->findEntities($qb);
}
Expand All @@ -116,8 +117,9 @@ public function findAllByOwnerId(string $ownerId): array {
->where(
$qb->expr()->eq('owner_id', $qb->createNamedParameter($ownerId))
)
//Newest forms first
->orderBy('created', 'DESC');
//Last updated forms first, then newest forms first
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

return $this->findEntities($qb);
}
Expand Down
1 change: 1 addition & 0 deletions lib/FormsMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
$form->setIsAnonymous($formData['isAnonymous']);
$form->setSubmitMultiple($formData['submitMultiple']);
$form->setShowExpiration($formData['showExpiration']);
$form->setLastUpdated($formData['lastUpdated']);

$this->formMapper->insert($form);

Expand Down
60 changes: 60 additions & 0 deletions lib/Migration/Version030100Date20230202175747.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Christian Hartmann <[email protected]>
*
* @author Christian Hartmann <[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\Forms\Migration;

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

class Version030100Date20230202175747 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): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable('forms_v2_forms');

if (!$table->hasColumn('last_updated')) {
$table->addColumn('last_updated', Types::INTEGER, [
'notnull' => false,
'default' => 0,
'comment' => 'unix-timestamp',
]);

return $schema;
}

return null;
}
}
12 changes: 12 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public function getPartialFormArray(int $id): array {
'hash' => $form->getHash(),
'title' => $form->getTitle(),
'expires' => $form->getExpires(),
'lastUpdated' => $form->getLastUpdated(),
'permissions' => $this->getPermissions($form->getId()),
'partial' => true
];
Expand Down Expand Up @@ -527,4 +528,15 @@ protected function getSharesWithUser(int $formId, string $userId): array {
}
});
}

/**
* Update lastUpdated timestamp for the given form
*
* @param int $formId The form to update
*/
public function setLastUpdatedTimestamp(int $formId): void {
$form = $this->formMapper->findById($formId);
$form->setLastUpdated(time());
$this->formMapper->update($form);
}
}
28 changes: 27 additions & 1 deletion src/Forms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@
</template>

<script>
import { emit } from '@nextcloud/event-bus'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { generateOcsUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'
import { showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
import moment from '@nextcloud/moment'

import NcAppContent from '@nextcloud/vue/dist/Components/NcAppContent.js'
import NcAppNavigation from '@nextcloud/vue/dist/Components/NcAppNavigation.js'
Expand Down Expand Up @@ -224,6 +225,14 @@ export default {
this.loadForms()
},

mounted() {
subscribe('forms:last-updated:set', (id) => this.onLastUpdatedByEventBus(id))
},

unmounted() {
unsubscribe('forms:last-updated:set', (id) => this.onLastUpdatedByEventBus(id))
},

methods: {
/**
* Closes the App-Navigation on mobile-devices
Expand Down Expand Up @@ -349,6 +358,23 @@ export default {
this.$router.push({ name: 'root' })
}
},

/**
* Update last updated timestamp in given form
*
* @param {number} id the form id
*/
onLastUpdatedByEventBus(id) {
const formIndex = this.forms.findIndex(form => form.id === id)
if (formIndex !== -1) {
this.forms[formIndex].lastUpdated = moment().unix()
this.forms.sort((b, a) => a.lastUpdated - b.lastUpdated)
} else {
const sharedFormIndex = this.sharedForms.findIndex(form => form.id === id)
this.sharedForms[sharedFormIndex].lastUpdated = moment().unix()
this.sharedForms.sort((a, b) => a.lastUpdated - b.lastUpdated)
}
},
},
}
</script>
4 changes: 3 additions & 1 deletion src/components/Questions/QuestionDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@
</template>

<script>
import { generateOcsUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { generateOcsUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import NcActionCheckbox from '@nextcloud/vue/dist/Components/NcActionCheckbox.js'
import NcMultiselect from '@nextcloud/vue/dist/Components/NcMultiselect.js'
Expand Down Expand Up @@ -190,6 +191,7 @@ export default {
*/
updateOptions(options) {
this.$emit('update:options', options)
emit('forms:last-updated:set', this.$attrs.formId)
},

/**
Expand Down
4 changes: 3 additions & 1 deletion src/components/Questions/QuestionMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@
</template>

<script>
import { generateOcsUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { generateOcsUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import NcActionCheckbox from '@nextcloud/vue/dist/Components/NcActionCheckbox.js'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
Expand Down Expand Up @@ -219,6 +220,7 @@ export default {
*/
updateOptions(options) {
this.$emit('update:options', options)
emit('forms:last-updated:set', this.$attrs.formId)
},

/**
Expand Down
Loading