Add lazy metadata to fix parse errors in pad2

pull/1/head
yflory 7 years ago
parent 226ef1fd00
commit fc5bdd3bed

@ -4,10 +4,11 @@ define([], function () {
var meta = UNINIT; var meta = UNINIT;
var members = []; var members = [];
var metadataObj = UNINIT; var metadataObj = UNINIT;
var metadataLazyObj = UNINIT;
var dirty = true; var dirty = true;
var changeHandlers = []; var changeHandlers = [];
var checkUpdate = function () { var checkUpdate = function (lazy) {
if (!dirty) { return; } if (!dirty) { return; }
if (meta === UNINIT) { throw new Error(); } if (meta === UNINIT) { throw new Error(); }
if (metadataObj === UNINIT) { if (metadataObj === UNINIT) {
@ -17,6 +18,7 @@ define([], function () {
type: meta.doc.type, type: meta.doc.type,
users: {} users: {}
}; };
metadataLazyObj = JSON.parse(JSON.stringify(metadataObj));
} }
var mdo = {}; var mdo = {};
// We don't want to add our user data to the object multiple times. // We don't want to add our user data to the object multiple times.
@ -34,56 +36,65 @@ define([], function () {
//if (!containsYou) { mdo[meta.user.netfluxId] = meta.user; } //if (!containsYou) { mdo[meta.user.netfluxId] = meta.user; }
mdo[meta.user.netfluxId] = meta.user; mdo[meta.user.netfluxId] = meta.user;
metadataObj.users = mdo; metadataObj.users = mdo;
if (lazy) {
metadataLazyObj.users = mdo;
}
dirty = false; dirty = false;
changeHandlers.forEach(function (f) { f(); }); changeHandlers.forEach(function (f) { f(); });
}; };
var change = function () { var change = function (lazy) {
dirty = true; dirty = true;
setTimeout(checkUpdate); setTimeout(function () {
checkUpdate(lazy);
});
}; };
sframeChan.on('EV_METADATA_UPDATE', function (ev) { sframeChan.on('EV_METADATA_UPDATE', function (ev) {
meta = ev; meta = ev;
change(); change(true);
}); });
sframeChan.on('EV_RT_CONNECT', function (ev) { sframeChan.on('EV_RT_CONNECT', function (ev) {
meta.user.netfluxId = ev.myID; meta.user.netfluxId = ev.myID;
members = ev.members; members = ev.members;
change(); change(true);
}); });
sframeChan.on('EV_RT_JOIN', function (ev) { sframeChan.on('EV_RT_JOIN', function (ev) {
members.push(ev); members.push(ev);
change(); change(false);
}); });
sframeChan.on('EV_RT_LEAVE', function (ev) { sframeChan.on('EV_RT_LEAVE', function (ev) {
var idx = members.indexOf(ev); var idx = members.indexOf(ev);
if (idx === -1) { console.log('Error: ' + ev + ' not in members'); return; } if (idx === -1) { console.log('Error: ' + ev + ' not in members'); return; }
members.splice(idx, 1); members.splice(idx, 1);
change(); change(false);
}); });
sframeChan.on('EV_RT_DISCONNECT', function () { sframeChan.on('EV_RT_DISCONNECT', function () {
members = []; members = [];
change(); change(true);
}); });
return Object.freeze({ return Object.freeze({
updateMetadata: function (m) { updateMetadata: function (m) {
if (JSON.stringify(metadataObj) === JSON.stringify(m)) { return; } if (JSON.stringify(metadataObj) === JSON.stringify(m)) { return; }
metadataObj = m; metadataObj = m;
change(); change(true);
}, },
getMetadata: function () { getMetadata: function () {
checkUpdate(); checkUpdate(false);
return metadataObj; return metadataObj;
}, },
getMetadataLazy: function () {
return metadataLazyObj;
},
onChange: function (f) { changeHandlers.push(f); }, onChange: function (f) { changeHandlers.push(f); },
getNetfluxId: function () { isConnected : function () {
return meta && meta.user && meta.user.netfluxId; return members.indexOf(meta.user.netfluxId) !== -1;
}, },
getUserlist: function () { getViewers : function () {
checkUpdate(false);
var list = members.slice().filter(function (m) { return m.length === 32; }); var list = members.slice().filter(function (m) { return m.length === 32; });
return list; return list.length - Object.keys(metadataObj.users).length;
} }
}); });
}; };

@ -122,7 +122,7 @@ define([
// Userlist elements // Userlist elements
var getOtherUsers = function(config) { var getOtherUsers = function(config) {
var userList = config.userList.getUserlist(); //var userList = config.userList.getUserlist();
var userData = config.userList.getMetadata().users; var userData = config.userList.getMetadata().users;
var i = 0; // duplicates counter var i = 0; // duplicates counter
@ -130,12 +130,12 @@ define([
// Display only one time each user (if he is connected in multiple tabs) // Display only one time each user (if he is connected in multiple tabs)
var uids = []; var uids = [];
userList.forEach(function(user) { Object.keys(userData).forEach(function(user) {
//if (user !== userNetfluxId) { //if (user !== userNetfluxId) {
var data = userData[user] || {}; var data = userData[user] || {};
var userId = data.uid; var userId = data.uid;
if (!userId) { return; } if (!userId) { return; }
data.netfluxId = user; //data.netfluxId = user;
if (uids.indexOf(userId) === -1) {// && (!myUid || userId !== myUid)) { if (uids.indexOf(userId) === -1) {// && (!myUid || userId !== myUid)) {
uids.push(userId); uids.push(userId);
list.push(data); list.push(data);
@ -176,16 +176,15 @@ define([
var $userButtons = toolbar.userlist; var $userButtons = toolbar.userlist;
var $userlistContent = toolbar.userlistContent; var $userlistContent = toolbar.userlistContent;
var userList = config.userList.getUserlist(); var metadataMgr = config.userList;
var userData = config.userList.getMetadata().users; var userData = metadataMgr.getMetadata().users;
console.log(userList, userData); var viewers = metadataMgr.getViewers();
var numberOfUsers = userList.length;
// If we are using old pads (readonly unavailable), only editing users are in userList. // If we are using old pads (readonly unavailable), only editing users are in userList.
// With new pads, we also have readonly users in userList, so we have to intersect with // With new pads, we also have readonly users in userList, so we have to intersect with
// the userData to have only the editing users. We can't use userData directly since it // the userData to have only the editing users. We can't use userData directly since it
// may contain data about users that have already left the channel. // may contain data about users that have already left the channel.
userList = config.readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData)); //userList = config.readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData));
// Names of editing users // Names of editing users
var others = getOtherUsers(config); var others = getOtherUsers(config);
@ -198,8 +197,8 @@ console.log(userList, userData);
return na.toLowerCase() > nb.toLowerCase(); return na.toLowerCase() > nb.toLowerCase();
}); });
var numberOfEditUsers = userList.length - duplicates; var numberOfEditUsers = Object.keys(userData).length - duplicates;
var numberOfViewUsers = numberOfUsers - userList.length; var numberOfViewUsers = viewers;
// Update the userlist // Update the userlist
var $editUsers = $userlistContent.find('.' + USERLIST_CLS).html(''); var $editUsers = $userlistContent.find('.' + USERLIST_CLS).html('');
@ -287,8 +286,7 @@ console.log(userList, userData);
//userList.change.push //userList.change.push
var metadataMgr = config.userList; var metadataMgr = config.userList;
metadataMgr.onChange(function () { metadataMgr.onChange(function () {
var users = metadataMgr.getUserlist(); if (metadataMgr.isConnected()) {toolbar.connected = true;}
if (users.indexOf(metadataMgr.getNetfluxId()) !== -1) {toolbar.connected = true;}
if (!toolbar.connected) { return; } if (!toolbar.connected) { return; }
//if (config.userList.data) { //if (config.userList.data) {
updateUserList(toolbar, config); updateUserList(toolbar, config);

@ -341,7 +341,7 @@ define([
var stringifyDOM = module.stringifyDOM = function (dom) { var stringifyDOM = module.stringifyDOM = function (dom) {
var hjson = Hyperjson.fromDOM(dom, isNotMagicLine, brFilter); var hjson = Hyperjson.fromDOM(dom, isNotMagicLine, brFilter);
hjson[3] = { hjson[3] = {
metadata: cpNfInner.metadataMgr.getMetadata() metadata: cpNfInner.metadataMgr.getMetadataLazy()
}; };
/*hjson[3] = { TODO /*hjson[3] = { TODO
users: UserList.userData, users: UserList.userData,
@ -470,8 +470,11 @@ define([
realtimeOptions.onInit = function (info) { realtimeOptions.onInit = function (info) {
console.log('onInit'); console.log('onInit');
var titleCfg = { getHeadingText: getHeadingText };
Title = Cryptpad.createTitle(titleCfg, realtimeOptions.onLocal, Cryptpad);
var configTb = { var configTb = {
displayed: ['userlist'], displayed: ['userlist'],
//title: Title.getTitleConfig(),
userList: cpNfInner.metadataMgr, userList: cpNfInner.metadataMgr,
readOnly: readOnly, readOnly: readOnly,
ifrw: window, ifrw: window,

Loading…
Cancel
Save