Skip to content

Commit

Permalink
feat(files): add node status
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Aug 25, 2023
1 parent af661f9 commit b26142c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions __tests__/files/file.spec.ts
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from 'vitest'
import { File } from '../../lib/files/file'
import { FileType } from '../../lib/files/fileType'
import { Permission } from '../../lib/permissions'
import { NodeStatus } from '../../lib/files/node'

describe('File creation', () => {
test('Valid dav file', () => {
Expand All @@ -12,6 +13,7 @@ describe('File creation', () => {
owner: 'emma',
mtime: new Date(Date.UTC(2023, 0, 1, 0, 0, 0)),
crtime: new Date(Date.UTC(1990, 0, 1, 0, 0, 0)),
status: NodeStatus.NEW,
})

expect(file).toBeInstanceOf(File)
Expand All @@ -35,6 +37,7 @@ describe('File creation', () => {
expect(file.path).toBe('/picture.jpg')
expect(file.isDavRessource).toBe(true)
expect(file.permissions).toBe(Permission.NONE)
expect(file.status).toBe(NodeStatus.NEW)
})

test('Valid dav file with root', () => {
Expand Down
10 changes: 10 additions & 0 deletions __tests__/files/node.spec.ts
Expand Up @@ -4,6 +4,7 @@ import { File } from '../../lib/files/file'
import { Folder } from '../../lib/files/folder'
import { Attribute, NodeData } from '../../lib/files/nodeData'
import { Permission } from '../../lib/permissions'
import { NodeStatus } from '../../lib/files/node'

describe('Node testing', () => {
test('Root null fallback', () => {
Expand Down Expand Up @@ -195,6 +196,15 @@ describe('Sanity checks', () => {
root: '/remote.php/dav/files/emma',
})).toThrowError('The root must be relative to the service. e.g /files/emma')
})

test('Invalid status', () => {
expect(() => new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
status: 'invalid' as unknown as NodeStatus,
})).toThrowError('Status must be a valid NodeStatus')
})
})

describe('Dav service detection', () => {
Expand Down
21 changes: 21 additions & 0 deletions lib/files/node.ts
Expand Up @@ -24,6 +24,13 @@ import { Permission } from '../permissions'
import { FileType } from './fileType'
import { Attribute, NodeData, isDavRessource, validateData } from './nodeData'

export enum NodeStatus {
NEW = 'new',
FAILED = 'failed',
LOADING = 'loading',
UNAVAILABLE = 'unavailable',
}

export abstract class Node {

private _data: NodeData
Expand Down Expand Up @@ -213,6 +220,20 @@ export abstract class Node {
return this._data?.id || this.attributes?.fileid
}

/**
* Get the node status.
*/
get status(): NodeStatus|undefined {
return this._data?.status
}

/**
* Set the node status.
*/
set status(status: NodeStatus|undefined) {
this._data.status = status

Check warning on line 234 in lib/files/node.ts

View check run for this annotation

Codecov / codecov/patch

lib/files/node.ts#L234

Added line #L234 was not covered by tests
}

/**
* Move the node to a new destination
*
Expand Down
9 changes: 9 additions & 0 deletions lib/files/nodeData.ts
Expand Up @@ -22,6 +22,7 @@

import { join } from 'path'
import { Permission } from '../permissions'
import { NodeStatus } from './node'

export interface Attribute { [key: string]: any }

Check warning on line 27 in lib/files/nodeData.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type

Expand Down Expand Up @@ -54,6 +55,7 @@ export interface NodeData {
/** The owner UID of this node */
owner: string|null

/** The node attributes */
attributes?: Attribute

/**
Expand All @@ -62,6 +64,9 @@ export interface NodeData {
* e.g. /files/emma
*/
root?: string

/** The node status */
status?: NodeStatus
}

/**
Expand Down Expand Up @@ -156,4 +161,8 @@ export const validateData = (data: NodeData, davService: RegExp) => {
throw new Error('The root must be relative to the service. e.g /files/emma')
}
}

if (data.status && !Object.values(NodeStatus).includes(data.status)) {
throw new Error('Status must be a valid NodeStatus')
}
}

0 comments on commit b26142c

Please sign in to comment.