linstrom/storage-new/genLinksForUser.go
mstar f991a1f353
All checks were successful
/ docker (push) Successful in 4m27s
Add func to add ap links to local users (untested)
2025-05-15 11:13:58 +02:00

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
}