More event logs point

This commit is contained in:
LittleSheep 2024-10-14 22:23:51 +08:00
parent a4a139c56e
commit 974c5d9700
13 changed files with 51 additions and 19 deletions

12
.idea/workspace.xml generated
View File

@ -4,10 +4,8 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Bug fixes on settings auth preferences"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: More event logs point">
<change afterPath="$PROJECT_DIR$/pkg/internal/grpc/events.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/server/api/sign_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/sign_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/grpc/server.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/server.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -154,8 +152,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Bot related bot key apis" />
<MESSAGE value=":bug: Fix bot related bot key apis path error" />
<MESSAGE value=":bug: Fix path parameters misplaced" /> <MESSAGE value=":bug: Fix path parameters misplaced" />
<MESSAGE value=":bug: Fix api key wasn't in auto maintain range" /> <MESSAGE value=":bug: Fix api key wasn't in auto maintain range" />
<MESSAGE value=":bug: Fix api key missing account id" /> <MESSAGE value=":bug: Fix api key missing account id" />
@ -179,7 +175,9 @@
<MESSAGE value=":recycle: Improve notifications mark read system" /> <MESSAGE value=":recycle: Improve notifications mark read system" />
<MESSAGE value=":bug: Bug fixes on multi-factors based authentication" /> <MESSAGE value=":bug: Bug fixes on multi-factors based authentication" />
<MESSAGE value=":bug: Bug fixes on settings auth preferences" /> <MESSAGE value=":bug: Bug fixes on settings auth preferences" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Bug fixes on settings auth preferences" /> <MESSAGE value=":sparkles: Implement event recorder grpc" />
<MESSAGE value=":sparkles: More event logs point" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: More event logs point" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -3,18 +3,17 @@ package grpc
import ( import (
"context" "context"
"git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
) )
func (v *Server) RecordEvent(ctx context.Context, request *proto.RecordEventRequest) (*proto.RecordEventResponse, error) { func (v *Server) RecordEvent(ctx context.Context, request *proto.RecordEventRequest) (*proto.RecordEventResponse, error) {
var user models.Account services.AddEvent(
var err error uint(request.GetUserId()),
if user, err = services.GetAccount(uint(request.GetUserId())); err != nil { request.GetAction(),
return nil, err request.GetTarget(),
} request.GetIp(),
request.GetUserAgent(),
services.AddEvent(user, request.GetAction(), request.GetTarget(), request.GetIp(), request.GetUserAgent()) )
return &proto.RecordEventResponse{IsSuccess: true}, nil return &proto.RecordEventResponse{IsSuccess: true}, nil
} }

View File

@ -155,6 +155,7 @@ func editUserinfo(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.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) services.InvalidAuthCacheWithUser(account.ID)
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)

View File

@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strconv"
) )
func setAvatar(c *fiber.Ctx) error { 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 { if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
services.AddEvent(user.ID, "profile.edit.avatar", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(user.ID) services.InvalidAuthCacheWithUser(user.ID)
} }
@ -52,6 +54,7 @@ func setBanner(c *fiber.Ctx) error {
if err := database.C.Save(&user).Error; err != nil { if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
services.AddEvent(user.ID, "profile.edit.banner", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(user.ID) services.InvalidAuthCacheWithUser(user.ID)
} }

View File

@ -7,6 +7,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/samber/lo" "github.com/samber/lo"
"strconv"
"time" "time"
) )
@ -66,6 +67,7 @@ func markNotificationRead(c *fiber.Ctx) error {
if err := database.C.Save(&notify).Error; err != nil { if err := database.C.Save(&notify).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
services.AddEvent(user.ID, "notifications.mark.read", strconv.Itoa(int(notify.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK) 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 { Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
services.AddEvent(user.ID, "notifications.markAll.read", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }
} }
@ -145,6 +148,7 @@ func addNotifySubscriber(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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) return c.JSON(subscriber)
} }
@ -163,5 +167,6 @@ func removeNotifySubscriber(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.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) return c.SendStatus(fiber.StatusOK)
} }

View File

