Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Move over initial sync code from core
Browse files Browse the repository at this point in the history
  • Loading branch information
arj03 committed Feb 12, 2020
1 parent 8cf24ce commit 26bf8c7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
101 changes: 101 additions & 0 deletions net.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,105 @@ SSB.getOOO = function(msgId, cb)
})
}

SSB.initialSync = function(onboard)
{
function writeOnboardProfiles(onboard)
{
let cleaned = {}
for (var key in onboard) {
cleaned[key] = {
image: onboard[key].image,
name: onboard[key].name,
description: onboard[key].description
}
}

// merge in user updates
for (var author in SSB.profiles) {
Object.assign(cleaned[author], SSB.profiles[author])
}

localStorage['profiles.json'] = JSON.stringify(cleaned)

SSB.profiles = cleaned
}

SSB.isInitialSync = true // for ssb-ebt
SSB.net.connect(SSB.remoteAddress, (err, rpc) => {
if (err) throw(err)

var d = new Date()
var onemonthsago = d.setMonth(d.getMonth() - 1)

var totalMessages = 0
var totalFilteredMessages = 0
var totalPrivateMessages = 0
var totalFeeds = 0

console.time("downloading messages")

function getMessagesForUser(index)
{
if (index >= Object.keys(onboard).length) {
console.log("feeds", totalFeeds)
console.log("messages", totalMessages)
console.log("filtered", totalFilteredMessages)
console.timeEnd("downloading messages")

SSB.isInitialSync = false
writeOnboardProfiles(onboard)

return
}

var user = Object.keys(onboard)[index]

// FIXME: filter out in script
if (onboard[user].latestMsg == null) {
getMessagesForUser(index+1)
return
}

if (onboard[user].latestMsg.timestamp < onemonthsago && user != SSB.net.id) {
//console.log("skipping older posts for", onboard[user].name)
getMessagesForUser(index+1)
return
}

var seqStart = onboard[user].latestMsg.seq - 25
if (seqStart < 0)
seqStart = 0

if (user == SSB.net.id) // always all
seqStart = 0
else
SSB.db.last.setPartialLogState(user, true)

++totalFeeds

//console.log(`Downloading messages for: ${onboard[user].name}, seq: ${seqStart}`)

pull(
rpc.partialReplication.partialReplication({ id: user, seq: seqStart, keys: false }),
pull.asyncMap((msg, cb) => {
++totalMessages
SSB.net.add(msg, (err, res) => {
if (res)
++totalFilteredMessages

cb(err, res)
})
}),
pull.collect((err) => {
if (err) throw err

SSB.state.queue = []

getMessagesForUser(index+1)
})
)
}

getMessagesForUser(0)
})
}
6 changes: 3 additions & 3 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"pull-abortable": "^4.1.1",
"pull-stream": "^3.6.14",
"rimraf": "^3.0.0",
"ssb-browser-core": "^1.4.2",
"ssb-browser-core": "^2.0.0",
"ssb-contact-msg": "^1.1.0",
"ssb-markdown": "^6.0.3",
"ssb-mentions": "github:ssbc/ssb-mentions#replace-equals",
Expand Down

0 comments on commit 26bf8c7

Please sign in to comment.