🗑️ Remove firebase deps

This commit is contained in:
LittleSheep 2024-06-02 20:15:04 +08:00
parent c7ba566c48
commit 61eff6ea49
10 changed files with 23 additions and 69 deletions

View File

@ -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...")

View File

@ -12,6 +12,7 @@ var AutoMaintainRange = []any{
&models.AccountPage{},
&models.AccountContact{},
&models.AccountFriendship{},
&models.Badge{},
&models.Realm{},
&models.RealmMember{},
&models.AuthTicket{},

View File

@ -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
}

View File

@ -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"`

10
pkg/models/badges.go Normal file
View File

@ -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"`
}

View File

@ -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 {

View File

@ -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())
}

View File

@ -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())
}

View File

@ -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:
}
}

View File

@ -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 <alphabot@smartsheep.studio>"
smtp_host = "smtp.exmail.qq.com"