diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f001504..1cd26b6 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,8 @@
-
-
-
-
+
+
@@ -154,8 +152,6 @@
-
-
@@ -179,7 +175,9 @@
-
+
+
+
true
diff --git a/pkg/internal/grpc/events.go b/pkg/internal/grpc/events.go
index a9ca492..e9a2a17 100644
--- a/pkg/internal/grpc/events.go
+++ b/pkg/internal/grpc/events.go
@@ -3,18 +3,17 @@ package grpc
import (
"context"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
- "git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
)
func (v *Server) RecordEvent(ctx context.Context, request *proto.RecordEventRequest) (*proto.RecordEventResponse, error) {
- var user models.Account
- var err error
- if user, err = services.GetAccount(uint(request.GetUserId())); err != nil {
- return nil, err
- }
-
- services.AddEvent(user, request.GetAction(), request.GetTarget(), request.GetIp(), request.GetUserAgent())
+ services.AddEvent(
+ uint(request.GetUserId()),
+ request.GetAction(),
+ request.GetTarget(),
+ request.GetIp(),
+ request.GetUserAgent(),
+ )
return &proto.RecordEventResponse{IsSuccess: true}, nil
}
diff --git a/pkg/internal/server/api/accounts_api.go b/pkg/internal/server/api/accounts_api.go
index 851b3f5..9f06241 100644
--- a/pkg/internal/server/api/accounts_api.go
+++ b/pkg/internal/server/api/accounts_api.go
@@ -155,6 +155,7 @@ func editUserinfo(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
+ services.AddEvent(user.ID, "profile.edit", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(account.ID)
return c.SendStatus(fiber.StatusOK)
diff --git a/pkg/internal/server/api/avatar_api.go b/pkg/internal/server/api/avatar_api.go
index 91bb432..13b9d5c 100644
--- a/pkg/internal/server/api/avatar_api.go
+++ b/pkg/internal/server/api/avatar_api.go
@@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
+ "strconv"
)
func setAvatar(c *fiber.Ctx) error {
@@ -27,6 +28,7 @@ func setAvatar(c *fiber.Ctx) error {
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
+ services.AddEvent(user.ID, "profile.edit.avatar", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(user.ID)
}
@@ -52,6 +54,7 @@ func setBanner(c *fiber.Ctx) error {
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
+ services.AddEvent(user.ID, "profile.edit.banner", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(user.ID)
}
diff --git a/pkg/internal/server/api/notifications_api.go b/pkg/internal/server/api/notifications_api.go
index 1ff58ae..4cd2b23 100644
--- a/pkg/internal/server/api/notifications_api.go
+++ b/pkg/internal/server/api/notifications_api.go
@@ -7,6 +7,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
+ "strconv"
"time"
)
@@ -66,6 +67,7 @@ func markNotificationRead(c *fiber.Ctx) error {
if err := database.C.Save(¬ify).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
+ services.AddEvent(user.ID, "notifications.mark.read", strconv.Itoa(int(notify.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK)
}
}
@@ -89,6 +91,7 @@ func markNotificationReadBatch(c *fiber.Ctx) error {
Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
+ services.AddEvent(user.ID, "notifications.markAll.read", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK)
}
}
@@ -145,6 +148,7 @@ func addNotifySubscriber(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
+ services.AddEvent(user.ID, "notifications.subscribe.push", data.DeviceID, c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(subscriber)
}
@@ -163,5 +167,6 @@ func removeNotifySubscriber(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
+ services.AddEvent(user.ID, "notifications.unsubscribe.push", device, c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK)
}
diff --git a/pkg/internal/server/api/oauth_api.go b/pkg/internal/server/api/oauth_api.go
index 97fc407..ec5fcde 100755
--- a/pkg/internal/server/api/oauth_api.go
+++ b/pkg/internal/server/api/oauth_api.go
@@ -95,7 +95,7 @@ func authorizeThirdClient(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
- services.AddEvent(user, "oauth.connect", client.Alias, c.IP(), c.Get(fiber.HeaderUserAgent))
+ services.AddEvent(user.ID, "oauth.connect", client.Alias, c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(fiber.Map{
"ticket": ticket,
"redirect_uri": redirect,
@@ -118,7 +118,7 @@ func authorizeThirdClient(c *fiber.Ctx) error {
} else if access, refresh, err := services.GetToken(ticket); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
- services.AddEvent(user, "oauth.connect", client.Alias, c.IP(), c.Get(fiber.HeaderUserAgent))
+ services.AddEvent(user.ID, "oauth.connect", client.Alias, c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(fiber.Map{
"access_token": access,
"refresh_token": refresh,
diff --git a/pkg/internal/server/api/preferences_api.go b/pkg/internal/server/api/preferences_api.go
index 46b62db..aca5c76 100644
--- a/pkg/internal/server/api/preferences_api.go
+++ b/pkg/internal/server/api/preferences_api.go
@@ -35,6 +35,8 @@ func updateAuthPreference(c *fiber.Ctx) error {
cfg, err := services.UpdateAuthPreference(user, data)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ services.AddEvent(user.ID, "preferences.edit", "auth", c.IP(), c.Get(fiber.HeaderUserAgent))
}
return c.JSON(cfg.Config.Data())
@@ -70,6 +72,8 @@ func updateNotificationPreference(c *fiber.Ctx) error {
notification, err := services.UpdateNotificationPreference(user, data.Config)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ services.AddEvent(user.ID, "preferences.edit", "notifications", c.IP(), c.Get(fiber.HeaderUserAgent))
}
return c.JSON(notification)
diff --git a/pkg/internal/server/api/realms_api.go b/pkg/internal/server/api/realms_api.go
index 728209e..7ae6b8a 100644
--- a/pkg/internal/server/api/realms_api.go
+++ b/pkg/internal/server/api/realms_api.go
@@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
+ "strconv"
)
func getRealm(c *fiber.Ctx) error {
@@ -85,7 +86,10 @@ func createRealm(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ services.AddEvent(user.ID, "realms.new", strconv.Itoa(int(realm.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
}
+
return c.JSON(realm)
}
@@ -131,6 +135,8 @@ func editRealm(c *fiber.Ctx) error {
realm, err := services.EditRealm(realm)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ services.AddEvent(user.ID, "realms.edit", strconv.Itoa(int(realm.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
}
return c.JSON(realm)
@@ -153,6 +159,8 @@ func deleteRealm(c *fiber.Ctx) error {
if err := services.DeleteRealm(realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ services.AddEvent(user.ID, "realms.delete", strconv.Itoa(int(realm.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
}
return c.SendStatus(fiber.StatusOK)
diff --git a/pkg/internal/server/api/relationships_api.go b/pkg/internal/server/api/relationships_api.go
index 77fe8d6..ca194f2 100644
--- a/pkg/internal/server/api/relationships_api.go
+++ b/pkg/internal/server/api/relationships_api.go
@@ -5,6 +5,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
+ "strconv"
)
func listRelationship(c *fiber.Ctx) error {
@@ -75,6 +76,7 @@ func editRelationship(c *fiber.Ctx) error {
if friendship, err := services.EditRelationship(relationship); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "relationships.edit", strconv.Itoa(int(relationship.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friendship)
}
}
@@ -98,6 +100,7 @@ func deleteRelationship(c *fiber.Ctx) error {
if err := services.DeleteRelationship(relationship); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "relationships.delete", strconv.Itoa(int(relationship.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(relationship)
}
}
@@ -132,6 +135,7 @@ func makeFriendship(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "relationships.friends.new", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friend)
}
}
@@ -164,6 +168,7 @@ func makeBlockship(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "relationships.blocks.new", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friend)
}
}
@@ -183,6 +188,7 @@ func acceptFriend(c *fiber.Ctx) error {
if err := services.HandleFriend(user, related, true); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "relationships.friends.accept", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK)
}
}
@@ -202,6 +208,7 @@ func declineFriend(c *fiber.Ctx) error {
if err := services.HandleFriend(user, related, false); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "relationships.friends.decline", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK)
}
}
diff --git a/pkg/internal/server/api/reports.go b/pkg/internal/server/api/reports_api.go
similarity index 100%
rename from pkg/internal/server/api/reports.go
rename to pkg/internal/server/api/reports_api.go
diff --git a/pkg/internal/server/api/sign_api.go b/pkg/internal/server/api/sign_api.go
index 5721a84..69bd3c1 100644
--- a/pkg/internal/server/api/sign_api.go
+++ b/pkg/internal/server/api/sign_api.go
@@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
+ "strconv"
)
func listDailySignRecord(c *fiber.Ctx) error {
@@ -98,6 +99,7 @@ func doDailySign(c *fiber.Ctx) error {
if record, err := services.DailySign(user); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "dailySign", strconv.Itoa(int(record.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(record)
}
}
diff --git a/pkg/internal/server/api/statuses_api.go b/pkg/internal/server/api/statuses_api.go
index 9135268..e8d3725 100644
--- a/pkg/internal/server/api/statuses_api.go
+++ b/pkg/internal/server/api/statuses_api.go
@@ -2,6 +2,7 @@ package api
import (
"fmt"
+ "strconv"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
@@ -90,6 +91,7 @@ func setStatus(c *fiber.Ctx) error {
if status, err := services.NewStatus(user, status); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "statuses.set", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(status)
}
}
@@ -128,6 +130,7 @@ func editStatus(c *fiber.Ctx) error {
if status, err := services.EditStatus(user, status); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
+ services.AddEvent(user.ID, "statuses.edit", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(status)
}
}
@@ -140,6 +143,8 @@ func clearStatus(c *fiber.Ctx) error {
if err := services.ClearStatus(user); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
+ } else {
+ services.AddEvent(user.ID, "statuses.clear", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
}
return c.SendStatus(fiber.StatusOK)
diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go
index 295e7fa..fcfbc4e 100644
--- a/pkg/internal/services/events.go
+++ b/pkg/internal/services/events.go
@@ -10,13 +10,13 @@ var writeEventQueue []models.ActionEvent
var writeAuditQueue []models.AuditRecord
// AddEvent to keep operation logs by user themselves clear to query
-func AddEvent(user models.Account, event, target, ip, ua string) {
+func AddEvent(user uint, event, target, ip, ua string) {
writeEventQueue = append(writeEventQueue, models.ActionEvent{
Type: event,
Target: target,
IpAddress: ip,
UserAgent: ua,
- AccountID: user.ID,
+ AccountID: user,
})
}