mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 21:19:44 +00:00
feat: add shortcut related activity
This commit is contained in:
parent
81ac8d9c5d
commit
d61c99f95a
12
api/v1/activity.go
Normal file
12
api/v1/activity.go
Normal file
@ -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"`
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/boojack/shortify/store"
|
"github.com/boojack/shortify/store"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) {
|
func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) {
|
||||||
@ -32,6 +34,35 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
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)
|
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
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/boojack/shortify/store"
|
"github.com/boojack/shortify/store"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
@ -25,8 +26,8 @@ const (
|
|||||||
VisibilityPrivate Visibility = "PRIVATE"
|
VisibilityPrivate Visibility = "PRIVATE"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e Visibility) String() string {
|
func (v Visibility) String() string {
|
||||||
switch e {
|
switch v {
|
||||||
case VisibilityPublic:
|
case VisibilityPublic:
|
||||||
return "PUBLIC"
|
return "PUBLIC"
|
||||||
case VisibilityWorkspace:
|
case VisibilityWorkspace:
|
||||||
@ -96,6 +97,10 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err)
|
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))
|
shortcutMessage, err := s.composeShortcut(ctx, convertShortcutFromStore(shortcut))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err)
|
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) {
|
func (s *APIV1Service) composeShortcut(ctx context.Context, shortcut *Shortcut) (*Shortcut, error) {
|
||||||
if shortcut == nil {
|
if shortcut == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -13,6 +13,11 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// BotID is the id of bot.
|
||||||
|
BotID = 0
|
||||||
|
)
|
||||||
|
|
||||||
// Role is the type of a role.
|
// Role is the type of a role.
|
||||||
type Role string
|
type Role string
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user