-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathSessionApi.js
More file actions
196 lines (169 loc) Β· 4.6 KB
/
SessionApi.js
File metadata and controls
196 lines (169 loc) Β· 4.6 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @copyright Copyright (c) 2022 Max <max@nextcloud.com>
*
* @author Max <max@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export class ConnectionClosedError extends Error {
constructor(message = 'Close has already been called on the connection', ...rest) {
super(message, ...rest)
}
}
class SessionApi {
#options
constructor(options = {}) {
this.#options = options
}
open({ fileId }) {
return axios.put(this.#url('session/create'), {
fileId,
filePath: this.#options.filePath,
token: this.#options.shareToken,
guestName: this.#options.guestName,
forceRecreate: this.#options.forceRecreate,
}).then(response => new Connection(response, this.#options))
}
#url(endpoint) {
const isPublic = !!this.#options.shareToken
return _endpointUrl(endpoint, isPublic)
}
}
export class Connection {
#content
#closed
#documentState
#document
#session
#lock
#readOnly
#options
constructor(response, options) {
const { document, session, lock, readOnly, content, documentState } = response.data
this.#document = document
this.#session = session
this.#lock = lock
this.#readOnly = readOnly
this.#content = content
this.#documentState = documentState
this.#options = options
this.closed = false
}
get document() {
return this.#document
}
get docStateVersion() {
return this.#documentState ? this.#document.lastSavedVersion : 0
}
get state() {
return {
document: { ...this.#document, readOnly: this.#readOnly },
session: this.#session,
documentSource: this.#content || '',
documentState: this.#documentState,
}
}
get #defaultParams() {
return {
documentId: this.#document.id,
sessionId: this.#session.id,
sessionToken: this.#session.token,
token: this.#options.shareToken,
}
}
sync({ version, autosaveContent, documentState, force, manualSave }) {
return this.#post(this.#url('session/sync'), {
...this.#defaultParams,
filePath: this.#options.filePath,
version,
autosaveContent,
documentState,
force,
manualSave,
})
}
push({ steps, version, awareness }) {
return this.#post(this.#url('session/push'), {
...this.#defaultParams,
filePath: this.#options.filePath,
steps,
version,
awareness,
})
}
// TODO: maybe return a new connection here so connections have immutable state
update(guestName) {
return this.#post(this.#url('session'), {
...this.#defaultParams,
guestName,
}).then(({ data }) => {
this.#session = data
})
}
uploadAttachment(file) {
const formData = new FormData()
formData.append('file', file)
const url = _endpointUrl('attachment/upload')
+ '?documentId=' + encodeURIComponent(this.#document.id)
+ '&sessionId=' + encodeURIComponent(this.#session.id)
+ '&sessionToken=' + encodeURIComponent(this.#session.token)
+ '&shareToken=' + encodeURIComponent(this.#options.shareToken || '')
return this.#post(url, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
}
insertAttachmentFile(filePath) {
return this.#post(_endpointUrl('attachment/filepath'), {
documentId: this.#document.id,
sessionId: this.#session.id,
sessionToken: this.#session.token,
filePath,
})
}
close() {
const promise = this.#post(this.#url('session/close'), this.#defaultParams)
this.closed = true
return promise
}
#post(...args) {
if (this.closed) {
return Promise.reject(new ConnectionClosedError())
}
return axios.post(...args)
}
#url(endpoint) {
const isPublic = !!this.#defaultParams.token
return _endpointUrl(endpoint, isPublic)
}
}
/**
*
* @param {string} endpoint - endpoint of the url inside apps/text
* @param {boolean} isPublic - public url or not
*/
function _endpointUrl(endpoint, isPublic = false) {
const _baseUrl = generateUrl('/apps/text')
if (isPublic) {
return `${_baseUrl}/public/${endpoint}`
}
return `${_baseUrl}/${endpoint}`
}
export default SessionApi