55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"database/sql"
|
|
"strings"
|
|
|
|
"git.mstar.dev/mstar/goutils/other"
|
|
"gorm.io/gorm"
|
|
|
|
"git.mstar.dev/mstar/linstrom/activitypub"
|
|
"git.mstar.dev/mstar/linstrom/config"
|
|
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
|
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
|
)
|
|
|
|
func EnsureLocalUserIdHasLinks(id string) error {
|
|
if strings.HasPrefix(id, "http") {
|
|
return nil
|
|
}
|
|
u := dbgen.User
|
|
user, err := u.Where(u.ID.Eq(id)).Preload(u.RemoteInfo).First()
|
|
switch err {
|
|
case gorm.ErrRecordNotFound:
|
|
return nil
|
|
case nil:
|
|
default:
|
|
return other.Error("storage", "Failed to get user by id", err)
|
|
}
|
|
if user.RemoteInfo != nil {
|
|
return nil
|
|
}
|
|
apId := activitypub.UserIdToApUrl(id)
|
|
info := models.UserRemoteLinks{
|
|
UserId: id,
|
|
ApLink: apId,
|
|
InboxLink: apId + "/inbox",
|
|
ViewLink: sql.NullString{
|
|
Valid: true,
|
|
String: config.GlobalConfig.General.GetFullPublicUrl() + "/user/" + user.Username,
|
|
},
|
|
FollowersLink: sql.NullString{
|
|
Valid: true,
|
|
String: apId + "/followers",
|
|
},
|
|
FollowingLink: sql.NullString{
|
|
Valid: true,
|
|
String: apId + "/following",
|
|
},
|
|
}
|
|
err = dbgen.UserRemoteLinks.Create(&info)
|
|
if err != nil {
|
|
return other.Error("storage", "Failed to store links for account", err)
|
|
}
|
|
return nil
|
|
}
|