Move server actor named to shared constant
All checks were successful
/ docker (push) Successful in 4m42s
All checks were successful
/ docker (push) Successful in 4m42s
This commit is contained in:
parent
5e93ecee73
commit
02c2e53c51
3 changed files with 27 additions and 5 deletions
|
@ -15,6 +15,7 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/shared"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
||||
webshared "git.mstar.dev/mstar/linstrom/web/shared"
|
||||
|
@ -52,7 +53,6 @@ type inboundImportUser struct {
|
|||
}
|
||||
|
||||
func ImportRemoteAccount(targetName string) (string, error) {
|
||||
|
||||
// Get the target user's link first
|
||||
webfinger, err := GetAccountWebfinger(targetName)
|
||||
if err != nil {
|
||||
|
@ -66,7 +66,7 @@ func ImportRemoteAccount(targetName string) (string, error) {
|
|||
}
|
||||
APLink := selfLinks[0]
|
||||
// Server actor key for signing
|
||||
linstromActor, err := dbgen.User.Where(dbgen.User.Username.Eq("linstrom")).First()
|
||||
linstromActor, err := dbgen.User.Where(dbgen.User.Username.Eq(shared.ServerActorName)).First()
|
||||
if err != nil {
|
||||
return "", other.Error("activitypub", "failed to get server actor", err)
|
||||
}
|
||||
|
@ -105,6 +105,7 @@ func ImportRemoteAccount(targetName string) (string, error) {
|
|||
Preload(dbgen.User.RemoteInfo).
|
||||
Preload(dbgen.User.InfoFields).
|
||||
Preload(dbgen.User.BeingTypes).
|
||||
Preload(dbgen.User.Roles).
|
||||
FirstOrCreate()
|
||||
if err != nil {
|
||||
return "", other.Error("activitypub", "failed to find or create user in db", err)
|
||||
|
@ -112,6 +113,25 @@ func ImportRemoteAccount(targetName string) (string, error) {
|
|||
user.Verified = true
|
||||
user.FinishedRegistration = true
|
||||
|
||||
if !sliceutils.ContainsFunc(user.Roles, func(t models.UserToRole) bool {
|
||||
return t.Role.ID == models.DefaultUserRole.ID
|
||||
}) {
|
||||
roleMapping := models.UserToRole{
|
||||
Role: models.DefaultUserRole,
|
||||
RoleId: models.DefaultUserRole.ID,
|
||||
UserId: user.ID,
|
||||
}
|
||||
if err = dbgen.UserToRole.Create(&roleMapping); err != nil {
|
||||
return "", other.Error(
|
||||
"activitypub",
|
||||
"failed to store default user role to imported account mapping",
|
||||
err,
|
||||
)
|
||||
}
|
||||
if err = dbgen.User.Roles.Model(user).Append(&roleMapping); err != nil {
|
||||
return "", other.Error("activitypub", "failed to attach default role to user", err)
|
||||
}
|
||||
}
|
||||
if user.RemoteInfo == nil {
|
||||
user.RemoteInfo = &models.UserRemoteLinks{
|
||||
UserId: user.ID,
|
||||
|
@ -191,7 +211,7 @@ func ImportRemoteAccount(targetName string) (string, error) {
|
|||
if sliceutils.ContainsFunc(user.BeingTypes, func(t models.UserToBeing) bool {
|
||||
return t.Being == string(models.BEING_CAT)
|
||||
}) {
|
||||
_, err = dbgen.UserToBeing.Where(dbgen.UserToBeing.UserId.Eq(user.ID)).Where(dbgen.UserToBeing.Being.Eq(models.BEING_CAT)).Delete()
|
||||
_, err = dbgen.UserToBeing.Where(dbgen.UserToBeing.UserId.Eq(user.ID)).Where(dbgen.UserToBeing.Being.Eq(string(models.BEING_CAT))).Delete()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to remove cat being type from user")
|
||||
}
|
||||
|
|
|
@ -7,4 +7,6 @@ const (
|
|||
// where (.revision) is optional and only used for cases
|
||||
// where multiple releases in a day are required
|
||||
Version = "0.0.1 pre-alpha"
|
||||
// Username for the server actor
|
||||
ServerActorName = "server.actor"
|
||||
)
|
||||
|
|
|
@ -100,7 +100,7 @@ func insertUser(
|
|||
server *models.RemoteServer,
|
||||
duckMedia *models.MediaMetadata,
|
||||
) (*models.User, error) {
|
||||
dbUser, err := dbgen.User.GetByUsername("linstrom")
|
||||
dbUser, err := dbgen.User.GetByUsername(shared.ServerActorName)
|
||||
if err == nil {
|
||||
return dbUser, nil
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ func insertUser(
|
|||
)
|
||||
}
|
||||
user := models.User{
|
||||
Username: "linstrom",
|
||||
Username: shared.ServerActorName,
|
||||
Server: *server,
|
||||
ServerId: server.ID,
|
||||
DisplayName: config.GlobalConfig.Self.ServerActorDisplayName,
|
||||
|
|
Loading…
Reference in a new issue