From 61eff6ea49ec8d6938d6b172d04a6f7b0780b0fb Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 2 Jun 2024 20:15:04 +0800 Subject: [PATCH] :wastebasket: Remove firebase deps --- pkg/cmd/main.go | 14 +++----------- pkg/database/migrator.go | 1 + pkg/external/firebase.go | 21 --------------------- pkg/models/accounts.go | 1 + pkg/models/badges.go | 10 ++++++++++ pkg/models/notifications.go | 7 +++++-- pkg/server/accounts_api.go | 1 + pkg/server/userinfo_api.go | 1 + pkg/services/notifications.go | 33 +-------------------------------- settings.toml | 3 --- 10 files changed, 23 insertions(+), 69 deletions(-) delete mode 100644 pkg/external/firebase.go create mode 100644 pkg/models/badges.go diff --git a/pkg/cmd/main.go b/pkg/cmd/main.go index 4dd3f45..cf65bb8 100644 --- a/pkg/cmd/main.go +++ b/pkg/cmd/main.go @@ -1,13 +1,13 @@ package main import ( - "git.solsynth.dev/hydrogen/passport/pkg" - "git.solsynth.dev/hydrogen/passport/pkg/i18n" "os" "os/signal" "syscall" - "git.solsynth.dev/hydrogen/passport/pkg/external" + "git.solsynth.dev/hydrogen/passport/pkg" + "git.solsynth.dev/hydrogen/passport/pkg/i18n" + "git.solsynth.dev/hydrogen/passport/pkg/grpc" "git.solsynth.dev/hydrogen/passport/pkg/server" "git.solsynth.dev/hydrogen/passport/pkg/services" @@ -45,14 +45,6 @@ func main() { log.Fatal().Err(err).Msg("An error occurred when running database auto migration.") } - // External - // All the things are optional so when error occurred the server won't crash - if err := external.SetupFirebase(viper.GetString("external.firebase.credentials")); err != nil { - log.Error().Err(err).Msg("An error occurred when starting firebase communicating...") - } else { - log.Info().Msg("Successfully setup firebase communication.") - } - // Connect other services if err := grpc.ConnectPaperclip(); err != nil { log.Fatal().Err(err).Msg("An error occurred when connecting to paperclip...") diff --git a/pkg/database/migrator.go b/pkg/database/migrator.go index 1f6deb4..98bb097 100644 --- a/pkg/database/migrator.go +++ b/pkg/database/migrator.go @@ -12,6 +12,7 @@ var AutoMaintainRange = []any{ &models.AccountPage{}, &models.AccountContact{}, &models.AccountFriendship{}, + &models.Badge{}, &models.Realm{}, &models.RealmMember{}, &models.AuthTicket{}, diff --git a/pkg/external/firebase.go b/pkg/external/firebase.go deleted file mode 100644 index aafd754..0000000 --- a/pkg/external/firebase.go +++ /dev/null @@ -1,21 +0,0 @@ -package external - -import ( - "context" - firebase "firebase.google.com/go" - "google.golang.org/api/option" -) - -var Fire *firebase.App - -func SetupFirebase(credentials string) error { - opt := option.WithCredentialsFile(credentials) - app, err := firebase.NewApp(context.Background(), nil, opt) - if err != nil { - return err - } else { - Fire = app - } - - return nil -} diff --git a/pkg/models/accounts.go b/pkg/models/accounts.go index 308ff7b..ac7907d 100644 --- a/pkg/models/accounts.go +++ b/pkg/models/accounts.go @@ -22,6 +22,7 @@ type Account struct { Profile AccountProfile `json:"profile"` PersonalPage AccountPage `json:"personal_page"` + Badges []Badge `json:"badges"` Contacts []AccountContact `json:"contacts"` RealmIdentities []RealmMember `json:"realm_identities"` diff --git a/pkg/models/badges.go b/pkg/models/badges.go new file mode 100644 index 0000000..d043765 --- /dev/null +++ b/pkg/models/badges.go @@ -0,0 +1,10 @@ +package models + +type Badge struct { + BaseModel + + Type string `json:"type"` + Label string `json:"label"` + Color string `json:"color"` + AccountID uint `json:"account_id"` +} diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index c1302bf..48102e8 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -1,15 +1,18 @@ package models import ( - "gorm.io/datatypes" "time" + + "gorm.io/datatypes" ) type Notification struct { BaseModel + Type string `json:"type"` Subject string `json:"subject"` Content string `json:"content"` + Metadata datatypes.JSONMap `json:"metadata"` Links datatypes.JSONSlice[NotificationLink] `json:"links"` IsImportant bool `json:"is_important"` IsRealtime bool `json:"is_realtime" gorm:"-"` @@ -25,7 +28,7 @@ type NotificationLink struct { } const ( - NotifySubscriberFirebase = "firebase" + NotifySubscriberAPN = "apple" ) type NotificationSubscriber struct { diff --git a/pkg/server/accounts_api.go b/pkg/server/accounts_api.go index d4d2aec..8c22673 100644 --- a/pkg/server/accounts_api.go +++ b/pkg/server/accounts_api.go @@ -23,6 +23,7 @@ func getUserinfo(c *fiber.Ctx) error { Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}). Preload("Profile"). Preload("Contacts"). + Preload("Badges"). First(&data).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/pkg/server/userinfo_api.go b/pkg/server/userinfo_api.go index 7a0a886..ed2d1f0 100644 --- a/pkg/server/userinfo_api.go +++ b/pkg/server/userinfo_api.go @@ -14,6 +14,7 @@ func getOtherUserinfo(c *fiber.Ctx) error { Where(&models.Account{Name: alias}). Omit("tickets", "challenges", "factors", "events", "clients", "notifications", "notify_subscribers"). Preload("Profile"). + Preload("Badges"). First(&account).Error; err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/services/notifications.go b/pkg/services/notifications.go index 83be5ed..67ce9ca 100644 --- a/pkg/services/notifications.go +++ b/pkg/services/notifications.go @@ -1,12 +1,9 @@ package services import ( - "context" "reflect" - "firebase.google.com/go/messaging" "git.solsynth.dev/hydrogen/passport/pkg/database" - "git.solsynth.dev/hydrogen/passport/pkg/external" "git.solsynth.dev/hydrogen/passport/pkg/models" "github.com/rs/zerolog/log" ) @@ -73,35 +70,7 @@ func PushNotification(notification models.Notification) error { for _, subscriber := range subscribers { switch subscriber.Provider { - case models.NotifySubscriberFirebase: - if external.Fire == nil { - // Didn't configure for firebase support - break - } - - ctx := context.Background() - client, err := external.Fire.Messaging(ctx) - if err != nil { - log.Warn().Err(err).Msg("An error occurred when getting firebase FCM client...") - break - } - - message := &messaging.Message{ - Notification: &messaging.Notification{ - Title: notification.Subject, - Body: notification.Content, - }, - Token: subscriber.DeviceToken, - } - - if response, err := client.Send(ctx, message); err != nil { - log.Warn().Err(err).Msg("An error occurred when notify subscriber though firebase FCM...") - } else { - log.Debug(). - Str("response", response). - Int("subscriber", int(subscriber.ID)). - Msg("Notified to subscriber though firebase FCM.") - } + case models.NotifySubscriberAPN: } } diff --git a/settings.toml b/settings.toml index cbddeb0..3056339 100644 --- a/settings.toml +++ b/settings.toml @@ -16,9 +16,6 @@ print_routes = false endpoint = "http://localhost:8443" grpc_endpoint = "localhost:7443" -[external.firebase] -credentials = "dist/firebase-certs.json" - [mailer] name = "Alphabot " smtp_host = "smtp.exmail.qq.com"