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.
156 lines
4.9 KiB
JavaScript
156 lines
4.9 KiB
JavaScript
5 years ago
|
define([
|
||
|
'/common/common-util.js',
|
||
|
'/common/common-hash.js',
|
||
|
'/common/userObject.js',
|
||
|
'/bower_components/nthen/index.js',
|
||
|
], function (Util, Hash, UserObject, nThen) {
|
||
|
var History = {};
|
||
|
var commands = {};
|
||
|
|
||
|
var getAccountChannels = function (ctx) {
|
||
|
var channels = [];
|
||
|
var edPublic = Util.find(ctx.store, ['proxy', 'edPublic']);
|
||
|
|
||
|
// Drive
|
||
|
channels.push(ctx.store.driveChannel);
|
||
|
|
||
|
// Profile
|
||
|
var profile = ctx.store.proxy.profile;
|
||
|
if (profile) {
|
||
|
var profileChan = profile.edit ? Hash.hrefToHexChannelId('/profile/#' + profile.edit, null) : null;
|
||
|
if (profileChan) { channels.push(profileChan); }
|
||
|
}
|
||
|
|
||
|
// Mailboxes
|
||
|
var mailboxes = ctx.store.proxy.mailboxes;
|
||
|
if (mailboxes) {
|
||
|
var mList = Object.keys(mailboxes).map(function (m) {
|
||
|
return {
|
||
|
lastKnownHash: mailboxes[m].lastKnownHash,
|
||
|
channel: mailboxes[m].channel
|
||
|
};
|
||
|
});
|
||
|
Array.prototype.push.apply(channels, mList);
|
||
|
}
|
||
|
|
||
|
// Shared folders owned by me
|
||
|
var sf = ctx.store.proxy[UserObject.SHARED_FOLDERS];
|
||
|
if (sf) {
|
||
|
console.log(sf);
|
||
|
var sfChannels = Object.keys(sf).map(function (fId) {
|
||
|
var data = sf[fId];
|
||
|
if (!data || !data.owners) { return; }
|
||
|
var isOwner = Array.isArray(data.owners) && data.owners.indexOf(edPublic) !== -1;
|
||
|
if (!isOwner) { return; }
|
||
|
return data.channel;
|
||
|
}).filter(Boolean);
|
||
|
console.log(sfChannels);
|
||
|
Array.prototype.push.apply(channels, sfChannels);
|
||
|
}
|
||
|
|
||
|
return channels;
|
||
|
};
|
||
|
|
||
|
commands.GET_HISTORY_SIZE = function (ctx, data, cId, cb) {
|
||
|
if (!ctx.store.loggedIn || !ctx.store.rpc) { return void cb({ error: 'INSUFFICIENT_PERMISSIONS' }); }
|
||
|
var channels = data.channels;
|
||
|
if (!Array.isArray(channels)) { return void cb({ error: 'EINVAL' }); }
|
||
|
|
||
|
// If account trim history, get the correct channels here
|
||
|
if (data.account) {
|
||
|
channels = getAccountChannels(ctx);
|
||
|
}
|
||
|
|
||
|
var size = 0;
|
||
|
var warning = false;
|
||
|
nThen(function (waitFor) {
|
||
|
// TODO: check if owner first?
|
||
|
channels.forEach(function (chan) {
|
||
|
size += Math.floor(Math.random()*1000) * 1024; // XXX
|
||
|
/*
|
||
|
var channel = chan;
|
||
|
var lastKnownHash;
|
||
|
if (typeof (chan) === "object" && chan.channel) {
|
||
|
channel = chan.channel;
|
||
|
lastKnownHash = chan.lastKnownHash;
|
||
|
}
|
||
|
ctx.store.rpc.getHistorySize({
|
||
|
channel: channel,
|
||
|
lastKnownHash: lastKnownHash
|
||
|
}, waitFor(function (err, value) {
|
||
|
if (err) {
|
||
|
warning = true;
|
||
|
return;
|
||
|
}
|
||
|
size += value;
|
||
|
}));
|
||
|
*/ // XXX TODO
|
||
|
});
|
||
|
}).nThen(function () {
|
||
|
cb({
|
||
|
warning: warning,
|
||
|
size: size
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
commands.TRIM_HISTORY = function (ctx, data, cId, cb) {
|
||
|
if (!ctx.store.loggedIn || !ctx.store.rpc) { return void cb({ error: 'INSUFFICIENT_PERMISSIONS' }); }
|
||
|
var channels = data.channels;
|
||
|
if (!Array.isArray(channels)) { return void cb({ error: 'EINVAL' }); }
|
||
|
|
||
|
// If account trim history, get the correct channels here
|
||
|
if (data.account) {
|
||
|
channels = getAccountChannels(ctx);
|
||
|
}
|
||
|
|
||
|
var warning = false;
|
||
|
nThen(function (waitFor) {
|
||
|
channels.forEach(function (chan) {
|
||
|
/*
|
||
|
ctx.store.rpc.trimHistory(chan, waitFor(function (err) {
|
||
|
if (err) {
|
||
|
warning = err;
|
||
|
return;
|
||
|
}
|
||
|
}));
|
||
|
*/ // XXX TODO
|
||
|
});
|
||
|
}).nThen(function () {
|
||
|
if (channels.length === 1 && warning) {
|
||
|
return void cb({error: err});
|
||
|
}
|
||
|
cb({ warning: warning });
|
||
|
});
|
||
|
};
|
||
|
|
||
|
History.init = function (cfg, waitFor, emit) {
|
||
|
var history = {};
|
||
|
if (!cfg.store) { return; }
|
||
|
var ctx = {
|
||
|
store: cfg.store,
|
||
|
Store: cfg.Store,
|
||
|
pinPads: cfg.pinPads,
|
||
|
updateMetadata: cfg.updateMetadata,
|
||
|
emit: emit,
|
||
|
};
|
||
|
|
||
|
history.execCommand = function (clientId, obj, cb) {
|
||
|
var cmd = obj.cmd;
|
||
|
var data = obj.data;
|
||
|
try {
|
||
|
commands[cmd](ctx, data, clientId, cb);
|
||
|
} catch (e) {
|
||
|
console.error(e);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return history;
|
||
|
};
|
||
|
|
||
|
return History;
|
||
|
});
|
||
|
|
||
|
|
||
|
|