@ -95,7 +95,7 @@ func authorizeThirdClient(c *fiber.Ctx) error {
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } 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{ return c.JSON(fiber.Map{
"ticket": ticket, "ticket": ticket,
"redirect_uri": redirect, "redirect_uri": redirect,
@ -118,7 +118,7 @@ func authorizeThirdClient(c *fiber.Ctx) error {
} else if access, refresh, err := services.GetToken(ticket); err != nil { } else if access, refresh, err := services.GetToken(ticket); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } 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{ return c.JSON(fiber.Map{
"access_token": access, "access_token": access,
"refresh_token": refresh, "refresh_token": refresh,

View File

@ -35,6 +35,8 @@ func updateAuthPreference(c *fiber.Ctx) error {
cfg, err := services.UpdateAuthPreference(user, data) cfg, err := services.UpdateAuthPreference(user, data)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) 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()) return c.JSON(cfg.Config.Data())
@ -70,6 +72,8 @@ func updateNotificationPreference(c *fiber.Ctx) error {
notification, err := services.UpdateNotificationPreference(user, data.Config) notification, err := services.UpdateNotificationPreference(user, data.Config)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) 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) return c.JSON(notification)

View File

@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strconv"
) )
func getRealm(c *fiber.Ctx) error { func getRealm(c *fiber.Ctx) error {
@ -85,7 +86,10 @@ func createRealm(c *fiber.Ctx) error {
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) 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) return c.JSON(realm)
} }
@ -131,6 +135,8 @@ func editRealm(c *fiber.Ctx) error {
realm, err := services.EditRealm(realm) realm, err := services.EditRealm(realm)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) 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) return c.JSON(realm)
@ -153,6 +159,8 @@ func deleteRealm(c *fiber.Ctx) error {
if err := services.DeleteRealm(realm); err != nil { if err := services.DeleteRealm(realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) 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) return c.SendStatus(fiber.StatusOK)

View File

@ -5,6 +5,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strconv"
) )
func listRelationship(c *fiber.Ctx) error { func listRelationship(c *fiber.Ctx) error {
@ -75,6 +76,7 @@ func editRelationship(c *fiber.Ctx) error {
if friendship, err := services.EditRelationship(relationship); err != nil { if friendship, err := services.EditRelationship(relationship); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "relationships.edit", strconv.Itoa(int(relationship.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friendship) return c.JSON(friendship)
} }
} }
@ -98,6 +100,7 @@ func deleteRelationship(c *fiber.Ctx) error {
if err := services.DeleteRelationship(relationship); err != nil { if err := services.DeleteRelationship(relationship); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "relationships.delete", strconv.Itoa(int(relationship.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(relationship) return c.JSON(relationship)
} }
} }
@ -132,6 +135,7 @@ func makeFriendship(c *fiber.Ctx) error {
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "relationships.friends.new", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friend) return c.JSON(friend)
} }
} }
@ -164,6 +168,7 @@ func makeBlockship(c *fiber.Ctx) error {
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "relationships.blocks.new", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friend) return c.JSON(friend)
} }
} }
@ -183,6 +188,7 @@ func acceptFriend(c *fiber.Ctx) error {
if err := services.HandleFriend(user, related, true); err != nil { if err := services.HandleFriend(user, related, true); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "relationships.friends.accept", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }
} }
@ -202,6 +208,7 @@ func declineFriend(c *fiber.Ctx) error {
if err := services.HandleFriend(user, related, false); err != nil { if err := services.HandleFriend(user, related, false); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "relationships.friends.decline", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }
} }

View File

@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services" "git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strconv"
) )
func listDailySignRecord(c *fiber.Ctx) error { func listDailySignRecord(c *fiber.Ctx) error {
@ -98,6 +99,7 @@ func doDailySign(c *fiber.Ctx) error {
if record, err := services.DailySign(user); err != nil { if record, err := services.DailySign(user); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "dailySign", strconv.Itoa(int(record.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(record) return c.JSON(record)
} }
} }

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"strconv"
"time" "time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database" "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 { if status, err := services.NewStatus(user, status); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "statuses.set", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(status) return c.JSON(status)
} }
} }
@ -128,6 +130,7 @@ func editStatus(c *fiber.Ctx) error {
if status, err := services.EditStatus(user, status); err != nil { if status, err := services.EditStatus(user, status); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
services.AddEvent(user.ID, "statuses.edit", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(status) return c.JSON(status)
} }
} }
@ -140,6 +143,8 @@ func clearStatus(c *fiber.Ctx) error {
if err := services.ClearStatus(user); err != nil { if err := services.ClearStatus(user); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) 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) return c.SendStatus(fiber.StatusOK)

View File

@ -10,13 +10,13 @@ var writeEventQueue []models.ActionEvent
var writeAuditQueue []models.AuditRecord var writeAuditQueue []models.AuditRecord
// AddEvent to keep operation logs by user themselves clear to query // 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{ writeEventQueue = append(writeEventQueue, models.ActionEvent{
Type: event, Type: event,
Target: target, Target: target,
IpAddress: ip, IpAddress: ip,
UserAgent: ua, UserAgent: ua,
AccountID: user.ID, AccountID: user,
}) })
} }