Add drive channel metadata to the pin log

This commit is contained in:
yflory 2023-09-06 17:15:00 +02:00
parent 708e36b3ee
commit dda4b8777c
7 changed files with 78 additions and 21 deletions

View File

@ -105,7 +105,10 @@ COMMANDS.start = (edPublic, blockId) => {
init(waitFor());
}).nThen((waitFor) => {
let lineHandler = Pins.createLineHandler(ref, (err) => { console.log(err); });
Env.pinStore.getMessages(safeKey, lineHandler, waitFor(() => {}));
Env.pinStore.readMessagesBin(safeKey, 0, (msgObj, readMore) => {
lineHandler(msgObj.buff.toString('utf8'));
readMore();
}, waitFor());
}).nThen((waitFor) => {
Log.info('MODERATION_ACCOUNT_ARCHIVAL_START', edPublic, waitFor());
var n = nThen;
@ -174,6 +177,7 @@ COMMANDS.start = (edPublic, blockId) => {
}
Log.info('MODERATION_ACCOUNT_LOG', safeKey, waitFor());
}));
blockId = blockId || ref.block;
if (!blockId) { return; }
BlockStore.archive(Env, blockId, waitFor(function (err) {
if (err) {
@ -291,4 +295,3 @@ parentPort.on('message', (message) => {
parentPort.postMessage('READY');

View File

@ -253,20 +253,18 @@ Channel.isNewChannel = function (Env, channel, cb) {
if (channel.length !== HK.STANDARD_CHANNEL_LENGTH &&
channel.length !== HK.ADMIN_CHANNEL_LENGTH) { return void cb('INVALID_CHAN'); }
// TODO replace with readMessagesBin
var done = false;
Env.msgStore.getMessages(channel, function (msg) {
if (done) { return; }
Env.msgStore.readMessagesBin(channel, 0, function (msgObj, readMore, abort) {
try {
var msg = msgObj.buff.toString('utf8');
if (typeof(msg) === 'string' && ARRAY_LINE.test(msg)) {
done = true;
abort();
return void cb(void 0, false);
}
} catch (e) {
Env.WARN('invalid message read from store', e);
}
readMore();
}, function () {
if (done) { return; }
// no more messages...
cb(void 0, true);
});

View File

@ -33,6 +33,29 @@ var createLineHandler = Pins.createLineHandler = function (ref, errorHandler) {
ref.index = 0;
ref.latest = 0; // the latest message (timestamp in ms)
ref.surplus = 0; // how many lines exist behind a reset
// Extract metadata from the channel list (#block, #drive)
let sanitize = (id, isPin) => {
if (typeof(id) !== "string") { return; }
let idx = id.indexOf('#');
if (idx < 0) { return id; }
let type = id.slice(idx+1);
let sanitized = id.slice(0, idx);
if (!isPin) { return sanitized; }
if (type === 'block') { // Note: teams don't have a block
ref.block = sanitized;
return;
}
if (type === 'drive') {
ref.drive = sanitized;
return sanitized;
}
return sanitized;
};
return function (line) {
ref.index++;
if (!Boolean(line)) { return; }
@ -55,17 +78,31 @@ var createLineHandler = Pins.createLineHandler = function (ref, errorHandler) {
switch (l[0]) {
case 'RESET': {
pins = ref.pins = {};
if (l[1] && l[1].length) { l[1].forEach((x) => { ref.pins[x] = 1; }); }
if (l[1] && l[1].length) {
l[1].forEach((x) => {
x = sanitize(x, true);
if (!x) { return; }
ref.pins[x] = 1;
});
}
ref.surplus = ref.index;
//jshint -W086
// fallthrough
}
case 'PIN': {
l[1].forEach((x) => { pins[x] = 1; });
l[1].forEach((x) => {
x = sanitize(x, true);
if (!x) { return; }
pins[x] = 1;
});
break;
}
case 'UNPIN': {
l[1].forEach((x) => { delete pins[x]; });
l[1].forEach((x) => {
x = sanitize(x, false);
if (!x) { return; }
delete pins[x];
});
break;
}
default:

View File

@ -387,8 +387,10 @@ const getPinState = function (data, cb) {
var lineHandler = Pins.createLineHandler(ref, Env.Log.error);
// if channels aren't in memory. load them from disk
// TODO replace with readMessagesBin
pinStore.getMessages(safeKey, lineHandler, function () {
pinStore.readMessagesBin(safeKey, 0, (msgObj, readMore) => {
lineHandler(msgObj.buff.toString('utf8'));
readMore();
}, function () {
cb(void 0, ref.pins); // FIXME no error handling?
});
};
@ -599,16 +601,18 @@ const getPinActivity = function (data, cb) {
var safeKey = Util.escapeKeyCharacters(data.key);
var first;
var latest;
pinStore.getMessages(safeKey, line => {
if (!line || !line.trim()) { return; }
pinStore.readMessagesBin(safeKey, 0, (msgObj, readMore) => {
var line = msgObj.buff.toString('utf8');
if (!line || !line.trim()) { return readMore(); }
try {
var parsed = JSON.parse(line);
var temp = parsed[parsed.length - 1];
if (!temp || typeof(temp) !== 'number') { return; }
if (!temp || typeof(temp) !== 'number') { return readMore(); }
latest = temp;
if (first) { return; }
if (first) { return readMore(); }
first = latest;
} catch (err) { }
readMore();
} catch (err) { readMore(); }
}, function (err) {
if (err) { return void cb(err); }
cb(void 0, {

View File

@ -2496,6 +2496,15 @@ define([
}));
}
}).nThen(function (waitFor) {
var blockHash = LocalStore.getBlockHash();
var blockId = '';
try {
var blockPath = (new URL(blockHash)).pathname;
var blockSplit = blockPath.split('/');
if (blockSplit[1] === 'block') {
blockId = blockSplit[3];
}
} catch (e) { }
var cfg = {
init: true,
userHash: userHash || LocalStore.getUserHash(),
@ -2508,7 +2517,8 @@ define([
neverDrive: rdyCfg.neverDrive,
disableCache: localStorage['CRYPTPAD_STORE|disableCache'],
driveEvents: !rdyCfg.noDrive, //rdyCfg.driveEvents // Boolean
lastVisit: Number(localStorage.lastVisit) || undefined
lastVisit: Number(localStorage.lastVisit) || undefined,
blockId: blockId
};
common.userHash = userHash;

View File

@ -203,7 +203,7 @@ define([
};
var getUserChannelList = function () {
var userChannel = store.driveChannel;
var userChannel = `${store.driveChannel}#drive`;
if (!userChannel) { return null; }
// Get the list of pads' channel ID in your drive
@ -245,6 +245,11 @@ define([
}
list.push(userChannel);
if (store.data && store.data.blockId) {
//list.push(`${store.data.blockId}#block`); // XXX 5.5.0?
}
list.sort();
return list;

View File

@ -149,7 +149,7 @@ define([
var list = store.manager.getChannelsList('pin');
var team = ctx.store.proxy.teams[id];
list.push(team.channel);
list.push(`${team.channel}#drive`);
var chatChannel = Util.find(team, ['keys', 'chat', 'channel']);
var membersChannel = Util.find(team, ['keys', 'roster', 'channel']);
var mailboxChannel = Util.find(team, ['keys', 'mailbox', 'channel']);