From fa7c267f26a6f635a9ef400e6ac49593e76d6b19 Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 28 Dec 2022 22:42:04 +0000 Subject: [PATCH] Add /@username routes for profile pages https://codeberg.org/silverpill/mitra/issues/22 --- CHANGELOG.md | 4 ++++ src/api/users.ts | 13 +++++++++++++ src/router/index.ts | 6 ++++++ src/views/Profile.vue | 17 +++++++++++++---- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c070687..dde40a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Added + +- Added `/@username` routes for profile pages. + ### Changed - Changed text on Ethereum page. diff --git a/src/api/users.ts b/src/api/users.ts index 4e5e239..ba1f52a 100644 --- a/src/api/users.ts +++ b/src/api/users.ts @@ -187,6 +187,19 @@ export async function getCurrentUser(authToken: string): Promise { return data } +export async function lookupProfile( + authToken: string | null, + acct: string, +): Promise { + const url = `${BACKEND_URL}/api/v1/accounts/lookup` + const response = await http(url, { authToken, queryParams: { acct } }) + const data = await response.json() + if (response.status !== 200) { + throw new Error(data.message) + } + return data +} + export async function getProfile( authToken: string | null, profileId: string, diff --git a/src/router/index.ts b/src/router/index.ts index 679cb88..bb067d6 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -146,6 +146,12 @@ const routes: Array = [ component: SubscriptionsSettings, meta: { onlyAuthenticated: true }, }, + { + path: "/@:acct(.*)", + name: "profile-by-acct", + component: ProfileView, + meta: { }, + }, { path: "/:pathMatch(.*)*", redirect: { name: "home" }, diff --git a/src/views/Profile.vue b/src/views/Profile.vue index b36ea16..2a91931 100644 --- a/src/views/Profile.vue +++ b/src/views/Profile.vue @@ -256,6 +256,7 @@ import { import { getReceivedSubscriptions } from "@/api/subscriptions-common" import { getProfile, + lookupProfile, Profile, ProfileField, ProfileWrapper, @@ -296,10 +297,18 @@ let followListNextPageUrl = $ref(null) onMounted(async () => { isLoading = true try { - const _profile = await getProfile( - authToken, - route.params.profileId as string, - ) + let _profile + if (route.params.acct) { + _profile = await lookupProfile( + authToken, + route.params.acct as string, + ) + } else { + _profile = await getProfile( + authToken, + route.params.profileId as string, + ) + } profile = new ProfileWrapper(_profile) } catch (error: any) { if (error.message === "profile not found") {