From 14e3750bd9cae5c8eceb6f21feb36453c98cba25 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 11 Sep 2024 23:58:02 +0800 Subject: [PATCH] :recycle: Use dealer's BaseModel --- go.sum | 6 ------ pkg/internal/gap/server.go | 3 ++- pkg/internal/grpc/health.go | 3 ++- pkg/internal/grpc/server.go | 3 ++- pkg/internal/models/base.go | 17 ----------------- pkg/internal/models/calls.go | 6 ++++-- pkg/internal/models/channels.go | 10 +++++++--- pkg/internal/models/events.go | 7 +++++-- pkg/internal/models/realms.go | 4 +++- pkg/internal/server/api/channels_api.go | 6 ++++-- pkg/internal/server/api/direct_channels_api.go | 1 + pkg/internal/server/api/realms_api.go | 1 + pkg/internal/server/api/whats_new_api.go | 1 + pkg/internal/services/calls.go | 3 ++- pkg/internal/services/channels.go | 3 ++- pkg/internal/services/direct_channels.go | 1 + pkg/internal/services/events.go | 5 +++-- pkg/internal/services/realms.go | 3 ++- pkg/internal/services/websocket.go | 3 ++- pkg/main.go | 7 ++++--- 20 files changed, 48 insertions(+), 45 deletions(-) delete mode 100644 pkg/internal/models/base.go diff --git a/go.sum b/go.sum index 611cbd8..c131b7f 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,3 @@ -git.solsynth.dev/hydrogen/dealer v0.0.0-20240823105014-4f93800f7283 h1:dfSqc32SSIBsUkhRTKzk90C4v8YCQObavUIrw/+ZfQc= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240823105014-4f93800f7283/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240823110807-df5676cbe4d8 h1:2uoqdm2Z5yhJck0udcx9EAPHaoNse0pPZihJG5J8Q8A= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240823110807-df5676cbe4d8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240823113302-208da7e90fdb h1:dv4uVDMe53eBprW2Q8ocAhZuO+DKWlWyxGiJMiwE62E= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240823113302-208da7e90fdb/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8 h1:kWheneSdSySG5tz9TAXrtr546JdMpQZTyWDFk4jeGwg= git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= diff --git a/pkg/internal/gap/server.go b/pkg/internal/gap/server.go index c6c026b..08fb42b 100644 --- a/pkg/internal/gap/server.go +++ b/pkg/internal/gap/server.go @@ -2,10 +2,11 @@ package gap import ( "fmt" + "strings" + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/rs/zerolog/log" - "strings" "github.com/spf13/viper" ) diff --git a/pkg/internal/grpc/health.go b/pkg/internal/grpc/health.go index d71549a..d8d2721 100644 --- a/pkg/internal/grpc/health.go +++ b/pkg/internal/grpc/health.go @@ -2,8 +2,9 @@ package grpc import ( "context" - health "google.golang.org/grpc/health/grpc_health_v1" "time" + + health "google.golang.org/grpc/health/grpc_health_v1" ) func (v *Server) Check(ctx context.Context, request *health.HealthCheckRequest) (*health.HealthCheckResponse, error) { diff --git a/pkg/internal/grpc/server.go b/pkg/internal/grpc/server.go index e9995a4..86fd3ed 100644 --- a/pkg/internal/grpc/server.go +++ b/pkg/internal/grpc/server.go @@ -1,12 +1,13 @@ package grpc import ( + "net" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/spf13/viper" "google.golang.org/grpc" health "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/reflection" - "net" ) type Server struct { diff --git a/pkg/internal/models/base.go b/pkg/internal/models/base.go deleted file mode 100644 index 0052acd..0000000 --- a/pkg/internal/models/base.go +++ /dev/null @@ -1,17 +0,0 @@ -package models - -import ( - "time" - - "gorm.io/datatypes" - "gorm.io/gorm" -) - -type JSONMap = datatypes.JSONType[map[string]any] - -type BaseModel struct { - ID uint `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"` -} diff --git a/pkg/internal/models/calls.go b/pkg/internal/models/calls.go index e20114b..b48c323 100644 --- a/pkg/internal/models/calls.go +++ b/pkg/internal/models/calls.go @@ -1,12 +1,14 @@ package models import ( - "github.com/livekit/protocol/livekit" "time" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "github.com/livekit/protocol/livekit" ) type Call struct { - BaseModel + hyper.BaseModel EndedAt *time.Time `json:"ended_at"` diff --git a/pkg/internal/models/channels.go b/pkg/internal/models/channels.go index 9d12e9b..65874c7 100644 --- a/pkg/internal/models/channels.go +++ b/pkg/internal/models/channels.go @@ -1,6 +1,10 @@ package models -import "fmt" +import ( + "fmt" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" +) type ChannelType = uint8 @@ -10,7 +14,7 @@ const ( ) type Channel struct { - BaseModel + hyper.BaseModel Alias string `json:"alias"` Name string `json:"name"` @@ -46,7 +50,7 @@ const ( ) type ChannelMember struct { - BaseModel + hyper.BaseModel ChannelID uint `json:"channel_id"` AccountID uint `json:"account_id"` diff --git a/pkg/internal/models/events.go b/pkg/internal/models/events.go index 0dd328d..047b47e 100644 --- a/pkg/internal/models/events.go +++ b/pkg/internal/models/events.go @@ -1,6 +1,9 @@ package models -import "gorm.io/datatypes" +import ( + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "gorm.io/datatypes" +) const ( EventMessageNew = "messages.new" @@ -10,7 +13,7 @@ const ( ) type Event struct { - BaseModel + hyper.BaseModel Uuid string `json:"uuid"` Body datatypes.JSONMap `json:"body"` diff --git a/pkg/internal/models/realms.go b/pkg/internal/models/realms.go index 01269af..940dd36 100644 --- a/pkg/internal/models/realms.go +++ b/pkg/internal/models/realms.go @@ -1,9 +1,11 @@ package models +import "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + // Realm profiles basically fetched from Hydrogen.Passport // But cache at here for better usage and database relations type Realm struct { - BaseModel + hyper.BaseModel Alias string `json:"alias"` Name string `json:"name"` diff --git a/pkg/internal/server/api/channels_api.go b/pkg/internal/server/api/channels_api.go index 7e90696..039c79c 100644 --- a/pkg/internal/server/api/channels_api.go +++ b/pkg/internal/server/api/channels_api.go @@ -2,6 +2,8 @@ package api import ( "fmt" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" @@ -176,7 +178,7 @@ func editChannel(c *fiber.Ctx) error { return err } - tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}}) + tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) if val, ok := c.Locals("realm").(models.Realm); ok { if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil { @@ -218,7 +220,7 @@ func deleteChannel(c *fiber.Ctx) error { user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("channelId", 0) - tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}}) + tx := database.C.Where(&models.Channel{BaseModel: hyper.BaseModel{ID: uint(id)}}) if val, ok := c.Locals("realm").(models.Realm); ok { if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil { diff --git a/pkg/internal/server/api/direct_channels_api.go b/pkg/internal/server/api/direct_channels_api.go index 23e819f..21712b5 100644 --- a/pkg/internal/server/api/direct_channels_api.go +++ b/pkg/internal/server/api/direct_channels_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" diff --git a/pkg/internal/server/api/realms_api.go b/pkg/internal/server/api/realms_api.go index b2653b4..50c8936 100644 --- a/pkg/internal/server/api/realms_api.go +++ b/pkg/internal/server/api/realms_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/services" "github.com/gofiber/fiber/v2" ) diff --git a/pkg/internal/server/api/whats_new_api.go b/pkg/internal/server/api/whats_new_api.go index 509e5e0..39fb8f8 100644 --- a/pkg/internal/server/api/whats_new_api.go +++ b/pkg/internal/server/api/whats_new_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" diff --git a/pkg/internal/services/calls.go b/pkg/internal/services/calls.go index 24820f5..e495616 100644 --- a/pkg/internal/services/calls.go +++ b/pkg/internal/services/calls.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" @@ -39,7 +40,7 @@ func GetCall(channel models.Channel, id uint) (models.Call, error) { var call models.Call if err := database.C. Where(models.Call{ - BaseModel: models.BaseModel{ID: id}, + BaseModel: hyper.BaseModel{ID: id}, ChannelID: channel.ID, }). Preload("Founder"). diff --git a/pkg/internal/services/channels.go b/pkg/internal/services/channels.go index 23a3447..28450af 100644 --- a/pkg/internal/services/channels.go +++ b/pkg/internal/services/channels.go @@ -4,6 +4,7 @@ import ( "fmt" "regexp" + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "github.com/samber/lo" @@ -21,7 +22,7 @@ func GetChannelAliasAvailability(alias string) error { func GetChannel(id uint) (models.Channel, error) { var channel models.Channel tx := database.C.Where(models.Channel{ - BaseModel: models.BaseModel{ID: id}, + BaseModel: hyper.BaseModel{ID: id}, }).Preload("Account").Preload("Realm") tx = PreloadDirectChannelMembers(tx) if err := tx.First(&channel).Error; err != nil { diff --git a/pkg/internal/services/direct_channels.go b/pkg/internal/services/direct_channels.go index f5ad30e..564372f 100644 --- a/pkg/internal/services/direct_channels.go +++ b/pkg/internal/services/direct_channels.go @@ -2,6 +2,7 @@ package services import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "github.com/spf13/viper" diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index 0fa5c4f..b6eeb56 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" @@ -47,7 +48,7 @@ func GetEvent(channel models.Channel, id uint) (models.Event, error) { var event models.Event if err := database.C. Where(models.Event{ - BaseModel: models.BaseModel{ID: id}, + BaseModel: hyper.BaseModel{ID: id}, ChannelID: channel.ID, }). Preload("Sender"). @@ -62,7 +63,7 @@ func GetEvent(channel models.Channel, id uint) (models.Event, error) { func GetEventWithSender(channel models.Channel, member models.ChannelMember, id uint) (models.Event, error) { var event models.Event if err := database.C.Where(models.Event{ - BaseModel: models.BaseModel{ID: id}, + BaseModel: hyper.BaseModel{ID: id}, ChannelID: channel.ID, SenderID: member.ID, }).First(&event).Error; err != nil { diff --git a/pkg/internal/services/realms.go b/pkg/internal/services/realms.go index 5ef6d25..b1b9e53 100644 --- a/pkg/internal/services/realms.go +++ b/pkg/internal/services/realms.go @@ -4,6 +4,8 @@ import ( "context" "errors" "fmt" + "reflect" + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" @@ -11,7 +13,6 @@ import ( "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "github.com/samber/lo" "gorm.io/gorm" - "reflect" ) func GetRealmWithExtID(id uint) (models.Realm, error) { diff --git a/pkg/internal/services/websocket.go b/pkg/internal/services/websocket.go index 3517377..71f1746 100644 --- a/pkg/internal/services/websocket.go +++ b/pkg/internal/services/websocket.go @@ -2,9 +2,10 @@ package services import ( "context" - "github.com/samber/lo" "time" + "github.com/samber/lo" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" diff --git a/pkg/main.go b/pkg/main.go index 42fa07f..5376e4e 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -1,18 +1,19 @@ package main import ( - "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" - "git.solsynth.dev/hydrogen/messaging/pkg/internal/grpc" "os" "os/signal" "syscall" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/grpc" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/services" "github.com/robfig/cron/v3" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server" - "git.solsynth.dev/hydrogen/messaging/pkg/internal" + pkg "git.solsynth.dev/hydrogen/messaging/pkg/internal" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "github.com/rs/zerolog" "github.com/rs/zerolog/log"