diff options
Diffstat (limited to 'internal/util')
-rw-r--r-- | internal/util/regexes.go | 13 | ||||
-rw-r--r-- | internal/util/uri.go | 29 |
2 files changed, 34 insertions, 8 deletions
diff --git a/internal/util/regexes.go b/internal/util/regexes.go index adab8c87f..55773c370 100644 --- a/internal/util/regexes.go +++ b/internal/util/regexes.go @@ -85,13 +85,18 @@ var ( // followingPathRegex parses a path that validates and captures the username part from eg /users/example_username/following followingPathRegex = regexp.MustCompile(followingPathRegexString) - likedPathRegexString = fmt.Sprintf(`^/?%s/%s/%s$`, UsersPath, usernameRegexString, LikedPath) - // followingPathRegex parses a path that validates and captures the username part from eg /users/example_username/liked - likedPathRegex = regexp.MustCompile(likedPathRegexString) - // see https://ihateregex.io/expr/uuid/ uuidRegexString = `[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}` + likedPathRegexString = fmt.Sprintf(`^/?%s/(%s)/%s$`, UsersPath, usernameRegexString, LikedPath) + // likedPathRegex parses a path that validates and captures the username part from eg /users/example_username/liked + likedPathRegex = regexp.MustCompile(likedPathRegexString) + + likePathRegexString = fmt.Sprintf(`^/?%s/(%s)/%s/(%s)$`, UsersPath, usernameRegexString, LikedPath, uuidRegexString) + // likePathRegex parses a path that validates and captures the username part and the uuid part + // from eg /users/example_username/liked/123e4567-e89b-12d3-a456-426655440000. + likePathRegex = regexp.MustCompile(likePathRegexString) + statusesPathRegexString = fmt.Sprintf(`^/?%s/(%s)/%s/(%s)$`, UsersPath, usernameRegexString, StatusesPath, uuidRegexString) // statusesPathRegex parses a path that validates and captures the username part and the uuid part // from eg /users/example_username/statuses/123e4567-e89b-12d3-a456-426655440000. diff --git a/internal/util/uri.go b/internal/util/uri.go index 0ee4a5120..8d64bdd8e 100644 --- a/internal/util/uri.go +++ b/internal/util/uri.go @@ -22,8 +22,6 @@ import ( "fmt" "net/url" "strings" - - "github.com/google/uuid" ) const ( @@ -109,8 +107,14 @@ type UserURIs struct { // GenerateURIForFollow returns the AP URI for a new follow -- something like: // https://example.org/users/whatever_user/follow/41c7f33f-1060-48d9-84df-38dcb13cf0d8 -func GenerateURIForFollow(username string, protocol string, host string) string { - return fmt.Sprintf("%s://%s/%s/%s/%s", protocol, host, UsersPath, FollowPath, uuid.NewString()) +func GenerateURIForFollow(username string, protocol string, host string, thisFollowID string) string { + return fmt.Sprintf("%s://%s/%s/%s/%s", protocol, host, UsersPath, FollowPath, thisFollowID) +} + +// GenerateURIForFollow returns the AP URI for a new like/fave -- something like: +// https://example.org/users/whatever_user/liked/41c7f33f-1060-48d9-84df-38dcb13cf0d8 +func GenerateURIForLike(username string, protocol string, host string, thisFavedID string) string { + return fmt.Sprintf("%s://%s/%s/%s/%s", protocol, host, UsersPath, LikedPath, thisFavedID) } // GenerateURIsForAccount throws together a bunch of URIs for the given username, with the given protocol and host. @@ -183,6 +187,11 @@ func IsLikedPath(id *url.URL) bool { return likedPathRegex.MatchString(strings.ToLower(id.Path)) } +// IsLikedPath returns true if the given URL path corresponds to eg /users/example_username/liked/SOME_UUID_OF_A_STATUS +func IsLikePath(id *url.URL) bool { + return likePathRegex.MatchString(strings.ToLower(id.Path)) +} + // IsStatusesPath returns true if the given URL path corresponds to eg /users/example_username/statuses/SOME_UUID_OF_A_STATUS func IsStatusesPath(id *url.URL) bool { return statusesPathRegex.MatchString(strings.ToLower(id.Path)) @@ -254,3 +263,15 @@ func ParseFollowingPath(id *url.URL) (username string, err error) { username = matches[1] return } + +// ParseLikedPath returns the username and uuid from a path such as /users/example_username/liked/SOME_UUID_OF_A_STATUS +func ParseLikedPath(id *url.URL) (username string, uuid string, err error) { + matches := likePathRegex.FindStringSubmatch(id.Path) + if len(matches) != 3 { + err = fmt.Errorf("expected 3 matches but matches length was %d", len(matches)) + return + } + username = matches[1] + uuid = matches[2] + return +} |