diff --git a/app/common/fetch.js b/app/common/fetch.js index eccb6d0b79..a3055b0c71 100644 --- a/app/common/fetch.js +++ b/app/common/fetch.js @@ -4,6 +4,7 @@ import {getClientString} from './constants'; import * as session from '../sync/session'; let commandListeners = []; + export function onCommand (callback) { commandListeners.push(callback); } @@ -62,16 +63,21 @@ async function _fetch (method, path, json, sessionId = null) { throw err; } - if (response.headers.get('content-type') === 'application/json') { + if (response.headers.get('content-type') === 'application/json' || path.match(/\.json$/)) { return response.json(); } else { return response.text(); } } -function _getUrl (path) { +function _getUrl (pathOrUrl) { + // If it's a URL, just return it + if (pathOrUrl.match(/^https?:\/\//)) { + return pathOrUrl; + } + const baseUrl = process.env.INSOMNIA_SYNC_URL || 'https://api.insomnia.rest'; - return `${baseUrl}${path}`; + return `${baseUrl}${pathOrUrl}`; } function _notifyCommandListeners (uri) { diff --git a/app/sync/index.js b/app/sync/index.js index 069b7fd6ca..612532d98d 100644 --- a/app/sync/index.js +++ b/app/sync/index.js @@ -82,17 +82,19 @@ export async function init () { const syncStartTime = Date.now(); + let extraDelay = 0; try { await push(); await pull(); } catch (err) { logger.error('Sync failed with', err); + extraDelay += PULL_PERIOD; } - const syncTotalTime = Date.now() - syncStartTime; - // Add sync duration to give the server some room if it's being slow - nextSyncTime = Date.now() + PULL_PERIOD + (syncTotalTime * 2); + extraDelay += (Date.now() - syncStartTime) * 2; + + nextSyncTime = Date.now() + PULL_PERIOD + extraDelay; isSyncing = false; }, PULL_PERIOD / 5); diff --git a/app/ui/components/toast.js b/app/ui/components/toast.js index bd46a58f90..d339110be7 100644 --- a/app/ui/components/toast.js +++ b/app/ui/components/toast.js @@ -8,6 +8,7 @@ import {trackEvent} from '../../analytics/index'; import * as models from '../../models/index'; import * as constants from '../../common/constants'; import * as db from '../../common/database'; +import {isLoggedIn} from '../../sync/session'; const LOCALSTORAGE_KEY = 'insomnia::notifications::seen'; @@ -28,45 +29,69 @@ class Toast extends PureComponent { this._dismissNotification(); } + _hasSeenNotification (notification) { + const seenNotifications = this._loadSeen(); + return seenNotifications[notification.key]; + } + async _handleCheckNotifications () { // If there is a notification open, skip check if (this.state.notification) { return; } - const seenNotifications = this._loadSeen(); const stats = await models.stats.get(); let notification; - try { - const data = { - lastLaunch: stats.lastLaunch, - firstLaunch: stats.created, - launches: stats.launches, - platform: constants.getAppPlatform(), - version: constants.getAppVersion(), - requests: await db.count(models.request.type), - requestGroups: await db.count(models.requestGroup.type), - environments: await db.count(models.environment.type), - workspaces: await db.count(models.workspace.type) - }; - notification = await fetch.post(`/notification`, data); - } catch (e) { - console.warn('[toast] Failed to fetch notifications', e); + // Try fetching user notification + if (isLoggedIn()) { + try { + const data = { + lastLaunch: stats.lastLaunch, + firstLaunch: stats.created, + launches: stats.launches, + platform: constants.getAppPlatform(), + version: constants.getAppVersion(), + requests: await db.count(models.request.type), + requestGroups: await db.count(models.requestGroup.type), + environments: await db.count(models.environment.type), + workspaces: await db.count(models.workspace.type) + }; + + notification = await fetch.post(`/notification`, data); + } catch (err) { + console.warn('[toast] Failed to fetch user notifications', err); + } + } + + // Try fetching guest version-specific notification + if (!notification || this._hasSeenNotification(notification)) { + try { + notification = await fetch.get( + `https://insomnia.rest/notifications/v${constants.getAppVersion()}.json` + ); + } catch (err) { + console.warn('[toast] Failed to fetch version notifications', err); + } + } + + // Try fetching guest generic notification + if (!notification || this._hasSeenNotification(notification)) { + try { + notification = await fetch.get('https://insomnia.rest/notifications/all.json'); + } catch (err) { + console.warn('[toast] Failed to fetch generic notifications', err); + } } // No new notifications - if (!notification) { - return; - } - - // We've already seen this one, so bail - if (seenNotifications && seenNotifications[notification.key]) { + if (!notification || this._hasSeenNotification(notification)) { return; } // Remember that we've seen it + const seenNotifications = this._loadSeen(); seenNotifications[notification.key] = true; const obj = JSON.stringify(seenNotifications, null, 2); window.localStorage.setItem(LOCALSTORAGE_KEY, obj);