♻️ Use the new dealer BaseUser and remove ExternalID

This commit is contained in:
2024-09-11 23:54:18 +08:00
parent 41ebb572fa
commit f962376f42
14 changed files with 45 additions and 109 deletions

View File

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

View File

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

View File

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

View File

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

View File

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