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

[stable22] Increase file count after sharing #3806

Merged
merged 5 commits into from
May 12, 2022
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
52 changes: 52 additions & 0 deletions lib/Cache/AttachmentCacheHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.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/>.
*
*/

declare(strict_types=1);


namespace OCA\Deck\Cache;

use OCP\ICache;
use OCP\ICacheFactory;

class AttachmentCacheHelper {
/** @var ICache */
private $cache;

public function __construct(ICacheFactory $cacheFactory) {
$this->cache = $cacheFactory->createDistributed('deck-attachments');
}

public function getAttachmentCount(int $cardId): ?int {
return $this->cache->get('count-' . $cardId);
}

public function setAttachmentCount(int $cardId, int $count): void {
$this->cache->set('count-' . $cardId, $count);
}

public function clearAttachmentCount(int $cardId): void {
$this->cache->remove('count-' . $cardId);
}
}
28 changes: 15 additions & 13 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException;
use OCA\Deck\Cache\AttachmentCacheHelper;
use OCA\Deck\StatusException;
use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Http\Response;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IL10N;

class AttachmentService {
Expand All @@ -49,23 +48,24 @@ class AttachmentService {

/** @var IAttachmentService[] */
private $services = [];
/** @var Application */
private $application;
/** @var ICache */
private $cache;
/** @var AttachmentCacheHelper */
private $attachmentCacheHelper;
/** @var IL10N */
private $l10n;
/** @var ActivityManager */
private $activityManager;
/** @var ChangeHelper */
private $changeHelper;

public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n, ActivityManager $activityManager) {
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, AttachmentCacheHelper $attachmentCacheHelper, $userId, IL10N $l10n, ActivityManager $activityManager) {
$this->attachmentMapper = $attachmentMapper;
$this->cardMapper = $cardMapper;
$this->permissionService = $permissionService;
$this->userId = $userId;
$this->application = $application;
$this->cache = $cacheFactory->createDistributed('deck-card-attachments-');
$this->attachmentCacheHelper = $attachmentCacheHelper;
$this->l10n = $l10n;
$this->activityManager = $activityManager;
$this->changeHelper = $changeHelper;
Expand Down Expand Up @@ -139,14 +139,16 @@ public function findAll($cardId, $withDeleted = false) {
* @param $cardId
* @return int|mixed
* @throws BadRequestException
* @throws InvalidAttachmentType
* @throws \OCP\DB\Exception
*/
public function count($cardId) {
if (is_numeric($cardId) === false) {
throw new BadRequestException('card id must be a number');
}

$count = $this->cache->get('card-' . $cardId);
if (!$count) {
$count = $this->attachmentCacheHelper->getAttachmentCount((int)$cardId);
if ($count === null) {
$count = count($this->attachmentMapper->findAll($cardId));

foreach (array_keys($this->services) as $attachmentType) {
Expand All @@ -156,7 +158,7 @@ public function count($cardId) {
}
}

$this->cache->set('card-' . $cardId, $count);
$this->attachmentCacheHelper->setAttachmentCount((int)$cardId, $count);
}

return $count;
Expand Down Expand Up @@ -186,7 +188,7 @@ public function create($cardId, $type, $data) {

$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);

$this->cache->clear('card-' . $cardId);
$this->attachmentCacheHelper->clearAttachmentCount((int)$cardId);
$attachment = new Attachment();
$attachment->setCardId($cardId);
$attachment->setType($type);
Expand Down Expand Up @@ -298,7 +300,7 @@ public function update($cardId, $attachmentId, $data, $type = 'deck_file') {
}

$this->permissionService->checkPermission($this->cardMapper, $attachment->getCardId(), Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $attachment->getCardId());
$this->attachmentCacheHelper->clearAttachmentCount($cardId);

$attachment->setData($data);
try {
Expand Down Expand Up @@ -356,7 +358,7 @@ public function delete(int $cardId, int $attachmentId, string $type = 'deck_file
}
}

$this->cache->clear('card-' . $attachment->getCardId());
$this->attachmentCacheHelper->clearAttachmentCount($cardId);
$this->changeHelper->cardChanged($attachment->getCardId());
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $attachment, ActivityManager::SUBJECT_ATTACHMENT_DELETE);
return $attachment;
Expand All @@ -370,7 +372,7 @@ public function restore(int $cardId, int $attachmentId, string $type = 'deck_fil
}

$this->permissionService->checkPermission($this->cardMapper, $attachment->getCardId(), Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $attachment->getCardId());
$this->attachmentCacheHelper->clearAttachmentCount($cardId);

try {
$service = $this->getService($attachment->getType());
Expand Down
21 changes: 19 additions & 2 deletions lib/Sharing/DeckShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use Doctrine\DBAL\Platforms\MySQLPlatform;
use OC\Files\Cache\Cache;
use OCA\Deck\Cache\AttachmentCacheHelper;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
Expand All @@ -46,7 +47,6 @@
use OCP\Files\Node;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\Security\ISecureRandom;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
Expand All @@ -70,6 +70,8 @@ class DeckShareProvider implements \OCP\Share\IShareProvider {
private $dbConnection;
/** @var IManager */
private $shareManager;
/** @var AttachmentCacheHelper */
private $attachmentCacheHelper;
/** @var BoardMapper */
private $boardMapper;
/** @var CardMapper */
Expand All @@ -78,14 +80,25 @@ class DeckShareProvider implements \OCP\Share\IShareProvider {
private $permissionService;
/** @var ITimeFactory */
private $timeFactory;
/** @var IL10N */
private $l;

public function __construct(IDBConnection $connection, IManager $shareManager, ISecureRandom $secureRandom, BoardMapper $boardMapper, CardMapper $cardMapper, PermissionService $permissionService, IL10N $l) {
public function __construct(
IDBConnection $connection,
IManager $shareManager,
BoardMapper $boardMapper,
CardMapper $cardMapper,
PermissionService $permissionService,
AttachmentCacheHelper $attachmentCacheHelper,
IL10N $l
) {
$this->dbConnection = $connection;
$this->shareManager = $shareManager;
$this->boardMapper = $boardMapper;
$this->cardMapper = $cardMapper;
$this->attachmentCacheHelper = $attachmentCacheHelper;
$this->permissionService = $permissionService;

$this->l = $l;
$this->timeFactory = \OC::$server->get(ITimeFactory::class);
}
Expand Down Expand Up @@ -153,6 +166,8 @@ public function create(IShare $share) {
);
$data = $this->getRawShare($shareId);

$this->attachmentCacheHelper->clearAttachmentCount((int)$cardId);

return $this->createShareObject($data);
}

Expand Down Expand Up @@ -340,6 +355,8 @@ public function delete(IShare $share) {
$qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));

$qb->execute();

$this->attachmentCacheHelper->clearAttachmentCount((int)$share->getSharedWith());
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/components/card/AttachmentList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ import relativeDate from '../../mixins/relativeDate'
import { formatFileSize } from '@nextcloud/files'
import { getCurrentUser } from '@nextcloud/auth'
import { generateUrl, generateOcsUrl, generateRemoteUrl } from '@nextcloud/router'
import { mapState } from 'vuex'
import { mapState, mapActions } from 'vuex'
import { loadState } from '@nextcloud/initial-state'
import attachmentUpload from '../../mixins/attachmentUpload'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
Expand Down Expand Up @@ -205,11 +205,14 @@ export default {
cardId: {
immediate: true,
handler() {
this.$store.dispatch('fetchAttachments', this.cardId)
this.fetchAttachments(this.cardId)
},
},
},
methods: {
...mapActions([
'fetchAttachments',
]),
handleUploadFile(event) {
const files = event.target.files ?? []
for (const file of files) {
Expand All @@ -233,7 +236,7 @@ export default {
shareType: 12,
shareWith: '' + this.cardId,
}).then(() => {
this.$store.dispatch('fetchAttachments', this.cardId)
this.fetchAttachments(this.cardId)
})
})
},
Expand Down
1 change: 1 addition & 0 deletions src/store/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default {
async fetchAttachments({ commit }, cardId) {
const attachments = await apiClient.fetchAttachments(cardId)
commit('createAttachments', { cardId, attachments })
commit('cardSetAttachmentCount', { cardId, count: attachments.length })
},

async createAttachment({ commit }, { cardId, formData, onUploadProgress }) {
Expand Down
6 changes: 6 additions & 0 deletions src/store/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ export default {
}
Vue.set(state.cards[existingIndex], 'lastModified', Date.now() / 1000)
},
cardSetAttachmentCount(state, { cardId, count }) {
const existingIndex = state.cards.findIndex(_card => _card.id === cardId)
if (existingIndex !== -1) {
Vue.set(state.cards[existingIndex], 'attachmentCount', count)
}
},
cardIncreaseAttachmentCount(state, cardId) {
const existingIndex = state.cards.findIndex(_card => _card.id === cardId)
if (existingIndex !== -1) {
Expand Down
Loading