Skip to content

Commit

Permalink
Merge pull request #266 from gregorylegarec/feat/better-realtime
Browse files Browse the repository at this point in the history
Better realtime
  • Loading branch information
CPatchane authored Sep 27, 2018
2 parents ef2cea1 + 3a0e396 commit 051f6f3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
60 changes: 29 additions & 31 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import React from 'react'
import { render } from 'react-dom'

import I18n from 'cozy-ui/react/I18n'
import realtime from 'cozy-realtime'
import stack from './lib/stack'
import {
deleteApp,
Expand Down Expand Up @@ -144,6 +143,26 @@ const getUserActionRequired = () => {
return undefined
}

const determineSSL = (ssl, cozyURL) => {
if (typeof ssl !== 'undefined') return ssl

let parsedURL
try {
parsedURL = new URL(cozyURL)
console.warn('Cozy-bar will soon need `ssl` and `domain` parameters to be properly configured, and will not rely on cozyURL.')
return parsedURL.protocol === 'https:'
} catch (error) {
console.warn(`cozyURL parameter passed to Cozy-bar is not a valid URL (${error.message}). Cozy-bar will rely on window.location to detect SSL.`)
}

if (window && window.location && window.location.protocol) {
return window.location.protocol === 'https:'
}

console.warn('Cozy-bar cannot detect SSL and will use default value (true)')
return true
}

const init = async ({
appName,
appNamePrefix = getAppNamePrefix(),
Expand All @@ -155,7 +174,8 @@ const init = async ({
replaceTitleOnMobile = false,
displayOnMobile,
isPublic = false,
onLogOut
onLogOut,
ssl
} = {}) => {
// Force public mode in `/public` URLs
if (/^\/public/.test(window.location.pathname)) {
Expand All @@ -168,39 +188,17 @@ const init = async ({
}

reduxStore.dispatch(setInfos(appName, appNamePrefix, appSlug))
stack.init({cozyURL, token})
stack.init({
cozyURL,
token,
onCreateApp: app => reduxStore.dispatch(receiveApp(app)),
onDeleteApp: app => reduxStore.dispatch(deleteApp(app)),
ssl: determineSSL(ssl, cozyURL)
})
if (lang) {
reduxStore.dispatch(setLocale(lang))
}

const realtimeConfig = {
// It's too weid to generate a fake URL here. We should just pass
// domain, token and a secure boolean to initialize realtime.
url: `${window.location.protocol}//${cozyURL}`,
token: token
}

try {
const realtimeApps = await realtime.subscribeAll(
realtimeConfig,
'io.cozy.apps'
)

realtimeApps.onCreate(async app => {
// Fetch direclty the app to get attributes `related` as well.
let fullApp
try {
fullApp = await stack.get.app(app.slug)
} catch (error) {
throw new Error(`Cannont fetch app ${app.slug}: ${error.message}`)
}
reduxStore.dispatch(receiveApp(fullApp))
})
realtimeApps.onDelete(app => reduxStore.dispatch(deleteApp(app)))
} catch (error) {
console.warn(`Cannot initialize realtime in Cozy-bar: ${error.message}`)
}

return injectBarInDOM({
appName,
appNamePrefix,
Expand Down
66 changes: 65 additions & 1 deletion src/lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global __TARGET__ */
/* eslint-env browser */

import realtime from 'cozy-realtime'

import {
ForbiddenException,
ServerErrorException,
Expand Down Expand Up @@ -140,12 +142,74 @@ async function getIcon (url, useCache = true) {
return iconUrl
}

async function initializeRealtime ({
onCreateApp,
onDeleteApp,
ssl,
url,
token
}) {
// Let's check the url. By default it's just the domain, but some apps are
// passing a full URL with protocol.
let parsedURL
try {
parsedURL = new URL(url)
} catch (error) {
console.warn(
`Cannot parse URL for realtime, using ${url} as domain (${error.message})`
)
}

const realtimeConfig = { token }
if (parsedURL) {
realtimeConfig.url = url
} else {
realtimeConfig.url = `${ssl ? 'https:' : 'http:'}${url}`
}

try {
const realtimeApps = await realtime.subscribeAll(
realtimeConfig,
'io.cozy.apps'
)

realtimeApps.onCreate(async app => {
// Fetch direclty the app to get attributes `related` as well.
let fullApp
try {
fullApp = await getApp(app.slug)
} catch (error) {
throw new Error(`Cannont fetch app ${app.slug}: ${error.message}`)
}

if (typeof onCreateApp === 'function') {
onCreateApp(fullApp)
}
})

realtimeApps.onDelete(app => {
if (typeof onDeleteApp === 'function') {
onDeleteApp(app)
}
})
} catch (error) {
console.warn(`Cannot initialize realtime in Cozy-bar: ${error.message}`)
}
}

const cache = {}

module.exports = {
init ({cozyURL, token}) {
async init ({ cozyURL, token, onCreateApp, onDeleteApp, ssl }) {
COZY_URL = `${__TARGET__ === 'mobile' ? '' : '//'}${cozyURL}`
COZY_TOKEN = token
await initializeRealtime({
onCreateApp,
onDeleteApp,
token: COZY_TOKEN,
url: COZY_URL,
ssl
})
},
updateAccessToken (token) {
COZY_TOKEN = token
Expand Down

0 comments on commit 051f6f3

Please sign in to comment.