2024-02-01 15:26:17 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-03-20 13:42:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
2024-02-01 15:26:17 +00:00
|
|
|
"gorm.io/gorm"
|
2024-06-22 09:29:53 +00:00
|
|
|
"reflect"
|
2024-02-01 15:26:17 +00:00
|
|
|
)
|
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
func LinkAccount(userinfo *proto.Userinfo) (models.Account, error) {
|
2024-02-01 15:26:17 +00:00
|
|
|
var account models.Account
|
2024-03-03 04:17:18 +00:00
|
|
|
if userinfo == nil {
|
|
|
|
return account, fmt.Errorf("remote userinfo was not found")
|
|
|
|
}
|
2024-02-01 15:26:17 +00:00
|
|
|
if err := database.C.Where(&models.Account{
|
2024-02-21 14:58:51 +00:00
|
|
|
ExternalID: uint(userinfo.Id),
|
2024-02-01 15:26:17 +00:00
|
|
|
}).First(&account).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
account = models.Account{
|
2024-02-05 07:51:31 +00:00
|
|
|
Name: userinfo.Name,
|
2024-02-21 14:58:51 +00:00
|
|
|
Nick: userinfo.Nick,
|
|
|
|
Avatar: userinfo.Avatar,
|
2024-03-20 13:42:14 +00:00
|
|
|
Banner: userinfo.Banner,
|
|
|
|
Description: userinfo.GetDescription(),
|
2024-02-01 15:26:17 +00:00
|
|
|
EmailAddress: userinfo.Email,
|
|
|
|
PowerLevel: 0,
|
2024-02-21 14:58:51 +00:00
|
|
|
ExternalID: uint(userinfo.Id),
|
2024-02-01 15:26:17 +00:00
|
|
|
}
|
|
|
|
return account, database.C.Save(&account).Error
|
|
|
|
}
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2024-03-26 14:49:12 +00:00
|
|
|
prev := account
|
2024-02-05 07:51:31 +00:00
|
|
|
account.Name = userinfo.Name
|
2024-02-21 14:58:51 +00:00
|
|
|
account.Nick = userinfo.Nick
|
|
|
|
account.Avatar = userinfo.Avatar
|
2024-03-20 13:42:14 +00:00
|
|
|
account.Banner = userinfo.Banner
|
|
|
|
account.Description = userinfo.GetDescription()
|
2024-02-05 07:51:31 +00:00
|
|
|
account.EmailAddress = userinfo.Email
|
|
|
|
|
2024-03-26 14:49:12 +00:00
|
|
|
var err error
|
|
|
|
if !reflect.DeepEqual(prev, account) {
|
|
|
|
err = database.C.Save(&account).Error
|
|
|
|
}
|
2024-02-05 07:51:31 +00:00
|
|
|
|
|
|
|
return account, err
|
2024-02-01 15:26:17 +00:00
|
|
|
}
|