From ae233a7c4da5366fe63659f7069a3476b4a28235 Mon Sep 17 00:00:00 2001 From: Ben Pate Date: Thu, 27 Apr 2023 12:21:10 -0600 Subject: [PATCH] Improve lookups for @username@server.name identifiers --- model/following_accessors.go | 2 +- service/following_connect.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/model/following_accessors.go b/model/following_accessors.go index c40340247..89d8d81e9 100644 --- a/model/following_accessors.go +++ b/model/following_accessors.go @@ -14,7 +14,7 @@ func FollowingSchema() schema.Element { "userId": schema.String{Format: "objectId"}, "folderId": schema.String{Format: "objectId", Required: true}, "label": schema.String{MaxLength: 128}, - "url": schema.String{Format: "url", Required: true, MaxLength: 1024}, + "url": schema.String{Required: true, MaxLength: 1024}, "profileUrl": schema.String{Format: "url", MaxLength: 1024}, "method": schema.String{Enum: []string{FollowMethodPoll, FollowMethodWebSub, FollowMethodActivityPub}}, "status": schema.String{Enum: []string{FollowingStatusNew, FollowingStatusLoading, FollowingStatusPending, FollowingStatusSuccess, FollowingStatusFailure}}, diff --git a/service/following_connect.go b/service/following_connect.go index 10aa24f20..0340d24be 100644 --- a/service/following_connect.go +++ b/service/following_connect.go @@ -3,12 +3,16 @@ package service import ( "bytes" "mime" + "net/mail" + "strings" "time" "github.com/EmissarySocial/emissary/model" "github.com/benpate/derp" "github.com/benpate/digit" + "github.com/benpate/domain" "github.com/benpate/remote" + "github.com/davecgh/go-spew/spew" ) // Connect attempts to connect to a new URL and determines how to follow it. @@ -46,7 +50,27 @@ func (service *Following) connect(following *model.Following) error { const location = "service.Following.connect" - // Try to load the targetURL. + // Try to use the following.URL as an email address (or @username@server.name) + emailAddress := strings.TrimPrefix(following.URL, "@") + + if email, err := mail.ParseAddress(emailAddress); err == nil { + spew.Dump(email, err) + resource, err := digit.Lookup(email.Address) + + if err != nil { + return derp.Wrap(err, location, "Error looking up email address", email.Address) + } + + following.Links = resource.Links + return nil + } + + // Try to use the following.URL as an actual URL. + + // Add protocol to the URL if it's missing + following.URL = domain.AddProtocol(following.URL) + + // Try to connect to the remote server var body bytes.Buffer transaction := remote. Get(following.URL). @@ -54,7 +78,7 @@ func (service *Following) connect(following *model.Following) error { Response(&body, nil) if err := transaction.Send(); err != nil { - return derp.Wrap(err, location, "Error connecting to remote website", following.URL) + return derp.Wrap(err, location, "Error connecting to remote website: "+following.URL) } // Look for Links to ActivityPub/Feeds/Hubs