From 6f26a4483833e79b197b23dac1c49e050d3839e0 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 14 Oct 2024 21:30:02 +0800 Subject: [PATCH] :sparkles: Record events into audit logs --- go.mod | 2 +- go.sum | 2 + pkg/internal/server/api/articles_api.go | 22 ++++++++- pkg/internal/server/api/posts_api.go | 32 +++++++++++++ pkg/internal/server/api/stories_api.go | 22 ++++++++- pkg/internal/server/api/subscriptions_api.go | 49 ++++++++++++++++++++ 6 files changed, 124 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index af1582d..64fce12 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.solsynth.dev/hydrogen/interactive go 1.21.6 require ( - git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827 + git.solsynth.dev/hydrogen/dealer v0.0.0-20241014131829-4de0c4c969ad github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index fc26847..f293fad 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f h1:3jLpcws4/ git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827 h1:1ACMPm2ArRpVNYrND/y/R6oPiuMfKe49fP+lG3mcNug= git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= +git.solsynth.dev/hydrogen/dealer v0.0.0-20241014131829-4de0c4c969ad h1:QzK7mg+02jvdfjsXu89KMP3l/QoAMRyWgGDuNzUvTpg= +git.solsynth.dev/hydrogen/dealer v0.0.0-20241014131829-4de0c4c969ad/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/pkg/internal/server/api/articles_api.go b/pkg/internal/server/api/articles_api.go index feefa5e..4e76170 100644 --- a/pkg/internal/server/api/articles_api.go +++ b/pkg/internal/server/api/articles_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "strconv" "time" "git.solsynth.dev/hydrogen/dealer/pkg/hyper" @@ -94,6 +95,14 @@ func createArticle(c *fiber.Ctx) error { item, err := services.NewPost(user, item) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + _ = gap.H.RecordAuditLog( + user.ID, + "posts.new", + strconv.Itoa(int(item.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) } return c.JSON(item) @@ -188,9 +197,18 @@ func editArticle(c *fiber.Ctx) error { } } - if item, err := services.EditPost(item); err != nil { + var err error + if item, err = services.EditPost(item); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { - return c.JSON(item) + _ = gap.H.RecordAuditLog( + user.ID, + "posts.edit", + strconv.Itoa(int(item.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) } + + return c.JSON(item) } diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 1df9033..2f39d15 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -266,6 +266,14 @@ func deletePost(c *fiber.Ctx) error { if err := services.DeletePost(item); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + _ = gap.H.RecordAuditLog( + user.ID, + "posts.delete", + strconv.Itoa(int(item.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) } return c.SendStatus(fiber.StatusOK) @@ -302,6 +310,14 @@ func reactPost(c *fiber.Ctx) error { if positive, reaction, err := services.ReactPost(user, reaction); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { + _ = gap.H.RecordAuditLog( + user.ID, + "posts.react", + strconv.Itoa(int(res.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction) } } @@ -320,8 +336,24 @@ func pinPost(c *fiber.Ctx) error { if status, err := services.PinPost(res); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else if status { + _ = gap.H.RecordAuditLog( + user.ID, + "posts.pin", + strconv.Itoa(int(res.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.SendStatus(fiber.StatusOK) } else { + _ = gap.H.RecordAuditLog( + user.ID, + "posts.unpin", + strconv.Itoa(int(res.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.SendStatus(fiber.StatusNoContent) } } diff --git a/pkg/internal/server/api/stories_api.go b/pkg/internal/server/api/stories_api.go index e899af3..950adff 100644 --- a/pkg/internal/server/api/stories_api.go +++ b/pkg/internal/server/api/stories_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "strconv" "time" "git.solsynth.dev/hydrogen/dealer/pkg/hyper" @@ -113,6 +114,14 @@ func createStory(c *fiber.Ctx) error { item, err := services.NewPost(user, item) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + _ = gap.H.RecordAuditLog( + user.ID, + "posts.new", + strconv.Itoa(int(item.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) } return c.JSON(item) @@ -207,9 +216,18 @@ func editStory(c *fiber.Ctx) error { } } - if item, err := services.EditPost(item); err != nil { + var err error + if item, err = services.EditPost(item); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { - return c.JSON(item) + _ = gap.H.RecordAuditLog( + user.ID, + "posts.edit", + strconv.Itoa(int(item.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) } + + return c.JSON(item) } diff --git a/pkg/internal/server/api/subscriptions_api.go b/pkg/internal/server/api/subscriptions_api.go index 289214a..5b417fb 100644 --- a/pkg/internal/server/api/subscriptions_api.go +++ b/pkg/internal/server/api/subscriptions_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "strconv" "git.solsynth.dev/hydrogen/interactive/pkg/internal/gap" "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" @@ -92,6 +93,14 @@ func subscribeToUser(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to user: %v", err)) } + _ = gap.H.RecordAuditLog( + user.ID, + "posts.subscribe.users", + strconv.Itoa(int(otherUser.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.JSON(subscription) } @@ -112,6 +121,14 @@ func subscribeToTag(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to tag: %v", err)) } + _ = gap.H.RecordAuditLog( + user.ID, + "posts.subscribe.tags", + strconv.Itoa(int(tag.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.JSON(subscription) } @@ -132,6 +149,14 @@ func subscribeToCategory(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to category: %v", err)) } + _ = gap.H.RecordAuditLog( + user.ID, + "posts.subscribe.categories", + strconv.Itoa(int(category.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.JSON(subscription) } @@ -152,6 +177,14 @@ func unsubscribeFromUser(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from user: %v", err)) } + _ = gap.H.RecordAuditLog( + user.ID, + "posts.unsubscribe.users", + strconv.Itoa(int(otherUser.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.SendStatus(fiber.StatusOK) } @@ -172,6 +205,14 @@ func unsubscribeFromTag(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from tag: %v", err)) } + _ = gap.H.RecordAuditLog( + user.ID, + "posts.unsubscribe.tags", + strconv.Itoa(int(tag.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.SendStatus(fiber.StatusOK) } @@ -192,5 +233,13 @@ func unsubscribeFromCategory(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from category: %v", err)) } + _ = gap.H.RecordAuditLog( + user.ID, + "posts.unsubscribe.categories", + strconv.Itoa(int(category.ID)), + c.IP(), + c.Get(fiber.HeaderUserAgent), + ) + return c.SendStatus(fiber.StatusOK) }