From 184fc3ecbae545351f9d8585dd57caa047511778 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 19 Sep 2024 22:23:32 +0800 Subject: [PATCH] :sparkles: Support broadcast deletion --- go.mod | 2 +- go.sum | 2 ++ pkg/internal/database/migrator.go | 4 ++-- pkg/internal/grpc/server.go | 2 ++ pkg/internal/grpc/services.go | 29 +++++++++++++++++++++++++++++ pkg/internal/services/cleaner.go | 2 +- 6 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 pkg/internal/grpc/services.go diff --git a/go.mod b/go.mod index 1c7bd25..b5689cf 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 toolchain go1.22.1 require ( - git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f + git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827 github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 7e7773c..3f466a0 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 h1:ucko8l9rC git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f h1:3jLpcws4/zmNUA60w1RtAtGNjcQd5NZCcbW5HQcUcvw= 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= 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/database/migrator.go b/pkg/internal/database/migrator.go index 3a18d47..c3cc0db 100644 --- a/pkg/internal/database/migrator.go +++ b/pkg/internal/database/migrator.go @@ -5,7 +5,7 @@ import ( "gorm.io/gorm" ) -var DatabaseAutoActionRange = []any{ +var AutoMaintainRange = []any{ &models.Account{}, &models.Realm{}, &models.Channel{}, @@ -15,7 +15,7 @@ var DatabaseAutoActionRange = []any{ } func RunMigration(source *gorm.DB) error { - if err := source.AutoMigrate(DatabaseAutoActionRange...); err != nil { + if err := source.AutoMigrate(AutoMaintainRange...); err != nil { return err } diff --git a/pkg/internal/grpc/server.go b/pkg/internal/grpc/server.go index 86fd3ed..c72db88 100644 --- a/pkg/internal/grpc/server.go +++ b/pkg/internal/grpc/server.go @@ -12,6 +12,7 @@ import ( type Server struct { proto.UnimplementedStreamControllerServer + proto.UnimplementedServiceDirectoryServer } var S *grpc.Server @@ -21,6 +22,7 @@ func NewGRPC() { health.RegisterHealthServer(S, &Server{}) proto.RegisterStreamControllerServer(S, &Server{}) + proto.RegisterServiceDirectoryServer(S, &Server{}) reflection.Register(S) } diff --git a/pkg/internal/grpc/services.go b/pkg/internal/grpc/services.go new file mode 100644 index 0000000..7b0b0a4 --- /dev/null +++ b/pkg/internal/grpc/services.go @@ -0,0 +1,29 @@ +package grpc + +import ( + "context" + "strconv" + + "git.solsynth.dev/hydrogen/dealer/pkg/proto" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" +) + +func (v *Server) BroadcastDeletion(ctx context.Context, request *proto.DeletionRequest) (*proto.DeletionResponse, error) { + switch request.GetResourceType() { + case "account": + numericId, err := strconv.Atoi(request.GetResourceId()) + if err != nil { + break + } + for _, model := range database.AutoMaintainRange { + switch model.(type) { + default: + database.C.Delete(model, "account_id = ?", numericId) + } + } + database.C.Delete(&models.Account{}, "id = ?", numericId) + } + + return &proto.DeletionResponse{}, nil +} diff --git a/pkg/internal/services/cleaner.go b/pkg/internal/services/cleaner.go index ebfd435..a0948f6 100644 --- a/pkg/internal/services/cleaner.go +++ b/pkg/internal/services/cleaner.go @@ -13,7 +13,7 @@ func DoAutoDatabaseCleanup() { // Deal soft-deletion var count int64 - for _, model := range database.DatabaseAutoActionRange { + for _, model := range database.AutoMaintainRange { tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline) if tx.Error != nil { log.Error().Err(tx.Error).Msg("An error occurred when running database cleanup...")