Correctly create remote user

This commit is contained in:
Anthony Wang 2022-06-13 19:30:08 -05:00
parent 22960444db
commit da1be7167e
Signed by: a
GPG key ID: BC96B00AEC5F2D76
5 changed files with 27 additions and 15 deletions

View file

@ -17,7 +17,7 @@ var (
ErrNameEmpty = errors.New("Name is empty")
// AlphaDashDotPattern characters prohibited in a user name (anything except A-Za-z0-9_.-)
AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.@]`)
)
// ErrNameReserved represents a "reserved name" error.

View file

@ -9,6 +9,7 @@ import (
"strings"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
ap "github.com/go-ap/activitypub"
)
@ -18,18 +19,29 @@ func Follow(ctx context.Context, activity ap.Follow) {
objectIRI := activity.Object.GetID()
actorIRISplit := strings.Split(actorIRI.String(), "/")
objectIRISplit := strings.Split(objectIRI.String(), "/")
actorName := actorIRISplit[len(actorIRISplit)-1]
objectName := objectIRISplit[len(actorIRISplit)-1]
actorName := actorIRISplit[len(actorIRISplit)-1] + "@" + actorIRISplit[2]
objectName := objectIRISplit[len(objectIRISplit)-1]
FederatedUserNew(actorName, actorIRI)
actorUser, _ := user_model.GetUserByName(ctx, actorName)
objectUser, _ := user_model.GetUserByName(ctx, objectName)
log.Warn("Follow object", activity.Object)
err := FederatedUserNew(actorName, actorIRI)
if err != nil {
log.Warn("Couldn't create new user", err)
}
actorUser, err := user_model.GetUserByName(ctx, actorName)
if err != nil {
log.Warn("Couldn't find actor", err)
}
objectUser, err := user_model.GetUserByName(ctx, objectName)
if err != nil {
log.Warn("Couldn't find object", err)
}
user_model.FollowUser(actorUser.ID, objectUser.ID)
accept := ap.AcceptNew(objectIRI, activity)
accept.Actor = activity.Object
accept.To = ap.ItemCollection{actorIRI}
accept.Actor = ap.Person{ID: objectIRI}
accept.To = ap.ItemCollection{ap.IRI(actorIRI.String()+"/inbox")}
Send(objectUser, accept)
}

View file

@ -5,8 +5,6 @@
package activitypub
import (
"strings"
"code.gitea.io/gitea/models/auth"
user_model "code.gitea.io/gitea/models/user"
@ -14,9 +12,9 @@ import (
)
func FederatedUserNew(name string, IRI ap.IRI) error {
instance := strings.Split(IRI.String(), "/")[2]
user := &user_model.User{
Name: name + "@" + instance,
Name: name,
Email: name,
LoginType: auth.Federated,
Website: IRI.String(),
}

View file

@ -120,8 +120,9 @@ func PersonInbox(ctx *context.APIContext) {
activity.UnmarshalJSON(body)
if activity.Type == ap.FollowType {
activitypub.Follow(ctx, activity)
} else {
log.Warn("ActivityStreams type not supported", activity)
}
log.Warn("ActivityStreams type not supported", activity)
ctx.Status(http.StatusNoContent)
}

View file

@ -7,6 +7,7 @@ package activitypub
import (
"net/http"
"code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/json"
)
@ -20,7 +21,7 @@ func response(ctx *context.APIContext, binary []byte) {
jsonmap["@context"] = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}
ctx.Resp.Header().Add("Content-Type", "application/activity+json")
ctx.Resp.Header().Add("Content-Type", activitypub.ActivityStreamsContentType)
ctx.Resp.WriteHeader(http.StatusOK)
binary, _ = json.Marshal(jsonmap)
ctx.Resp.Write(binary)