Skip to content

Commit

Permalink
fix(Federation): Show some icon for federated users on shares
Browse files Browse the repository at this point in the history
Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
Fenn-CS committed Sep 18, 2024
1 parent ad66160 commit 99b6b92
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 10 deletions.
10 changes: 10 additions & 0 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class FilesPlugin extends ServerPlugin {
public const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
public const MOUNT_ROOT_PROPERTYNAME = '{http://nextcloud.org/ns}is-mount-root';
public const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
public const IS_FEDERATED_PROPERTYNAME = '{http://nextcloud.org/ns}is-federated';
public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
Expand Down Expand Up @@ -149,6 +150,7 @@ public function initialize(Server $server) {
$server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
$server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
$server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
$server->protectedProperties[] = self::IS_FEDERATED_PROPERTYNAME;
$server->protectedProperties[] = self::SHARE_NOTE;

// normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
Expand Down Expand Up @@ -413,6 +415,14 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
$propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) {
return $node->getName();
});

$propFind->handle(self::IS_FEDERATED_PROPERTYNAME, function () use ($node) {
<<<<<<< HEAD

Check failure on line 420 in apps/dav/lib/Connector/Sabre/FilesPlugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ParseError

apps/dav/lib/Connector/Sabre/FilesPlugin.php:420:1: ParseError: Syntax error, unexpected T_SL on line 420 (see https://psalm.dev/173)

Check failure

Code scanning / Psalm

ParseError Error

Syntax error, unexpected T_SL on line 420
return $node->getOwner()->isFederated();
=======
return $node->getOwner()->isFederated() ? 'true' : 'false';
>>>>>>> 6ee2ba5b277 (wip)
});
}

