diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f0963a..0aa06bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Added "Aliases" page. +- Form for adding aliases. ## [1.19.0] - 2023-03-30 diff --git a/src/api/search.ts b/src/api/search.ts index 8ed7b93..587b141 100644 --- a/src/api/search.ts +++ b/src/api/search.ts @@ -29,12 +29,16 @@ export async function getSearchResults( } export async function searchProfilesByAcct( + authToken: string | null, acct: string, + resolve: boolean = false, + limit = 40, ): Promise { const url = `${BACKEND_URL}/api/v1/accounts/search` const response = await http(url, { method: "GET", - queryParams: { q: acct }, + queryParams: { q: acct, resolve, limit }, + authToken, }) const data = await response.json() if (response.status !== 200) { diff --git a/src/api/settings.ts b/src/api/settings.ts index 9ac42ca..1cec7e1 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -1,6 +1,6 @@ import { BACKEND_URL } from "@/constants" import { http } from "./common" -import { User } from "./users" +import { Aliases, User } from "./users" export async function changePassword( authToken: string, @@ -20,6 +20,24 @@ export async function changePassword( } } +export async function addAlias( + authToken: string, + acct: string, +): Promise { + const url = `${BACKEND_URL}/api/v1/settings/aliases` + const response = await http(url, { + method: "POST", + json: { acct: acct }, + authToken, + }) + const data = await response.json() + if (response.status !== 200) { + throw new Error(data.error_description) + } else { + return data + } +} + async function downloadBlob(blob: Blob, name: string) { const fileUrl = window.URL.createObjectURL(blob) const hiddenLink = document.createElement("a") diff --git a/src/components/SubscriptionMonero.vue b/src/components/SubscriptionMonero.vue index 0f0ccc7..976ac0a 100644 --- a/src/components/SubscriptionMonero.vue +++ b/src/components/SubscriptionMonero.vue @@ -194,7 +194,7 @@ async function identifySender() { return } isLoading = true - const profiles = await searchProfilesByAcct(senderAcct) + const profiles = await searchProfilesByAcct(null, senderAcct) if (profiles.length > 1) { senderError = "Please provide full address" } else { diff --git a/src/views/ProfileAliases.vue b/src/views/ProfileAliases.vue index 7afd5b3..dd1e5d3 100644 --- a/src/views/ProfileAliases.vue +++ b/src/views/ProfileAliases.vue @@ -22,9 +22,40 @@ -
- No aliases found -
+
+

Add alias

+
+
+ +
+ +
+
+ +
+ {{ newAliasError }} +
+
+
@@ -34,16 +65,21 @@ import { onMounted } from "vue" import { $, $ref } from "vue/macros" -import { getAliases, Aliases } from "@/api/users" +import { searchProfilesByAcct } from "@/api/search" +import { addAlias } from "@/api/settings" +import { getAliases, Aliases, Profile } from "@/api/users" import SidebarLayout from "@/components/SidebarLayout.vue" import Loader from "@/components/Loader.vue" import ProfileListItem from "@/components/ProfileListItem.vue" import { useCurrentUser } from "@/store/user" -const { ensureCurrentUser } = $(useCurrentUser()) +const { ensureCurrentUser, ensureAuthToken } = $(useCurrentUser()) let aliases = $ref({ declared: [], verified: [] }) let isLoading = $ref(false) +let newAlias = $ref("") +let newAliasSuggestions = $ref([]) +let newAliasError = $ref(null) onMounted(async () => { isLoading = true @@ -51,13 +87,37 @@ onMounted(async () => { isLoading = false }) -function isEmpty(): boolean { - return aliases.declared.length === 0 && aliases.verified.length === 0 +function canAddAlias(): boolean { + return newAlias.length > 0 && newAliasError === null +} + +async function onAddAlias() { + isLoading = true + const profiles = await searchProfilesByAcct( + ensureAuthToken(), + newAlias, + true, + 5, + ) + if (profiles.length === 0) { + newAliasError = "Profile not found" + isLoading = false + return + } + if (profiles.length === 1 && profiles[0].acct === newAlias) { + aliases = await addAlias(ensureAuthToken(), newAlias) + newAlias = "" + } else { + newAliasSuggestions = profiles + } + isLoading = false }