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

Commit

Permalink
Merge pull request #157 from 0x0ade/cblgh/feature/sameAs
Browse files Browse the repository at this point in the history
Add very basic "event" handling in Portal.js; Build sameAs on it
  • Loading branch information
cblgh authored Nov 23, 2017
2 parents 128c266 + 9dae054 commit 4ddd694
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 63 deletions.
8 changes: 4 additions & 4 deletions scripts/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function Entry(data,host)
if(data.quote && this.target && this.target[0]){
var icon = this.target[0].replace(/\/$/, "") + "/media/content/icon.svg"
// set the source's icon for quotes of remotes
if (host && host.json && host.json.sameAs && host.json.sameAs.indexOf(this.target[0]) >= 0) {
if (host && host.json && host.json.sameAs && has_hash(host.json.sameAs, this.target[0])) {
icon = host.icon
}
var dummy_portal = {"url":this.target[0], "icon": icon, "json":{"name":r.escape_html(portal_from_hash(this.target[0].toString())).substring(1)}};
this.quote = new Entry(data.quote, dummy_portal);
}

this.is_seed = this.host ? r.home.portal.json.port.indexOf(this.host.url) > -1 : false;
this.is_seed = this.host && has_hash(r.home.portal.json.port, this.host.url);
}
this.update(data, host);

Expand Down Expand Up @@ -363,8 +363,8 @@ function Entry(data,host)
space = m.length;
var word = m.substring(c, space);

if (word.length > 1 && word[0] == "@") {
var name_match = r.operator.name_pattern.exec(word);
var name_match;
if (word.length > 1 && word[0] == "@" && (name_match = r.operator.name_pattern.exec(word))) {
var remnants = word.substr(name_match[0].length);
if (name_match[1] == r.home.portal.json.name) {
n += "<t class='highlight'>"+name_match[0]+"</t>"+remnants;
Expand Down
30 changes: 23 additions & 7 deletions scripts/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ function Feed(feed_urls)
return;
}

var url = r.home.feed.queue[0];
var entry = r.home.feed.queue[0];
var url = entry;
if (entry.url) {
url = entry.url;
}

r.home.feed.queue = r.home.feed.queue.slice(1);

Expand All @@ -151,8 +155,12 @@ function Feed(feed_urls)
r.home.feed.next();
return;
}
// if everything went well loading the portal, let's try to load its remotes
portal.connect().then(portal.load_remotes);

if (entry.oncreate)
portal.fire(entry.oncreate);
if (entry.onparse)
portal.onparse.push(entry.onparse);
portal.connect();
r.home.feed.update_log();
}

Expand All @@ -178,6 +186,10 @@ function Feed(feed_urls)
this.__get_portal_cache__[hashes[id]] = portal;
}

if (!portal.is_remote) {
portal.load_remotes();
}

// Invalidate the collected network cache and recollect.
r.home.collect_network(true);

Expand Down Expand Up @@ -263,6 +275,8 @@ function Feed(feed_urls)
        var portal = r.home.feed.portals[id];
        await portal.refresh();
      }
if (r.home.feed.portal_rotonde)
await r.home.feed.portal_rotonde.connect_service();
      r.home.feed.refresh('delayed: ' + why);
    }, 750);
     return;
Expand All @@ -284,7 +298,9 @@ function Feed(feed_urls)
var portal = r.home.feed.portals[id];
entries.push.apply(entries, portal.entries());
}

if (r.home.feed.portal_rotonde)
entries.push.apply(entries, r.home.feed.portal_rotonde.entries());

this.mentions = 0;
this.whispers = 0;

Expand Down Expand Up @@ -320,7 +336,7 @@ function Feed(feed_urls)
}

