If we use user.loginname to store the actor.id we can search for local users earlier

This commit is contained in:
Michael Jerger 2023-12-06 09:07:09 +01:00
parent ed1af14ceb
commit 9b5d8bbeda
2 changed files with 54 additions and 51 deletions

View file

@ -88,6 +88,11 @@ func (a ActorID) GetUserId() int {
return result return result
} }
func (a ActorID) GetNormailzedUri() string {
result := fmt.Sprintf("%s://%s:%s/%s/%s", a.schema, a.host, a.port, a.path, a.userId)
return result
}
// Returns the combination of host:port if port exists, host otherwise // Returns the combination of host:port if port exists, host otherwise
func (a ActorID) GetHostAndPort() string { func (a ActorID) GetHostAndPort() string {

View file

@ -68,12 +68,12 @@ func generateRandomPassword() (string, error) {
return res, err return res, err
} }
func searchUsersByPerson(remoteStargazer string, person ap.Actor) ([]*user_model.User, error) { func searchUsersByPerson(actorId string) ([]*user_model.User, error) {
actionsUser.IsAdmin = true actionsUser.IsAdmin = true
options := &user_model.SearchUserOptions{ options := &user_model.SearchUserOptions{
LoginName: remoteStargazer, LoginName: actorId,
Actor: actionsUser, Actor: actionsUser,
Type: user_model.UserTypeRemoteUser, Type: user_model.UserTypeRemoteUser,
OrderBy: db.SearchOrderByAlphabetically, OrderBy: db.SearchOrderByAlphabetically,
@ -145,27 +145,35 @@ func RepositoryInbox(ctx *context.APIContext) {
log.Info("RepositoryInbox: repo %v, %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name) log.Info("RepositoryInbox: repo %v, %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name)
activity := web.GetForm(ctx).(*forgefed.Star) activity := web.GetForm(ctx).(*forgefed.Star)
log.Info("RepositoryInbox: Activity.Source %v", activity.Source) log.Info("RepositoryInbox: Activity.Source %v", activity.Source)
log.Info("RepositoryInbox: Activity.Actor %v", activity.Actor) log.Info("RepositoryInbox: Activity.Actor %v", activity.Actor)
// assume actor is: "actor": "https://codeberg.org/api/v1/activitypub/user-id/12345" - NB: This might be actually the ID? Maybe check vocabulary. // assume actor is: "actor": "https://codeberg.org/api/v1/activitypub/user-id/12345" - NB: This might be actually the ID? Maybe check vocabulary.
// "https://Codeberg.org/api/v1/activitypub/user-id/12345" // "https://Codeberg.org/api/v1/activitypub/user-id/12345"
// "https:443//codeberg.org/api/v1/activitypub/user-id/12345" // "https://codeberg.org:443/api/v1/activitypub/user-id/12345"
// "https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345" // "https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345"
// "https://user:password@codeberg.org/api/v1/activitypub/user-id/12345"
// "https://codeberg.org/api/v1/activitypub//user-id/12345"
// parse actor // parse actor
actor, err := activitypub.ParseActorIDFromStarActivity(activity) actor, err := activitypub.ParseActorIDFromStarActivity(activity)
// Is the actor IRI well formed? // Is the actor IRI well formed?
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Is the ActorData Struct valid? // Is the ActorData Struct valid?
actor.PanicIfInvalid() actor.PanicIfInvalid()
log.Info("RepositoryInbox: Actor parsed. %v", actor) log.Info("RepositoryInbox: Actor parsed. %v", actor)
// Check if user already exists
// TODO: If we where able to search for federated id there would be no need to get the remote person.
// N.B. We need the username as a display name from the remote host. This requires us to make another request
// We might extend the Star Activity by the username, then this request would become redundant
users, err := searchUsersByPerson(actor.GetNormailzedUri())
if err != nil {
panic(fmt.Errorf("searching for user failed: %v", err))
}
if len(users) == 0 {
/* /*
Make http client, this should make a get request on given url Make http client, this should make a get request on given url
We then need to parse the answer and put it into a person-struct We then need to parse the answer and put it into a person-struct
@ -174,8 +182,9 @@ func RepositoryInbox(ctx *context.APIContext) {
*/ */
// make http client // make http client
host := activity.To.GetID().String() // TODO: Never use unvalidated input for actions - we have a validated actor already!
client, err := api.NewClient(ctx, actionsUser, host) // ToDo: This is hacky, we need a hostname from somewhere actorId := activity.To.GetID().String()
client, err := api.NewClient(ctx, actionsUser, actorId) // ToDo: This is hacky, we need a hostname from somewhere
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -207,16 +216,6 @@ func RepositoryInbox(ctx *context.APIContext) {
log.Info("Person Name is: %v", person.PreferredUsername) log.Info("Person Name is: %v", person.PreferredUsername)
log.Info("Person URL is: %v", person.URL) log.Info("Person URL is: %v", person.URL)
// Check if user already exists
// TODO: If we where able to search for federated id there would be no need to get the remote person.
// N.B. We need the username as a display name from the remote host. This requires us to make another request
// We might extend the Star Activity by the username, then this request would become redundant
users, err := searchUsersByPerson(remoteStargazer, person)
if err != nil {
panic(fmt.Errorf("searching for user failed: %v", err))
}
if len(users) == 0 {
// create user // create user
// ToDo: We need a remote server with federation enabled to properly test this // ToDo: We need a remote server with federation enabled to properly test this
@ -268,5 +267,4 @@ func RepositoryInbox(ctx *context.APIContext) {
// wait 15 sec. // wait 15 sec.
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }