2017-07-28 07:40:24 +00:00
|
|
|
import localDbSvc from './localDbSvc';
|
|
|
|
import store from '../store';
|
|
|
|
import utils from './utils';
|
2017-08-25 10:37:46 +00:00
|
|
|
import diffUtils from './diffUtils';
|
2017-12-11 00:53:46 +00:00
|
|
|
import networkSvc from './networkSvc';
|
2018-04-27 14:37:05 +00:00
|
|
|
import providerRegistry from './providers/common/providerRegistry';
|
2017-12-10 23:49:20 +00:00
|
|
|
import googleDriveAppDataProvider from './providers/googleDriveAppDataProvider';
|
2018-01-24 07:31:54 +00:00
|
|
|
import './providers/couchdbWorkspaceProvider';
|
2018-04-27 14:37:05 +00:00
|
|
|
import './providers/githubWorkspaceProvider';
|
|
|
|
import './providers/googleDriveWorkspaceProvider';
|
2018-03-12 00:45:54 +00:00
|
|
|
import tempFileSvc from './tempFileSvc';
|
2018-05-04 18:07:28 +00:00
|
|
|
import fileSvc from './fileSvc';
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2018-04-27 14:37:05 +00:00
|
|
|
const minAutoSyncEvery = 60 * 1000; // 60 sec
|
2017-08-15 10:43:26 +00:00
|
|
|
const inactivityThreshold = 3 * 1000; // 3 sec
|
2017-08-17 23:10:35 +00:00
|
|
|
const restartSyncAfter = 30 * 1000; // 30 sec
|
2018-04-27 14:37:05 +00:00
|
|
|
const restartContentSyncAfter = 500; // Restart if an authorize window pops up
|
2018-04-08 10:18:56 +00:00
|
|
|
const maxContentHistory = 20;
|
2017-08-25 10:37:46 +00:00
|
|
|
|
2018-04-27 14:37:05 +00:00
|
|
|
const LAST_SEEN = 0;
|
|
|
|
const LAST_MERGED = 1;
|
|
|
|
const LAST_SENT = 2;
|
|
|
|
|
2018-01-04 20:19:10 +00:00
|
|
|
let actionProvider;
|
|
|
|
let workspaceProvider;
|
2017-12-10 23:49:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use a lock in the local storage to prevent multiple windows concurrency.
|
|
|
|
*/
|
|
|
|
let lastSyncActivity;
|
2017-12-11 00:53:46 +00:00
|
|
|
const getLastStoredSyncActivity = () =>
|
|
|
|
parseInt(localStorage.getItem(store.getters['workspace/lastSyncActivityKey']), 10) || 0;
|
2017-12-10 23:49:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if workspace sync is possible.
|
|
|
|
*/
|
2017-12-17 15:08:52 +00:00
|
|
|
const isWorkspaceSyncPossible = () => !!store.getters['workspace/syncToken'];
|
2017-12-10 23:49:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if file has at least one explicit sync location.
|
|
|
|
*/
|
2017-08-25 10:37:46 +00:00
|
|
|
const hasCurrentFileSyncLocations = () => !!store.getters['syncLocation/current'].length;
|
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Return true if we are online and we have something to sync.
|
|
|
|
*/
|
2017-08-25 10:37:46 +00:00
|
|
|
const isSyncPossible = () => !store.state.offline &&
|
2017-12-10 23:49:20 +00:00
|
|
|
(isWorkspaceSyncPossible() || hasCurrentFileSyncLocations());
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Return true if we are the many window, ie we have the lastSyncActivity lock.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const isSyncWindow = () => {
|
2017-10-07 11:22:24 +00:00
|
|
|
const storedLastSyncActivity = getLastStoredSyncActivity();
|
2017-08-15 10:43:26 +00:00
|
|
|
return lastSyncActivity === storedLastSyncActivity ||
|
|
|
|
Date.now() > inactivityThreshold + storedLastSyncActivity;
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
2017-12-11 00:53:46 +00:00
|
|
|
* Return true if auto sync can start, ie if lastSyncActivity is old enough.
|
2017-12-10 23:49:20 +00:00
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const isAutoSyncReady = () => {
|
2018-05-06 00:46:33 +00:00
|
|
|
let { autoSyncEvery } = store.getters['data/computedSettings'];
|
2017-12-17 15:08:52 +00:00
|
|
|
if (autoSyncEvery < minAutoSyncEvery) {
|
|
|
|
autoSyncEvery = minAutoSyncEvery;
|
|
|
|
}
|
|
|
|
return Date.now() > autoSyncEvery + getLastStoredSyncActivity();
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Update the lastSyncActivity, assuming we have the lock.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const setLastSyncActivity = () => {
|
2017-08-15 10:43:26 +00:00
|
|
|
const currentDate = Date.now();
|
|
|
|
lastSyncActivity = currentDate;
|
2017-12-11 00:53:46 +00:00
|
|
|
localStorage.setItem(store.getters['workspace/lastSyncActivityKey'], currentDate);
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2018-04-27 14:37:05 +00:00
|
|
|
/**
|
|
|
|
* Upgrade hashes if syncedContent is from an old version
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const upgradeSyncedContent = (syncedContent) => {
|
2018-04-27 14:37:05 +00:00
|
|
|
if (syncedContent.v) {
|
|
|
|
return syncedContent;
|
|
|
|
}
|
|
|
|
const hashUpgrades = {};
|
|
|
|
const historyData = {};
|
|
|
|
const syncHistory = {};
|
|
|
|
Object.entries(syncedContent.historyData).forEach(([hash, content]) => {
|
|
|
|
const newContent = utils.addItemHash(content);
|
|
|
|
historyData[newContent.hash] = newContent;
|
|
|
|
hashUpgrades[hash] = newContent.hash;
|
|
|
|
});
|
|
|
|
Object.entries(syncedContent.syncHistory).forEach(([id, hashEntries]) => {
|
|
|
|
syncHistory[id] = hashEntries.map(hash => hashUpgrades[hash]);
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
...syncedContent,
|
|
|
|
historyData,
|
|
|
|
syncHistory,
|
|
|
|
v: 1,
|
|
|
|
};
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2018-04-27 14:37:05 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Clean a syncedContent.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const cleanSyncedContent = (syncedContent) => {
|
2017-08-25 10:37:46 +00:00
|
|
|
// Clean syncHistory from removed syncLocations
|
|
|
|
Object.keys(syncedContent.syncHistory).forEach((syncLocationId) => {
|
|
|
|
if (syncLocationId !== 'main' && !store.state.syncLocation.itemMap[syncLocationId]) {
|
|
|
|
delete syncedContent.syncHistory[syncLocationId];
|
|
|
|
}
|
|
|
|
});
|
2018-05-06 00:46:33 +00:00
|
|
|
const allSyncLocationHashSet = new Set([]
|
|
|
|
.concat(...Object.keys(syncedContent.syncHistory)
|
|
|
|
.map(id => syncedContent.syncHistory[id])));
|
2017-08-25 10:37:46 +00:00
|
|
|
// Clean historyData from unused contents
|
2018-05-06 00:46:33 +00:00
|
|
|
Object.keys(syncedContent.historyData)
|
|
|
|
.map(hash => parseInt(hash, 10))
|
|
|
|
.forEach((hash) => {
|
|
|
|
if (!allSyncLocationHashSet.has(hash)) {
|
|
|
|
delete syncedContent.historyData[hash];
|
|
|
|
}
|
|
|
|
});
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2017-08-25 10:37:46 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Apply changes retrieved from the main provider. Update sync data accordingly.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const applyChanges = (changes) => {
|
2017-08-17 23:10:35 +00:00
|
|
|
const storeItemMap = { ...store.getters.allItemMap };
|
|
|
|
const syncData = { ...store.getters['data/syncData'] };
|
2017-12-23 18:25:14 +00:00
|
|
|
let saveSyncData = false;
|
2017-08-17 23:10:35 +00:00
|
|
|
|
|
|
|
changes.forEach((change) => {
|
2017-12-17 15:08:52 +00:00
|
|
|
const existingSyncData = syncData[change.syncDataId];
|
2017-10-05 07:18:21 +00:00
|
|
|
const existingItem = existingSyncData && storeItemMap[existingSyncData.itemId];
|
2017-12-23 18:25:14 +00:00
|
|
|
if (!change.item && existingSyncData) {
|
|
|
|
// Item was removed
|
2018-04-27 14:37:05 +00:00
|
|
|
if (syncData[change.syncDataId]) {
|
|
|
|
delete syncData[change.syncDataId];
|
|
|
|
saveSyncData = true;
|
|
|
|
}
|
2017-10-05 07:18:21 +00:00
|
|
|
if (existingItem) {
|
|
|
|
// Remove object from the store
|
|
|
|
store.commit(`${existingItem.type}/deleteItem`, existingItem.id);
|
|
|
|
delete storeItemMap[existingItem.id];
|
|
|
|
}
|
2017-12-23 18:25:14 +00:00
|
|
|
} else if (change.item && change.item.hash) {
|
|
|
|
// Item was modifed
|
2018-04-27 14:37:05 +00:00
|
|
|
if ((existingSyncData || {}).hash !== change.syncData.hash) {
|
|
|
|
syncData[change.syncDataId] = change.syncData;
|
|
|
|
saveSyncData = true;
|
|
|
|
}
|
2017-12-23 18:25:14 +00:00
|
|
|
if (
|
|
|
|
// If no sync data or existing one is different
|
|
|
|
(existingSyncData || {}).hash !== change.item.hash
|
|
|
|
// And no existing item or existing item is different
|
|
|
|
&& (existingItem || {}).hash !== change.item.hash
|
|
|
|
// And item is not content nor data, which will be merged later
|
|
|
|
&& change.item.type !== 'content' && change.item.type !== 'data'
|
|
|
|
) {
|
|
|
|
store.commit(`${change.item.type}/setItem`, change.item);
|
|
|
|
storeItemMap[change.item.id] = change.item;
|
|
|
|
}
|
2017-08-17 23:10:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-23 18:25:14 +00:00
|
|
|
if (saveSyncData) {
|
2017-08-17 23:10:35 +00:00
|
|
|
store.dispatch('data/setSyncData', syncData);
|
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2017-08-17 23:10:35 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Create a sync location by uploading the current file content.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const createSyncLocation = (syncLocation) => {
|
2017-09-23 19:01:50 +00:00
|
|
|
syncLocation.id = utils.uid();
|
|
|
|
const currentFile = store.getters['file/current'];
|
|
|
|
const fileId = currentFile.id;
|
|
|
|
syncLocation.fileId = fileId;
|
|
|
|
// Use deepCopy to freeze item
|
|
|
|
const content = utils.deepCopy(store.getters['content/current']);
|
2018-05-06 00:46:33 +00:00
|
|
|
store.dispatch(
|
|
|
|
'queue/enqueue',
|
2018-05-13 13:27:33 +00:00
|
|
|
async () => {
|
2017-09-23 19:01:50 +00:00
|
|
|
const provider = providerRegistry.providers[syncLocation.providerId];
|
|
|
|
const token = provider.getToken(syncLocation);
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncLocationToStore = await provider.uploadContent(token, {
|
2017-09-23 19:01:50 +00:00
|
|
|
...content,
|
|
|
|
history: [content.hash],
|
2018-05-13 13:27:33 +00:00
|
|
|
}, syncLocation);
|
|
|
|
await localDbSvc.loadSyncedContent(fileId);
|
|
|
|
const newSyncedContent = utils.deepCopy(upgradeSyncedContent(store.state.syncedContent.itemMap[`${fileId}/syncedContent`]));
|
|
|
|
const newSyncHistoryItem = [];
|
|
|
|
newSyncedContent.syncHistory[syncLocation.id] = newSyncHistoryItem;
|
|
|
|
newSyncHistoryItem[LAST_SEEN] = content.hash;
|
|
|
|
newSyncHistoryItem[LAST_SENT] = content.hash;
|
|
|
|
newSyncedContent.historyData[content.hash] = content;
|
|
|
|
|
|
|
|
store.commit('syncedContent/patchItem', newSyncedContent);
|
|
|
|
store.commit('syncLocation/setItem', syncLocationToStore);
|
|
|
|
store.dispatch('notification/info', `A new synchronized location was added to "${currentFile.name}".`);
|
2018-05-06 00:46:33 +00:00
|
|
|
},
|
|
|
|
);
|
2018-05-13 13:27:33 +00:00
|
|
|
};
|
2017-09-23 19:01:50 +00:00
|
|
|
|
2018-06-07 23:56:11 +00:00
|
|
|
/**
|
|
|
|
* Prevent from sending new data too long after old data has been fetched.
|
|
|
|
*/
|
2018-04-27 14:37:05 +00:00
|
|
|
const tooLateChecker = (timeout) => {
|
|
|
|
const tooLateAfter = Date.now() + timeout;
|
2018-06-07 23:56:11 +00:00
|
|
|
return (cb) => {
|
2018-04-27 14:37:05 +00:00
|
|
|
if (tooLateAfter < Date.now()) {
|
|
|
|
throw new Error('TOO_LATE');
|
|
|
|
}
|
2018-06-07 23:56:11 +00:00
|
|
|
return cb();
|
2018-04-27 14:37:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-06-07 23:56:11 +00:00
|
|
|
/**
|
|
|
|
* Return true if file is in the temp folder or it's a welcome file.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const isTempFile = (fileId) => {
|
2018-06-07 23:56:11 +00:00
|
|
|
const contentId = `${fileId}/content`;
|
|
|
|
if (store.getters['data/syncDataByItemId'][contentId]) {
|
2018-05-13 13:27:33 +00:00
|
|
|
// If file has already been synced, it's not a temp file
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const file = store.state.file.itemMap[fileId];
|
2018-06-07 23:56:11 +00:00
|
|
|
const content = store.state.content.itemMap[contentId];
|
2018-05-13 13:27:33 +00:00
|
|
|
if (!file || !content) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (file.parentId === 'temp') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const locations = [
|
|
|
|
...store.getters['syncLocation/filteredGroupedByFileId'][fileId] || [],
|
|
|
|
...store.getters['publishLocation/filteredGroupedByFileId'][fileId] || [],
|
|
|
|
];
|
|
|
|
if (locations.length) {
|
|
|
|
// If file has sync/publish locations, it's not a temp file
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Return true if it's a welcome file that has no discussion
|
|
|
|
const { welcomeFileHashes } = store.getters['data/localSettings'];
|
|
|
|
const hash = utils.hash(content.text);
|
|
|
|
const hasDiscussions = Object.keys(content.discussions).length;
|
|
|
|
return file.name === 'Welcome file' && welcomeFileHashes[hash] && !hasDiscussions;
|
|
|
|
};
|
|
|
|
|
2017-11-04 16:59:48 +00:00
|
|
|
class SyncContext {
|
2018-03-12 00:45:54 +00:00
|
|
|
restart = false;
|
|
|
|
attempted = {};
|
2017-11-04 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 00:46:33 +00:00
|
|
|
/**
|
2017-12-10 23:49:20 +00:00
|
|
|
* Sync one file with all its locations.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncFile = async (fileId, syncContext = new SyncContext()) => {
|
2018-06-07 23:56:11 +00:00
|
|
|
const contentId = `${fileId}/content`;
|
|
|
|
syncContext.attempted[contentId] = true;
|
2018-03-12 00:45:54 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
await localDbSvc.loadSyncedContent(fileId);
|
|
|
|
try {
|
2018-06-07 23:56:11 +00:00
|
|
|
await localDbSvc.loadItem(contentId);
|
2018-05-13 13:27:33 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Item may not exist if content has not been downloaded yet
|
|
|
|
}
|
2018-06-07 23:56:11 +00:00
|
|
|
|
|
|
|
const getContent = () => store.state.content.itemMap[contentId];
|
2018-05-13 13:27:33 +00:00
|
|
|
const getSyncedContent = () => upgradeSyncedContent(store.state.syncedContent.itemMap[`${fileId}/syncedContent`]);
|
|
|
|
const getSyncHistoryItem = syncLocationId => getSyncedContent().syncHistory[syncLocationId];
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (isTempFile(fileId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const syncLocations = [
|
|
|
|
...store.getters['syncLocation/filteredGroupedByFileId'][fileId] || [],
|
|
|
|
];
|
|
|
|
if (isWorkspaceSyncPossible()) {
|
|
|
|
syncLocations.unshift({ id: 'main', providerId: workspaceProvider.id, fileId });
|
|
|
|
}
|
|
|
|
|
|
|
|
await utils.awaitSequence(syncLocations, async (syncLocation) => {
|
|
|
|
const provider = providerRegistry.providers[syncLocation.providerId];
|
|
|
|
if (!provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const token = provider.getToken(syncLocation);
|
|
|
|
if (!token) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 23:56:11 +00:00
|
|
|
const downloadContent = async () => {
|
|
|
|
// On simple provider, call simply downloadContent
|
|
|
|
if (syncLocation.id !== 'main') {
|
|
|
|
return provider.downloadContent(token, syncLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
// On workspace provider, call downloadWorkspaceContent
|
|
|
|
const oldSyncData = provider.isGit
|
|
|
|
? store.getters['data/syncData'][store.getters.itemGitPaths[contentId]]
|
|
|
|
: store.getters['data/syncDataByItemId'][contentId];
|
|
|
|
if (!oldSyncData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { item, syncData } = await provider.downloadWorkspaceContent(token, oldSyncData);
|
|
|
|
if (!item) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update sync data if changed
|
|
|
|
if (syncData
|
|
|
|
&& utils.serializeObject(oldSyncData) !== utils.serializeObject(syncData)
|
|
|
|
) {
|
|
|
|
store.dispatch('data/patchSyncData', {
|
|
|
|
[syncData.id]: syncData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
|
|
|
|
const uploadContent = async (item, ifNotTooLate) => {
|
|
|
|
// On simple provider, call simply uploadContent
|
|
|
|
if (syncLocation.id !== 'main') {
|
|
|
|
return provider.uploadContent(token, item, syncLocation, ifNotTooLate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// On workspace provider, call uploadWorkspaceContent
|
|
|
|
const oldSyncData = provider.isGit
|
|
|
|
? store.getters['data/syncData'][store.getters.itemGitPaths[contentId]]
|
|
|
|
: store.getters['data/syncDataByItemId'][contentId];
|
|
|
|
if (oldSyncData && oldSyncData.hash === item.hash) {
|
|
|
|
return syncLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
const syncData = await provider.uploadWorkspaceContent(
|
|
|
|
token,
|
|
|
|
item,
|
|
|
|
oldSyncData,
|
|
|
|
ifNotTooLate,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update sync data if changed
|
|
|
|
if (syncData
|
|
|
|
&& utils.serializeObject(oldSyncData) !== utils.serializeObject(syncData)
|
|
|
|
) {
|
|
|
|
store.dispatch('data/patchSyncData', {
|
|
|
|
[syncData.id]: syncData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return syncLocation
|
|
|
|
return syncLocation;
|
|
|
|
};
|
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
const doSyncLocation = async () => {
|
2018-06-07 23:56:11 +00:00
|
|
|
const serverContent = await downloadContent(token, syncLocation);
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncedContent = getSyncedContent();
|
|
|
|
const syncHistoryItem = getSyncHistoryItem(syncLocation.id);
|
2018-06-07 23:56:11 +00:00
|
|
|
|
|
|
|
// Merge content
|
|
|
|
let mergedContent;
|
|
|
|
const clientContent = utils.deepCopy(getContent());
|
|
|
|
if (!clientContent) {
|
|
|
|
mergedContent = utils.deepCopy(serverContent || null);
|
|
|
|
} else if (!serverContent // If sync location has not been created yet
|
|
|
|
// Or server and client contents are synced
|
|
|
|
|| serverContent.hash === clientContent.hash
|
|
|
|
// Or server content has not changed or has already been merged
|
|
|
|
|| syncedContent.historyData[serverContent.hash]
|
|
|
|
) {
|
|
|
|
mergedContent = clientContent;
|
|
|
|
} else {
|
|
|
|
// Perform a merge with last merged content if any, or perform a simple fusion otherwise
|
2018-05-13 13:27:33 +00:00
|
|
|
let lastMergedContent = utils.someResult(
|
|
|
|
serverContent.history,
|
|
|
|
hash => syncedContent.historyData[hash],
|
|
|
|
);
|
|
|
|
if (!lastMergedContent && syncHistoryItem) {
|
|
|
|
lastMergedContent = syncedContent.historyData[syncHistoryItem[LAST_MERGED]];
|
|
|
|
}
|
2018-06-07 23:56:11 +00:00
|
|
|
mergedContent = diffUtils.mergeContent(serverContent, clientContent, lastMergedContent);
|
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
if (!mergedContent) {
|
|
|
|
return;
|
2017-11-15 08:12:56 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
|
|
|
|
// Update or set content in store
|
|
|
|
store.commit('content/setItem', {
|
2018-06-07 23:56:11 +00:00
|
|
|
id: contentId,
|
2018-05-13 13:27:33 +00:00
|
|
|
text: utils.sanitizeText(mergedContent.text),
|
|
|
|
properties: utils.sanitizeText(mergedContent.properties),
|
|
|
|
discussions: mergedContent.discussions,
|
|
|
|
comments: mergedContent.comments,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Retrieve content with its new hash value and freeze it
|
|
|
|
mergedContent = utils.deepCopy(getContent());
|
|
|
|
|
|
|
|
// Make merged content history
|
|
|
|
const mergedContentHistory = serverContent ? serverContent.history.slice() : [];
|
|
|
|
let skipUpload = true;
|
|
|
|
if (mergedContentHistory[0] !== mergedContent.hash) {
|
|
|
|
// Put merged content hash at the beginning of history
|
|
|
|
mergedContentHistory.unshift(mergedContent.hash);
|
|
|
|
// Server content is either out of sync or its history is incomplete, do upload
|
|
|
|
skipUpload = false;
|
2017-11-15 08:12:56 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
if (syncHistoryItem
|
|
|
|
&& syncHistoryItem[LAST_SENT] != null
|
|
|
|
&& syncHistoryItem[LAST_SENT] !== mergedContent.hash
|
|
|
|
) {
|
|
|
|
// Clean up by removing the hash we've previously added
|
|
|
|
const idx = mergedContentHistory.lastIndexOf(syncHistoryItem[LAST_SENT]);
|
|
|
|
if (idx !== -1) {
|
|
|
|
mergedContentHistory.splice(idx, 1);
|
|
|
|
}
|
2018-03-12 00:45:54 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
|
2018-06-07 23:56:11 +00:00
|
|
|
// Update synced content
|
2018-05-13 13:27:33 +00:00
|
|
|
const newSyncedContent = utils.deepCopy(syncedContent);
|
|
|
|
const newSyncHistoryItem = newSyncedContent.syncHistory[syncLocation.id] || [];
|
|
|
|
newSyncedContent.syncHistory[syncLocation.id] = newSyncHistoryItem;
|
|
|
|
if (serverContent &&
|
|
|
|
(serverContent.hash === newSyncHistoryItem[LAST_SEEN] ||
|
|
|
|
serverContent.history.indexOf(newSyncHistoryItem[LAST_SEEN]) !== -1)
|
|
|
|
) {
|
|
|
|
// That's the 2nd time we've seen this content, trust it for future merges
|
|
|
|
newSyncHistoryItem[LAST_MERGED] = newSyncHistoryItem[LAST_SEEN];
|
|
|
|
}
|
|
|
|
newSyncHistoryItem[LAST_MERGED] = newSyncHistoryItem[LAST_MERGED] || null;
|
|
|
|
newSyncHistoryItem[LAST_SEEN] = mergedContent.hash;
|
|
|
|
newSyncHistoryItem[LAST_SENT] = skipUpload ? null : mergedContent.hash;
|
|
|
|
newSyncedContent.historyData[mergedContent.hash] = mergedContent;
|
|
|
|
|
|
|
|
// Clean synced content from unused revisions
|
|
|
|
cleanSyncedContent(newSyncedContent);
|
|
|
|
// Store synced content
|
|
|
|
store.commit('syncedContent/patchItem', newSyncedContent);
|
|
|
|
|
|
|
|
if (skipUpload) {
|
|
|
|
// Server content and merged content are equal, skip content upload
|
|
|
|
return;
|
2018-03-12 00:45:54 +00:00
|
|
|
}
|
2017-11-04 16:59:48 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
// Upload merged content
|
2018-06-07 23:56:11 +00:00
|
|
|
const item = {
|
2018-05-13 13:27:33 +00:00
|
|
|
...mergedContent,
|
|
|
|
history: mergedContentHistory.slice(0, maxContentHistory),
|
2018-06-07 23:56:11 +00:00
|
|
|
};
|
|
|
|
const syncLocationToStore = await uploadContent(
|
|
|
|
item,
|
|
|
|
tooLateChecker(restartContentSyncAfter),
|
|
|
|
);
|
2018-05-13 13:27:33 +00:00
|
|
|
|
|
|
|
// Replace sync location if modified
|
|
|
|
if (utils.serializeObject(syncLocation) !==
|
|
|
|
utils.serializeObject(syncLocationToStore)
|
|
|
|
) {
|
|
|
|
store.commit('syncLocation/patchItem', syncLocationToStore);
|
|
|
|
}
|
2018-03-12 00:45:54 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
// If content was just created, restart sync to create the file as well
|
|
|
|
if (provider === workspaceProvider &&
|
|
|
|
!store.getters['data/syncDataByItemId'][fileId]
|
|
|
|
) {
|
|
|
|
syncContext.restart = true;
|
2017-08-25 10:37:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
await store.dispatch('queue/doWithLocation', {
|
|
|
|
location: syncLocation,
|
|
|
|
action: async () => {
|
|
|
|
try {
|
|
|
|
await doSyncLocation();
|
|
|
|
} catch (err) {
|
|
|
|
if (store.state.offline || (err && err.message === 'TOO_LATE')) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
console.error(err); // eslint-disable-line no-console
|
|
|
|
store.dispatch('notification/error', err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-11-04 16:59:48 +00:00
|
|
|
});
|
2018-05-13 13:27:33 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err && err.message === 'TOO_LATE') {
|
|
|
|
// Restart sync
|
|
|
|
await syncFile(fileId, syncContext);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
await localDbSvc.unloadContents();
|
|
|
|
}
|
|
|
|
};
|
2017-08-25 10:37:46 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
2017-12-17 15:08:52 +00:00
|
|
|
* Sync a data item, typically settings, workspaces and templates.
|
2017-12-10 23:49:20 +00:00
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncDataItem = async (dataId) => {
|
2017-12-17 15:08:52 +00:00
|
|
|
const getItem = () => store.state.data.itemMap[dataId]
|
|
|
|
|| store.state.data.lsItemMap[dataId];
|
|
|
|
|
2018-06-07 23:56:11 +00:00
|
|
|
const oldItem = getItem();
|
|
|
|
const oldSyncData = store.getters['data/syncDataByItemId'][dataId];
|
2018-05-13 13:27:33 +00:00
|
|
|
// Sync if item hash and syncData hash are out of sync
|
2018-06-07 23:56:11 +00:00
|
|
|
if (oldSyncData && oldItem && oldItem.hash === oldSyncData.hash) {
|
2018-05-13 13:27:33 +00:00
|
|
|
return;
|
2017-09-26 22:54:26 +00:00
|
|
|
}
|
2017-12-17 15:08:52 +00:00
|
|
|
|
2018-06-07 23:56:11 +00:00
|
|
|
const token = workspaceProvider.getToken();
|
|
|
|
const { item, syncData } = await workspaceProvider.downloadWorkspaceData(
|
|
|
|
token,
|
|
|
|
dataId,
|
|
|
|
oldSyncData,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update sync data if changed
|
|
|
|
if (syncData
|
|
|
|
&& utils.serializeObject(oldSyncData) !== utils.serializeObject(syncData)
|
|
|
|
) {
|
|
|
|
store.dispatch('data/patchSyncData', {
|
|
|
|
[syncData.id]: syncData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverItem = item;
|
2018-05-13 13:27:33 +00:00
|
|
|
const dataSyncData = store.getters['data/dataSyncData'][dataId];
|
|
|
|
let mergedItem = (() => {
|
|
|
|
const clientItem = utils.deepCopy(getItem());
|
|
|
|
if (!clientItem) {
|
|
|
|
return serverItem;
|
|
|
|
}
|
|
|
|
if (!serverItem) {
|
|
|
|
return clientItem;
|
|
|
|
}
|
|
|
|
if (!dataSyncData) {
|
|
|
|
return serverItem;
|
|
|
|
}
|
|
|
|
if (dataSyncData.hash !== serverItem.hash) {
|
|
|
|
// Server version has changed
|
|
|
|
if (dataSyncData.hash !== clientItem.hash && typeof clientItem.data === 'object') {
|
|
|
|
// Client version has changed as well, merge data objects
|
|
|
|
return {
|
|
|
|
...clientItem,
|
|
|
|
data: diffUtils.mergeObjects(serverItem.data, clientItem.data),
|
|
|
|
};
|
2017-09-27 20:27:12 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
return serverItem;
|
|
|
|
}
|
|
|
|
return clientItem;
|
|
|
|
})();
|
2017-09-27 20:27:12 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
if (!mergedItem) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
// Update item in store
|
|
|
|
store.commit('data/setItem', {
|
|
|
|
id: dataId,
|
|
|
|
...mergedItem,
|
|
|
|
});
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
// Retrieve item with new `hash` and freeze it
|
|
|
|
mergedItem = utils.deepCopy(getItem());
|
|
|
|
|
|
|
|
if (serverItem && serverItem.hash === mergedItem.hash) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-07 23:56:11 +00:00
|
|
|
|
|
|
|
// Upload merged data item
|
|
|
|
const newSyncData = await workspaceProvider.uploadWorkspaceData(
|
|
|
|
token,
|
|
|
|
mergedItem,
|
|
|
|
syncData,
|
|
|
|
tooLateChecker(restartContentSyncAfter),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update sync data if changed
|
|
|
|
if (newSyncData
|
|
|
|
&& utils.serializeObject(syncData) !== utils.serializeObject(newSyncData)
|
|
|
|
) {
|
|
|
|
store.dispatch('data/patchSyncData', {
|
|
|
|
[newSyncData.id]: newSyncData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update data sync data
|
2018-05-13 13:27:33 +00:00
|
|
|
store.dispatch('data/patchDataSyncData', {
|
|
|
|
[dataId]: utils.deepCopy(store.getters['data/syncDataByItemId'][dataId]),
|
|
|
|
});
|
|
|
|
};
|
2017-09-26 22:54:26 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Sync the whole workspace with the main provider and the current file explicit locations.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncWorkspace = async () => {
|
|
|
|
try {
|
|
|
|
const workspace = store.getters['workspace/currentWorkspace'];
|
|
|
|
const syncContext = new SyncContext();
|
|
|
|
|
|
|
|
// Store the sub in the DB since it's not safely stored in the token
|
|
|
|
const syncToken = store.getters['workspace/syncToken'];
|
|
|
|
const localSettings = store.getters['data/localSettings'];
|
|
|
|
if (!localSettings.syncSub) {
|
|
|
|
store.dispatch('data/patchLocalSettings', {
|
|
|
|
syncSub: syncToken.sub,
|
|
|
|
});
|
|
|
|
} else if (localSettings.syncSub !== syncToken.sub) {
|
|
|
|
throw new Error('Synchronization failed due to token inconsistency.');
|
|
|
|
}
|
2017-08-17 23:10:35 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
const changes = await workspaceProvider.getChanges();
|
|
|
|
// Apply changes
|
|
|
|
applyChanges(changes);
|
|
|
|
if (workspaceProvider.onChangesApplied) {
|
|
|
|
workspaceProvider.onChangesApplied();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent from sending items too long after changes have been retrieved
|
|
|
|
const ifNotTooLate = tooLateChecker(restartSyncAfter);
|
|
|
|
|
|
|
|
// Called until no item to save
|
2018-06-07 23:56:11 +00:00
|
|
|
const saveNextItem = () => ifNotTooLate(async () => {
|
2018-05-13 13:27:33 +00:00
|
|
|
const storeItemMap = {
|
|
|
|
...store.state.file.itemMap,
|
|
|
|
...store.state.folder.itemMap,
|
|
|
|
...store.state.syncLocation.itemMap,
|
|
|
|
...store.state.publishLocation.itemMap,
|
|
|
|
// Deal with contents and data later
|
|
|
|
};
|
2018-06-07 23:56:11 +00:00
|
|
|
|
|
|
|
let getSyncData;
|
|
|
|
if (workspaceProvider.isGit) {
|
|
|
|
const syncData = store.getters['data/syncData'];
|
|
|
|
getSyncData = id => syncData[store.getters.itemGitPaths[id]];
|
|
|
|
} else {
|
|
|
|
const syncDataByItemId = store.getters['data/syncDataByItemId'];
|
|
|
|
getSyncData = id => syncDataByItemId[id];
|
|
|
|
}
|
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
const [changedItem, syncDataToUpdate] = utils.someResult(
|
|
|
|
Object.entries(storeItemMap),
|
|
|
|
([id, item]) => {
|
2018-06-07 23:56:11 +00:00
|
|
|
const existingSyncData = getSyncData(id);
|
2017-12-23 18:25:14 +00:00
|
|
|
if ((!existingSyncData || existingSyncData.hash !== item.hash)
|
|
|
|
// Add file/folder if parent has been added
|
2018-06-07 23:56:11 +00:00
|
|
|
&& (!storeItemMap[item.parentId] || getSyncData(item.parentId))
|
2017-12-23 18:25:14 +00:00
|
|
|
// Add file if content has been added
|
2018-06-07 23:56:11 +00:00
|
|
|
&& (item.type !== 'file' || getSyncData(`${id}/content`))
|
2017-09-27 20:27:12 +00:00
|
|
|
) {
|
2018-05-13 13:27:33 +00:00
|
|
|
return [item, existingSyncData];
|
2017-08-17 23:10:35 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
) || [];
|
2017-08-17 23:10:35 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
if (changedItem) {
|
|
|
|
const resultSyncData = await workspaceProvider
|
2018-06-07 23:56:11 +00:00
|
|
|
.saveWorkspaceItem(
|
2017-08-17 23:10:35 +00:00
|
|
|
// Use deepCopy to freeze objects
|
2018-05-13 13:27:33 +00:00
|
|
|
utils.deepCopy(changedItem),
|
|
|
|
utils.deepCopy(syncDataToUpdate),
|
|
|
|
ifNotTooLate,
|
|
|
|
);
|
|
|
|
store.dispatch('data/patchSyncData', {
|
|
|
|
[resultSyncData.id]: resultSyncData,
|
2017-08-25 10:37:46 +00:00
|
|
|
});
|
2018-05-13 13:27:33 +00:00
|
|
|
await saveNextItem();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await saveNextItem();
|
|
|
|
|
|
|
|
// Called until no item to remove
|
2018-06-07 23:56:11 +00:00
|
|
|
const removeNextItem = () => ifNotTooLate(async () => {
|
|
|
|
let getItem;
|
|
|
|
let getFileItem;
|
|
|
|
if (workspaceProvider.isGit) {
|
|
|
|
const { gitPathItems } = store.getters;
|
|
|
|
getItem = syncData => gitPathItems[syncData.id];
|
|
|
|
getFileItem = syncData => gitPathItems[syncData.id.slice(1)]; // Remove leading /
|
|
|
|
} else {
|
|
|
|
const { allItemMap } = store.getters;
|
|
|
|
getItem = syncData => allItemMap[syncData.itemId];
|
|
|
|
getFileItem = syncData => allItemMap[syncData.itemId.split('/')[0]];
|
|
|
|
}
|
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncData = store.getters['data/syncData'];
|
|
|
|
const syncDataToRemove = utils.deepCopy(utils.someResult(
|
|
|
|
Object.values(syncData),
|
2018-06-07 23:56:11 +00:00
|
|
|
(existingSyncData) => {
|
|
|
|
if (!getItem(existingSyncData)
|
|
|
|
// We don't want to delete data items, especially on first sync
|
|
|
|
&& existingSyncData.type !== 'data'
|
|
|
|
// Remove content only if file has been removed
|
|
|
|
&& (existingSyncData.type !== 'content'
|
|
|
|
|| !getFileItem(existingSyncData))
|
|
|
|
) {
|
|
|
|
return existingSyncData;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2018-05-13 13:27:33 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if (syncDataToRemove) {
|
|
|
|
// Use deepCopy to freeze objects
|
2018-06-07 23:56:11 +00:00
|
|
|
await workspaceProvider.removeWorkspaceItem(syncDataToRemove, ifNotTooLate);
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncDataCopy = { ...store.getters['data/syncData'] };
|
|
|
|
delete syncDataCopy[syncDataToRemove.id];
|
|
|
|
store.dispatch('data/setSyncData', syncDataCopy);
|
|
|
|
await removeNextItem();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await removeNextItem();
|
2017-08-25 10:37:46 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
// Sync settings and workspaces only in the main workspace
|
|
|
|
if (workspace.id === 'main') {
|
|
|
|
await syncDataItem('settings');
|
|
|
|
await syncDataItem('workspaces');
|
|
|
|
}
|
|
|
|
await syncDataItem('templates');
|
|
|
|
|
|
|
|
const getOneFileIdToSync = () => {
|
|
|
|
const contentIds = [...new Set([
|
|
|
|
...Object.keys(localDbSvc.hashMap.content),
|
|
|
|
...store.getters['file/items'].map(file => `${file.id}/content`),
|
|
|
|
])];
|
2018-06-07 23:56:11 +00:00
|
|
|
const contentMap = store.state.content.itemMap;
|
|
|
|
const syncDataById = store.getters['data/syncData'];
|
|
|
|
let getSyncData;
|
|
|
|
if (workspaceProvider.isGit) {
|
|
|
|
const { itemGitPaths } = store.getters;
|
|
|
|
getSyncData = contentId => syncDataById[itemGitPaths[contentId]];
|
|
|
|
} else {
|
|
|
|
const syncDataByItemId = store.getters['data/syncDataByItemId'];
|
|
|
|
getSyncData = contentId => syncDataByItemId[contentId];
|
|
|
|
}
|
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
return utils.someResult(contentIds, (contentId) => {
|
|
|
|
// Get content hash from itemMap or from localDbSvc if not loaded
|
2018-06-07 23:56:11 +00:00
|
|
|
const loadedContent = contentMap[contentId];
|
2018-05-13 13:27:33 +00:00
|
|
|
const hash = loadedContent ? loadedContent.hash : localDbSvc.hashMap.content[contentId];
|
2018-06-07 23:56:11 +00:00
|
|
|
const syncData = getSyncData(contentId);
|
2018-05-13 13:27:33 +00:00
|
|
|
if (
|
|
|
|
// Sync if content syncing was not attempted yet
|
|
|
|
!syncContext.attempted[contentId] &&
|
|
|
|
// And if syncData does not exist or if content hash and syncData hash are inconsistent
|
|
|
|
(!syncData || syncData.hash !== hash)
|
|
|
|
) {
|
|
|
|
const [fileId] = contentId.split('/');
|
|
|
|
return fileId;
|
2017-09-27 20:27:12 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
};
|
2017-08-17 23:10:35 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
const syncNextFile = async () => {
|
|
|
|
const fileId = getOneFileIdToSync();
|
|
|
|
if (fileId) {
|
|
|
|
await syncFile(fileId, syncContext);
|
|
|
|
await syncNextFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const currentFileId = store.getters['file/current'].id;
|
|
|
|
if (currentFileId) {
|
|
|
|
// Sync current file first
|
|
|
|
await syncFile(currentFileId, syncContext);
|
|
|
|
}
|
|
|
|
await syncNextFile();
|
|
|
|
|
|
|
|
if (syncContext.restart) {
|
|
|
|
// Restart sync
|
|
|
|
await syncWorkspace();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err && err.message === 'TOO_LATE') {
|
|
|
|
// Restart sync
|
|
|
|
await syncWorkspace();
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (workspaceProvider.onSyncEnd) {
|
|
|
|
workspaceProvider.onSyncEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
/**
|
|
|
|
* Enqueue a sync task, if possible.
|
|
|
|
*/
|
2018-05-13 13:27:33 +00:00
|
|
|
const requestSync = () => {
|
2018-03-12 00:45:54 +00:00
|
|
|
// No sync in light mode
|
|
|
|
if (store.state.light) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
store.dispatch('queue/enqueueSyncRequest', async () => {
|
2017-08-15 10:43:26 +00:00
|
|
|
let intervalId;
|
2018-05-13 13:27:33 +00:00
|
|
|
const attempt = async () => {
|
2017-08-15 10:43:26 +00:00
|
|
|
// Only start syncing when these conditions are met
|
2017-12-11 00:53:46 +00:00
|
|
|
if (networkSvc.isUserActive() && isSyncWindow()) {
|
2017-08-15 10:43:26 +00:00
|
|
|
clearInterval(intervalId);
|
2017-08-25 10:37:46 +00:00
|
|
|
if (!isSyncPossible()) {
|
2017-08-15 10:43:26 +00:00
|
|
|
// Cancel sync
|
2018-05-13 13:27:33 +00:00
|
|
|
throw new Error('Sync not possible.');
|
2017-08-15 10:43:26 +00:00
|
|
|
}
|
2017-08-25 10:37:46 +00:00
|
|
|
|
2017-10-07 11:22:24 +00:00
|
|
|
// Determine if we have to clean files
|
|
|
|
const fileHashesToClean = {};
|
|
|
|
if (getLastStoredSyncActivity() + utils.cleanTrashAfter < Date.now()) {
|
|
|
|
// Last synchronization happened 7 days ago
|
|
|
|
const syncDataByItemId = store.getters['data/syncDataByItemId'];
|
|
|
|
store.getters['file/items'].forEach((file) => {
|
|
|
|
// If file is in the trash and has not been modified since it was last synced
|
|
|
|
const syncData = syncDataByItemId[file.id];
|
|
|
|
if (syncData && file.parentId === 'trash' && file.hash === syncData.hash) {
|
|
|
|
fileHashesToClean[file.id] = file.hash;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-25 10:37:46 +00:00
|
|
|
// Call setLastSyncActivity periodically
|
|
|
|
intervalId = utils.setInterval(() => setLastSyncActivity(), 1000);
|
|
|
|
setLastSyncActivity();
|
2017-10-07 11:22:24 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
try {
|
|
|
|
if (isWorkspaceSyncPossible()) {
|
|
|
|
await syncWorkspace();
|
|
|
|
} else if (hasCurrentFileSyncLocations()) {
|
|
|
|
// Only sync current file if workspace sync is unavailable.
|
|
|
|
// We could sync all files that are out-of-sync but it would
|
|
|
|
// require to load all the syncedContent objects from the DB.
|
|
|
|
await syncFile(store.getters['file/current'].id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean files
|
|
|
|
Object.entries(fileHashesToClean).forEach(([fileId, fileHash]) => {
|
|
|
|
const file = store.state.file.itemMap[fileId];
|
|
|
|
if (file && file.hash === fileHash) {
|
|
|
|
fileSvc.deleteFile(fileId);
|
2017-08-25 10:37:46 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
clearInterval(intervalId);
|
|
|
|
}
|
2017-08-15 10:43:26 +00:00
|
|
|
}
|
|
|
|
};
|
2018-05-13 13:27:33 +00:00
|
|
|
|
2017-08-15 10:43:26 +00:00
|
|
|
intervalId = utils.setInterval(() => attempt(), 1000);
|
2018-06-07 23:56:11 +00:00
|
|
|
return attempt();
|
2018-05-13 13:27:33 +00:00
|
|
|
});
|
|
|
|
};
|
2017-08-15 10:43:26 +00:00
|
|
|
|
2017-12-10 23:49:20 +00:00
|
|
|
export default {
|
2018-05-13 13:27:33 +00:00
|
|
|
async init() {
|
|
|
|
// Load workspaces and tokens from localStorage
|
|
|
|
localDbSvc.syncLocalStorage();
|
|
|
|
|
|
|
|
// Try to find a suitable action provider
|
|
|
|
actionProvider = providerRegistry.providers[utils.queryParams.providerId];
|
|
|
|
if (actionProvider && actionProvider.initAction) {
|
|
|
|
await actionProvider.initAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a suitable workspace sync provider
|
|
|
|
workspaceProvider = providerRegistry.providers[utils.queryParams.providerId];
|
|
|
|
if (!workspaceProvider || !workspaceProvider.initWorkspace) {
|
|
|
|
workspaceProvider = googleDriveAppDataProvider;
|
|
|
|
}
|
|
|
|
const workspace = await workspaceProvider.initWorkspace();
|
|
|
|
store.dispatch('workspace/setCurrentWorkspaceId', workspace.id);
|
|
|
|
await localDbSvc.init();
|
|
|
|
|
|
|
|
// Try to find a suitable action provider
|
|
|
|
actionProvider = providerRegistry.providers[utils.queryParams.providerId] || actionProvider;
|
|
|
|
if (actionProvider && actionProvider.performAction) {
|
|
|
|
const newSyncLocation = await actionProvider.performAction();
|
|
|
|
if (newSyncLocation) {
|
|
|
|
this.createSyncLocation(newSyncLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await tempFileSvc.init();
|
|
|
|
|
|
|
|
if (!store.state.light) {
|
|
|
|
// Sync periodically
|
|
|
|
utils.setInterval(() => {
|
|
|
|
if (isSyncPossible()
|
|
|
|
&& networkSvc.isUserActive()
|
|
|
|
&& isSyncWindow()
|
|
|
|
&& isAutoSyncReady()
|
|
|
|
) {
|
|
|
|
requestSync();
|
2018-01-04 20:19:10 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
}, 1000);
|
2017-08-19 10:24:08 +00:00
|
|
|
|
2018-05-13 13:27:33 +00:00
|
|
|
// Unload contents from memory periodically
|
|
|
|
utils.setInterval(() => {
|
|
|
|
// Wait for sync and publish to finish
|
|
|
|
if (store.state.queue.isEmpty) {
|
|
|
|
localDbSvc.unloadContents();
|
2018-03-12 00:45:54 +00:00
|
|
|
}
|
2018-05-13 13:27:33 +00:00
|
|
|
}, 5000);
|
|
|
|
}
|
2017-12-10 23:49:20 +00:00
|
|
|
},
|
2017-08-25 10:37:46 +00:00
|
|
|
isSyncPossible,
|
2017-08-15 10:43:26 +00:00
|
|
|
requestSync,
|
2017-09-23 19:01:50 +00:00
|
|
|
createSyncLocation,
|
2017-08-15 10:43:26 +00:00
|
|
|
};
|