diff options
-rw-r--r-- | lib/logbook-lib.js | 70 |
1 files changed, 23 insertions, 47 deletions
diff --git a/lib/logbook-lib.js b/lib/logbook-lib.js index ae3fab9..451e61d 100644 --- a/lib/logbook-lib.js +++ b/lib/logbook-lib.js @@ -5,7 +5,7 @@ var Pushable = require("pull-pushable"); var many = require("pull-many"); // Change to 'true' to get debugging output -DEBUG = false +DEBUG = !false // Change to switch the default monitored channel DEFAULT_CHANNEL_NAME = "logbook" @@ -24,7 +24,6 @@ let outputStream = Pushable(); let allowedBlobs = []; let idsInMainChannel = {}; let allMessages = {}; -let messagesByUser = {}; let opts = DEFAULT_OPTS; function pushMessage(newMsg) { @@ -40,45 +39,28 @@ function sortAndPushAllMessages() { for(let msg of messages.sort((a, b) => b.value.timestamp - a.value.timestamp)) { outputStream.push(msg); } + exit() } -function getReplies(unchckedMessages, recursionCount) { - if(recursionCount == MAX_REPLY_RECURSIONS) { - debug("Maximum recursions reached for reply chain; continuing to fetch profile pictures...\n Messages with unchecked replies start with: \n" + uncheckedMessages); - sortAndPushAllMessages(); - getRelevantProfilePictures(); - } - +function getReplies(rootMessages) { let replyStreams = []; - - for(userId of Object.entries(idsInMainChannel)) { - messagesByUser[userId] = []; + + for(userId of Object.keys(idsInMainChannel)) { replyStreams.push(createUserStream(client, userId)); } - - let newMessages = {}; - let foundNewMessages = false; + pull( many(replyStreams), pull.filter(function(msg) { - userMessages[msg.value.author].push(msg); - let messageIsValid = (value in msg) && (content in msg.value) && (link in msg.value.content); - return messageIsValid && !(msg.key in uncheckedMessages) && (msg.value.content.link in uncheckedMessages); + let messageIsValid = msg.value && msg.value.content && msg.value.content.root; + return messageIsValid && !(rootMessages.includes(msg.key)) && (rootMessages.includes(msg.value.content.root)); }), pull.drain(function(msg) { pushMessage(msg); - - newMessages[msg.key] = msg; - foundNewMessages = true; }, function() { - if(!foundNewMessages) { - sortAndPushAllMessages(); - getRelevantProfilePictures(); - } - else { - getReplies(newMessages, recursionCount + 1); - } + sortAndPushAllMessages(); + getRelevantProfilePictures(); }) ); } @@ -104,17 +86,18 @@ function getRelevantProfilePictures() { let relevantIds = Object.keys(idsInMainChannel); let profilePictureStreams = [] let profilePictureFound = {}; - + for(let userId of relevantIds) { profilePictureFound[userId] = false; profilePictureStreams.push(createMetadataStream(client, userId)); } let collectedStream = many(profilePictureStreams); - + if(relevantIds.length == 0) { // avoid the edge case where checkIfDone is never called - exit(); + // exit(cb, preserve); + return; } - + pull( collectedStream, pull.drain(function(msg) { @@ -133,8 +116,6 @@ function getRelevantProfilePictures() { debug("Message has unknown msg.value.content.image value: " + JSON.stringify(msg.value.content.image)); } } - }, function() { - exit(); }) ); } @@ -155,7 +136,8 @@ function getBlob(blobId) { } function exit() { // is called at the end of the getReplies -> getRelevantProfilePictures chain - outputStream.end(); + debug("exiting...") + outputStream.end(); } function debug(message) { @@ -197,15 +179,8 @@ function createChannelStream(client, channelName) { } function createUserStream(client, userId) { - var query = client.query.read({ - query: [{ - "$filter": { - value: { - author: userId - } - } - }], - reverse: true + var query = client.createUserStream({ + id: userId }); return query; @@ -236,12 +211,14 @@ function getMessagesFrom(channelName, followedIds, preserve, cb) { var channelStream = createChannelStream(client, channelName); var stream = many([hashtagStream, channelStream]); + outputStream = Pushable(); // have to create a new outputStream every time, or all calls to getMessages after the first will return the already empty outputStream, causing patchfoo to return no messages + pull(stream, pull.filter(function(msg) { return followedIds.includes(msg.value.author.toLowerCase()); }), pull.drain(function(msg) { pushMessage(msg); }, function() { - getReplies(allMessages, 0); + getReplies(Object.keys(allMessages)); })); cb(outputStream, preserve); @@ -258,7 +235,7 @@ module.exports = { getMessages: function(ssbClient, channelName, ssbOpts, preserve, cb, hops=MAX_HOPS) { client = ssbClient; // set global variables opts = ssbOpts; - + client.friends.hops({ dunbar: Number.MAX_SAFE_INTEGER, max: hops @@ -272,4 +249,3 @@ module.exports = { }); } } - |