-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathsync.ts
More file actions
85 lines (79 loc) · 2.15 KB
/
Copy pathsync.ts
File metadata and controls
85 lines (79 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { unref, type ShallowRef } from 'vue'
import type { Connection } from '../composables/useConnection.ts'
import type { Document, Session, Step } from '../services/SyncService.ts'
interface PushData {
version: number
steps: string[]
awareness: string
recoveryAttempt?: number
}
interface PushResponse {
data: {
steps: Step[]
documentState: string
version: number
}
}
/**
* Send data to the server
* @param connection the active connection
* @param data data to push to the server
*/
export function push(
connection: ShallowRef<Connection> | Connection,
data: PushData,
): Promise<PushResponse> {
const con = unref(connection)
const pub = con.shareToken ? '/public' : ''
const url = generateUrl(`apps/text${pub}/session/${con.documentId}/push`)
return axios.post(url, {
documentId: con.documentId,
sessionId: con.sessionId,
sessionToken: con.sessionToken,
token: con.shareToken,
baseVersionEtag: con.baseVersionEtag,
version: data.version,
steps: data.steps.filter((s) => s),
awareness: data.awareness,
recoveryAttempt: data.recoveryAttempt,
})
}
interface SyncResponse {
data: {
steps: Step[]
documentState: string
awareness: Record<string, string>
document: Document
sessions: Session[]
readOnly?: boolean
}
}
/**
* Receive updates from the server
* @param connection the active connection
* @param data data to push to the server
* @param data.version Changes up to this version are already known.
*/
export function sync(
connection: ShallowRef<Connection> | Connection,
data: { version: number },
): Promise<SyncResponse> {
const con = unref(connection)
const pub = con.shareToken ? '/public' : ''
const url = generateUrl(`apps/text${pub}/session/${con.documentId}/sync`)
return axios.post(url, {
documentId: con.documentId,
sessionId: con.sessionId,
sessionToken: con.sessionToken,
token: con.shareToken,
filePath: con.filePath,
baseVersionEtag: con.baseVersionEtag,
version: data.version,
})
}