diff --git a/apps/files_sharing/src/services/SharingService.spec.ts b/apps/files_sharing/src/services/SharingService.spec.ts index ce603e670a4ce..edfa10483f123 100644 --- a/apps/files_sharing/src/services/SharingService.spec.ts +++ b/apps/files_sharing/src/services/SharingService.spec.ts @@ -21,12 +21,12 @@ */ import type { OCSResponse } from '@nextcloud/typings/ocs' import { expect } from '@jest/globals' -import { Type } from '@nextcloud/sharing' +import { File, Folder } from '@nextcloud/files' +import { ShareType } from '@nextcloud/sharing' import * as auth from '@nextcloud/auth' import axios from '@nextcloud/axios' import { getContents } from './SharingService' -import { File, Folder } from '@nextcloud/files' import logger from './logger' global.window.OC = { @@ -158,7 +158,7 @@ describe('SharingService filtering', () => { data: [ { id: '62', - share_type: Type.SHARE_TYPE_USER, + share_type: ShareType.User, uid_owner: 'test', displayname_owner: 'test', permissions: 31, @@ -189,7 +189,7 @@ describe('SharingService filtering', () => { }) test('Shared with others filtering', async () => { - const shares = await getContents(false, true, false, false, [Type.SHARE_TYPE_USER]) + const shares = await getContents(false, true, false, false, [ShareType.User]) expect(axios.get).toHaveBeenCalledTimes(1) expect(shares.contents).toHaveLength(1) @@ -198,7 +198,7 @@ describe('SharingService filtering', () => { }) test('Shared with others filtering empty', async () => { - const shares = await getContents(false, true, false, false, [Type.SHARE_TYPE_LINK]) + const shares = await getContents(false, true, false, false, [ShareType.Link]) expect(axios.get).toHaveBeenCalledTimes(1) expect(shares.contents).toHaveLength(0) @@ -294,6 +294,25 @@ describe('SharingService share to Node mapping', () => { tags: [window.OC.TAG_FAVORITE], } + const remoteFile = { + mimetype: 'text/markdown', + mtime: 1688721600, + permissions: 19, + type: 'file', + file_id: 1234, + id: 4, + share_type: ShareType.User, + parent: null, + remote: 'http://exampe.com', + remote_id: '12345', + share_token: 'share-token', + name: '/test.md', + mountpoint: '/shares/test.md', + owner: 'owner-uid', + user: 'sharee-uid', + accepted: true, + } + test('File', async () => { jest.spyOn(axios, 'get').mockReturnValueOnce(Promise.resolve({ data: { @@ -353,6 +372,35 @@ describe('SharingService share to Node mapping', () => { expect(folder.attributes.favorite).toBe(1) }) + test('Remote file', async () => { + jest.spyOn(axios, 'get').mockReturnValueOnce(Promise.resolve({ + data: { + ocs: { + data: [remoteFile], + }, + }, + })) + + const shares = await getContents(false, true, false, false) + + expect(axios.get).toHaveBeenCalledTimes(1) + expect(shares.contents).toHaveLength(1) + + const file = shares.contents[0] as File + expect(file).toBeInstanceOf(File) + expect(file.fileid).toBe(1234) + expect(file.source).toBe('http://localhost/remote.php/dav/files/test/shares/test.md') + expect(file.owner).toBe('owner-uid') + expect(file.mime).toBe('text/markdown') + expect(file.mtime?.getTime()).toBe(remoteFile.mtime * 1000) + // not available for remote shares + expect(file.size).toBe(undefined) + expect(file.permissions).toBe(0) + expect(file.root).toBe('/files/test') + expect(file.attributes).toBeInstanceOf(Object) + expect(file.attributes.favorite).toBe(0) + }) + test('Empty', async () => { jest.spyOn(logger, 'error').mockImplementationOnce(() => {}) jest.spyOn(axios, 'get').mockReturnValueOnce(Promise.resolve({ diff --git a/apps/files_sharing/src/services/SharingService.ts b/apps/files_sharing/src/services/SharingService.ts index 7841b71a634b1..043ea9821f9d4 100644 --- a/apps/files_sharing/src/services/SharingService.ts +++ b/apps/files_sharing/src/services/SharingService.ts @@ -19,8 +19,7 @@ * along with this program. If not, see . * */ -/* eslint-disable camelcase, n/no-extraneous-import */ -import type { AxiosPromise } from 'axios' +import type { AxiosPromise } from '@nextcloud/axios' import type { OCSResponse } from '@nextcloud/typings/ocs' import { Folder, File, type ContentsWithRoot, Permission } from '@nextcloud/files' @@ -40,10 +39,16 @@ const ocsEntryToNode = async function(ocsEntry: any): Promise (ocsEntry?.item_mtime || 0)) { mtime = new Date((ocsEntry.stime) * 1000) } @@ -88,7 +94,8 @@ const ocsEntryToNode = async function(ocsEntry: any): Promise> { /** * Group an array of objects (here Nodes) by a key * and return an array of arrays of them. + * @param nodes Nodes to group + * @param key The attribute to group by */ const groupBy = function(nodes: (Folder | File)[], key: string) { return Object.values(nodes.reduce(function(acc, curr) { @@ -168,7 +177,7 @@ const groupBy = function(nodes: (Folder | File)[], key: string) { } export const getContents = async (sharedWithYou = true, sharedWithOthers = true, pendingShares = false, deletedshares = false, filterTypes: number[] = []): Promise => { - const promises = [] as AxiosPromise>[] + const promises = [] as AxiosPromise>[] if (sharedWithYou) { promises.push(getSharedWithYou(), getRemoteShares())