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

Feature/share single note #1146

Merged
merged 3 commits into from
Nov 8, 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
19 changes: 19 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\GenericEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Share\Events\BeforeShareCreatedEvent;
use OCP\Share\IShare;

Check warning on line 15 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Possibly zero references to use statement for classlike/namespace IShare (\OCP\Share\IShare)

Check warning on line 15 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Possibly zero references to use statement for classlike/namespace IShare (\OCP\Share\IShare)

class Application extends App implements IBootstrap {
public const APP_ID = 'notes';
Expand All @@ -26,6 +30,21 @@
BeforeTemplateRenderedEvent::class,
BeforeTemplateRenderedListener::class
);
if (\class_exists(BeforeShareCreatedEvent::class)) {

Check warning on line 33 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Saw reference to \OCP\Share\Events\BeforeShareCreatedEvent declared at vendor/nextcloud/ocp/OCP/Share/Events/BeforeShareCreatedEvent.php:34 which is also declared at tests/stubs/ocp.php:7. This may lead to confusing errors. It may be possible to exclude the class that isn't used with exclude_file_list. In addition to normal ways to suppress issues, this issue type can be suppressed on either of the class definitions if it is impractical to exclude one file.
$context->registerEventListener(
BeforeShareCreatedEvent::class,

Check warning on line 35 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Saw reference to \OCP\Share\Events\BeforeShareCreatedEvent declared at vendor/nextcloud/ocp/OCP/Share/Events/BeforeShareCreatedEvent.php:34 which is also declared at tests/stubs/ocp.php:7. This may lead to confusing errors. It may be possible to exclude the class that isn't used with exclude_file_list. In addition to normal ways to suppress issues, this issue type can be suppressed on either of the class definitions if it is impractical to exclude one file.
BeforeShareCreatedListener::class
);
} else {
// FIXME: Remove once Nextcloud 28 is the minimum supported version
\OCP\Server::get(IEventDispatcher::class)->addListener('OCP\Share::preShare', function (GenericEvent $event) {

Check warning on line 40 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 122 characters

Check warning on line 40 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 122 characters
/** @var IShare $share */
$share = $event->getSubject();

Check warning on line 42 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Call to deprecated function \OCP\EventDispatcher\GenericEvent::getSubject() defined at vendor/nextcloud/ocp/OCP/EventDispatcher/GenericEvent.php:70

Check warning on line 42 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Call to deprecated function \OCP\EventDispatcher\GenericEvent::getSubject() defined at vendor/nextcloud/ocp/OCP/EventDispatcher/GenericEvent.php:70

$modernListener = \OCP\Server::get(BeforeShareCreatedListener::class);
$modernListener->overwriteShareTarget($share);
}, 1000);
}
}

