Skip to content

Commit dd602ff

Browse files
committed
chore(refactor): split open api functions
Two api endpoints deserve two different functions in particular as they also expect different params. This also allows narrowing down the types a little more. Signed-off-by: Max <max@nextcloud.com>
1 parent 70d6b85 commit dd602ff

3 files changed

Lines changed: 82 additions & 18 deletions

File tree

src/apis/connect.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ import axios from '@nextcloud/axios'
1010
import { generateUrl } from '@nextcloud/router'
1111

1212
export interface OpenParams {
13-
fileId?: number
13+
fileId: number
14+
filePath?: string // not send to the api but included in the connection
15+
baseVersionEtag?: string
16+
}
17+
18+
export interface OpenShareParams {
19+
token: string
20+
filePath?: string
1421
baseVersionEtag?: string
15-
filePath: string
16-
token?: string
1722
guestName?: string
1823
}
1924

@@ -27,16 +32,31 @@ export interface OpenData {
2732
hasOwner: boolean
2833
}
2934

35+
/**
36+
* Open editing connection to a file when logged in
37+
*
38+
* @param params Parameters identifying the document
39+
*/
40+
export async function openFile(params: OpenParams): Promise<{ connection: Connection, data: OpenData }> {
41+
const url = generateUrl(`/apps/text/session/${params.fileId}/create`)
42+
const response = await axios.put(url, params)
43+
const { document, session } = response.data
44+
const connection = {
45+
documentId: document.id,
46+
sessionId: session.id,
47+
sessionToken: session.token,
48+
baseVersionEtag: document.baseVersionEtag,
49+
}
50+
return { connection, data: response.data }
51+
}
52+
3053
/**
3154
* Open editing connection to the document
3255
*
3356
* @param params Parameters identifying the document
3457
*/
35-
export async function open(params: OpenParams): Promise<{ connection: Connection, data: OpenData }> {
36-
const _baseUrl = params.token
37-
? generateUrl('/apps/text/public')
38-
: generateUrl('/apps/text')
39-
const url = `${_baseUrl}/session/${params.fileId}/create`
58+
export async function openShare(params: OpenShareParams): Promise<{ connection: Connection, data: OpenData }> {
59+
const url = generateUrl('/apps/text/public/session/123/create')
4060
const response = await axios.put(url, params)
4161
const { document, session } = response.data
4262
const connection = {

src/composables/useConnection.ts

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import type { OpenData } from '../apis/connect.ts'
88
import type { Document, Session } from '../services/SyncService.ts'
99

1010
import { inject, provide, shallowRef } from 'vue'
11-
import { open } from '../apis/connect.ts'
11+
import * as api from '../apis/connect.ts'
1212

1313
export interface Connection {
1414
documentId: number
1515
sessionId: number
1616
sessionToken: string
1717
baseVersionEtag: string
18-
filePath: string
18+
filePath?: string
1919
shareToken?: string
2020
}
2121

@@ -65,13 +65,8 @@ export function provideConnection(
6565
const guestName = localStorage.getItem('nick') ?? ''
6666
const { connection: opened, data }
6767
= openInitialSession(props, baseVersionEtag)
68-
|| (await open({
69-
fileId: props.fileId,
70-
guestName,
71-
token: props.shareToken,
72-
filePath: props.relativePath,
73-
baseVersionEtag,
74-
}))
68+
|| await openShare(props, baseVersionEtag, guestName)
69+
|| await openFile(props, baseVersionEtag)
7570
await setBaseVersionEtag(data.document.baseVersionEtag)
7671
connection.value = opened
7772
openData.value = data
@@ -134,3 +129,52 @@ function openInitialSession(
134129
return { connection, data: props.initialSession }
135130
}
136131
}
132+
133+
/**
134+
* Get the connection and additional data from the initial session if available.
135+
*
136+
* @param props Props of the editor component
137+
* @param props.relativePath Relative path to the file.
138+
* @param props.shareToken Share token of the file.
139+
* @param baseVersionEtag Etag from the last editing session.
140+
* @param guestName to be shown to other participants.
141+
*/
142+
async function openShare(
143+
props: {
144+
relativePath: string
145+
shareToken?: string
146+
},
147+
baseVersionEtag: string | undefined,
148+
guestName: string | undefined,
149+
) {
150+
if (props.shareToken) {
151+
return api.openShare({
152+
guestName,
153+
token: props.shareToken,
154+
filePath: props.relativePath,
155+
baseVersionEtag,
156+
})
157+
}
158+
}
159+
160+
/**
161+
* Get the connection and additional data from the initial session if available.
162+
*
163+
* @param props Props of the editor component
164+
* @param props.fileId id of the file
165+
* @param props.relativePath Relative path to the file.
166+
* @param baseVersionEtag Etag from the last editing session.
167+
*/
168+
async function openFile(
169+
props: {
170+
fileId: number
171+
relativePath: string
172+
},
173+
baseVersionEtag: string | undefined,
174+
) {
175+
return api.openFile({
176+
fileId: props.fileId,
177+
filePath: props.relativePath,
178+
baseVersionEtag,
179+
})
180+
}

src/tests/services/SyncService.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Sync service', () => {
5454
setBaseVersionEtag,
5555
)
5656
vi.mock('../../apis/connect')
57-
vi.mocked(connect.open).mockResolvedValue(openResult)
57+
vi.mocked(connect.openFile).mockResolvedValue(openResult)
5858
const openHandler = vi.fn()
5959
const service = new SyncService({ connection, openConnection })
6060
service.bus.on('opened', openHandler)

0 commit comments

Comments
 (0)