refactor (client): clean up conditions a bit

This commit is contained in:
naskya 2024-04-26 02:03:15 +09:00
parent 171e2f3973
commit 2d15d6dcfa
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C

View file

@ -15,64 +15,60 @@ if (acctUri == null) {
throw new Error("acct required");
}
let useThisAccount = isSignedIn(me) ? true : false;
// If the user is already logged in, ask whether to follow using the current account.
if (useThisAccount) {
if (isSignedIn(me)) {
const { canceled } = await os.confirm({
type: "question",
text: i18n.ts.useThisAccountConfirm,
});
// use the current account
if (!canceled) {
waiting();
window.location.href = `/authorize-follow?acct=${acctUri}`;
} else {
useThisAccount = false;
}
}
if (!useThisAccount) {
// Ask the user what the account ID is
const remoteAccountId = await os.inputText({
text: i18n.ts.inputAccountId,
});
// Otherwise ask the user what the other account ID is
const remoteAccountId = await os.inputText({
text: i18n.ts.inputAccountId,
});
// If the user do not want enter uri, the user will be redirected to the user page.
if (!remoteAccountId.result) {
// If the user do not want enter uri, the user will be redirected to the user page.
if (!remoteAccountId.result) {
waiting();
window.location.href = `/@${acctUri}`;
} else {
const remoteAcctInfo = acct.parse(remoteAccountId.result);
// If the user on this server, redirect directly
if (remoteAcctInfo.host === hostRaw || remoteAcctInfo.host === null) {
waiting();
window.location.href = `/@${acctUri}`;
window.location.href = `/authorize-follow?acct=${acctUri}`;
} else {
const remoteAcctInfo = acct.parse(remoteAccountId.result);
// If the user on this server, redirect directly
if (remoteAcctInfo.host === hostRaw || remoteAcctInfo.host === null) {
waiting();
window.location.href = `/authorize-follow?acct=${acctUri}`;
} else {
waiting();
// If not, find the interaction url through webfinger interface
fetch(
`https://${remoteAcctInfo.host}/.well-known/webfinger?resource=${remoteAcctInfo.username}@${remoteAcctInfo.host}`,
{
method: "GET",
},
)
.then((response) => response.json())
.then((data) => {
const subscribeUri = data.links.find(
(link) => link.rel === "http://ostatus.org/schema/1.0/subscribe",
).template;
window.location.href = subscribeUri.replace(
"{uri}",
acctUri.includes("@") ? acctUri : `${acctUri}@${hostRaw}`,
);
})
.catch((e) => {
// TODO: It would be better to provide more information, but the priority of
// waiting component is too high and the pop-up window will be blocked.
window.location.href = `/@${acctUri}`;
});
}
waiting();
// If not, find the interaction url through webfinger interface
fetch(
`https://${remoteAcctInfo.host}/.well-known/webfinger?resource=${remoteAcctInfo.username}@${remoteAcctInfo.host}`,
{
method: "GET",
},
)
.then((response) => response.json())
.then((data) => {
const subscribeUri = data.links.find(
(link: { rel: string; }) => link.rel === "http://ostatus.org/schema/1.0/subscribe",
).template;
window.location.href = subscribeUri.replace(
"{uri}",
acctUri.includes("@") ? acctUri : `${acctUri}@${hostRaw}`,
);
})
.catch((_) => {
// TODO: It would be better to provide more information, but the priority of
// waiting component is too high and the pop-up window will be blocked.
window.location.href = `/@${acctUri}`;
});
}
}
</script>