public function boot(IBootContext $context): void {
Expand Down
52 changes: 52 additions & 0 deletions lib/AppInfo/BeforeShareCreatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace OCA\Notes\AppInfo;

use OCA\Notes\Service\NoteUtil;
use OCA\Notes\Service\SettingsService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\File;
use OCP\Share\Events\BeforeShareCreatedEvent;
use OCP\Share\IShare;

class BeforeShareCreatedListener implements IEventListener {
private SettingsService $settings;
private NoteUtil $noteUtil;

public function __construct(SettingsService $settings, NoteUtil $noteUtil) {
$this->settings = $settings;
$this->noteUtil = $noteUtil;
}

public function handle(Event $event): void {
if (!($event instanceof BeforeShareCreatedEvent)) {

Check warning on line 25 in lib/AppInfo/BeforeShareCreatedListener.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Saw reference to \OCP\Share\Events\BeforeShareCreatedEvent declared at vendor/nextcloud/ocp/OCP/Share/Events/BeforeShareCreatedEvent.php:34 which is also declared at tests/stubs/ocp.php:7. This may lead to confusing errors. It may be possible to exclude the class that isn't used with exclude_file_list. In addition to normal ways to suppress issues, this issue type can be suppressed on either of the class definitions if it is impractical to exclude one file.
return;
}

$this->overwriteShareTarget($event->getShare());
}

public function overwriteShareTarget(IShare $share): void {
$itemType = $share->getNode() instanceof File ? 'file' : 'folder';
$fileSourcePath = $share->getNode()->getPath();
$itemTarget = $share->getTarget();
$uidOwner = $share->getSharedBy();
$ownerPath = $this->noteUtil->getRoot()->getUserFolder($uidOwner)->getPath();
$ownerNotesPath = $ownerPath . '/' . $this->settings->get($uidOwner, 'notesPath');

$receiver = $share->getSharedWith();
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
$this->noteUtil->getOrCreateFolder($receiverNotesPath);

if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {
return;
}

$share->setTarget('/' . $receiverNotesInternalPath . $itemTarget);
}
}
10 changes: 10 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace OCA\Notes\Controller;

use OCA\Files\Event\LoadSidebar;
use OCA\Notes\AppInfo\Application;
use OCA\Notes\Service\NotesService;

use OCA\Notes\Service\SettingsService;
use OCA\Text\Event\LoadEditor;
use OCA\Viewer\Event\LoadViewer;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
Expand Down Expand Up @@ -66,6 +68,14 @@ public function index() : TemplateResponse {
$this->eventDispatcher->dispatchTyped(new LoadEditor());
}

if (class_exists(LoadSidebar::class)) {
$this->eventDispatcher->dispatchTyped(new LoadSidebar());
}

if (\OCP\Server::get(IAppManager::class)->isEnabledForUser('viewer') && class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}

$this->initialState->provideInitialState(
'config',
(array)\OCP\Server::get(SettingsService::class)->getPublic($this->userSession->getUser()->getUID())
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public function getData(array $exclude = []) : array {
if (!in_array('readonly', $exclude)) {
$data['readonly'] = $this->getReadOnly();
}
$data['internalPath'] = $this->noteUtil->getPathForUser($this->file);
$data['shareTypes'] = $this->noteUtil->getShareTypes($this->file);
$data['isShared'] = (bool) count($data['shareTypes']);
juliushaertl marked this conversation as resolved.
Show resolved Hide resolved
$data['error'] = false;
$data['errorType'] = '';
if (!in_array('content', $exclude)) {
Expand Down
42 changes: 41 additions & 1 deletion lib/Service/NoteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,49 @@

namespace OCA\Notes\Service;

use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IDBConnection;
use OCP\IUserSession;
use OCP\Share\IManager;
use OCP\Share\IShare;

class NoteUtil {
private const MAX_TITLE_LENGTH = 100;
public Util $util;
private IDBConnection $db;
private IRootFolder $root;
private TagService $tagService;
private IManager $shareManager;
private IUserSession $userSession;

public function __construct(
Util $util,
IRootFolder $root,
IDBConnection $db,
TagService $tagService
TagService $tagService,
IManager $shareManager,
IUserSession $userSession
) {
$this->util = $util;
$this->root = $root;
$this->db = $db;
$this->tagService = $tagService;
$this->shareManager = $shareManager;
$this->userSession = $userSession;
}

public function getRoot() : IRootFolder {
return $this->root;
}

public function getPathForUser(File $file) {
$userFolder = $this->root->getUserFolder($this->userSession->getUser()->getUID());
return $userFolder->getRelativePath($file->getPath());
}

public function getTagService() : TagService {
return $this->tagService;
}
Expand Down Expand Up @@ -203,4 +218,29 @@ public function ensureNoteIsWritable(Node $node) : void {
throw new NoteNotWritableException();
}
}

public function getShareTypes(File $file): array {
$userId = $file->getOwner()->getUID();
$requestedShareTypes = [
IShare::TYPE_USER,
IShare::TYPE_GROUP,
IShare::TYPE_LINK,
IShare::TYPE_REMOTE,
IShare::TYPE_EMAIL,
IShare::TYPE_ROOM,
IShare::TYPE_DECK,
IShare::TYPE_SCIENCEMESH,
];
$shareTypes = [];

foreach ($requestedShareTypes as $shareType) {
$shares = $this->shareManager->getSharesBy($userId, $shareType, $file, false, 1, 0);

if (count($shares)) {
$shareTypes[] = $shareType;
}
}

return $shareTypes;
}
}
2 changes: 2 additions & 0 deletions src/NotesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const refreshNote = (noteId, lastETag) => {
return response.headers.etag
}
const currentContent = store.getters.getNote(noteId).content
store.commit('setNoteAttribute', { noteId, attribute: 'internalPath', value: response.data.internalPath })
// only update if local content has not changed
if (oldContent === currentContent) {
_updateLocalNote(response.data)
Expand Down Expand Up @@ -290,6 +291,7 @@ export const autotitleNote = noteId => {
.put(url('/notes/' + noteId + '/autotitle'))
.then((response) => {
store.commit('setNoteAttribute', { noteId, attribute: 'title', value: response.data })
refreshNote(noteId)
})
.catch(err => {
console.error(err)
Expand Down
43 changes: 43 additions & 0 deletions src/components/NoteItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@
fill-color="var(--color-text-lighter)"
/>
</template>
<template v-if="isShared" #indicator>
<ShareVariantIcon :size="16" fill-color="#0082c9" />
</template>
<template #actions>
<NcActionButton :icon="actionFavoriteIcon" @click="onToggleFavorite">
{{ actionFavoriteText }}
</NcActionButton>

<NcActionButton @click="onToggleSharing">
<template #icon>
<ShareVariantIcon :size="20" />
</template>
{{ t('notes', 'Share') }}
</NcActionButton>

<NcActionButton v-if="!showCategorySelect" @click="showCategorySelect = true">
<template #icon>
<FolderIcon :size="20" />
Expand Down Expand Up @@ -90,6 +100,8 @@ import StarIcon from 'vue-material-design-icons/Star.vue'
import { categoryLabel, routeIsNewNote } from '../Util.js'
import { showError } from '@nextcloud/dialogs'
import { setFavorite, setTitle, fetchNote, deleteNote, setCategory } from '../NotesService.js'
import ShareVariantIcon from 'vue-material-design-icons/ShareVariant.vue'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'

export default {
name: 'NoteItem',
Expand All @@ -104,6 +116,7 @@ export default {
NcActionSeparator,
NcActionInput,
PencilIcon,
ShareVariantIcon,
},

props: {
Expand All @@ -122,13 +135,17 @@ export default {
newTitle: '',
renaming: false,
showCategorySelect: false,
isShareCreated: false,
}
},

computed: {
isSelected() {
return this.$store.getters.getSelectedNote() === this.note.id
},
isShared() {
return this.note.isShared || this.isShareCreated
},

title() {
return this.note.title + (this.note.unsaved ? ' *' : '')
Expand Down Expand Up @@ -167,6 +184,15 @@ export default {
]
},
},

mounted() {
subscribe('files_sharing:share:created', this.onShareCreated)
},

destroyed() {
unsubscribe('files_sharing:share:created', this.onShareCreated)
},

methods: {
onMenuChange(state) {
this.actionsOpen = state
Expand Down Expand Up @@ -251,6 +277,23 @@ export default {
this.actionsOpen = false
}
},
onToggleSharing() {
if (window?.OCA?.Files?.Sidebar?.setActiveTab) {
emit('toggle-navigation', { open: false })
setTimeout(() => {
window.dispatchEvent(new Event('resize'))
}, 200)
window.OCA.Files.Sidebar.setActiveTab('sharing')
window.OCA.Files.Sidebar.open(this.note.internalPath)
}
},
async onShareCreated(event) {
const { share } = event

if (share.fileSource === this.note.id) {
this.isShareCreated = true
}
},
},
}
</script>
Expand Down
25 changes: 24 additions & 1 deletion src/components/NotePlain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ import {
isMobile,
} from '@nextcloud/vue'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'

import EditIcon from 'vue-material-design-icons/LeadPencil.vue'
import EyeIcon from 'vue-material-design-icons/Eye.vue'
Expand Down Expand Up @@ -193,6 +193,8 @@ export default {
document.addEventListener('fullscreenchange', this.onDetectFullscreen)
document.addEventListener('keydown', this.onKeyPress)
document.addEventListener('visibilitychange', this.onVisibilityChange)
subscribe('files_versions:restore:requested', this.onFileRestoreRequested)
subscribe('files_versions:restore:restored', this.onFileRestored)
},

destroyed() {
Expand All @@ -203,6 +205,8 @@ export default {
document.removeEventListener('keydown', this.onKeyPress)
document.removeEventListener('visibilitychange', this.onVisibilityChange)
this.onUpdateTitle(null)
unsubscribe('files_versions:restore:requested', this.onFileRestoreRequested)
unsubscribe('files_versions:restore:restored', this.onFileRestored)
},

methods: {
Expand Down Expand Up @@ -392,6 +396,25 @@ export default {
conflictSolutionRemote(this.note)
this.showConflict = false
},

async onFileRestoreRequested(event) {
const { fileInfo } = event

if (fileInfo.id !== this.note.id) {
return
}

this.loading = true
},

async onFileRestored(version) {
if (version.fileId !== this.note.id) {
return
}

this.refreshNote()
this.loading = false
},
},
}
</script>
Expand Down
Loading
Loading