From 7549c807acde3be9f28d4110c326cad33d5b7e7d Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 1 Oct 2023 14:14:33 +0800 Subject: [PATCH] chore: update memo view activity --- api/v1/activity.go | 9 +++++++-- api/v1/common.go | 3 --- api/v1/memo.go | 27 +++++++++++++++++++++++++-- server/server.go | 7 +++---- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/api/v1/activity.go b/api/v1/activity.go index ff90bbf0..e667d802 100644 --- a/api/v1/activity.go +++ b/api/v1/activity.go @@ -25,6 +25,8 @@ const ( // ActivityMemoCreate is the type for creating memos. ActivityMemoCreate ActivityType = "memo.create" + // ActivityMemoView is the type for viewing memos. + ActivityMemoView ActivityType = "memo.view" // ActivityMemoUpdate is the type for updating memos. ActivityMemoUpdate ActivityType = "memo.update" // ActivityMemoDelete is the type for deleting memos. @@ -87,8 +89,11 @@ type ActivityUserAuthSignUpPayload struct { } type ActivityMemoCreatePayload struct { - Content string `json:"content"` - Visibility string `json:"visibility"` + MemoID int32 `json:"memoId"` +} + +type ActivityMemoViewPayload struct { + MemoID int32 `json:"memoId"` } type ActivityResourceCreatePayload struct { diff --git a/api/v1/common.go b/api/v1/common.go index b8a18ebb..a0b49bbc 100644 --- a/api/v1/common.go +++ b/api/v1/common.go @@ -1,8 +1,5 @@ package v1 -// UnknownID is the ID for unknowns. -const UnknownID = -1 - // RowStatus is the status for a row. type RowStatus string diff --git a/api/v1/memo.go b/api/v1/memo.go index 7534dbb3..39972155 100644 --- a/api/v1/memo.go +++ b/api/v1/memo.go @@ -555,6 +555,9 @@ func (s *APIV1Service) GetMemo(c echo.Context) error { return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session") } } + if err := s.createMemoViewActivity(c, memo, userID); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) + } memoResponse, err := s.convertMemoFromStore(ctx, memo) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err) @@ -753,8 +756,7 @@ func (s *APIV1Service) UpdateMemo(c echo.Context) error { func (s *APIV1Service) createMemoCreateActivity(ctx context.Context, memo *store.Memo) error { payload := ActivityMemoCreatePayload{ - Content: memo.Content, - Visibility: memo.Visibility.String(), + MemoID: memo.ID, } payloadBytes, err := json.Marshal(payload) if err != nil { @@ -772,6 +774,27 @@ func (s *APIV1Service) createMemoCreateActivity(ctx context.Context, memo *store return err } +func (s *APIV1Service) createMemoViewActivity(c echo.Context, memo *store.Memo, userID int32) error { + ctx := c.Request().Context() + payload := ActivityMemoViewPayload{ + MemoID: memo.ID, + } + payloadBytes, err := json.Marshal(payload) + if err != nil { + return errors.Wrap(err, "failed to marshal activity payload") + } + activity, err := s.Store.CreateActivity(ctx, &store.Activity{ + CreatorID: userID, + Type: string(ActivityMemoView), + Level: string(ActivityInfo), + Payload: string(payloadBytes), + }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } + return err +} + func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Memo) (*Memo, error) { memoResponse := &Memo{ ID: memo.ID, diff --git a/server/server.go b/server/server.go index 616860b4..3781b614 100644 --- a/server/server.go +++ b/server/server.go @@ -221,10 +221,9 @@ func (s *Server) createServerStartActivity(ctx context.Context) error { return errors.Wrap(err, "failed to marshal activity payload") } activity, err := s.Store.CreateActivity(ctx, &store.Activity{ - CreatorID: apiv1.UnknownID, - Type: apiv1.ActivityServerStart.String(), - Level: apiv1.ActivityInfo.String(), - Payload: string(payloadBytes), + Type: apiv1.ActivityServerStart.String(), + Level: apiv1.ActivityInfo.String(), + Payload: string(payloadBytes), }) if err != nil || activity == nil { return errors.Wrap(err, "failed to create activity")