var now = new Date();
var entries_now = new Set();
var entries_now = [];
for (id in sorted_entries){
var entry = sorted_entries[id];

Expand All @@ -339,7 +355,7 @@ function Feed(feed_urls)
c = -2;
var elem = !entry ? null : entry.to_element(timeline, c, cmin, cmax, coffset);
if (elem != null) {
entries_now.add(entry);
entries_now.push(entry);
}
if (c >= 0)
ca++;
Expand All @@ -348,7 +364,7 @@ function Feed(feed_urls)
// Remove any "zombie" entries - removed entries not belonging to any portal.
for (id in this.entries_prev) {
var entry = this.entries_prev[id];
if (entries_now.has(entry))
if (entries_now.indexOf(entry) > -1)
continue;
entry.remove_element();
}
Expand Down
46 changes: 24 additions & 22 deletions scripts/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ function Operator(el)
if(r.home.portal.json.port.indexOf(path) > -1){
r.home.portal.json.port.splice(r.home.portal.json.port.indexOf(path), 1);
}
else if(r.home.portal.json.port.indexOf(path+"/") > -1){
r.home.portal.json.port.splice(r.home.portal.json.port.indexOf(path+"/"), 1);
}
else{
console.log("could not find",path)
}
Expand All @@ -177,7 +174,6 @@ function Operator(el)
r.home.feed.portals[id].id = id;
}
portal.badge_remove();
portal.entries_remove();
}

r.home.save();
Expand All @@ -189,12 +185,12 @@ function Operator(el)
var remote = p;
if(remote.slice(-1) !== "/") { remote += "/" }

for(id in r.home.portal.json.sameAs) {
var port_url = r.home.portal.json.sameAs[id];
if(port_url.indexOf(remote) > -1){
return;
}
}
if (!r.home.portal.json.sameAs)
r.home.portal.json.sameAs = [];

if (has_hash(r.home.portal.json.sameAs, remote))
return;

// create the array if it doesn't exist
if (!r.home.portal.json.sameAs) { r.home.portal.json.sameAs = [] }
r.home.portal.json.sameAs.push(remote);
Expand All @@ -212,6 +208,9 @@ function Operator(el)
var remote = p;
if(remote.slice(-1) !== "/") { remote += "/" }

if (!r.home.portal.json.sameAs)
r.home.portal.json.sameAs = [];

// Remove
if (r.home.portal.json.sameAs.indexOf(remote) > -1) {
r.home.portal.json.sameAs.splice(r.home.portal.json.sameAs.indexOf(remote), 1);
Expand All @@ -221,17 +220,15 @@ function Operator(el)
}

var portal = r.home.feed.get_portal(remote);
if (portal) {
if (portal && portal.is_remote) {
r.home.feed.portals.splice(portal.id, 1)[0];
for (var id in r.home.feed.portals) {
r.home.feed.portals[id].id = id;
}
portal.badge_remove();
portal.entries_remove();
}

r.home.save();
r.home.feed.refresh("unfollowing: "+option);
r.home.feed.refresh("mirroring: "+option);
}

this.commands.dat = function(p,option)
Expand Down Expand Up @@ -273,8 +270,7 @@ function Operator(el)
this.commands.quote = function(p,option)
{
var message = p;
var name = option.split("-")[0];
var ref = parseInt(option.split("-")[1]);
var {name, ref} = r.operator.split_nameref(option);

var portals = r.operator.lookup_name(name);

Expand Down Expand Up @@ -412,8 +408,7 @@ function Operator(el)

this.commands.expand = function(p, option)
{
var name = option.split("-")[0];
var ref = parseInt(option.split("-")[1]);
var {name, ref} = r.operator.split_nameref(option);

var portals = r.operator.lookup_name(name);

Expand All @@ -427,8 +422,7 @@ function Operator(el)

this.commands.collapse = function(p, option)
{
var name = option.split("-")[0];
var ref = parseInt(option.split("-")[1]);
var {name, ref} = r.operator.split_nameref(option);

var portals = r.operator.lookup_name(name);

Expand All @@ -447,8 +441,7 @@ function Operator(el)
return;
}

var name = option.split("-")[0];
var ref = parseInt(option.split("-")[1]);
var {name, ref} = r.operator.split_nameref(option);

var portals = r.operator.lookup_name(name);

Expand Down Expand Up @@ -674,6 +667,8 @@ function Operator(el)

this.lookup_name = function(name)
{
if (r.home.feed.portal_rotonde && name === r.home.feed.portal_rotonde.json.name)
return [r.home.feed.portal_rotonde];
// We return an array since multiple people might be using the same name.
var results = [];
for(var url in r.home.feed.portals){
Expand All @@ -682,6 +677,13 @@ function Operator(el)
}
return results;
}

this.split_nameref = function(option)
{
var index = option.lastIndexOf("-");
if (index < 0) return;
return { name: option.substring(0, index), ref: parseInt(option.substring(index + 1)) };
}
}

r.confirm("script","operator");
80 changes: 50 additions & 30 deletions scripts/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,43 @@ function Portal(url)
this.dat = "dat://"+hash+"/";
});

