cryptpad/www/common/outer/calendar.js

420 lines
13 KiB
JavaScript
Raw Normal View History

2021-04-02 11:44:28 +00:00
define([
'/common/common-util.js',
'/common/common-hash.js',
'/common/common-constants.js',
'/common/common-realtime.js',
'/customize/messages.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad/chainpad.dist.js',
], function (Util, Hash, Constants, Realtime, Messages, Listmap, Crypto, ChainPad) {
var Calendar = {};
/* TODO
* Calendar
{
href,
roHref,
channel, (pinning)
title, (when created from the UI, own calendar has no title)
color
}
* Own drive
{
calendars: {
2021-04-02 13:18:59 +00:00
uid: calendar,
uid: calendar
2021-04-02 11:44:28 +00:00
}
}
* Team drive
{
calendars: {
2021-04-02 13:18:59 +00:00
uid: calendar,
uid: calendar
2021-04-02 11:44:28 +00:00
}
}
* Calendars are listmap
{
content: {},
metadata: {
title: "pewpewpew"
}
}
ctx.calendars[channel] = {
lm: lm,
proxy: lm.proxy?
stores: [teamId, teamId, 1]
}
* calendar app can subscribe to this module
* when a listmap changes, push an update for this calendar to subscribed tabs
* Ability to open a calendar not stored in the stores but from its URL directly
* No "userlist" visible in the UI
* No framework
*/
var makeCalendar = function () {
var hash = Hash.createRandomHash('calendar');
var secret = Hash.getSecrets('calendar', hash);
var roHash = Hash.getViewHashFromKeys(secret);
var href = Hash.hashToHref(hash, 'calendar');
var roHref = Hash.hashToHref(roHash, 'calendar');
return {
href: href,
roHref: roHref,
channel: secret.channel,
};
};
var initializeCalendars = function (ctx, cb) {
var proxy = ctx.store.proxy;
var calendars = proxy.calendars = proxy.calendars || {};
2021-04-02 13:18:59 +00:00
/*if (!calendars.own) {
2021-04-02 11:44:28 +00:00
var own = calendars.own = makeCalendar(true);
own.color = ctx.Store.getUserColor();
}
2021-04-02 13:18:59 +00:00
*/
2021-04-02 11:44:28 +00:00
setTimeout(cb);
// XXX for each team, if we have edit rights, create the team calendar?
// XXX or maybe do it from the team app?
};
var sendUpdate = function (ctx, c) {
ctx.emit('UPDATE', {
teams: c.stores,
id: c.channel,
readOnly: c.readOnly,
2021-04-06 08:37:31 +00:00
deleted: !c.stores.length,
2021-04-02 13:18:59 +00:00
content: Util.clone(c.proxy)
2021-04-02 11:44:28 +00:00
}, ctx.clients);
};
2021-04-02 16:21:59 +00:00
var updateLocalCalendars = function (ctx, c, data) {
// Also update local data
c.stores.forEach(function (id) {
var s = getStore(ctx, id);
if (!s || !s.proxy) { return; }
if (!s.rpc) { return; } // team viewer
if (!s.proxy.calendars) { return; }
var cal = s.proxy.calendars[c.channel];
if (!cal) { return; }
if (cal.color !== data.color) { cal.color = data.color; }
if (cal.title !== data.title) { cal.title = data.title; }
});
};
2021-04-02 13:18:59 +00:00
var openChannel = function (ctx, cfg, _cb) {
var cb = Util.once(Util.mkAsync(_cb || function () {}));
2021-04-02 11:44:28 +00:00
var teamId = cfg.storeId;
var data = cfg.data;
var channel = data.channel;
if (!channel) { return; }
var c = ctx.calendars[channel];
if (c) {
if (c.stores && c.stores.indexOf(teamId) !== -1) { return; }
if (c.readOnly && data.href) {
// XXX UPGRADE
// XXX different cases if already ready or not?
}
c.stores.push(teamId);
return;
}
// Multiple teams can have the same calendar. Make sure we remember the list of stores
// that know it so that we don't close the calendar when leaving/deleting a team.
c = ctx.calendars[channel] = {
ready: false,
channel: channel,
readOnly: !data.href,
stores: [teamId]
};
var update = function () {
console.log(ctx.clients);
sendUpdate(ctx, c);
};
var parsed = Hash.parsePadUrl(data.href || data.roHref);
var secret = Hash.getSecrets('calendar', parsed.hash);
var crypto = Crypto.createEncryptor(secret.keys);
// Set the owners as the first store opening it. We don't know yet if it's a new or
// existing calendar. "owners' will be ignored if the calendar already exists.
var edPublic;
if (teamId === 1) {
edPublic = ctx.store.proxy.edPublic;
} else {
var teams = ctx.store.modules.team && ctx.store.modules.team.getTeamsData();
var team = teams && teams[teamId];
edPublic = team ? team.edPublic : undefined;
}
var config = {
data: {},
network: ctx.store.network, // XXX offline
channel: secret.channel,
crypto: crypto,
owners: [edPublic],
ChainPad: ChainPad,
validateKey: secret.keys.validateKey || undefined,
userName: 'calendar',
classic: true
};
console.error(channel, config);
var lm = Listmap.create(config);
c.lm = lm;
2021-04-02 13:18:59 +00:00
var proxy = c.proxy = lm.proxy;
2021-04-02 11:44:28 +00:00
lm.proxy.on('ready', function () {
c.ready = true;
console.warn('READY', channel);
2021-04-02 13:18:59 +00:00
if (!proxy.metadata) {
proxy.metadata = {
color: data.color,
title: data.title
};
}
2021-04-02 11:44:28 +00:00
setTimeout(update);
2021-04-02 13:18:59 +00:00
if (cb) { cb(null, lm.proxy); }
2021-04-02 11:44:28 +00:00
}).on('change', [], function () {
2021-04-02 16:21:59 +00:00
if (!c.ready) { return; }
2021-04-02 11:44:28 +00:00
setTimeout(update);
2021-04-02 16:21:59 +00:00
}).on('change', ['metadata'], function () {
// if title or color have changed, update our local values
var md = proxy.metadata;
if (!md || !md.title || !md.color) { return; }
updateLocalCalendars(ctx, c, md);
2021-04-02 13:18:59 +00:00
}).on('error', function (info) {
if (info && info.error) { cb(info); }
2021-04-02 11:44:28 +00:00
});
};
var openChannels = function (ctx) {
var findFromStore = function (store) {
var c = store.proxy.calendars;
if (!c) { return; }
2021-04-02 13:18:59 +00:00
Object.keys(c).forEach(function (channel) {
console.log(c[channel]);
2021-04-02 11:44:28 +00:00
openChannel(ctx, {
storeId: store.id || 1,
2021-04-02 13:18:59 +00:00
data: c[channel]
2021-04-02 11:44:28 +00:00
});
2021-04-02 13:18:59 +00:00
});
2021-04-02 11:44:28 +00:00
};
// Personal drive
findFromStore(ctx.store);
};
var subscribe = function (ctx, data, cId, cb) {
// Subscribe to new notifications
var idx = ctx.clients.indexOf(cId);
if (idx === -1) {
ctx.clients.push(cId);
}
2021-04-02 13:18:59 +00:00
cb({
empty: !Object.keys(ctx.calendars).length
});
2021-04-02 11:44:28 +00:00
Object.keys(ctx.calendars).forEach(function (channel) {
var c = ctx.calendars[channel] || {};
console.log(channel, c);
if (!c.ready) { return; }
sendUpdate(ctx, c);
});
};
2021-04-02 13:18:59 +00:00
var getStore = function (ctx, id) {
if (!id || id === 1) {
return ctx.store;
}
var m = ctx.store.modules && ctx.store.modules.team;
if (!m) { return; }
return m.getTeam(id);
};
var createCalendar = function (ctx, data, cId, cb) {
var store = getStore(ctx, data.teamId);
if (!store) { return void cb({error: "NO_STORE"}); }
// Check team edit rights: viewers in teams don't have rpc
if (!store.rpc) { return void cb({error: "EFORBIDDEN"}); }
var c = store.proxy.calendars = store.proxy.calendars || {};
var cal = makeCalendar();
cal.color = data.color;
cal.title = data.title;
openChannel(ctx, {
storeId: store.id || 1,
data: cal
}, function (err, proxy) {
if (err) {
// Can't open this channel, don't store it
console.error(err);
return void cb({error: err.error})
}
// Add the calendar and call back
c[cal.channel] = cal;
2021-04-06 08:26:30 +00:00
var pin = store.pin || ctx.pinPads;
pin([cal.channel], function (res) {
if (res && res.error) {
console.error(res.error);
}
});
2021-04-02 13:18:59 +00:00
ctx.Store.onSync(store.id, cb);
});
};
2021-04-02 16:21:59 +00:00
var updateCalendar = function (ctx, data, cId, cb) {
var id = data.id;
var c = ctx.calendars[id];
if (!c) { return void cb({error: "ENOENT"}); }
var md = Util.find(c, ['proxy', 'metadata']);
if (!md) { return void cb({error: 'EINVAL'}); }
md.title = data.title;
md.color = data.color;
Realtime.whenRealtimeSyncs(c.lm.realtime, cb);
sendUpdate(ctx, c);
updateLocalCalendars(ctx, c, data);
};
var deleteCalendar = function (ctx, data, cId, cb) {
var store = getStore(ctx, data.teamId);
if (!store) { return void cb({error: "NO_STORE"}); }
if (!store.rpc) { return void cb({error: "EFORBIDDEN"}); }
if (!store.proxy.calendars) { return; }
var id = data.id;
var cal = store.proxy.calendars[id];
if (!cal) { return void cb(); } // Already deleted
delete store.proxy.calendars[id];
2021-04-06 08:26:30 +00:00
var unpin = store.unpin || ctx.unpinPads;
unpin([id], function (res) {
if (res && res.error) {
console.error(res.error);
}
});
2021-04-06 08:37:31 +00:00
var ctxCal = ctx.calendars[id];
var idx = ctxCal.stores.indexOf(store.id || 1);
// Remove this store from the calendar's clients
ctxCal.stores.splice(idx, 1);
if (!ctxCal.stores.length) {
// If the calendar doesn't exist in any other team, stop it and delete it
// from ctx
ctxCal.lm.stop();
delete ctx.calendars[id];
}
ctx.Store.onSync(store.id, function () {
sendUpdate(ctx, ctxCal);
cb();
});
2021-04-02 16:21:59 +00:00
// XXX broadcast to inner
};
// XXX when we destroy a calendar, make sure we also delete it
2021-04-02 13:18:59 +00:00
var createEvent = function (ctx, data, cId, cb) {
var id = data.calendarId;
var c = ctx.calendars[id];
if (!c) { return void cb({error: "ENOENT"}); }
c.proxy.content = c.proxy.content || {};
c.proxy.content[data.id] = data;
Realtime.whenRealtimeSyncs(c.lm.realtime, cb);
};
var updateEvent = function (ctx, data, cId, cb) {
if (!data || !data.ev) { return void cb({error: 'EINVAL'}); }
var id = data.ev.calendarId;
var c = ctx.calendars[id];
if (!c || !c.proxy || !c.proxy.content) { return void cb({error: "ENOENT"}); }
// Find the event
var ev = c.proxy.content[data.ev.id];
if (!ev) { return void cb({error: "EINVAL"}); }
// update the event
var changes = data.changes || {};
Object.keys(changes).forEach(function (key) {
ev[key] = changes[key];
});
Realtime.whenRealtimeSyncs(c.lm.realtime, cb);
};
var deleteEvent = function (ctx, data, cId, cb) {
var id = data.calendarId;
var c = ctx.calendars[id];
if (!c) { return void cb({error: "ENOENT"}); }
c.proxy.content = c.proxy.content || {};
delete c.proxy.content[data.id];
Realtime.whenRealtimeSyncs(c.lm.realtime, cb);
};
2021-04-02 11:44:28 +00:00
var removeClient = function (ctx, cId) {
var idx = ctx.clients.indexOf(cId);
ctx.clients.splice(idx, 1);
};
Calendar.init = function (cfg, waitFor, emit) {
var calendar = {};
var store = cfg.store;
if (!store.loggedIn || !store.proxy.edPublic) { return; } // XXX logged in only?
var ctx = {
store: store,
Store: cfg.Store,
pinPads: cfg.pinPads,
updateMetadata: cfg.updateMetadata,
emit: emit,
onReady: Util.mkEvent(true),
calendars: {},
clients: [],
};
initializeCalendars(ctx, waitFor(function (err) {
if (err) { return; }
openChannels(ctx);
}));
calendar.removeClient = function (clientId) {
removeClient(ctx, clientId);
};
calendar.execCommand = function (clientId, obj, cb) {
var cmd = obj.cmd;
var data = obj.data;
if (cmd === 'SUBSCRIBE') {
return void subscribe(ctx, data, clientId, cb);
}
2021-04-02 13:18:59 +00:00
if (cmd === 'CREATE') {
return void createCalendar(ctx, data, clientId, cb);
}
2021-04-02 16:21:59 +00:00
if (cmd === 'UPDATE') {
return void updateCalendar(ctx, data, clientId, cb);
}
2021-04-02 13:18:59 +00:00
if (cmd === 'CREATE_EVENT') {
return void createEvent(ctx, data, clientId, cb);
}
if (cmd === 'UPDATE_EVENT') {
return void updateEvent(ctx, data, clientId, cb);
}
if (cmd === 'DELETE_EVENT') {
return void deleteEvent(ctx, data, clientId, cb);
}
2021-04-02 11:44:28 +00:00
};
return calendar;
};
return Calendar;
});