♻️ Use the new dealer BaseUser and remove ExternalID
This commit is contained in:
parent
41ebb572fa
commit
f962376f42
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.22
|
||||
toolchain go1.22.1
|
||||
|
||||
require (
|
||||
git.solsynth.dev/hydrogen/dealer v0.0.0-20240823113302-208da7e90fdb
|
||||
git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8
|
||||
github.com/go-playground/validator/v10 v10.17.0
|
||||
github.com/gofiber/fiber/v2 v2.52.4
|
||||
github.com/google/uuid v1.6.0
|
||||
|
2
go.sum
2
go.sum
@ -4,6 +4,8 @@ git.solsynth.dev/hydrogen/dealer v0.0.0-20240823110807-df5676cbe4d8 h1:2uoqdm2Z5
|
||||
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=
|
||||
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=
|
||||
|
@ -1,18 +1,9 @@
|
||||
package models
|
||||
|
||||
// Account profiles basically fetched from Hydrogen.Identity
|
||||
// But cached at here for better usage
|
||||
// At the same time, this model can make relations between local models
|
||||
type Account struct {
|
||||
BaseModel
|
||||
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
|
||||
Name string `json:"name"`
|
||||
Nick string `json:"nick"`
|
||||
Avatar string `json:"avatar"`
|
||||
Banner string `json:"banner"`
|
||||
Description string `json:"description"`
|
||||
EmailAddress string `json:"email_address"`
|
||||
PowerLevel int `json:"power_level"`
|
||||
Channels []Channel `json:"channels"`
|
||||
ExternalID uint `json:"external_id"`
|
||||
type Account struct {
|
||||
hyper.BaseUser
|
||||
|
||||
Channels []Channel `json:"channels"`
|
||||
}
|
||||
|
@ -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"
|
||||
|
||||
@ -88,7 +90,7 @@ func addChannelMember(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
if err := database.C.Where(&models.Account{
|
||||
if err := database.C.Where(&hyper.BaseUser{
|
||||
Name: data.Target,
|
||||
}).First(&account).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
@ -133,7 +135,7 @@ func removeChannelMember(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
if err := database.C.Where(&models.Account{
|
||||
if err := database.C.Where(&hyper.BaseUser{
|
||||
Name: data.Target,
|
||||
}).First(&account).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
@ -215,7 +217,7 @@ func joinChannel(c *fiber.Ctx) error {
|
||||
|
||||
if realm, err := services.GetRealmWithExtID(channel.Realm.ExternalID); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("invalid channel, related realm was not found: %v", err))
|
||||
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ExternalID); err != nil {
|
||||
} else if _, err := services.GetRealmMember(realm.ExternalID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a part of the realm: %v", err))
|
||||
}
|
||||
|
||||
|
@ -125,7 +125,7 @@ func createChannel(c *fiber.Ctx) error {
|
||||
|
||||
var realm *models.Realm
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
|
||||
@ -179,7 +179,7 @@ func editChannel(c *fiber.Ctx) error {
|
||||
tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}})
|
||||
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it")
|
||||
@ -221,7 +221,7 @@ func deleteChannel(c *fiber.Ctx) error {
|
||||
tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}})
|
||||
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil {
|
||||
return fmt.Errorf("you must be a part of that realm then can delete channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fmt.Errorf("you must be a moderator of that realm then can delete channel related to it")
|
||||
|
@ -33,7 +33,7 @@ func createDirectChannel(c *fiber.Ctx) error {
|
||||
|
||||
var realm *models.Realm
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
|
||||
|
@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
||||
@ -15,7 +16,7 @@ func getUserinfo(c *fiber.Ctx) error {
|
||||
|
||||
var data models.Account
|
||||
if err := database.C.
|
||||
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
|
||||
Where(&hyper.BaseModel{ID: user.ID}).
|
||||
First(&data).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
@ -28,7 +29,7 @@ func getOthersInfo(c *fiber.Ctx) error {
|
||||
|
||||
var data models.Account
|
||||
if err := database.C.
|
||||
Where(&models.Account{Name: accountId}).
|
||||
Where(&hyper.BaseUser{Name: accountId}).
|
||||
First(&data).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
package exts
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func LinkAccountMiddleware(c *fiber.Ctx) error {
|
||||
if val, ok := c.Locals("p_user").(*proto.UserInfo); ok {
|
||||
if account, err := services.LinkAccount(val); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else {
|
||||
c.Locals("user", account)
|
||||
}
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
@ -3,9 +3,11 @@ package server
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/api"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
@ -54,7 +56,15 @@ func NewServer() {
|
||||
}))
|
||||
|
||||
app.Use(gap.H.AuthMiddleware)
|
||||
app.Use(exts.LinkAccountMiddleware)
|
||||
app.Use(hyper.LinkAccountMiddleware(
|
||||
database.C,
|
||||
&models.Account{},
|
||||
func(u hyper.BaseUser) models.Account {
|
||||
return models.Account{
|
||||
BaseUser: u,
|
||||
}
|
||||
},
|
||||
))
|
||||
|
||||
api.MapAPIs(app, "/api")
|
||||
}
|
||||
|
@ -35,8 +35,8 @@ func CheckUserPerm(userId, otherId uint, key string, val any) error {
|
||||
return err
|
||||
}
|
||||
out, err := proto.NewAuthClient(pc).EnsureUserPermGranted(ctx, &proto.CheckUserPermRequest{
|
||||
UserId: uint64(user.ExternalID),
|
||||
OtherId: uint64(other.ExternalID),
|
||||
UserId: uint64(user.ID),
|
||||
OtherId: uint64(other.ID),
|
||||
Key: key,
|
||||
Value: encodedData,
|
||||
})
|
||||
@ -59,7 +59,7 @@ func NotifyAccountMessager(user models.Account, notification *proto.NotifyReques
|
||||
return err
|
||||
}
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
||||
UserId: uint64(user.ExternalID),
|
||||
UserId: uint64(user.ID),
|
||||
Notify: notification,
|
||||
})
|
||||
|
||||
@ -76,7 +76,7 @@ func NotifyAccountMessagerBatch(users []models.Account, notification *proto.Noti
|
||||
}
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: lo.Map(users, func(item models.Account, idx int) uint64 {
|
||||
return uint64(item.ExternalID)
|
||||
return uint64(item.ID)
|
||||
}),
|
||||
Notify: notification,
|
||||
})
|
||||
|
@ -1,51 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
||||
"gorm.io/gorm"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func LinkAccount(userinfo *proto.UserInfo) (models.Account, error) {
|
||||
var account models.Account
|
||||
if userinfo == nil {
|
||||
return account, fmt.Errorf("remote userinfo was not found")
|
||||
}
|
||||
if err := database.C.Where(&models.Account{
|
||||
ExternalID: uint(userinfo.Id),
|
||||
}).First(&account).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
account = models.Account{
|
||||
Name: userinfo.Name,
|
||||
Nick: userinfo.Nick,
|
||||
Avatar: userinfo.Avatar,
|
||||
Banner: userinfo.Banner,
|
||||
Description: userinfo.GetDescription(),
|
||||
EmailAddress: userinfo.Email,
|
||||
PowerLevel: 0,
|
||||
ExternalID: uint(userinfo.Id),
|
||||
}
|
||||
return account, database.C.Save(&account).Error
|
||||
}
|
||||
return account, err
|
||||
}
|
||||
|
||||
prev := account
|
||||
account.Name = userinfo.Name
|
||||
account.Nick = userinfo.Nick
|
||||
account.Avatar = userinfo.Avatar
|
||||
account.Banner = userinfo.Banner
|
||||
account.Description = userinfo.GetDescription()
|
||||
account.EmailAddress = userinfo.Email
|
||||
|
||||
var err error
|
||||
if !reflect.DeepEqual(account, prev) {
|
||||
err = database.C.Save(&account).Error
|
||||
}
|
||||
|
||||
return account, err
|
||||
}
|
@ -113,7 +113,7 @@ func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call,
|
||||
if member.ID != call.Founder.ID {
|
||||
pendingUsers = append(pendingUsers, member.Account)
|
||||
}
|
||||
PushCommand(member.Account.ExternalID, models.UnifiedCommand{
|
||||
PushCommand(member.Account.ID, models.UnifiedCommand{
|
||||
Action: "calls.new",
|
||||
Payload: call,
|
||||
})
|
||||
@ -129,7 +129,7 @@ func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call,
|
||||
Body: fmt.Sprintf("%s is calling", call.Founder.Account.Name),
|
||||
Avatar: &call.Founder.Account.Avatar,
|
||||
Metadata: EncodeJSONBody(map[string]any{
|
||||
"user_id": call.Founder.Account.ExternalID,
|
||||
"user_id": call.Founder.Account.ID,
|
||||
"user_name": call.Founder.Account.Name,
|
||||
"user_nick": call.Founder.Account.Nick,
|
||||
"channel_id": call.ChannelID,
|
||||
@ -163,7 +163,7 @@ func EndCall(call models.Call) (models.Call, error) {
|
||||
}).Preload("Account").Find(&members).Error; err == nil {
|
||||
call, _ = GetCall(call.Channel, call.ID)
|
||||
for _, member := range members {
|
||||
PushCommand(member.Account.ExternalID, models.UnifiedCommand{
|
||||
PushCommand(member.Account.ID, models.UnifiedCommand{
|
||||
Action: "calls.end",
|
||||
Payload: call,
|
||||
})
|
||||
|
@ -85,7 +85,7 @@ func NewEvent(event models.Event) (models.Event, error) {
|
||||
|
||||
event, _ = GetEvent(event.Channel, event.ID)
|
||||
idxList := lo.Map(members, func(item models.ChannelMember, index int) uint64 {
|
||||
return uint64(item.Account.ExternalID)
|
||||
return uint64(item.Account.ID)
|
||||
})
|
||||
PushCommandBatch(idxList, models.UnifiedCommand{
|
||||
Action: "events.new",
|
||||
@ -114,7 +114,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
||||
case models.NotifyLevelNone:
|
||||
continue
|
||||
case models.NotifyLevelMentioned:
|
||||
if len(body.RelatedUsers) != 0 && lo.Contains(body.RelatedUsers, member.Account.ExternalID) {
|
||||
if len(body.RelatedUsers) != 0 && lo.Contains(body.RelatedUsers, member.Account.ID) {
|
||||
mentionedUsers = append(mentionedUsers, member.Account)
|
||||
}
|
||||
continue
|
||||
@ -122,7 +122,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
||||
break
|
||||
}
|
||||
|
||||
if lo.Contains(body.RelatedUsers, member.Account.ExternalID) {
|
||||
if lo.Contains(body.RelatedUsers, member.Account.ID) {
|
||||
mentionedUsers = append(mentionedUsers, member.Account)
|
||||
} else {
|
||||
pendingUsers = append(pendingUsers, member.Account)
|
||||
@ -170,7 +170,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
||||
Body: displayText,
|
||||
Avatar: &event.Sender.Account.Avatar,
|
||||
Metadata: EncodeJSONBody(map[string]any{
|
||||
"user_id": event.Sender.Account.ExternalID,
|
||||
"user_id": event.Sender.Account.ID,
|
||||
"user_name": event.Sender.Account.Name,
|
||||
"user_nick": event.Sender.Account.Nick,
|
||||
"channel_id": event.ChannelID,
|
||||
@ -200,7 +200,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
||||
Body: displayText,
|
||||
Avatar: &event.Sender.Account.Avatar,
|
||||
Metadata: EncodeJSONBody(map[string]any{
|
||||
"user_id": event.Sender.Account.ExternalID,
|
||||
"user_id": event.Sender.Account.ID,
|
||||
"user_name": event.Sender.Account.Name,
|
||||
"user_nick": event.Sender.Account.Nick,
|
||||
"channel_id": event.ChannelID,
|
||||
|
@ -60,7 +60,7 @@ func SetTypingStatus(channelId uint, userId uint) error {
|
||||
}
|
||||
|
||||
for _, item := range channel.Members {
|
||||
broadcastTarget = append(broadcastTarget, uint64(item.Account.ExternalID))
|
||||
broadcastTarget = append(broadcastTarget, uint64(item.Account.ID))
|
||||
}
|
||||
|
||||
data = map[string]any{
|
||||
|
Loading…
Reference in New Issue
Block a user