You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/common/metadata-manager.js

92 lines
3.1 KiB
JavaScript

7 years ago
define([], function () {
var UNINIT = 'uninitialized';
7 years ago
var create = function (sframeChan) {
var meta = UNINIT;
7 years ago
var members = [];
var metadataObj = UNINIT;
7 years ago
var dirty = true;
var changeHandlers = [];
7 years ago
7 years ago
var checkUpdate = function () {
if (!dirty) { return; }
if (meta === UNINIT) { throw new Error(); }
if (metadataObj === UNINIT) {
metadataObj = {
defaultTitle: meta.doc.defaultTitle,
title: meta.doc.defaultTitle,
type: meta.doc.type,
users: {}
};
}
7 years ago
var mdo = {};
// We don't want to add our user data to the object multiple times.
7 years ago
//var containsYou = false;
//console.log(metadataObj);
7 years ago
console.log(metadataObj.users);
Object.keys(metadataObj.users).forEach(function (x) {
7 years ago
if (members.indexOf(x) === -1) { return; }
mdo[x] = metadataObj.users[x];
7 years ago
/*if (metadataObj.users[x].uid === meta.user.uid) {
//console.log('document already contains you');
containsYou = true;
7 years ago
}*/
7 years ago
});
7 years ago
//if (!containsYou) { mdo[meta.user.netfluxId] = meta.user; }
mdo[meta.user.netfluxId] = meta.user;
metadataObj.users = mdo;
7 years ago
7 years ago
dirty = false;
changeHandlers.forEach(function (f) { f(); });
};
var change = function () {
dirty = true;
setTimeout(checkUpdate);
7 years ago
};
sframeChan.on('EV_METADATA_UPDATE', function (ev) {
meta = ev;
7 years ago
change();
});
sframeChan.on('EV_RT_CONNECT', function (ev) {
meta.user.netfluxId = ev.myID;
7 years ago
members = ev.members;
change();
});
sframeChan.on('EV_RT_JOIN', function (ev) {
members.push(ev);
change();
});
sframeChan.on('EV_RT_LEAVE', function (ev) {
var idx = members.indexOf(ev);
if (idx === -1) { console.log('Error: ' + ev + ' not in members'); return; }
members.splice(idx, 1);
change();
});
sframeChan.on('EV_RT_DISCONNECT', function () {
members = [];
change();
});
7 years ago
7 years ago
return Object.freeze({
updateMetadata: function (m) {
if (JSON.stringify(metadataObj) === JSON.stringify(m)) { return; }
metadataObj = m;
7 years ago
change();
},
getMetadata: function () {
checkUpdate();
return metadataObj;
},
7 years ago
onChange: function (f) { changeHandlers.push(f); },
getNetfluxId: function () {
return meta && meta.user && meta.user.netfluxId;
},
getUserlist: function () {
var list = members.slice().filter(function (m) { return m.length === 32; });
return list;
}
7 years ago
});
};
return Object.freeze({ create: create });
7 years ago
});