Record events into audit logs

This commit is contained in:
LittleSheep 2024-10-14 21:30:02 +08:00
parent 362e691b37
commit 6f26a44838
6 changed files with 124 additions and 5 deletions

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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 {
_ = gap.H.RecordAuditLog(
user.ID,
"posts.edit",
strconv.Itoa(int(item.ID)),
c.IP(),
c.Get(fiber.HeaderUserAgent),
)
}
return c.JSON(item)
}
}

View File

@ -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)
}
}

View File

@ -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 {
_ = gap.H.RecordAuditLog(
user.ID,
"posts.edit",
strconv.Itoa(int(item.ID)),
c.IP(),
c.Get(fiber.HeaderUserAgent),
)
}
return c.JSON(item)
}
}

View File

@ -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)
}