This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
portal.js
425 lines (364 loc) · 11.8 KB
/
portal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
function Portal(url)
{
var p = this;
this.url = url;
this.icon = url.replace(/\/$/, "") + "/media/content/icon.svg";
if (this.url === r.client_url || this.url === "$rotonde") {
this.icon = r.client_url.replace(/\/$/, "") + "/media/logo.svg";
}
this.file = null;
this.json = null;
this.is_remove = false;
this.archive = new DatArchive(this.url);
// Resolve "masked" (f.e. hashbase) dat URLs to "hashed" (dat://0123456789abcdef/) one.
DatArchive.resolveName(this.url).then(hash => {
if (!hash) return;
this.dat = "dat://"+hash+"/";
});
this.last_entry = null;
this.badge_element = null;
this.badge_element_html = null;
this.start = async function()
{
var file = await this.archive.readFile('/portal.json',{timeout: 2000}).then(console.log("done!"));
this.json = JSON.parse(file);
this.maintenance();
}
this.maintenance = function()
{
// Remove portals duplicate
var checked = new Set();
var portals = this.json.port;
this.json.port = [];
for(id in portals){
var hash = to_hash(portals[id]);
if(checked.has(hash)){ continue; }
checked.add(hash);
this.json.port.push("dat://"+hash+"/");
}
}
this.connect = async function()
{
console.log('connecting to: ', p.url);
try {
p.file = await promiseTimeout(p.archive.readFile('/portal.json', {timeout: 2000}), 2000);
} catch (err) {
console.log('connection failed: ', p.url);
r.home.feed.next();
return;
} // Bypass slow loading feeds
try {
p.json = JSON.parse(p.file);
p.file = null;
r.home.feed.register(p);
} catch (err) {
console.log('parsing failed: ', p.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) {
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?")
})
}
}
this.connect_service = async function()
{
console.log('connecting to rotonde client service messages: ', p.url);
try {
p.file = await promiseTimeout(p.archive.readFile('/service.json', {timeout: 2000}), 2000);
} catch (err) {
console.log('connection failed: ', p.url);
r.home.feed.next();
return;
} // Bypass slow loading feeds
try {
p.json = JSON.parse(p.file);
p.file = null;
r.home.feed.portals.push(r.home.feed.portal_rotonde = this);
} catch (err) {
console.log('parsing failed: ', p.url);
}
}
this.discover = async function()
{
console.log('connecting to: ', p.url);
try {
p.file = await promiseTimeout(p.archive.readFile('/portal.json', {timeout: 1000}), 1000);
} catch (err) {
console.log('connection failed: ', p.url);
r.home.discover_next();
return;
} // Bypass slow loading feeds
try {
p.json = JSON.parse(p.file);
p.file = null;
} catch (err) {
console.log('parsing failed: ', p.url);
r.home.discover_next();
return;
}
r.home.discover_next(p);
}
this.refresh = async function()
{
try {
console.log("refreshing: ",p.url)
p.file = await promiseTimeout(p.archive.readFile('/portal.json',{timeout: 1000}), 1000);
} catch (err) {
console.log("connection failed: ",p.url)
return;
}
for(id in r.home.feed.portals){
if(r.home.feed.portals[id].url == p.url){
r.home.feed.portals[id] = p;
}
}
try {
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;
}
p.file = null;
} catch (err) {
console.log('parsing failed: ', p.url);
}
p.__entries_cache__ = null;
}
// Cache entries when possible.
this.__entries_map__ = {};
this.__entries_cache__;
this.entries = function()
{
if (this.__entries_cache__)
return this.__entries_cache__;
var e = this.__entries_cache__ = [];
var entry;
for (var id in this.json.feed) {
var raw = this.json.feed[id];
entry = this.__entries_map__[raw.timestamp];
if (entry == null)
this.__entries_map__[raw.timestamp] = entry = new Entry(this.json.feed[id], p);
else
entry.update(this.json.feed[id], p);
entry.id = id;
entry.is_mention = entry.detect_mention();
e[id] = entry;
}
this.last_entry = entry;
return e;
}
this.entries_remove = function() {
var entries = this.entries();
for (var id in entries) {
entries[id].remove_element();
}
}
this.relationship = function(target = r.home.portal.hashes_set())
{
if (this === r.home.feed.portal_rotonde) 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");
return create_rune("portal", "follow");
}
this.updated = function(include_edits = true)
{
if(this.json == null || this.json.feed == null){ return 0; }
if(this.json.feed.length < 1){ return 0; }
var max = 0;
for (var id in this.json.feed) {
var entry = this.json.feed[id];
var timestamp = (include_edits ? entry.editstamp : null) || entry.timestamp;
if (timestamp < max)
continue;
max = timestamp;
}
return max;
}
this.time_offset = function() // days
{
return parseInt((Date.now() - this.updated())/1000);
}
this.badge_add = function(special_class, container, c, cmin, cmax, offset)
{
if (c !== undefined && (c < 0 || c < cmin || cmax <= c)) {
// Out of bounds - remove if existing, don't add.
this.badge_remove();
return null;
}
var html = this.badge(special_class);
if (this.badge_element_html != html) {
if (this.badge_element == null) {
// Thin wrapper required.
this.badge_element = document.createElement('div');
this.badge_element.className = 'thin-wrapper';
}
this.badge_element.innerHTML = html;
this.badge_element_html = html;
container.appendChild(this.badge_element);
}
// If c !== undefined, the badge is being added to an ordered collection.
if (c !== undefined)
move_element(this.badge_element, c - cmin + offset);
return this.badge_element;
}
this.badge_remove = function() {
if (this.badge_element == null)
return;
// Simpler alternative than elem.parentElement.remove(elem);
this.badge_element.remove();
this.badge_element = null;
this.badge_element_html = null;
}
this.badge = function(special_class)
{
// Avoid 'null' class.
special_class = special_class || '';
var html = "";
html += "<img src='"+this.archive.url+"/media/content/icon.svg'/>";
html += "<a data-operation='"+this.url+"' href='"+this.url+"'>"+this.relationship()+r.escape_html(this.json.name)+"</a> ";
html += "<br />"
var updated = this.updated(false)
if(updated){
html += "<span class='time_ago'>"+timeSince(updated)+" ago</span>"
}
html += "<br />"
// Version
if(this.json.client_version){
// Used to check if the rotonde version matches when mod version is present.
var version_regex = /^[0-9.]+[a-z]?/;
var version_self = this.json.client_version.match(version_regex);
var version_portal = r.home.portal.json.client_version.match(version_regex);
var version_match =
// Don't compare if either string doesn't contain a match.
version_self &&
version_portal &&
version_self[0] == version_portal[0];
// The version to display.
var version = r.escape_html(this.json.client_version)
.split(/\r\n|\n/).slice(0, 2).join("<br>"); // Allow 2 lines for mod versions
html += "<span class='version "+(version_match ? 'same' : '')+"'>"+version+"</span>"
}
html += "<span>"+this.json.port.length+" Portals</span>"
return "<yu class='badge "+special_class+"' data-operation='"+(special_class === "discovery"?"":"un")+this.url+"'>"+html+"</yu>";
}
this.__hashes__ = null;
this.__hashes_set__ = null;
this.__hashes_urls__ = {};
this.__hashes_generate__ = function()
{
if (
this.__hashes_urls__.url == this.url &&
this.__hashes_urls__.archive_url == this.archive.url &&
this.__hashes_urls__.dat == this.dat
) return; // URLs didn't update - use cached hashes.
var hashes = this.__hashes__ = [];
var hash;
if (hash = to_hash(this.__hashes_urls__.url = this.url))
hashes.push(hash);
if (hash = to_hash(this.__hashes_urls__.archive_url = this.archive.url))
hashes.push(hash);
if (hash = to_hash(this.__hashes_urls__.dat = this.dat))
hashes.push(hash);
this.__hashes_set__ = new Set(hashes);
}
this.hashes = function()
{
this.__hashes_generate__();
return this.__hashes__;
}
this.hashes_set = function()
{
this.__hashes_generate__();
return this.__hashes_set__;
}
this.is_known = function(discovered)
{
var hashes = this.hashes_set();
for (var id in r.home.feed.portals) {
var lookup = r.home.feed.portals[id];
if (has_hash(hashes, lookup))
return true;
}
if (discovered) {
for (var id in r.home.discovered) {
var lookup = r.home.discovered[id];
if (has_hash(hashes, lookup))
return true;
}
}
return false;
}
}
function promiseTimeout(promise, timeout) {
return new Promise((resolve, reject) => {
var rejectout = setTimeout(() => reject(new Error("Promise hanging, timeout!")), timeout);
promise.then(
function() {
clearTimeout(rejectout);
resolve.apply(this, arguments);
},
function() {
clearTimeout(rejectout);
reject.apply(this, arguments);
}
);
});
}
function move_element(el, index) {
if (!el)
return;
var offset = index;
var tmp = el;
while (tmp = tmp.previousElementSibling)
offset--;
// offset == 0: We're fine.
if (offset == 0)
return;
if (offset < 0) {
// offset < 0: Element needs to be pushed "left" / "up".
// -offset is the "# of elements we expected there not to be",
// thus how many places we need to shift to the left.
var swap;
tmp = el;
while ((swap = tmp) && (tmp = tmp.previousElementSibling) && offset < 0)
offset++;
swap.before(el);
} else {
// offset > 0: Element needs to be pushed "right" / "down".
// offset is the "# of elements we expected before us but weren't there",
// thus how many places we need to shift to the right.
var swap;
tmp = el;
while ((swap = tmp) && (tmp = tmp.nextElementSibling) && offset > 0)
offset--;
swap.after(el);
}
}
r.confirm("script","portal");