mirror of
1
Fork 0

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,78 +145,77 @@ 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)
/*
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
fill the person struct using some kind of unmarshall function given in
activitypub package/actor.go
*/
// make http client
host := activity.To.GetID().String()
client, err := api.NewClient(ctx, actionsUser, host) // ToDo: This is hacky, we need a hostname from somewhere
if err != nil {
panic(err)
}
// get_person_by_rest
bytes := []byte{0} // no body needed for getting user actor
remoteStargazer := activity.Actor.GetID().String() // used as LoginName in newly created user
response, err := client.Get(bytes, remoteStargazer)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
// parse response
person := ap.Person{}
err = person.UnmarshalJSON(body)
if err != nil {
panic(err)
}
log.Info("remoteStargazer: %v", remoteStargazer)
log.Info("http client. %v", client)
log.Info("response: %v\n error: ", response, err)
log.Info("Person is: %v", person)
log.Info("Person Name is: %v", person.PreferredUsername)
log.Info("Person URL is: %v", person.URL)
// Check if user already exists // 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. // 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 // 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 // We might extend the Star Activity by the username, then this request would become redundant
users, err := searchUsersByPerson(actor.GetNormailzedUri())
users, err := searchUsersByPerson(remoteStargazer, person)
if err != nil { if err != nil {
panic(fmt.Errorf("searching for user failed: %v", err)) panic(fmt.Errorf("searching for user failed: %v", err))
} }
if len(users) == 0 { if len(users) == 0 {
/*
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
fill the person struct using some kind of unmarshall function given in
activitypub package/actor.go
*/
// make http client
// TODO: Never use unvalidated input for actions - we have a validated actor already!
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 {
panic(err)
}
// get_person_by_rest
bytes := []byte{0} // no body needed for getting user actor
remoteStargazer := activity.Actor.GetID().String() // used as LoginName in newly created user
response, err := client.Get(bytes, remoteStargazer)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
// parse response
person := ap.Person{}
err = person.UnmarshalJSON(body)
if err != nil {
panic(err)
}
log.Info("remoteStargazer: %v", remoteStargazer)
log.Info("http client. %v", client)
log.Info("response: %v\n error: ", response, err)
log.Info("Person is: %v", person)
log.Info("Person Name is: %v", person.PreferredUsername)
log.Info("Person URL is: %v", person.URL)
// 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)
} }