Skip to content

Commit

Permalink
Improve lookups for @username@server.name identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
benpate committed Apr 27, 2023
1 parent 11f6dbd commit ae233a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion model/following_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand Down
28 changes: 26 additions & 2 deletions service/following_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -46,15 +50,35 @@ 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).
Header("Accept", followingMimeStack).
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
Expand Down

0 comments on commit ae233a7

Please sign in to comment.