From d61c99f95a8fc23badeca0cf63de31a9271cd0c0 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 24 Jun 2023 15:55:54 +0800 Subject: [PATCH] feat: add shortcut related activity --- api/v1/activity.go | 12 ++++++++++++ api/v1/redirector.go | 31 +++++++++++++++++++++++++++++++ api/v1/shortcut.go | 30 ++++++++++++++++++++++++++++-- api/v1/user.go | 5 +++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 api/v1/activity.go diff --git a/api/v1/activity.go b/api/v1/activity.go new file mode 100644 index 0000000..dfb1898 --- /dev/null +++ b/api/v1/activity.go @@ -0,0 +1,12 @@ +package v1 + +type ActivityShorcutCreatePayload struct { + ShortcutID int `json:"shortcutId"` +} + +type ActivityShorcutViewPayload struct { + ShortcutID int `json:"shortcutId"` + IP string `json:"ip"` + Referer string `json:"referer"` + UserAgent string `json:"userAgent"` +} diff --git a/api/v1/redirector.go b/api/v1/redirector.go index b3eb3eb..a376387 100644 --- a/api/v1/redirector.go +++ b/api/v1/redirector.go @@ -1,10 +1,12 @@ package v1 import ( + "encoding/json" "net/http" "github.com/boojack/shortify/store" "github.com/labstack/echo/v4" + "github.com/pkg/errors" ) func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) { @@ -32,6 +34,35 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") } } + + if err := s.createShortcutViewActivity(c, shortcut); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) + } + return c.Redirect(http.StatusSeeOther, shortcut.Link) }) } + +func (s *APIV1Service) createShortcutViewActivity(c echo.Context, shortcut *store.Shortcut) error { + payload := &ActivityShorcutViewPayload{ + ShortcutID: shortcut.ID, + IP: c.RealIP(), + Referer: c.Request().Referer(), + UserAgent: c.Request().UserAgent(), + } + payloadStr, err := json.Marshal(payload) + if err != nil { + return errors.Wrap(err, "Failed to marshal activity payload") + } + activity := &store.Activity{ + CreatorID: BotID, + Type: store.ActivityShortcutView, + Level: store.ActivityInfo, + Payload: string(payloadStr), + } + _, err = s.Store.CreateActivity(c.Request().Context(), activity) + if err != nil { + return errors.Wrap(err, "Failed to create activity") + } + return nil +} diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index c5f3dbc..58e35b6 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/boojack/shortify/store" + "github.com/pkg/errors" "github.com/labstack/echo/v4" ) @@ -25,8 +26,8 @@ const ( VisibilityPrivate Visibility = "PRIVATE" ) -func (e Visibility) String() string { - switch e { +func (v Visibility) String() string { + switch v { case VisibilityPublic: return "PUBLIC" case VisibilityWorkspace: @@ -96,6 +97,10 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err) } + if err := s.createShortcutCreateActivity(ctx, shortcut); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut activity").SetInternal(err) + } + shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut)) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) @@ -244,6 +249,27 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { }) } +func (s *APIV1Service) createShortcutCreateActivity(ctx context.Context, shortcut *store.Shortcut) error { + payload := &ActivityShorcutCreatePayload{ + ShortcutID: shortcut.ID, + } + payloadStr, err := json.Marshal(payload) + if err != nil { + return errors.Wrap(err, "Failed to marshal activity payload") + } + activity := &store.Activity{ + CreatorID: shortcut.CreatorID, + Type: store.ActivityShortcutCreate, + Level: store.ActivityInfo, + Payload: string(payloadStr), + } + _, err = s.Store.CreateActivity(ctx, activity) + if err != nil { + return errors.Wrap(err, "Failed to create activity") + } + return nil +} + func (s *APIV1Service) composeShortcut(ctx context.Context, shortcut *Shortcut) (*Shortcut, error) { if shortcut == nil { return nil, nil diff --git a/api/v1/user.go b/api/v1/user.go index 040563e..2e12c12 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -13,6 +13,11 @@ import ( "golang.org/x/crypto/bcrypt" ) +const ( + // BotID is the id of bot. + BotID = 0 +) + // Role is the type of a role. type Role string