Skip to content

Instantly share code, notes, and snippets.

@ltlapy
Created November 10, 2022 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltlapy/0621d88579f152316ae202ec963e4ca9 to your computer and use it in GitHub Desktop.
Save ltlapy/0621d88579f152316ae202ec963e4ca9 to your computer and use it in GitHub Desktop.
users = [
"@localuser",
"@remoteuser@instance.tld",
];
uid_bulk_lookup = async function (instanceUrl, token, userlist) {
let uidList = [];
let failList = [];
const fediuserregex = new RegExp('@([0-9a-zA-Z_]+)(?:@([0-9a-zA-Z_.]+))?');
if (!Array.isArray(userlist)) {
console.error("userlist Array expected, got", typeof (userlist));
return null;
}
// 때리!자
let username, host;
for (const element of userlist) {
if (typeof (element) !== 'string') {
console.warning(element, "is not a string");
continue;
}
// 뼈와 살을 바릅니다
[_, username, host] = element.match(fediuserregex);
const reqbody = {
'i': token,
'username': username,
'host': host,
};
// request를 짜서 날립니다
// console.log("fetching", element);
// [username, host, reqbody].forEach(s => console.log(s));
const res = await fetch(instanceUrl + "/api/users/show", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(reqbody),
});
// 결과를 기록합니다
res.json().then((data) => {
if (data.error) {
throw Error(data.error.message);
}
uidList.push(data.id);
}).catch((err) => {
console.warning("Failed to fetch uid of", element, err);
failList.push(element);
});
};
return [uidList, failList];
};
addUserInList = async function (token, listId, userId) {
const reqbody = {
'i': token,
'listId': listId,
'userId': userId,
};
// request를 짜서 날립니다
// console.log("fetching", element);
// [username, host, reqbody].forEach(s => console.log(s));
return await fetch(instanceUrl + "/api/users/lists/push", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(reqbody),
});
};
async function main() {
let res;
const token = "";
const listId = "";
const instanceUrl = "https://k.lapy.link"
res = await uid_bulk_lookup(instanceUrl, token, users);
console.log(`Adding ${res[0].length} users to list...`);
res[0].forEach(uid => addUserInList(instanceUrl, token, listId, uid));
console.warn('failed users:', JSON.stringify(res[1]));
return;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment