Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
add basic remote behaviour to client scripts
Browse files Browse the repository at this point in the history
this commit adds the ability to read the sameAs attribute of portal.json

portal.sameAs is an array of portal dat:// addresses that state that
this portal is the same as those portals, and that we should load their
messages under our name

this makes it possible to effectively post to the same account from
different machines

note: port_1 needs to have port_2 in its sameAs array, and port_2 needs to
have port_1 in its sameAs array, i.e. the relationship needs to be mutual
  • Loading branch information
cblgh committed Nov 22, 2017
1 parent 17b29be commit 36969fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion scripts/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ function Feed(feed_urls)
r.home.feed.next();
return;
}
portal.connect()
// if everything went well loading the portal, let's try to load its remotes
portal.connect().then(portal.load_remotes)
r.home.feed.update_log();
}

Expand Down
32 changes: 32 additions & 0 deletions scripts/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ function Portal(url)
setTimeout(r.home.feed.next, r.home.feed.connection_delay);
}

this.load_remotes = async function() {
if (p.json && p.json.sameAs && p.json.sameAs.length > 0) {
// naive solution while testing
var remote_promises = p.json.sameAs.map((remote_url) => {
return new Promise((resolve, reject) => {
console.log("remote url", remote_url)
var remote = new Portal(remote_url)
remote.start().then(() => {
console.log("loaded remote")
if (remote.json.sameAs && remote.json.sameAs.indexOf(p.dat) >= 0) {
console.log(remote.dat + "has a mutual relationship w/ us :)")
// set remote name
remote.json.name = `${p.json.name} (${remote.json.name})`
// remote.url = p.url
r.home.feed.register(remote);
} else {
console.log(remote.dat + " wasn't a mutual with us :<")
}
}).then(resolve).catch((err) => {
console.error("something went wrong when loading remotes")
console.error(err)
reject()
})
})
})

Promise.all(remote_promises).then(() => {
console.log("all remotes have been handled?")
})
}
}

this.discover = async function()
{
console.log('connecting to: ', p.url);
Expand Down

4 comments on commit 36969fa

@0x0ade
Copy link
Member

@0x0ade 0x0ade commented on 36969fa Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var remote_promises = p.json.sameAs.map((remote_url) => {
return new Promise((resolve, reject) => {
console.log("remote url", remote_url)
var remote = new Portal(remote_url)
remote.start().then(() => {

If I understand this right, this attempts to connect to all remotes at once. While this is faster than what we're doing normally and won't hurt with a good network connection, IMO it would still be better to queue and iterate through them (similarly to how it's being done in portal.connect / Home.js).

Also, I've never seen start() being used on any other portal than our own, but it should be fine in this case :)

@cblgh
Copy link
Contributor Author

@cblgh cblgh commented on 36969fa Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i think it's good to move to a queue-based process when everything works as a whole, but as it is there are still wrinkles to iron out.

not sure if you saw my message on rotonde, but if you look at portal.js you'll see that i change the name of the remotes

// set remote name
remote.json.name = `${p.json.name}=${remote.json.name}`

the issue being that name is reset upon a refresh, caused by e.g. a new dat or message being added. do you know what's going on there? i suspect the portal's data is being reloaded, to fetch any changes done in the meantime, but i am not sure.

@0x0ade
Copy link
Member

@0x0ade 0x0ade commented on 36969fa Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got your message on Rotonde and sent a response, but went offline a few minutes afterwards 😅

Does the issue only affect the entries? Or does it affect the portals' JSON in r.home.feed.portals? I'm going offline in a few minutes (will still be available via Twitter for a hour or two), but taking a quick look at it

Regarding the name being reset: You're right, portal.refresh can occur on a few occasions and that redownloads portal.json. One of such occasions is whenever home.save invokes a delayed feed.refresh (refresh portals to poll the entries, then refresh entries in feed). Another one is hitting F5 / the refresh button in the Portals tab / the portals_refresh command.

Possible solution: onparse "event" for portals which gets fired after parsing portal.json in start / connect / refresh. "sameAs"-resolved portals can then listen for the event and change the name after portal.json gets parsed.

For our intents and purposes, we don't even need to mimic an event, but instead just set a default this.onparse = json => {}; in Portal.js and then later in load_remotes, set

remote.onparse = json => json.name = `${p.json.name}=${json.name}`;

We could also "queue" an onparse into the connection queue. That'd make it possible for us to just throw all remote URLs + a matching onparse into the queue and reuse all the existing code.

If you don't mind, I'll try to rework sameAs towards that in my own repo and then send a PR :) I'll try to get it finished today as I'll be away during the weekend.

@cblgh
Copy link
Contributor Author

@cblgh cblgh commented on 36969fa Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha yeah, i think we've been talking past each other. as soon as i saw your message on rotonde i realized how to fix it!

in portal.js's refresh method:

var oldName = p.json.name;
p.json = JSON.parse(p.file);
// don't replace name for remotes
if (p.is_remote) {
p.json.name = oldName;
}

i wouldn't mind moving to an event-based structure though :) so feel free to rework sameAs! (make sure you've got the latest updates tho, since i've fixed a few issues that i came across while testing)

Please sign in to comment.