if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
Expand Down
2 changes: 2 additions & 0 deletions apps/files/src/components/VirtualList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ export default Vue.extend({
}
const items = this.dataSources.slice(this.startIndex, this.startIndex + this.shownItems) as Node[]
console.log("Items list", items)
const oldItems = items.filter(item => Object.values(this.$_recycledPool).includes(item[this.dataKey]))
const oldItemsKeys = oldItems.map(item => item[this.dataKey] as string)
const unusedKeys = Object.keys(this.$_recycledPool).filter(key => !oldItemsKeys.includes(this.$_recycledPool[key]))
console.log("Unused keys", unusedKeys)
return items.map(item => {
const index = Object.values(this.$_recycledPool).indexOf(item[this.dataKey])
Expand Down
1 change: 1 addition & 0 deletions apps/files/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ registerPreviewServiceWorker()

registerDavProperty('nc:hidden', { nc: 'http://nextcloud.org/ns' })
registerDavProperty('nc:is-mount-root', { nc: 'http://nextcloud.org/ns' })
registerDavProperty('nc:is-federated', { nc: 'http://nextcloud.org/ns' })

initLivePhotos()
24 changes: 16 additions & 8 deletions apps/files_sharing/src/actions/sharingStatusAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@
*/
import { Node, View, registerFileAction, FileAction, Permission } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import { Type } from '@nextcloud/sharing'
import { ShareType } from '@nextcloud/sharing'

import AccountGroupSvg from '@mdi/svg/svg/account-group.svg?raw'
import AccountPlusSvg from '@mdi/svg/svg/account-plus.svg?raw'
import LinkSvg from '@mdi/svg/svg/link.svg?raw'
import CircleSvg from '../../../../core/img/apps/circles.svg?raw'

import { action as sidebarAction } from '../../../files/src/actions/sidebarAction'
import { generateUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { generateAvatarSvg } from '../utils/AccountIcon.ts'

import './sharingStatusAction.scss'

<<<<<<< Updated upstream
=======
const isDarkMode = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches === true
|| document.querySelector('[data-themes*=dark]') !== null

const generateAvatarSvg = (userId: string, isGuest = false) => {
console.debug("USER ID, is Guest", userId, isGuest)
const url = isDarkMode ? '/avatar/{userId}/32/dark' : '/avatar/{userId}/32'
const avatarUrl = generateUrl(isGuest ? url : url + '?guestFallback=true', { userId })
return `<svg width="32" height="32" viewBox="0 0 32 32"
Expand All @@ -46,6 +49,7 @@ const generateAvatarSvg = (userId: string, isGuest = false) => {
</svg>`
}

>>>>>>> Stashed changes
const isExternal = (node: Node) => {
return node.attributes.remote_id !== undefined
}
Expand Down Expand Up @@ -84,6 +88,7 @@ export const action = new FileAction({

iconSvgInline(nodes: Node[]) {
const node = nodes[0]
console.debug("Node check", node)
const shareTypes = Object.values(node?.attributes?.['share-types'] || {}).flat() as number[]

// Mixed share types
Expand All @@ -92,25 +97,28 @@ export const action = new FileAction({
}

// Link shares
if (shareTypes.includes(Type.SHARE_TYPE_LINK)
|| shareTypes.includes(Type.SHARE_TYPE_EMAIL)) {
if (shareTypes.includes(ShareType.Link)
|| shareTypes.includes(ShareType.Email)) {
return LinkSvg
}

// Group shares
if (shareTypes.includes(Type.SHARE_TYPE_GROUP)
|| shareTypes.includes(Type.SHARE_TYPE_REMOTE_GROUP)) {
if (shareTypes.includes(ShareType.Grup)
|| shareTypes.includes(ShareType.RemoteGroup)) {
return AccountGroupSvg
}

// Circle shares
if (shareTypes.includes(Type.SHARE_TYPE_CIRCLE)) {
if (shareTypes.includes(ShareType.Team)) {
// Circles was renamed to Teams
return CircleSvg
}

const ownerId = node?.attributes?.['owner-id']
if (ownerId && (ownerId !== getCurrentUser()?.uid || isExternal(node))) {
return generateAvatarSvg(ownerId, isExternal(node))
console.debug("IS EXTERNAL", isExternal(node))
console.debug("EXTENAL NODE", node)
return generateAvatarSvg(ownerId, true)
}

return AccountPlusSvg
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/src/services/SharingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const ocsEntryToNode = async function(ocsEntry: any): Promise<Folder | File | nu

let mtime = ocsEntry.item_mtime ? new Date((ocsEntry.item_mtime) * 1000) : undefined
// Prefer share time if more recent than item mtime
if (ocsEntry?.stime > (ocsEntry?.item_mtime || 0)) {
if (ocsEntry?.stime > (ocsEntry?.item_mtime || 0)) {ocsEntryToNode
mtime = new Date((ocsEntry.stime) * 1000)
}

Expand Down
17 changes: 17 additions & 0 deletions apps/files_sharing/src/utils/AccountIcon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { generateUrl } from '@nextcloud/router'

const isDarkMode = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches === true
|| document.querySelector('[data-themes*=dark]') !== null

export const generateAvatarSvg = (userId: string, isGuest = false) => {
const url = isDarkMode ? '/avatar/{userId}/32/dark' : '/avatar/{userId}/32'
const avatarUrl = generateUrl(isGuest ? url : url + '?guestFallback=true', { userId })
return `<svg width="32" height="32" viewBox="0 0 32 32"
xmlns="http://www.w3.org/2000/svg" class="sharing-status__avatar">
<image href="${avatarUrl}" height="32" width="32" />
</svg>`
}
13 changes: 13 additions & 0 deletions apps/files_sharing/src/views/SharingDetailsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,21 @@ export default {
}
await this.getNode()
console.log("Newly fetched", this.node)
emit('files:node:updated', this.node)
<<<<<<< Updated upstream
=======
if (this.$refs.externalLinkActions?.length > 0) {
await Promise.allSettled(this.$rethis.nodefs.externalLinkActions.map((action) => {
if (typeof action.$children.at(0)?.onSave !== 'function') {
return Promise.resolve()
}
return action.$children.at(0)?.onSave?.()
}))
}
>>>>>>> Stashed changes
this.$emit('close-sharing-details')
},
/**
Expand Down
14 changes: 13 additions & 1 deletion core/src/files/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import escapeHTML from 'escape-html'
Client.PROPERTY_SIZE = '{' + Client.NS_OWNCLOUD + '}size'
Client.PROPERTY_GETCONTENTLENGTH = '{' + Client.NS_DAV + '}getcontentlength'
Client.PROPERTY_ISENCRYPTED = '{' + Client.NS_DAV + '}is-encrypted'
Client.PROPERTY_ISFEDERATED = '{' + Client.NS_DAV + '}is-federated'
Client.PROPERTY_SHARE_PERMISSIONS = '{' + Client.NS_OCS + '}share-permissions'
Client.PROPERTY_SHARE_ATTRIBUTES = '{' + Client.NS_NEXTCLOUD + '}share-attributes'
Client.PROPERTY_QUOTA_AVAILABLE_BYTES = '{' + Client.NS_DAV + '}quota-available-bytes'
Expand Down Expand Up @@ -157,6 +158,10 @@ import escapeHTML from 'escape-html'
* Encryption state
*/
[Client.NS_NEXTCLOUD, 'is-encrypted'],
/**
* Federation state
*/
[Client.NS_NEXTCLOUD, 'is-federated'],
/**
* Share permissions
*/
Expand Down Expand Up @@ -303,7 +308,7 @@ import escapeHTML from 'escape-html'
*
* @returns {Array.<FileInfo>} array of file info
*/
_parseFileInfo: function(response) {
_parseFileInfo: function(response) {Encrypted
let path = decodeURIComponent(response.href)
if (path.substr(0, this._root.length) === this._root) {
path = path.substr(this._root.length)
Expand Down Expand Up @@ -355,6 +360,13 @@ import escapeHTML from 'escape-html'
data.isEncrypted = false
}

const isFederatedProp = props['{' + Client.NS_NEXTCLOUD + '}is-federated']
if (!_.isUndefined(isFederatedProp)) {
data.isFederated = isFederatedProp === '1'
} else {
data.isFederated = false
}

const isFavouritedProp = props['{' + Client.NS_OWNCLOUD + '}favorite']
if (!_.isUndefined(isFavouritedProp)) {
data.isFavourited = isFavouritedProp === '1'
Expand Down
17 changes: 17 additions & 0 deletions lib/private/User/LazyUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ private function getUser(): IUser {
$this->user = $this->userManager->get($this->uid);
}
}

if ($this->user === null) {
throw new \Exception('User not found');
}

/** @var IUser */
$user = $this->user;
return $user;
Expand Down Expand Up @@ -167,4 +172,16 @@ public function getManagerUids(): array {
public function setManagerUids(array $uids): void {
$this->getUser()->setManagerUids($uids);
}

public function isFederated(): bool {
try {
// If getUser succeeds then user is definitely not federated
// This could fail for other reasons (especially a race condition where a user is deleted)
// But it's what we have now
$this->getUser();
return false;
} catch (\Exception $e) {
return true;
}
}
}
6 changes: 6 additions & 0 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,10 @@ public function triggerChange($feature, $value = null, $oldValue = null) {
$this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]);
}
}

public function isFederated(): bool {
// Federated users are instantiated via LazyUser
// DAV also only uses LazyUser
return false;
}
}
8 changes: 8 additions & 0 deletions lib/public/IUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,12 @@ public function getManagerUids(): array;
* @since 27.0.0
*/
public function setManagerUids(array $uids): void;

/**
* Check if the user is federated (from another server)
*
* @return boll

Check failure on line 294 in lib/public/IUser.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

UndefinedDocblockClass

lib/public/IUser.php:294:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\boll does not exist (see https://psalm.dev/200)

Check failure

Code scanning / Psalm

UndefinedDocblockClass Error

Docblock-defined class, interface or enum named OCP\boll does not exist
* @since 28.0.11
*/
public function isFederated(): bool;
}

0 comments on commit 99b6b92

Please sign in to comment.