cryptpad/www/common/cryptpad-common.js

912 lines
31 KiB
JavaScript
Raw Normal View History

2016-05-28 11:13:54 +00:00
define([
'/api/config',
2017-04-13 10:18:08 +00:00
'/customize/messages.js',
'/common/common-util.js',
2017-04-13 15:04:15 +00:00
'/common/common-hash.js',
2017-07-05 16:42:32 +00:00
'/common/common-messaging.js',
2017-11-21 15:46:19 +00:00
'/common/common-constants.js',
2017-11-23 11:28:49 +00:00
'/common/common-feedback.js',
'/common/outer/local-store.js',
2017-11-30 09:33:09 +00:00
'/common/outer/store-rpc.js',
2017-07-05 09:57:53 +00:00
'/customize/application_config.js',
2017-09-14 09:23:37 +00:00
'/bower_components/nthen/index.js',
2017-11-30 16:21:58 +00:00
], function (Config, Messages, Util, Hash,
2017-12-12 12:47:24 +00:00
Messaging, Constants, Feedback, LocalStore, AStore,
AppConfig, Nthen) {
/* This file exposes functionality which is specific to Cryptpad, but not to
any particular pad type. This includes functions for committing metadata
about pads to your local storage for future use and improved usability.
Additionally, there is some basic functionality for import/export.
*/
2017-11-30 09:33:09 +00:00
var postMessage = function (cmd, data, cb) {
setTimeout(function () {
AStore.query(cmd, data, cb);
});
};
var tryParsing = function (x) {
try { return JSON.parse(x); }
catch (e) {
console.error(e);
return null;
}
};
2017-06-09 08:46:11 +00:00
var origin = encodeURIComponent(window.location.hostname);
2016-12-21 17:06:05 +00:00
var common = window.Cryptpad = {
2016-10-24 09:39:28 +00:00
Messages: Messages,
2017-06-09 08:46:11 +00:00
donateURL: 'https://accounts.cryptpad.fr/#/donate?on=' + origin,
upgradeURL: 'https://accounts.cryptpad.fr/#/?on=' + origin,
account: {},
};
var PINNING_ENABLED = AppConfig.enablePinning;
2017-12-01 13:49:21 +00:00
// COMMON
common.getLanguage = function () {
return Messages._languageUsed;
};
common.setLanguage = function (l, cb) {
2017-12-01 15:05:20 +00:00
var LS_LANG = "CRYPTPAD_LANG";
localStorage.setItem(LS_LANG, l);
cb();
2017-12-01 13:49:21 +00:00
};
2017-11-30 09:33:09 +00:00
// RESTRICTED
// Settings only
common.resetDrive = function (cb) {
postMessage("RESET_DRIVE", null, function (obj) {
if (obj.error) { return void cb(obj.error); }
cb();
});
};
common.logoutFromAll = function (cb) {
var token = Math.floor(Math.random()*Number.MAX_SAFE_INTEGER);
localStorage.setItem(Constants.tokenKey, token);
postMessage("SET", {
key: [Constants.tokenKey],
value: token
}, function (obj) {
2017-11-30 17:22:26 +00:00
if (obj && obj.error) { return void cb(obj.error); }
2017-11-30 09:33:09 +00:00
cb();
});
};
2018-03-13 10:31:08 +00:00
// Settings and drive and auth
2017-11-30 09:33:09 +00:00
common.getUserObject = function (cb) {
postMessage("GET", [], function (obj) {
cb(obj);
});
};
// Settings and ready
common.mergeAnonDrive = function (cb) {
var data = {
anonHash: LocalStore.getFSHash()
};
postMessage("MIGRATE_ANON_DRIVE", data, cb);
};
2018-03-19 13:04:44 +00:00
// Settings
common.deleteAccount = function (cb) {
postMessage("DELETE_ACCOUNT", null, cb);
};
// Drive
common.userObjectCommand = function (data, cb) {
postMessage("DRIVE_USEROBJECT", data, cb);
};
2017-12-05 17:09:43 +00:00
common.drive = {};
common.drive.onLog = Util.mkEvent();
common.drive.onChange = Util.mkEvent();
common.drive.onRemove = Util.mkEvent();
2017-11-30 09:33:09 +00:00
// Profile
common.getProfileEditUrl = function (cb) {
postMessage("GET", ['profile', 'edit'], function (obj) {
cb(obj);
});
};
common.setNewProfile = function (profile) {
postMessage("SET", {
key: ['profile'],
value: profile
}, function () {});
};
2017-11-30 14:01:17 +00:00
common.setAvatar = function (data, cb) {
var postData = {
key: ['profile', 'avatar']
};
// If we don't have "data", it means we want to remove the avatar and we should not have a
// "postData.value", even set to undefined (JSON.stringify transforms undefined to null)
if (data) { postData.value = data; }
postMessage("SET", postData, cb);
};
// Todo
common.getTodoHash = function (cb) {
postMessage("GET", ['todo'], function (obj) {
cb(obj);
});
};
common.setTodoHash = function (hash) {
postMessage("SET", {
key: ['todo'],
value: hash
}, function () {});
};
2017-11-30 09:33:09 +00:00
2017-12-01 13:49:21 +00:00
// RPC
common.pinPads = function (pads, cb) {
postMessage("PIN_PADS", pads, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj.hash);
});
};
2017-12-01 13:49:21 +00:00
common.unpinPads = function (pads, cb) {
postMessage("UNPIN_PADS", pads, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj.hash);
});
};
common.getPinnedUsage = function (cb) {
postMessage("GET_PINNED_USAGE", null, function (obj) {
if (obj.error) { return void cb(obj.error); }
cb(null, obj.bytes);
});
};
common.updatePinLimit = function (cb) {
postMessage("UPDATE_PIN_LIMIT", null, function (obj) {
if (obj.error) { return void cb(obj.error); }
cb(undefined, obj.limit, obj.plan, obj.note);
});
};
common.getPinLimit = function (cb) {
postMessage("GET_PIN_LIMIT", null, function (obj) {
if (obj.error) { return void cb(obj.error); }
cb(undefined, obj.limit, obj.plan, obj.note);
});
};
2017-11-13 15:32:40 +00:00
2017-12-01 13:49:21 +00:00
common.isOverPinLimit = function (cb) {
if (!LocalStore.isLoggedIn()) { return void cb(null, false); }
var usage;
var andThen = function (e, limit, plan) {
if (e) { return void cb(e); }
var data = {usage: usage, limit: limit, plan: plan};
if (usage > limit) {
return void cb (null, true, data);
}
return void cb (null, false, data);
};
var todo = function (e, used) {
if (e) { return void cb(e); }
usage = used;
common.getPinLimit(andThen);
};
common.getPinnedUsage(todo);
};
common.clearOwnedChannel = function (channel, cb) {
postMessage("CLEAR_OWNED_CHANNEL", channel, cb);
};
common.removeOwnedChannel = function (channel, cb) {
postMessage("REMOVE_OWNED_CHANNEL", channel, cb);
};
2017-12-01 13:49:21 +00:00
2018-01-25 16:54:21 +00:00
common.getDeletedPads = function (cb) {
postMessage("GET_DELETED_PADS", null, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
common.uploadComplete = function (id, cb) {
postMessage("UPLOAD_COMPLETE", id, function (obj) {
2017-12-01 13:49:21 +00:00
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
common.uploadStatus = function (size, cb) {
postMessage("UPLOAD_STATUS", {size: size}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
common.uploadCancel = function (size, cb) {
postMessage("UPLOAD_CANCEL", {size: size}, function (obj) {
2017-12-01 13:49:21 +00:00
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
common.uploadChunk = function (data, cb) {
postMessage("UPLOAD_CHUNK", {chunk: data}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
// ANON RPC
// SFRAME: talk to anon_rpc from the iframe
common.anonRpcMsg = function (msg, data, cb) {
if (!msg) { return; }
postMessage("ANON_RPC_MESSAGE", {
msg: msg,
data: data
}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
common.getFileSize = function (href, password, cb) {
postMessage("GET_FILE_SIZE", {href: href, password: password}, function (obj) {
2017-12-01 13:49:21 +00:00
if (obj && obj.error) { return void cb(obj.error); }
cb(undefined, obj.size);
});
};
common.getMultipleFileSize = function (files, cb) {
postMessage("GET_MULTIPLE_FILE_SIZE", {files:files}, function (obj) {
if (obj.error) { return void cb(obj.error); }
cb(undefined, obj.size);
});
};
common.isNewChannel = function (href, password, cb) {
postMessage('IS_NEW_CHANNEL', {href: href, password: password}, function (obj) {
if (obj.error) { return void cb(obj.error); }
if (!obj) { return void cb('INVALID_RESPONSE'); }
cb(undefined, obj.isNew);
});
};
2017-12-01 13:49:21 +00:00
// Store
2017-11-30 09:33:09 +00:00
common.getMetadata = function (cb) {
postMessage("GET_METADATA", null, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
2017-11-30 09:33:09 +00:00
cb(null, obj);
});
};
2017-11-30 09:33:09 +00:00
common.setDisplayName = function (value, cb) {
postMessage("SET_DISPLAY_NAME", value, cb);
};
2017-10-09 09:52:34 +00:00
common.setPadAttribute = function (attr, value, cb, href) {
cb = cb || function () {};
2017-11-13 15:32:40 +00:00
href = Hash.getRelativeHref(href || window.location.href);
2017-11-30 09:33:09 +00:00
postMessage("SET_PAD_ATTRIBUTE", {
href: href,
attr: attr,
value: value
}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb();
2016-09-22 15:12:46 +00:00
});
};
common.getPadAttribute = function (attr, cb, href) {
href = Hash.getRelativeHref(href || window.location.href);
2017-11-30 09:33:09 +00:00
postMessage("GET_PAD_ATTRIBUTE", {
href: href,
attr: attr,
}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
};
common.setAttribute = function (attr, value, cb) {
cb = cb || function () {};
2017-11-30 09:33:09 +00:00
postMessage("SET_ATTRIBUTE", {
attr: attr,
value: value
}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb();
});
2016-07-22 10:24:54 +00:00
};
2017-05-04 14:16:09 +00:00
common.getAttribute = function (attr, cb) {
2017-11-30 09:33:09 +00:00
postMessage("GET_ATTRIBUTE", {
attr: attr
}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
2016-09-22 15:12:46 +00:00
});
};
2017-08-31 16:06:26 +00:00
2017-11-30 09:33:09 +00:00
// Tags
2017-09-19 13:30:08 +00:00
common.resetTags = function (href, tags, cb) {
2017-11-30 09:33:09 +00:00
// set pad attribute
2017-11-30 14:01:17 +00:00
cb = cb || function () {};
2017-09-19 13:30:08 +00:00
if (!Array.isArray(tags)) { return void cb('INVALID_TAGS'); }
2017-11-30 09:33:09 +00:00
common.setPadAttribute('tags', tags.slice(), cb, href);
2017-09-19 13:30:08 +00:00
};
2017-08-31 16:06:26 +00:00
common.tagPad = function (href, tag, cb) {
if (typeof(cb) !== 'function') {
return void console.error('EXPECTED_CALLBACK');
}
if (typeof(tag) !== 'string') { return void cb('INVALID_TAG'); }
2017-11-30 09:33:09 +00:00
common.getPadAttribute('tags', function (e, tags) {
2017-08-31 16:06:26 +00:00
if (e) { return void cb(e); }
2017-11-30 09:33:09 +00:00
var newTags;
if (!tags) {
newTags = [tag];
} else if (tags.indexOf(tag) === -1) {
newTags = tags.slice();
newTags.push(tag);
2017-08-31 16:06:26 +00:00
}
2017-11-30 09:33:09 +00:00
common.setPadAttribute('tags', newTags, cb, href);
}, href);
2017-08-31 16:06:26 +00:00
};
common.untagPad = function (href, tag, cb) {
if (typeof(cb) !== 'function') {
return void console.error('EXPECTED_CALLBACK');
}
if (typeof(tag) !== 'string') { return void cb('INVALID_TAG'); }
2017-11-30 09:33:09 +00:00
common.getPadAttribute('tags', function (e, tags) {
2017-08-31 16:06:26 +00:00
if (e) { return void cb(e); }
2017-11-30 09:33:09 +00:00
if (!tags) { return void cb(); }
var idx = tags.indexOf(tag);
2017-08-31 16:06:26 +00:00
if (idx === -1) { return void cb(); }
2017-11-30 09:33:09 +00:00
var newTags = tags.slice();
newTags.splice(idx, 1);
common.setPadAttribute('tags', newTags, cb, href);
}, href);
2017-08-31 16:06:26 +00:00
};
common.getPadTags = function (href, cb) {
if (typeof(cb) !== 'function') { return; }
2017-11-30 09:33:09 +00:00
common.getPadAttribute('tags', function (e, tags) {
if (e) { return void cb(e); }
cb(void 0, tags ? tags.slice() : []);
}, href);
2017-08-31 16:06:26 +00:00
};
2017-09-05 14:52:22 +00:00
common.listAllTags = function (cb) {
2017-11-30 09:33:09 +00:00
postMessage("LIST_ALL_TAGS", null, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(void 0, obj);
2017-09-05 14:52:22 +00:00
});
};
// STORAGE - TEMPLATES
2017-11-30 09:33:09 +00:00
common.listTemplates = function (type, cb) {
postMessage("GET_TEMPLATES", null, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
if (!Array.isArray(obj)) { return void cb ('NOT_AN_ARRAY'); }
2017-11-30 16:21:58 +00:00
if (!type) { return void cb(null, obj); }
2017-11-30 09:33:09 +00:00
var templates = obj.filter(function (f) {
var parsed = Hash.parsePadUrl(f.href);
return parsed.type === type;
});
2017-11-30 16:21:58 +00:00
cb(null, templates);
});
};
2017-11-30 09:33:09 +00:00
common.saveAsTemplate = function (Cryptput, data, cb) {
var p = Hash.parsePadUrl(window.location.href);
if (!p.type) { return; }
2018-04-27 15:23:23 +00:00
// PPP: password for the new template?
var hash = Hash.createRandomHash(p.type);
2017-11-30 09:33:09 +00:00
var href = '/' + p.type + '/#' + hash;
2018-04-27 15:23:23 +00:00
// PPP: add password as cryptput option
2017-11-30 09:33:09 +00:00
Cryptput(hash, data.toSave, function (e) {
if (e) { throw new Error(e); }
postMessage("ADD_PAD", {
href: href,
2017-11-30 16:21:58 +00:00
title: data.title,
path: ['template']
2017-11-30 09:33:09 +00:00
}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb();
});
2017-06-08 15:52:00 +00:00
});
};
2017-11-30 09:33:09 +00:00
common.isTemplate = function (href, cb) {
2017-11-13 15:32:40 +00:00
var rhref = Hash.getRelativeHref(href);
2017-11-30 16:21:58 +00:00
common.listTemplates(null, function (err, templates) {
2017-11-30 09:33:09 +00:00
cb(void 0, templates.some(function (t) {
return t.href === rhref;
}));
});
};
common.useTemplate = function (href, Crypt, cb, optsPut) {
2017-12-07 17:51:50 +00:00
// opts is used to overrides options for chainpad-netflux in cryptput
// it allows us to add owners and expiration time if it is a new file
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(href);
var parsed2 = Hash.parsePadUrl(window.location.href);
2017-09-05 09:35:15 +00:00
if(!parsed) { throw new Error("Cannot get template hash"); }
2018-04-13 16:52:55 +00:00
postMessage("INCREMENT_TEMPLATE_USE", href);
optsPut = optsPut || {};
var optsGet = {};
Nthen(function (waitFor) {
if (parsed.hashData && parsed.hashData.password) {
common.getPadAttribute('password', waitFor(function (err, password) {
optsGet.password = password;
}), href);
}
if (parsed2.hashData && parsed2.hashData.password && !optsPut.password) {
common.getPadAttribute('password', waitFor(function (err, password) {
optsPut.password = password;
}));
}
}).nThen(function () {
Crypt.get(parsed.hash, function (err, val) {
if (err) { throw new Error(err); }
Crypt.put(parsed2.hash, val, cb, optsPut);
}, optsGet);
2017-09-05 09:35:15 +00:00
});
};
2016-07-22 10:24:54 +00:00
2017-11-30 09:33:09 +00:00
// Forget button
common.moveToTrash = function (cb, href) {
href = href || window.location.href;
postMessage("MOVE_TO_TRASH", { href: href }, cb);
};
2017-11-30 09:33:09 +00:00
// When opening a new pad or renaming it, store the new title
2018-04-27 09:54:23 +00:00
common.setPadTitle = function (data, cb) {
if (!data || typeof (data) !== "object") { return cb ('Data is not an object'); }
2018-04-27 09:54:23 +00:00
var href = data.href || window.location.href;
2017-11-13 15:32:40 +00:00
var parsed = Hash.parsePadUrl(href);
2018-04-27 09:54:23 +00:00
if (!parsed.hash) { return cb ('Invalid hash'); }
data.href = parsed.getUrl({present: parsed.present});
2016-07-11 15:36:53 +00:00
2018-04-27 09:54:23 +00:00
if (typeof (data.title) !== "string") { return cb('Missing title'); }
if (data.title.trim() === "") { data.title = Hash.getDefaultName(parsed); }
2016-10-21 16:16:27 +00:00
2018-04-27 09:54:23 +00:00
postMessage("SET_PAD_TITLE", data, function (obj) {
2017-11-30 09:33:09 +00:00
if (obj && obj.error) {
console.log("unable to set pad title");
2017-11-30 09:33:09 +00:00
return void cb(obj.error);
}
2017-11-30 09:33:09 +00:00
cb();
});
2016-10-21 16:16:27 +00:00
};
2017-03-15 14:55:55 +00:00
// Needed for the secure filepicker app
common.getSecureFilesList = function (query, cb) {
2017-11-30 09:33:09 +00:00
postMessage("GET_SECURE_FILES_LIST", query, function (list) {
cb(void 0, list);
});
2017-04-10 15:42:35 +00:00
};
2018-03-13 10:31:08 +00:00
// Get a template href from its id
common.getPadData = function (id, cb) {
postMessage("GET_PAD_DATA", id, function (data) {
cb(void 0, data);
});
};
2017-04-10 15:42:35 +00:00
2017-12-01 13:49:21 +00:00
// Messaging (manage friends from the userlist)
common.inviteFromUserlist = function (netfluxId, cb) {
postMessage("INVITE_FROM_USERLIST", {
netfluxId: netfluxId,
href: window.location.href
2017-11-30 09:33:09 +00:00
}, function (obj) {
2017-11-30 14:01:17 +00:00
if (obj && obj.error) { return void cb(obj.error); }
2017-12-01 13:49:21 +00:00
cb();
2017-11-30 09:33:09 +00:00
});
2017-08-21 10:01:38 +00:00
};
// Network
common.onNetworkDisconnect = Util.mkEvent();
common.onNetworkReconnect = Util.mkEvent();
2017-12-15 15:19:22 +00:00
// Messaging
var messaging = common.messaging = {};
messaging.onFriendRequest = Util.mkEvent();
messaging.onFriendComplete = Util.mkEvent();
2017-12-01 13:49:21 +00:00
// Messenger
var messenger = common.messenger = {};
messenger.getFriendList = function (cb) {
postMessage("CONTACTS_GET_FRIEND_LIST", null, cb);
};
2017-12-01 13:49:21 +00:00
messenger.getMyInfo = function (cb) {
postMessage("CONTACTS_GET_MY_INFO", null, cb);
};
2017-12-01 13:49:21 +00:00
messenger.getFriendInfo = function (curvePublic, cb) {
postMessage("CONTACTS_GET_FRIEND_INFO", curvePublic, cb);
2017-05-11 14:12:44 +00:00
};
2017-12-01 13:49:21 +00:00
messenger.removeFriend = function (curvePublic, cb) {
postMessage("CONTACTS_REMOVE_FRIEND", curvePublic, cb);
};
2017-12-01 13:49:21 +00:00
messenger.openFriendChannel = function (curvePublic, cb) {
postMessage("CONTACTS_OPEN_FRIEND_CHANNEL", curvePublic, cb);
};
2017-12-01 13:49:21 +00:00
messenger.getFriendStatus = function (curvePublic, cb) {
postMessage("CONTACTS_GET_FRIEND_STATUS", curvePublic, cb);
2017-11-30 17:22:26 +00:00
};
2017-12-01 13:49:21 +00:00
messenger.getMoreHistory = function (data, cb) {
postMessage("CONTACTS_GET_MORE_HISTORY", data, cb);
2017-07-12 16:54:08 +00:00
};
2017-12-01 13:49:21 +00:00
messenger.sendMessage = function (data, cb) {
postMessage("CONTACTS_SEND_MESSAGE", data, cb);
2017-05-18 10:36:12 +00:00
};
2017-12-01 13:49:21 +00:00
messenger.setChannelHead = function (data, cb) {
postMessage("CONTACTS_SET_CHANNEL_HEAD", data, cb);
2017-11-30 14:01:17 +00:00
};
2017-12-01 13:49:21 +00:00
messenger.onMessageEvent = Util.mkEvent();
messenger.onJoinEvent = Util.mkEvent();
messenger.onLeaveEvent = Util.mkEvent();
messenger.onUpdateEvent = Util.mkEvent();
messenger.onFriendEvent = Util.mkEvent();
messenger.onUnfriendEvent = Util.mkEvent();
2016-09-27 16:33:03 +00:00
// Pad RPC
var pad = common.padRpc = {};
pad.joinPad = function (data, cb) {
postMessage("JOIN_PAD", data, cb);
};
pad.sendPadMsg = function (data, cb) {
postMessage("SEND_PAD_MSG", data, cb);
};
pad.onReadyEvent = Util.mkEvent();
pad.onMessageEvent = Util.mkEvent();
pad.onJoinEvent = Util.mkEvent();
pad.onLeaveEvent = Util.mkEvent();
pad.onDisconnectEvent = Util.mkEvent();
2018-02-13 17:20:13 +00:00
pad.onErrorEvent = Util.mkEvent();
// Loading events
common.loading = {};
common.loading.onDriveEvent = Util.mkEvent();
common.getFullHistory = function (data, cb) {
postMessage("GET_FULL_HISTORY", data, cb);
};
2017-08-21 12:41:56 +00:00
common.getShareHashes = function (secret, cb) {
2017-11-30 14:01:17 +00:00
var hashes;
2017-08-21 12:41:56 +00:00
if (!window.location.hash) {
hashes = Hash.getHashes(secret);
2017-08-21 12:41:56 +00:00
return void cb(null, hashes);
}
2017-11-30 09:33:09 +00:00
var parsed = Hash.parsePadUrl(window.location.href);
if (!parsed.type || !parsed.hashData) { return void cb('E_INVALID_HREF'); }
hashes = Hash.getHashes(secret);
2017-11-30 09:33:09 +00:00
if (secret.version === 0) {
2017-11-30 09:33:09 +00:00
// It means we're using an old hash
hashes.editHash = window.location.hash.slice(1);
return void cb(null, hashes);
}
if (hashes.editHash) {
// no need to find stronger if we already have edit hash
return void cb(null, hashes);
}
2017-11-30 09:33:09 +00:00
postMessage("GET_STRONGER_HASH", {
href: window.location.href,
channel: secret.channel,
password: secret.password
2017-11-30 09:33:09 +00:00
}, function (hash) {
if (hash) { hashes.editHash = hash; }
cb(null, hashes);
});
};
var CRYPTPAD_VERSION = 'cryptpad-version';
var updateLocalVersion = function () {
// Check for CryptPad updates
var urlArgs = Config.requireConf ? Config.requireConf.urlArgs : null;
if (!urlArgs) { return; }
var arr = /ver=([0-9.]+)(-[0-9]*)?/.exec(urlArgs);
var ver = arr[1];
if (!ver) { return; }
var verArr = ver.split('.');
verArr[2] = 0;
if (verArr.length !== 3) { return; }
var stored = localStorage[CRYPTPAD_VERSION] || '0.0.0';
var storedArr = stored.split('.');
storedArr[2] = 0;
var shouldUpdate = parseInt(verArr[0]) > parseInt(storedArr[0]) ||
(parseInt(verArr[0]) === parseInt(storedArr[0]) &&
parseInt(verArr[1]) > parseInt(storedArr[1]));
if (!shouldUpdate) { return; }
localStorage[CRYPTPAD_VERSION] = ver;
};
2017-11-30 09:33:09 +00:00
var _onMetadataChanged = [];
common.onMetadataChanged = function (h) {
if (typeof(h) !== "function") { return; }
if (_onMetadataChanged.indexOf(h) !== -1) { return; }
_onMetadataChanged.push(h);
};
common.changeMetadata = function () {
_onMetadataChanged.forEach(function (h) { h(); });
};
var requestLogin = function () {
// log out so that you don't go into an endless loop...
LocalStore.logout();
// redirect them to log in, and come back when they're done.
sessionStorage.redirectTo = window.location.href;
window.location.href = '/login/';
};
2018-03-21 17:27:20 +00:00
common.startAccountDeletion = function (cb) {
// Logout other tabs
LocalStore.logout(null, true);
cb();
};
2017-11-30 09:33:09 +00:00
var onMessage = function (cmd, data, cb) {
cb = cb || function () {};
switch (cmd) {
case 'REQUEST_LOGIN': {
requestLogin();
break;
}
case 'UPDATE_METADATA': {
common.changeMetadata();
break;
}
case 'UPDATE_TOKEN': {
var localToken = tryParsing(localStorage.getItem(Constants.tokenKey));
if (localToken !== data.token) { requestLogin(); }
break;
}
2017-11-30 14:01:17 +00:00
case 'Q_FRIEND_REQUEST': {
2017-12-15 15:19:22 +00:00
common.messaging.onFriendRequest.fire(data, cb);
2017-11-30 14:01:17 +00:00
break;
}
case 'EV_FRIEND_COMPLETE': {
2017-12-15 15:19:22 +00:00
common.messaging.onFriendComplete.fire(data);
2017-11-30 14:01:17 +00:00
break;
}
// Network
case 'NETWORK_DISCONNECT': {
common.onNetworkDisconnect.fire(); break;
}
case 'NETWORK_RECONNECT': {
common.onNetworkReconnect.fire(data); break;
}
2017-12-01 13:49:21 +00:00
// Messenger
case 'CONTACTS_MESSAGE': {
common.messenger.onMessageEvent.fire(data); break;
}
case 'CONTACTS_JOIN': {
common.messenger.onJoinEvent.fire(data); break;
}
case 'CONTACTS_LEAVE': {
common.messenger.onLeaveEvent.fire(data); break;
}
case 'CONTACTS_UPDATE': {
common.messenger.onUpdateEvent.fire(data); break;
}
case 'CONTACTS_FRIEND': {
common.messenger.onFriendEvent.fire(data); break;
}
case 'CONTACTS_UNFRIEND': {
common.messenger.onUnfriendEvent.fire(data); break;
}
// Pad
case 'PAD_READY': {
common.padRpc.onReadyEvent.fire(); break;
}
case 'PAD_MESSAGE': {
common.padRpc.onMessageEvent.fire(data); break;
}
case 'PAD_JOIN': {
common.padRpc.onJoinEvent.fire(data); break;
}
case 'PAD_LEAVE': {
common.padRpc.onLeaveEvent.fire(data); break;
}
case 'PAD_DISCONNECT': {
common.padRpc.onDisconnectEvent.fire(data); break;
}
2018-02-13 17:20:13 +00:00
case 'PAD_ERROR': {
common.padRpc.onErrorEvent.fire(data); break;
}
// Drive
case 'DRIVE_LOG': {
2017-12-05 17:10:53 +00:00
common.drive.onLog.fire(data); break;
2017-12-05 17:09:43 +00:00
}
case 'DRIVE_CHANGE': {
2017-12-05 17:10:53 +00:00
common.drive.onChange.fire(data); break;
2017-12-05 17:09:43 +00:00
}
case 'DRIVE_REMOVE': {
2017-12-05 17:10:53 +00:00
common.drive.onRemove.fire(data); break;
}
2018-03-21 17:27:20 +00:00
// Account deletion
case 'DELETE_ACCOUNT': {
common.startAccountDeletion(cb); break;
}
// Loading
case 'LOADING_DRIVE': {
common.loading.onDriveEvent.fire(data); break;
}
2017-11-30 09:33:09 +00:00
}
};
common.ready = (function () {
var env = {};
var initialized = false;
2017-12-01 13:49:21 +00:00
return function (f, rdyCfg) {
2017-12-05 14:07:35 +00:00
rdyCfg = rdyCfg || {};
if (initialized) {
2017-09-14 09:23:37 +00:00
return void setTimeout(function () { f(void 0, env); });
}
2017-09-14 09:23:37 +00:00
var provideFeedback = function () {
if (typeof(window.Proxy) === 'undefined') {
2017-11-23 11:28:49 +00:00
Feedback.send("NO_PROXIES");
}
2017-08-29 09:49:10 +00:00
var shimPattern = /CRYPTPAD_SHIM/;
if (shimPattern.test(Array.isArray.toString())) {
2017-11-23 11:28:49 +00:00
Feedback.send("NO_ISARRAY");
}
2017-08-29 09:49:10 +00:00
if (shimPattern.test(Array.prototype.fill.toString())) {
2017-11-23 11:28:49 +00:00
Feedback.send("NO_ARRAYFILL");
2017-08-29 09:49:10 +00:00
}
if (typeof(Symbol) === 'undefined') {
2017-11-23 11:28:49 +00:00
Feedback.send('NO_SYMBOL');
}
2017-12-06 17:32:48 +00:00
if (typeof(SharedWorker) === "undefined") {
Feedback.send('NO_SHAREDWORKER');
} else {
Feedback.send('SHAREDWORKER');
2017-12-06 17:32:48 +00:00
}
if (typeof(Worker) === "undefined") {
Feedback.send('NO_WEBWORKER');
}
2017-12-21 16:30:54 +00:00
if (typeof(ServiceWorker) === "undefined") {
Feedback.send('NO_SERVICEWORKER');
}
2017-12-06 17:32:48 +00:00
2017-11-23 11:28:49 +00:00
Feedback.reportScreenDimensions();
Feedback.reportLanguage();
};
2017-11-30 09:33:09 +00:00
var initFeedback = function (feedback) {
2017-11-23 11:28:49 +00:00
// Initialize feedback
2017-11-30 09:33:09 +00:00
Feedback.init(feedback);
2017-11-23 11:28:49 +00:00
provideFeedback();
2017-09-14 09:23:37 +00:00
};
2017-06-22 15:42:24 +00:00
2017-09-14 09:23:37 +00:00
Nthen(function (waitFor) {
if (AppConfig.beforeLogin) {
AppConfig.beforeLogin(LocalStore.isLoggedIn(), waitFor());
}
}).nThen(function (waitFor) {
2017-11-30 09:33:09 +00:00
var cfg = {
query: onMessage, // TODO temporary, will be replaced by a webworker channel
userHash: LocalStore.getUserHash(),
anonHash: LocalStore.getFSHash(),
2017-12-01 13:49:21 +00:00
localToken: tryParsing(localStorage.getItem(Constants.tokenKey)),
language: common.getLanguage(),
2017-12-05 17:09:43 +00:00
messenger: rdyCfg.messenger,
driveEvents: rdyCfg.driveEvents
2017-11-30 09:33:09 +00:00
};
2017-11-30 16:21:58 +00:00
if (sessionStorage[Constants.newPadPathKey]) {
cfg.initialPath = sessionStorage[Constants.newPadPathKey];
delete sessionStorage[Constants.newPadPathKey];
}
2017-11-30 09:33:09 +00:00
AStore.query("CONNECT", cfg, waitFor(function (data) {
if (data.error) { throw new Error(data.error); }
2017-12-01 17:29:59 +00:00
if (data.state === 'ALREADY_INIT') {
data = data.returned;
}
2017-11-30 09:33:09 +00:00
if (data.anonHash && !cfg.userHash) { LocalStore.setFSHash(data.anonHash); }
2018-03-21 17:27:20 +00:00
/*if (cfg.userHash && sessionStorage) {
2017-11-30 09:33:09 +00:00
// copy User_hash into sessionStorage because cross-domain iframes
// on safari replaces localStorage with sessionStorage or something
sessionStorage.setItem(Constants.userHashKey, cfg.userHash);
2018-03-21 17:27:20 +00:00
}*/
2017-11-30 09:33:09 +00:00
if (cfg.userHash) {
var localToken = tryParsing(localStorage.getItem(Constants.tokenKey));
if (localToken === null) {
// if that number hasn't been set to localStorage, do so.
localStorage.setItem(Constants.tokenKey, data[Constants.tokenKey]);
}
}
initFeedback(data.feedback);
initialized = true;
2017-11-30 09:33:09 +00:00
}));
2017-09-14 09:23:37 +00:00
}).nThen(function (waitFor) {
// Load the new pad when the hash has changed
var oldHref = document.location.href;
2017-12-07 17:51:50 +00:00
window.onhashchange = function (ev) {
if (ev && ev.reset) { oldHref = document.location.href; return; }
2017-09-14 09:23:37 +00:00
var newHref = document.location.href;
2018-04-27 15:23:23 +00:00
// Compare the URLs without /embed and /present
var parsedOld = Hash.parsePadUrl(oldHref);
var parsedNew = Hash.parsePadUrl(newHref);
if (parsedOld.hashData && parsedNew.hashData &&
parsedOld.getUrl() !== parsedNew.getUrl()) {
2018-04-27 15:23:23 +00:00
if (!parsedOld.hashData.key) { oldHref = newHref; return; }
// If different, reload
2017-09-14 09:23:37 +00:00
document.location.reload();
return;
}
if (parsedNew.hashData) { oldHref = newHref; }
2017-09-14 09:23:37 +00:00
};
// Listen for login/logout in other tabs
window.addEventListener('storage', function (e) {
2017-11-21 15:46:19 +00:00
if (e.key !== Constants.userHashKey) { return; }
var o = e.oldValue;
var n = e.newValue;
if (!o && n) {
document.location.reload();
} else if (o && !n) {
2017-11-23 11:28:49 +00:00
LocalStore.logout();
2017-11-30 09:33:09 +00:00
postMessage("DISCONNECT");
}
});
2017-11-23 11:28:49 +00:00
if (PINNING_ENABLED && LocalStore.isLoggedIn()) {
2017-09-14 09:23:37 +00:00
console.log("logged in. pads will be pinned");
2017-11-30 14:01:17 +00:00
postMessage("INIT_RPC", null, waitFor(function (obj) {
2017-09-14 09:23:37 +00:00
console.log('RPC handshake complete');
2017-11-30 14:01:17 +00:00
if (obj.error) { return; }
localStorage.plan = obj.plan;
2017-11-30 09:33:09 +00:00
}));
2017-09-14 09:23:37 +00:00
} else if (PINNING_ENABLED) {
console.log('not logged in. pads will not be pinned');
} else {
console.log('pinning disabled');
}
2017-11-30 09:33:09 +00:00
postMessage("INIT_ANON_RPC", null, waitFor(function () {
console.log('Anonymous RPC ready');
}));
}).nThen(function (waitFor) {
if (sessionStorage.createReadme) {
2017-11-30 09:33:09 +00:00
var data = {
driveReadme: Messages.driveReadme,
driveReadmeTitle: Messages.driveReadmeTitle,
};
postMessage("CREATE_README", data, waitFor(function (e) {
if (e && e.error) { return void console.error(e.error); }
delete sessionStorage.createReadme;
2017-11-30 09:33:09 +00:00
}));
}
2017-09-22 17:35:06 +00:00
}).nThen(function (waitFor) {
if (sessionStorage.migrateAnonDrive) {
2017-11-30 09:33:09 +00:00
common.mergeAnonDrive(waitFor(function() {
delete sessionStorage.migrateAnonDrive;
}));
2017-09-22 17:35:06 +00:00
}
}).nThen(function (waitFor) {
if (AppConfig.afterLogin) {
AppConfig.afterLogin(common, waitFor());
}
2017-09-14 09:23:37 +00:00
}).nThen(function () {
updateLocalVersion();
f(void 0, env);
if (typeof(window.onhashchange) === 'function') { window.onhashchange(); }
});
};
}());
2016-05-28 11:13:54 +00:00
return common;
});