-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.js
66 lines (59 loc) · 1.73 KB
/
common.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
async function get_follows(apiClient, target) {
const t = target.startsWith('@') ? target.substring(1) : target;
const user = await apiClient.users.getUserByName(t);
if (!user) {
return [];
}
const cursor = apiClient.users.getFollowsPaginated({ user: user.id });
const follows = [];
for await (const f of cursor) {
follows.push(f.followedUserName)
}
return follows;
}
async function get_followers(apiClient, target) {
const t = target.startsWith('@') ? target.substring(1) : target;
const user = await apiClient.users.getUserByName(t);
if (!user) {
return [];
}
const cursor = apiClient.users.getFollowsPaginated({ followedUser: user.id });
const followers = [];
for await (const f of cursor) {
followers.push(f.userName)
}
return followers;
}
async function command(apiClient, user, target) {
if (!user) {
return null;
}
const my_follows = await get_follows(apiClient, user);
const my_followers = await get_followers(apiClient, user);
if (!target) {
return null;
}
const your_follows = await get_follows(apiClient, target);
const your_followers = await get_followers(apiClient, target);
const common_follows = [];
for (x of my_follows) {
for (y of your_follows) {
if (x == y) {
common_follows.push(x);
}
}
}
const common_followers = [];
for (x of my_followers) {
for (y of your_followers) {
if (x == y) {
common_followers.push(x);
}
}
}
return {
'common_follows': common_follows,
'common_followers': common_followers
};
}
module.exports = { command };