-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
287 lines (248 loc) · 7.28 KB
/
index.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
"use strict";
function makeValidUrl(url)
{
let parsed = new URL(url);
if (parsed.protocol == "http:" || parsed.protocol == "https:")
return url;
else
return "https://" + url;
}
function restore()
{
for (let key of Object.keys(localStorage))
{
let element = document.getElementById(key);
if (element && element.localName == "input")
element.value = localStorage[key];
}
}
function save(input)
{
localStorage[input.id] = input.value;
}
function setProgress(text, error = false)
{
document.getElementById("progress-label").textContent = text;
if (error)
document.getElementById("progress").classList.add("error");
else
document.getElementById("progress").classList.remove("error");
}
function setError(text)
{
setProgress(text, true);
}
function parseAccountName(account)
{
let parts = account.trim().replace(/^@/, "").split("@");
if (parts.length != 2)
throw `Unexpected account name ${account}, expected two parts separated by @.`;
if (/[:\\/]/.test(parts[1]))
throw `Unexpected account name ${account}, host part cannot contain slashes or colons.`;
return {
user: parts[0].trim(),
host: parts[1].trim()
};
}
function parseNextLink(link)
{
for (let entry of String(link).split(/, /))
{
let match = /^<(.+?)>;\s*rel="next"/.exec(entry.trim());
if (match)
return makeValidUrl(match[1]);
}
return null;
}
async function apiCall(host, path)
{
let url = `https://${host}/api/v1/${path}`;
let result = null;
do
{
let response = await fetch(url, {
credentials: "omit",
});
if (result)
result.push(...await response.json());
else
result = await response.json();
if (Array.isArray(result))
url = parseNextLink(response.headers.get("link"));
else
url = null;
} while (url);
return result;
}
async function resolveAccount(account)
{
let {user, host} = parseAccountName(account);
let response;
try
{
response = await fetch(`https://${host}/.well-known/webfinger?resource=acct:${user}@${host}`);
}
catch (e)
{
console.error(e);
throw `Failed resolving account ${account}, maybe not a Mastodon server?`;
}
if (response.status == 404)
throw `Account ${account} does not exist.`;
else if (response.status != 200)
throw `Got response ${response.status} resolving account ${account}.`;
response = await response.json();
const prefix = "acct:";
if (!response.subject || !response.subject.startsWith(prefix))
throw `Unexpected response resolving account ${account}`;
let result = parseAccountName(response.subject.slice(prefix.length));
if (response.aliases && response.aliases.length)
{
try
{
result.host = new URL(response.aliases[0]).hostname || result.host;
}
catch (e)
{
}
}
return result;
}
function listToMap(list)
{
let map = new Map();
for (let account of list)
map.set(account.url, account);
return map;
}
function compareLists(followees1, followers1, followees2, followers2, account1, account2)
{
followees1 = listToMap(followees1);
followers1 = listToMap(followers1);
followees2 = listToMap(followees2);
followers2 = listToMap(followers2);
let seen = new Set();
let accepted = [];
for (let account of [...followees1.values(), ...followers1.values()])
{
if (seen.has(account.url))
continue;
seen.add(account.url);
if (!followees2.has(account.url) && !followers2.has(account.url))
continue;
let score = 0;
let note = [];
if (followees1.has(account.url) && followees2.has(account.url))
{
score += 3;
note.push("followed by both");
}
else if (followees1.has(account.url))
{
score += 1;
note.push(`followed by ${account1}`);
}
else if (followees2.has(account.url))
{
score += 1;
note.push(`followed by ${account2}`);
}
if (followers1.has(account.url) && followers2.has(account.url))
{
score += 5;
note.push("following both");
}
else if (followers1.has(account.url))
{
score += 2;
note.push(`following ${account1}`);
}
else if (followers2.has(account.url))
{
score += 2;
note.push(`following ${account2}`);
}
accepted.push({
score,
sortKey: (account.display_name || account.acct).toLowerCase(),
note: note.join(", "),
account
});
}
accepted.sort((a, b) =>
{
if (a.score != b.score)
return b.score - a.score;
else if (a.sortKey < b.sortKey)
return -1;
else if (a.sortKey > b.sortKey)
return 1;
return 0;
});
let output = document.getElementById("result");
if (accepted.length)
{
output.innerText = "";
for (let {account, note} of accepted)
{
let template = document.getElementById("account").content.cloneNode(true);
template.querySelector(".account").href = makeValidUrl(account.url);
template.querySelector(".avatar").src = makeValidUrl(account.avatar);
if (account.display_name)
template.querySelector(".name").textContent = account.display_name;
let acct = account.acct;
if (!acct.includes("@"))
acct += "@" + new URL(account.url).hostname;
template.querySelector(".id").textContent = acct;
template.querySelector(".note").textContent = note;
output.appendChild(template);
}
}
else
output.innerText = "No bubble intersections found.";
}
let comparing = false;
async function doCompare()
{
if (comparing)
return;
comparing = true;
document.getElementById("submit").disabled = true;
document.getElementById("progress").classList.remove("done");
try
{
let account1 = document.getElementById("account1").value;
let account2 = document.getElementById("account2").value;
if (account1 == account2)
throw "Please enter two different accounts.";
setProgress(`Resolving account ${account1}.`);
let {user: user1, host: host1} = await resolveAccount(account1);
let id1 = (await apiCall(host1, `accounts/lookup?acct=${encodeURIComponent(user1)}`)).id;
setProgress(`Resolving account ${account2}.`);
let {user: user2, host: host2} = await resolveAccount(account2);
let id2 = (await apiCall(host2, `accounts/lookup?acct=${encodeURIComponent(user2)}`)).id;
if (host1 == host2 && id1 == id2)
throw `Accounts ${account1} and ${account2} both resolve to the same account.`;
setProgress(`Fetching ${account1} followees.`);
let followees1 = await apiCall(host1, `accounts/${encodeURIComponent(id1)}/following?limit=100`);
setProgress(`Fetching ${account1} followers.`);
let followers1 = await apiCall(host1, `accounts/${encodeURIComponent(id1)}/followers?limit=100`);
setProgress(`Fetching ${account2} followees.`);
let followees2 = await apiCall(host2, `accounts/${encodeURIComponent(id2)}/following?limit=100`);
setProgress(`Fetching ${account2} followers.`);
let followers2 = await apiCall(host2, `accounts/${encodeURIComponent(id2)}/followers?limit=100`);
compareLists(followees1, followers1, followees2, followers2, account1, account2);
}
catch (e)
{
setError(e.toString());
if (typeof e != "string")
console.error(e);
}
finally
{
comparing = false;
document.getElementById("submit").disabled = false;
document.getElementById("progress").classList.add("done");
}
}