Skip to content

Commit 1b5576f

Browse files
committed
enh(api): expose hasOwner in the SyncService
Signed-off-by: Max <max@nextcloud.com>
1 parent eb8868c commit 1b5576f

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/services/SessionApi.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,27 @@ export class Connection {
4949
#session
5050
#lock
5151
#readOnly
52+
#hasOwner
5253
#options
5354

5455
constructor(response, options) {
55-
const { document, session, lock, readOnly, content, documentState } = response.data
56+
const {
57+
document,
58+
session,
59+
lock,
60+
readOnly,
61+
content,
62+
documentState,
63+
hasOwner,
64+
} = response.data
5665
this.#document = document
5766
this.#session = session
5867
this.#lock = lock
5968
this.#readOnly = readOnly
6069
this.#content = content
6170
this.#documentState = documentState
6271
this.#options = options
72+
this.#hasOwner = hasOwner
6373
this.isPublic = !!options.shareToken
6474
this.closed = false
6575
}
@@ -89,6 +99,10 @@ export class Connection {
8999
return this.closed
90100
}
91101

102+
get hasOwner() {
103+
return this.#hasOwner
104+
}
105+
92106
get #defaultParams() {
93107
return {
94108
documentId: this.#document.id,

src/services/SyncService.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class SyncService {
8888
return this.#connection.state.document.readOnly
8989
}
9090

91+
get hasOwner() {
92+
return this.#connection?.hasOwner
93+
}
94+
9195
get guestName() {
9296
return this.#connection.session.guestName
9397
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { describe, it, vi, expect } from 'vitest'
7+
import axios from '@nextcloud/axios'
8+
import SessionApi, { Connection } from '../../services/SessionApi.js'
9+
10+
vi.mock('@nextcloud/axios', () => {
11+
const put = vi.fn()
12+
return { default: { put } }
13+
})
14+
15+
describe('Session api', () => {
16+
it('opens a connection', async () => {
17+
const api = new SessionApi()
18+
axios.put.mockResolvedValue({ data: { hasOwner: true } })
19+
const connection = await api.open({ fileId: 123, baseBersionEtag: 'abc' })
20+
expect(connection).toBeInstanceOf(Connection)
21+
expect(connection.isClosed).toBe(false)
22+
expect(connection.hasOwner).toBe(true)
23+
})
24+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { describe, it, vi, expect } from 'vitest'
7+
import { SyncService } from '../../services/SyncService.js'
8+
9+
describe('Sync service', () => {
10+
11+
it('opens a connection', async () => {
12+
const api = mockApi({ hasOwner: true })
13+
const service = new SyncService({ api, baseVersionEtag: 'abc' })
14+
await service.open({ fileId: 123 })
15+
expect(service.hasOwner).toBe(true)
16+
})
17+
18+
it('opens a connection to a file without owner', async () => {
19+
const api = mockApi({ hasOwner: false })
20+
const service = new SyncService({ api, baseVersionEtag: 'abc' })
21+
await service.open({ fileId: 123 })
22+
expect(service.hasOwner).toBe(false)
23+
})
24+
25+
it('hasOwner is undefined without connection', async () => {
26+
const service = new SyncService({})
27+
expect(service.hasOwner).toBe(undefined)
28+
})
29+
30+
})
31+
32+
const mockApi = (connectionOptions = {}) => {
33+
const defaults = { document: { baseVersionEtag: 'abc' } }
34+
const open = vi.fn().mockResolvedValue({ ...defaults, ...connectionOptions })
35+
return { open }
36+
}

0 commit comments

Comments
 (0)