From 24672e0c5eda674b60b147ed95b803abe34a7fc6 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 15 Dec 2023 08:12:10 +0800 Subject: [PATCH] chore: update memo metadata --- server/frontend/frontend.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go index 81eea94c..68620876 100644 --- a/server/frontend/frontend.go +++ b/server/frontend/frontend.go @@ -67,35 +67,44 @@ func (s *FrontendService) registerRoutes(e *echo.Echo) { ctx := c.Request().Context() memoID, err := util.ConvertStringToInt32(c.Param("memoID")) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "invalid memo id") + // Redirect to `index.html` if any error occurs. + return c.HTML(http.StatusOK, rawIndexHTML) } memo, err := s.Store.GetMemo(ctx, &store.FindMemo{ ID: &memoID, }) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "failed to retrieve memo") + return c.HTML(http.StatusOK, rawIndexHTML) } if memo == nil { - return echo.NewHTTPError(http.StatusNotFound, "memo not found") + return c.HTML(http.StatusOK, rawIndexHTML) } if memo.Visibility != store.Public { - return echo.NewHTTPError(http.StatusForbidden, "memo is not public") + return c.HTML(http.StatusOK, rawIndexHTML) } - indexHTML := strings.ReplaceAll(rawIndexHTML, "", generateMemoMetadata(memo)) + creator, err := s.Store.GetUser(ctx, &store.FindUser{ + ID: &memo.CreatorID, + }) + if err != nil { + return c.HTML(http.StatusOK, rawIndexHTML) + } + + // Inject memo metadata into `index.html`. + indexHTML := strings.ReplaceAll(rawIndexHTML, "", generateMemoMetadata(memo, creator)) return c.HTML(http.StatusOK, indexHTML) }) } -func generateMemoMetadata(memo *store.Memo) string { +func generateMemoMetadata(memo *store.Memo, creator *store.User) string { metadataList := []string{ fmt.Sprintf(``, memo.Content), - fmt.Sprintf(``, fmt.Sprintf("Memos - %d", memo.ID)), + fmt.Sprintf(``, fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username)), fmt.Sprintf(``, memo.Content), fmt.Sprintf(``, "https://www.usememos.com/logo.png"), ``, // Twitter related metadata. - fmt.Sprintf(``, fmt.Sprintf("Memos - %d", memo.ID)), + fmt.Sprintf(``, fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username)), fmt.Sprintf(``, memo.Content), fmt.Sprintf(``, "https://www.usememos.com/logo.png"), ``,