Skip to content

Commit c8a18ae

Browse files
doublefacedoubleface
authored andcommitted
feat(cozy-realtime): Add support for shared drives files
1 parent cfc81dd commit c8a18ae

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

packages/cozy-realtime/src/CozyRealtime.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ class CozyRealtime {
3434
* @param {CozyClient} options.client A cozy-client instance
3535
* @param {Function} [options.createWebSocket] The function used to create WebSocket instances
3636
* @param {object} [options.logger] A custom logger
37+
* @param {string} [options.sharedDriveId] - The ID of the shared drive to connect to
3738
*/
3839
constructor(options) {
40+
this.sharedDriveId = options.sharedDriveId
3941
this.client = getCozyClientFromOptions(options)
4042
this.createWebSocket = options.createWebSocket || createWebSocket
4143
this.logger = options.logger || defaultLogger
@@ -83,7 +85,7 @@ class CozyRealtime {
8385
this.revokeWebSocket()
8486
}
8587
this.logger.info('creating a new websocket…')
86-
const url = getUrl(this.client)
88+
const url = getUrl(this.client, this.sharedDriveId)
8789
try {
8890
this.websocket = this.createWebSocket(url, protocol)
8991
this.websocket.authenticated = false
@@ -295,7 +297,7 @@ class CozyRealtime {
295297
const has = this.subscriptions.hasSameTypeAndId(sub)
296298
this.subscriptions.add(sub)
297299
// send to the server if there wasn't any subscription with that type & id before
298-
if (!has && this.isWebSocketAuthenticated()) {
300+
if (!this.sharedDriveId && !has && this.isWebSocketAuthenticated()) {
299301
this.sendSubscription(sub.type, sub.id)
300302
}
301303
}
@@ -316,7 +318,7 @@ class CozyRealtime {
316318
// if there is no more subscription of this type & id
317319
// then unsubscribe to the server
318320
const has = this.subscriptions.hasSameTypeAndId(sub)
319-
if (!has && this.isWebSocketAuthenticated()) {
321+
if (!this.sharedDriveId && !has && this.isWebSocketAuthenticated()) {
320322
this.sendUnsubscription(sub.type, sub.id)
321323
}
322324
// if this was the last subscription, stop the realtime

packages/cozy-realtime/src/CozyRealtime.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,26 @@ describe('CozyRealtime', () => {
539539
})
540540
})
541541

542+
describe('with sharedDriveId', () => {
543+
it('creates a WebSocket using the shared drive URL when provided', async () => {
544+
const createWebSocket = jest.fn()
545+
const sharedDriveId = 'drive-123'
546+
const sharedDriveWsURI =
547+
defaultClientUri.replace(/^http/, 'ws') +
548+
`sharings/drives/${sharedDriveId}/realtime`
549+
550+
createSocketServer({ wsuri: sharedDriveWsURI })
551+
const realtime = createRealtime({ createWebSocket, sharedDriveId })
552+
553+
const handler = jest.fn()
554+
realtime.subscribe(event, type, handler)
555+
556+
await sleep(100)
557+
558+
expect(createWebSocket).toHaveBeenCalledWith(sharedDriveWsURI, protocol)
559+
})
560+
})
561+
542562
describe('cozy-client events', () => {
543563
describe('login', () => {
544564
it('should authenticate again', async () => {

packages/cozy-realtime/src/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ function getInstanceUri(client) {
5353
* @param {CozyClient} client - CozyClient instance
5454
* @return {string} WebSocket url
5555
*/
56-
export function getUrl(client) {
56+
export function getUrl(client, sharedDriveId) {
5757
const url = getInstanceUri(client)
5858
const protocol = isSecureUrl(url) ? 'wss:' : 'ws:'
5959
const host = new URL(url).host
60-
return `${protocol}//${host}/realtime/`
60+
return sharedDriveId
61+
? `${protocol}//${host}/sharings/drives/${sharedDriveId}/realtime`
62+
: `${protocol}//${host}/realtime/`
6163
}
6264

6365
/**

0 commit comments

Comments
 (0)