this.is_remote = false;
this.remote_parent = null;

this.last_entry = null;

this.badge_element = null;
this.badge_element_html = null;

this.onparse = []; // Contains functions of format json => {...}
this.fire = function(event) {
var handlers;
if (typeof(event) === "function")
handlers = [event];
else if (event.length && typeof(event[0]) === "function")
handlers = event;
else
handlers = this["on"+event];
if (!handlers || handlers.length === 0) return true; // Return true by default.
var args = Array.prototype.splice.call(arguments, 1);
for (var id in handlers) {
var result = handlers[id].apply(this, args);
if (result === true) // We only want true, not truly values.
continue; // If the handler returned true, continue to the next handler.
else if (result === false) // We only want false, not falsy values.
return false; // Exit early.
else if (result !== undefined)
return result; // If the handler returned something, return it early.
}
return true;
}

this.start = async function()
{
var file = await this.archive.readFile('/portal.json',{timeout: 2000}).then(console.log("done!"));

this.json = JSON.parse(file);
if (!this.fire("parse", this.json)) throw new Error("onparse returned false!");
this.maintenance();
}

Expand Down Expand Up @@ -58,6 +85,7 @@ function Portal(url)

try {
p.json = JSON.parse(p.file);
if (!p.fire("parse", p.json)) throw new Error("onparse returned false!");
p.file = null;
r.home.feed.register(p);
} catch (err) {
Expand All @@ -68,35 +96,25 @@ function Portal(url)
}

this.load_remotes = async function() {
if (p.json && p.json.sameAs && p.json.sameAs.length > 0) {
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.is_remote = true;
remote.start().then(() => {
console.log("loaded remote")
if (remote.json && 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.icon = p.url + "/media/content/icon.svg";
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 appear to have been handled?")
})
if (!p.json || !p.json.sameAs || p.json.sameAs.length === 0) {
return;
}

r.home.feed.queue.push.apply(r.home.feed.queue, p.json.sameAs.map((remote_url) => {
return {
url: remote_url,
oncreate: function() {
this.is_remote = true;
this.remote_parent = p;
this.icon = p.url + "/media/content/icon.svg";
},
onparse: function(json) {
this.json.name = `${p.json.name}=${json.name}`
return this.json.sameAs && has_hash(this.json.sameAs, p.hashes());
}
}
}));
r.home.feed.connect();
}

this.connect_service = async function()
Expand All @@ -113,8 +131,9 @@ function Portal(url)

try {
p.json = JSON.parse(p.file);
if (!p.fire("parse", p.json)) throw new Error("onparse returned false!");
p.file = null;
r.home.feed.portals.push(r.home.feed.portal_rotonde = this);
r.home.feed.portal_rotonde = this;
} catch (err) {
console.log('parsing failed: ', p.url);
}
Expand All @@ -134,6 +153,7 @@ function Portal(url)

try {
p.json = JSON.parse(p.file);
if (!p.fire("parse", p.json)) throw new Error("onparse returned false!");
p.file = null;
} catch (err) {
console.log('parsing failed: ', p.url);
Expand Down Expand Up @@ -210,7 +230,7 @@ function Portal(url)

this.relationship = function(target = r.home.portal.hashes_set())
{
if (this === r.home.feed.portal_rotonde) return create_rune("portal", "rotonde");
if (this.url === r.client_url) return create_rune("portal", "rotonde");
if (has_hash(this, target)) return create_rune("portal", "self");
if (has_hash(this.json.port, target)) return create_rune("portal", "both");

Expand Down

0 comments on commit 4ddd694

Please sign in to comment.