fix: get user by username api (#2034)

This commit is contained in:
boojack 2023-07-26 22:41:21 +08:00 committed by GitHub
parent 56c321aeaa
commit d8d6de9fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 21 additions and 43 deletions

View File

@ -542,13 +542,6 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
findMemoMessage.Pinned = &pinned findMemoMessage.Pinned = &pinned
} }
if username := c.QueryParam("creatorUsername"); username != "" {
user, _ := s.Store.GetUser(ctx, &store.FindUser{Username: &username})
if user != nil {
findMemoMessage.CreatorID = &user.ID
}
}
contentSearch := []string{} contentSearch := []string{}
tag := c.QueryParam("tag") tag := c.QueryParam("tag")
if tag != "" { if tag != "" {

View File

@ -258,8 +258,9 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) {
return c.JSON(http.StatusOK, userMessage) return c.JSON(http.StatusOK, userMessage)
}) })
// GET /user/:username - Get user by username. // GET /user/name/:username - Get user by username.
g.GET("/user/:username", func(c echo.Context) error { // NOTE: This should be moved to /api/v2/user/:username
g.GET("/user/name/:username", func(c echo.Context) error {
ctx := c.Request().Context() ctx := c.Request().Context()
username := c.Param("username") username := c.Param("username")
user, err := s.Store.GetUser(ctx, &store.FindUser{Username: &username}) user, err := s.Store.GetUser(ctx, &store.FindUser{Username: &username})

View File

@ -17,15 +17,11 @@ interface Props {
const ResourceItemDropdown = ({ resource }: Props) => { const ResourceItemDropdown = ({ resource }: Props) => {
const t = useTranslate(); const t = useTranslate();
const resourceStore = useResourceStore(); const resourceStore = useResourceStore();
const resources = resourceStore.state.resources;
const handlePreviewBtnClick = (resource: Resource) => { const handlePreviewBtnClick = (resource: Resource) => {
const resourceUrl = getResourceUrl(resource); const resourceUrl = getResourceUrl(resource);
if (resource.type.startsWith("image")) { if (resource.type.startsWith("image")) {
showPreviewImageDialog( showPreviewImageDialog([getResourceUrl(resource)], 0);
resources.filter((r) => r.type.startsWith("image")).map((r) => getResourceUrl(r)),
resources.findIndex((r) => r.id === resource.id)
);
} else { } else {
window.open(resourceUrl); window.open(resourceUrl);
} }

View File

@ -57,7 +57,7 @@ export function getUserList() {
} }
export function getUserByUsername(username: string) { export function getUserByUsername(username: string) {
return axios.get<User>(`/api/v1/user/${username}`); return axios.get<User>(`/api/v1/user/name/${username}`);
} }
export function upsertUserSetting(upsert: UserSettingUpsert) { export function upsertUserSetting(upsert: UserSettingUpsert) {

View File

@ -40,7 +40,7 @@ const Archived = () => {
<section className="w-full min-h-full flex flex-col md:flex-row justify-start items-start px-4 sm:px-2 sm:pt-4 pb-8 bg-zinc-100 dark:bg-zinc-800"> <section className="w-full min-h-full flex flex-col md:flex-row justify-start items-start px-4 sm:px-2 sm:pt-4 pb-8 bg-zinc-100 dark:bg-zinc-800">
<MobileHeader showSearch={false} /> <MobileHeader showSearch={false} />
<div className="archived-memo-page"> <div className="archived-memo-page">
<div className="mb-4 mt-2 w-full"> <div className="mb-2 mt-2 w-full">
<SearchBar /> <SearchBar />
</div> </div>
<MemoFilter /> <MemoFilter />

View File

@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import { useTranslate } from "@/utils/i18n"; import { useTranslate } from "@/utils/i18n";
import { useLocation } from "react-router-dom"; import { useLocation } from "react-router-dom";
import { useFilterStore, useMemoStore, useUserStore } from "@/store/module"; import { useFilterStore, useGlobalStore, useMemoStore } from "@/store/module";
import { TAG_REG } from "@/labs/marked/parser"; import { TAG_REG } from "@/labs/marked/parser";
import { DEFAULT_MEMO_LIMIT } from "@/helpers/consts"; import { DEFAULT_MEMO_LIMIT } from "@/helpers/consts";
import useLoading from "@/hooks/useLoading"; import useLoading from "@/hooks/useLoading";
@ -15,9 +15,9 @@ import SearchBar from "@/components/SearchBar";
const Explore = () => { const Explore = () => {
const t = useTranslate(); const t = useTranslate();
const location = useLocation(); const location = useLocation();
const globalStore = useGlobalStore();
const filterStore = useFilterStore(); const filterStore = useFilterStore();
const memoStore = useMemoStore(); const memoStore = useMemoStore();
const userStore = useUserStore();
const filter = filterStore.state; const filter = filterStore.state;
const { memos } = memoStore.state; const { memos } = memoStore.state;
const [isComplete, setIsComplete] = useState<boolean>(false); const [isComplete, setIsComplete] = useState<boolean>(false);
@ -55,19 +55,13 @@ const Explore = () => {
}) })
: memos; : memos;
const username = userStore.getUsernameFromPath(); const sortedMemos = fetchedMemos
let sortedMemos = fetchedMemos
.filter((m) => m.rowStatus === "NORMAL" && m.visibility !== "PRIVATE") .filter((m) => m.rowStatus === "NORMAL" && m.visibility !== "PRIVATE")
.sort((mi, mj) => mj.displayTs - mi.displayTs); .sort((mi, mj) => mj.displayTs - mi.displayTs);
if (username != undefined) {
sortedMemos = sortedMemos.filter((m) => m.creatorUsername === username);
}
useEffect(() => { useEffect(() => {
const username = userStore.getUsernameFromPath();
memoStore memoStore
.fetchAllMemos(DEFAULT_MEMO_LIMIT, 0, username) .fetchAllMemos(DEFAULT_MEMO_LIMIT, 0)
.then((fetchedMemos) => { .then((fetchedMemos) => {
if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) { if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) {
setIsComplete(true); setIsComplete(true);
@ -82,8 +76,7 @@ const Explore = () => {
const handleFetchMoreClick = async () => { const handleFetchMoreClick = async () => {
try { try {
const username = userStore.getUsernameFromPath(); const fetchedMemos = await memoStore.fetchAllMemos(DEFAULT_MEMO_LIMIT, memos.length);
const fetchedMemos = await memoStore.fetchAllMemos(DEFAULT_MEMO_LIMIT, memos.length, username);
if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) { if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) {
setIsComplete(true); setIsComplete(true);
} else { } else {
@ -98,9 +91,11 @@ const Explore = () => {
return ( return (
<section className="w-full max-w-3xl min-h-full flex flex-col justify-start items-center px-4 sm:px-2 sm:pt-4 pb-8 bg-zinc-100 dark:bg-zinc-800"> <section className="w-full max-w-3xl min-h-full flex flex-col justify-start items-center px-4 sm:px-2 sm:pt-4 pb-8 bg-zinc-100 dark:bg-zinc-800">
<MobileHeader showSearch={false} /> <MobileHeader showSearch={false} />
<div className="mb-4 mt-2 w-full"> {globalStore.isDev() && (
<SearchBar /> <div className="mb-4 mt-2 w-full">
</div> <SearchBar />
</div>
)}
{!loadingState.isLoading && ( {!loadingState.isLoading && (
<main className="relative w-full h-auto flex flex-col justify-start items-start"> <main className="relative w-full h-auto flex flex-col justify-start items-start">
<MemoFilter /> <MemoFilter />

View File

@ -16,11 +16,9 @@ const Home = () => {
useEffect(() => { useEffect(() => {
const currentUsername = userStore.getCurrentUsername(); const currentUsername = userStore.getCurrentUsername();
userStore.getUserByUsername(currentUsername).then((user) => { userStore.getUserByUsername(currentUsername).catch((error) => {
if (!user) { console.error(error);
toast.error(t("message.user-not-found")); toast.error(t("message.user-not-found"));
return;
}
}); });
}, [userStore.getCurrentUsername()]); }, [userStore.getCurrentUsername()]);

View File

@ -81,7 +81,7 @@ const router = createBrowserRouter([
}, },
{ {
path: "u/:username", path: "u/:username",
element: <Explore />, element: <Home />,
loader: async () => { loader: async () => {
await initialGlobalStateLoader(); await initialGlobalStateLoader();

View File

@ -54,7 +54,7 @@ export const useMemoStore = () => {
return fetchedMemos; return fetchedMemos;
}, },
fetchAllMemos: async (limit = DEFAULT_MEMO_LIMIT, offset?: number, username?: string) => { fetchAllMemos: async (limit = DEFAULT_MEMO_LIMIT, offset?: number) => {
store.dispatch(setIsFetching(true)); store.dispatch(setIsFetching(true));
const memoFind: MemoFind = { const memoFind: MemoFind = {
rowStatus: "NORMAL", rowStatus: "NORMAL",
@ -62,10 +62,6 @@ export const useMemoStore = () => {
offset, offset,
}; };
if (username != undefined) {
memoFind.creatorUsername = username;
}
const { data } = await api.getAllMemos(memoFind); const { data } = await api.getAllMemos(memoFind);
const fetchedMemos = data.map((m) => convertResponseModelMemo(m)); const fetchedMemos = data.map((m) => convertResponseModelMemo(m));
store.dispatch(upsertMemos(fetchedMemos)); store.dispatch(upsertMemos(fetchedMemos));

View File

@ -3,7 +3,6 @@ type ShortcutId = number;
interface Shortcut { interface Shortcut {
id: ShortcutId; id: ShortcutId;
creatorUsername: string;
rowStatus: RowStatus; rowStatus: RowStatus;
createdTs: TimeStamp; createdTs: TimeStamp;
updatedTs: TimeStamp; updatedTs: TimeStamp;