2024-05-17 07:59:51 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-06-22 04:18:54 +00:00
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
2024-06-22 04:29:20 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
2024-05-17 07:59:51 +00:00
|
|
|
"gorm.io/gorm"
|
2024-06-22 04:29:20 +00:00
|
|
|
"reflect"
|
2024-05-17 07:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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(prev, account) {
|
|
|
|
err = database.C.Save(&account).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|