Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: update memo stats api (#387)

boojackef5492074e02
diff --git a/server/acl.go b/server/acl.go
index b826562e1..3580abe34 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -104,7 +104,7 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
}
}
- if common.HasPrefixes(path, "/api/memo/all", "/api/memo/:memoId") && c.Request().Method == http.MethodGet {
+ if common.HasPrefixes(path, "/api/memo/all", "/api/memo/:memoId", "/api/memo/amount", "/api/memo/stats") && c.Request().Method == http.MethodGet {
return next(c)
}
diff --git a/server/memo.go b/server/memo.go
index 7ee1cab75..a31077636 100644
--- a/server/memo.go
+++ b/server/memo.go
@@ -229,13 +229,35 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil
})
+ g.GET("/memo/amount", func(c echo.Context) error {
+ ctx := c.Request().Context()
+ normalRowStatus := api.Normal
+ memoFind := &api.MemoFind{
+ RowStatus: &normalRowStatus,
+ }
+ if userID, err := strconv.Atoi(c.QueryParam("userId")); err == nil {
+ memoFind.CreatorID = &userID
+ }
+
+ memoList, err := s.Store.FindMemoList(ctx, memoFind)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(len(memoList))); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
+ }
+ return nil
+ })
+
g.GET("/memo/stats", func(c echo.Context) error {
ctx := c.Request().Context()
normalStatus := api.Normal
memoFind := &api.MemoFind{
RowStatus: &normalStatus,
}
- if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
+ if userID, err := strconv.Atoi(c.QueryParam("userId")); err == nil {
memoFind.CreatorID = &userID
}
@@ -525,28 +547,4 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return c.JSON(http.StatusOK, true)
})
-
- g.GET("/memo/amount", func(c echo.Context) error {
- ctx := c.Request().Context()
- userID, ok := c.Get(getUserIDContextKey()).(int)
- if !ok {
- return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
- }
- normalRowStatus := api.Normal
- memoFind := &api.MemoFind{
- CreatorID: &userID,
- RowStatus: &normalRowStatus,
- }
-
- memoList, err := s.Store.FindMemoList(ctx, memoFind)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
- }
-
- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(len(memoList))); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
- }
- return nil
- })
}
diff --git a/web/src/components/UsageHeatMap.tsx b/web/src/components/UsageHeatMap.tsx
index 5bf998c36..0a650bf83 100644
--- a/web/src/components/UsageHeatMap.tsx
+++ b/web/src/components/UsageHeatMap.tsx
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { useAppSelector } from "../store";
-import { locationService } from "../services";
+import { locationService, userService } from "../services";
import { getMemoStats } from "../helpers/api";
import { DAILY_TIMESTAMP } from "../helpers/consts";
import * as utils from "../helpers/utils";
@@ -28,19 +28,18 @@ interface DailyUsageStat {
}
const UsageHeatMap = () => {
+ const { memos } = useAppSelector((state) => state.memo);
+ const [allStat, setAllStat] = useState<DailyUsageStat[]>([]);
+ const [currentStat, setCurrentStat] = useState<DailyUsageStat | null>(null);
+ const containerElRef = useRef<HTMLDivElement>(null);
const todayTimeStamp = utils.getDateStampByDate(Date.now());
const todayDay = new Date(todayTimeStamp).getDay() + 1;
const nullCell = new Array(7 - todayDay).fill(0);
const usedDaysAmount = (tableConfig.width - 1) * tableConfig.height + todayDay;
const beginDayTimestemp = todayTimeStamp - usedDaysAmount * DAILY_TIMESTAMP;
- const { memos } = useAppSelector((state) => state.memo);
- const [allStat, setAllStat] = useState<DailyUsageStat[]>(getInitialUsageStat(usedDaysAmount, beginDayTimestemp));
- const [currentStat, setCurrentStat] = useState<DailyUsageStat | null>(null);
- const containerElRef = useRef<HTMLDivElement>(null);
-
useEffect(() => {
- getMemoStats()
+ getMemoStats(userService.getCurrentUserId())
.then(({ data: { data } }) => {
const newStat: DailyUsageStat[] = getInitialUsageStat(usedDaysAmount, beginDayTimestemp);
for (const record of data) {
diff --git a/web/src/components/UserBanner.tsx b/web/src/components/UserBanner.tsx
index e465e3ff7..21f7627ad 100644
--- a/web/src/components/UserBanner.tsx
+++ b/web/src/components/UserBanner.tsx
@@ -37,7 +37,7 @@ const UserBanner = () => {
}, [isVisitorMode, user, owner]);
useEffect(() => {
- getMemoStats()
+ getMemoStats(userService.getCurrentUserId())
.then(({ data: { data } }) => {
setMemoAmount(data.length);
})
diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts
index 7b576af2f..0988de8f8 100644
--- a/web/src/helpers/api.ts
+++ b/web/src/helpers/api.ts
@@ -90,8 +90,8 @@ export function getMemoList(memoFind?: MemoFind) {
return axios.get<ResponseObject<Memo[]>>(`/api/memo?${queryList.join("&")}`);
}
-export function getMemoStats() {
- return axios.get<ResponseObject<number[]>>(`/api/memo/stats`);
+export function getMemoStats(userId: UserId) {
+ return axios.get<ResponseObject<number[]>>(`/api/memo/stats?userId=${userId}`);
}
export function getMemoById(id: MemoId) {
diff --git a/web/src/services/userService.ts b/web/src/services/userService.ts
index d03681412..2d3c5ee78 100644
--- a/web/src/services/userService.ts
+++ b/web/src/services/userService.ts
@@ -1,5 +1,6 @@
import { locationService } from ".";
import * as api from "../helpers/api";
+import { UNKNOWN_ID } from "../helpers/consts";
import store from "../store";
import { setUser, patchUser, setHost, setOwner } from "../store/modules/user";
@@ -57,6 +58,14 @@ const userService = {
}
},
+ getCurrentUserId: () => {
+ if (userService.isVisitorMode()) {
+ return userService.getUserIdFromPath() || UNKNOWN_ID;
+ } else {
+ return userService.getState().user?.id || UNKNOWN_ID;
+ }
+ },
+
isVisitorMode: () => {
return !(userService.getUserIdFromPath() === undefined);
},