Skip to content

Commit

Permalink
feat: Open previews using the viewer
Browse files Browse the repository at this point in the history
Add full file path to previews so the viewer can load it

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Nov 9, 2023
1 parent 2857ef3 commit 95617d3
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 13 deletions.
7 changes: 5 additions & 2 deletions lib/Controller/APIv2Controller.php
Expand Up @@ -316,8 +316,11 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
} else {
$fileInfo = $info['node'] ?? null;
if (!($fileInfo instanceof FileInfo)) {
$preview = $this->getPreviewFromPath($fileId, $filePath, $info);
} elseif ($this->preview->isAvailable($fileInfo)) {
return $this->getPreviewFromPath($fileId, $filePath, $info);
}

$preview['filePath'] = $fileInfo->getPath();
if ($this->preview->isAvailable($fileInfo)) {
$params = [
'forceIcon' => 0,
'a' => 0,
Expand Down
6 changes: 6 additions & 0 deletions lib/Controller/ActivitiesController.php
Expand Up @@ -25,6 +25,7 @@

use OCA\Activity\Data;
use OCA\Activity\Event\LoadAdditionalScriptsEvent;
use OCA\Viewer\Event\LoadViewer;
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -78,6 +79,11 @@ public function showList(string $filter = 'all'): TemplateResponse {
$this->eventDispatcher->dispatchTyped($event);
$this->eventDispatcher->dispatch(LoadAdditionalScriptsEvent::EVENT_ENTITY, $event);

// Load the viewer
if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}

$this->initialState->provideInitialState('settings', [
'enableAvatars' => $this->config->getSystemValue('enable_avatars', true),
'personalSettingsLink' => $this->getPersonalSettingsLink(),
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Expand Up @@ -49,5 +49,6 @@
<stubs>
<file name="tests/stubs/oc_hooks_emitter.php" />
<file name="tests/stubs/oca_files_event.php" preloadClasses="true"/>
<file name="tests/stubs/oca_viewer_event.php" preloadClasses="true"/>
</stubs>
</psalm>
57 changes: 48 additions & 9 deletions src/components/Activity.vue
Expand Up @@ -2,6 +2,7 @@
- @copyright 2021 Louis Chemineau <louis@chmn.me>
-
- @author Louis Chemineau <louis@chmn.me>
- @author Ferdinand Thiessen <opensource@fthiessen.de>
-
- @license AGPL-3.0-or-later
-
Expand Down Expand Up @@ -38,7 +39,8 @@
v-for="preview, index in activity.previews"
:key="preview.fileId ?? `preview-${index}`"
class="activity-entry__preview"
:href="preview.link">
:href="preview.link"
@click="handlePreviewClick($event, preview)">
<img class="activity-entry__preview-image"
:class="{
'activity-entry__preview-mimetype': preview.isMimeTypeIcon,
Expand All @@ -50,20 +52,36 @@
</li>
</template>
<script>
<script lang="ts">
import type { IPreview } from '../models/types'
import { translate as t } from '@nextcloud/l10n'
import {
NcAvatar,
NcUserBubble,
NcRichText,
} from '@nextcloud/vue'
import { defineComponent } from 'vue'
import ActivityModel from '../models/ActivityModel.ts'
import ActivityModel from '../models/ActivityModel'
import FileRichArgument from './richArgumentsTypes/FileRichArgument.vue'
import EmailRichArgument from './richArgumentsTypes/EmailRichArgument.vue'
import SystemTagRichArgument from './richArgumentsTypes/SystemTagRichArgument.vue'
import CalendarEventRichArgument from './richArgumentsTypes/CalendarEventRichArgument.vue'
import OpenGraphRichArgument from './richArgumentsTypes/OpenGraphRichArgument.vue'
import logger from '../logger'
declare global {
interface Window {
OCA?: {
Viewer?: {
open(options: { path?: string, fileInfo?: unknown }): void
get mimetypes(): string[]
}
}
}
}
/**
* @typedef RichObject
Expand All @@ -72,7 +90,7 @@ import OpenGraphRichArgument from './richArgumentsTypes/OpenGraphRichArgument.vu
* @property {string} type - The type of the file object.
*/
export default {
export default defineComponent({
name: 'Activity',
components: {
NcAvatar,
Expand Down Expand Up @@ -136,21 +154,41 @@ export default {
},
created() {
this.updateDateFromNow()
this.dateInterval = setInterval(this.updateDateFromNow, 60 * 1000)
this.dateInterval = window.setInterval(this.updateDateFromNow, 60 * 1000)
},
destroyed() {
clearInterval(this.dateInterval)
},
methods: {
t,
/**
* Handle clicking a preview
* Check if viewer is available and can open the file, if not navigate to it
* @param event The click event
* @param preview The preview to open
*/
handlePreviewClick(event: MouseEvent, preview: IPreview) {
if (preview.filePath && window?.OCA?.Viewer?.open !== undefined && window.OCA.Viewer.mimetypes.includes(preview.mimeType)) {
try {
window.OCA.Viewer.open({ path: preview.filePath.replace(/^\/[^/]+\/files/, '') })
event.preventDefault()
event.stopPropagation()
} catch (error) {
logger.debug(error as Error)
}
}
},
updateDateFromNow() {
this.dateFromNow = this.activity.dateFromNow
},
/**
* Map an collection of rich text objects to rich arguments for the RichText component
*
* @param {Array.<Object<string, RichObject>>} richObjects - The rich text object
* @return {Object<string, object>}
* @param {Record<string, RichObject>[]} richObjects - The rich text object
* @return {Record<string, object>}
*/
mapRichObjectsToRichArguments(richObjects) {
const args = {}
Expand All @@ -165,7 +203,7 @@ export default {
/**
* Map rich text object to rich argument for the RichText component
*
* @param {Object<string, RichObject>} richObject - The rich text object
* @param {Record<string, object>} richObject - The rich text object
* @return {object}}
*/
mapRichObjectToRichArgument(richObject) {
Expand Down Expand Up @@ -214,8 +252,9 @@ export default {
}
},
},
}
})
</script>
<style lang="scss" scoped>
.activity-entry {
display: flex;
Expand Down
1 change: 1 addition & 0 deletions src/models/types.ts
Expand Up @@ -64,6 +64,7 @@ export interface IPreview {
* The file ID
*/
fileId: number
filePath?: string,
view: string
filename: string
}
Expand Down
20 changes: 20 additions & 0 deletions src/views/ActivityAppFeed.vue
@@ -1,3 +1,23 @@
<!--
- @copyright 2023 Ferdinand Thiessen <opensource@fthiessen.de>
-
- @author Ferdinand Thiessen <opensource@fthiessen.de>
-
- @license AGPL-3.0-or-later
-
- 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/>.
-->
<template>
<NcAppContent class="activity-app">
<h1 class="activity-app__heading">
Expand Down
8 changes: 6 additions & 2 deletions tests/Controller/APIv2ControllerTest.php
Expand Up @@ -688,6 +688,9 @@ public function testGetPreview(string $author, int $fileId, string $path, string
->with($node)
->willReturn($isMimeSup);

$node->expects($this->atLeastOnce())
->method('getPath')
->willReturn($path);
if (!$isMimeSup) {
$node->expects($this->atLeastOnce())
->method('getMimetype')
Expand Down Expand Up @@ -730,15 +733,16 @@ public function testGetPreview(string $author, int $fileId, string $path, string
'node' => $node,
]);

$this->assertSame([
$this->assertSame(array_merge([
'link' => 'files.viewcontroller.showFile#' . $fileId,
'source' => $source,
'mimeType' => $mimeType,
'isMimeTypeIcon' => $isMimeTypeIcon,
'fileId' => $fileId,
'view' => 'files',
'filename' => basename($path),
], self::invokePrivate($controller, 'getPreview', [$author, $fileId, $path]));
], $validFileInfo && !$isDir ? ['filePath' => $path ] : []),
self::invokePrivate($controller, 'getPreview', [$author, $fileId, $path]));
}

public function dataGetPreviewFromPath(): array {
Expand Down
6 changes: 6 additions & 0 deletions tests/stubs/oca_viewer_event.php
@@ -0,0 +1,6 @@
<?php

namespace OCA\Viewer\Event {
class LoadViewer extends \OCP\EventDispatcher\Event {
}
}

0 comments on commit 95617d3

Please sign in to comment.