Skip to content

Instantly share code, notes, and snippets.

@bodil
Created November 8, 2022 22:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodil/7b896079ad1e76859cb43ee53d407524 to your computer and use it in GitHub Desktop.
Save bodil/7b896079ad1e76859cb43ee53d407524 to your computer and use it in GitHub Desktop.
Add migration aliases to a Pleroma account
import generator, { OAuth } from "megalodon";
import PleromaAPI from "megalodon/lib/src/pleroma/api_client";
import { DEFAULT_UA } from "megalodon/lib/src/default";
import * as readline from "readline-sync";
async function main() {
const baseUrl = readline.question('URL of Pleroma instance (ex. "https://lol.camp"): ');
const registerClient = generator("pleroma", baseUrl);
const appData = await registerClient.registerApp("User Alias Editor", {});
const clientId = appData.clientId;
const clientSecret = appData.clientSecret;
console.log("Log in using the following URL:", appData.url);
const accessCode = readline.question("Enter the access code: ");
const tokenData = await registerClient.fetchAccessToken(clientId, clientSecret, accessCode);
const accessToken = tokenData.accessToken;
const aliases: string[] = [];
console.log("To find your current aliases, use the following command:");
console.log(
" curl 'https://my.old.server/.well-known/webfinger?resource=username@my.old.server' | jq .aliases"
);
console.log("Enter your aliases, one per line, blank line to end:");
let count = 0;
while (true) {
count++;
const alias = readline.question(`${count}: `);
if (alias.length > 0) {
aliases.push(alias);
} else {
break;
}
}
const rawClient = new PleromaAPI.Client(baseUrl, accessToken, DEFAULT_UA, false);
const result: any = await rawClient.patch("/api/v1/accounts/update_credentials", {
also_known_as: aliases,
});
console.log("Result:", result.data.pleroma.also_known_as);
}
main().then(
() => process.exit(0),
(err) => {
console.error(err);
process.exit(1);
}
);
{
"name": "pleroma-add-user-aliases",
"version": "1.0.0",
"author": "Bodil",
"license": "GPL-3.0+",
"dependencies": {
"@types/node": "^18.11.9",
"@types/readline-sync": "^1.4.4",
"megalodon": "^4.1.1",
"readline-sync": "^1.4.10",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"scripts": {
"start": "ts-node main.ts"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment