diff options
Diffstat (limited to 'internal/util')
-rw-r--r-- | internal/util/regexes.go | 5 | ||||
-rw-r--r-- | internal/util/uri.go | 25 |
2 files changed, 30 insertions, 0 deletions
diff --git a/internal/util/regexes.go b/internal/util/regexes.go index 25d90417c..1ca34708f 100644 --- a/internal/util/regexes.go +++ b/internal/util/regexes.go @@ -104,4 +104,9 @@ var ( // from eg /users/example_username/statuses/01F7XT5JZW1WMVSW1KADS8PVDH // The regex can be played with here: https://regex101.com/r/G9zuxQ/1 statusesPathRegex = regexp.MustCompile(statusesPathRegexString) + + blockPathRegexString = fmt.Sprintf(`^/?%s/(%s)/%s/(%s)$`, UsersPath, usernameRegexString, BlocksPath, ulidRegexString) + // blockPathRegex parses a path that validates and captures the username part and the ulid part + // from eg /users/example_username/blocks/01F7XT5JZW1WMVSW1KADS8PVDH + blockPathRegex = regexp.MustCompile(blockPathRegexString) ) diff --git a/internal/util/uri.go b/internal/util/uri.go index 5eb291628..370b2fa6f 100644 --- a/internal/util/uri.go +++ b/internal/util/uri.go @@ -50,6 +50,8 @@ const ( FollowPath = "follow" // UpdatePath is used to generate the URI for an account update UpdatePath = "updates" + // BlocksPath is used to generate the URI for a block + BlocksPath = "blocks" ) // APContextKey is a type used specifically for settings values on contexts within go-fed AP request chains @@ -124,6 +126,12 @@ func GenerateURIForUpdate(username string, protocol string, host string, thisUpd return fmt.Sprintf("%s://%s/%s/%s#%s/%s", protocol, host, UsersPath, username, UpdatePath, thisUpdateID) } +// GenerateURIForBlock returns the AP URI for a new block activity -- something like: +// https://example.org/users/whatever_user/blocks/01F7XTH1QGBAPMGF49WJZ91XGC +func GenerateURIForBlock(username string, protocol string, host string, thisBlockID string) string { + return fmt.Sprintf("%s://%s/%s/%s/%s/%s", protocol, host, UsersPath, username, BlocksPath, thisBlockID) +} + // GenerateURIsForAccount throws together a bunch of URIs for the given username, with the given protocol and host. func GenerateURIsForAccount(username string, protocol string, host string) *UserURIs { // The below URLs are used for serving web requests @@ -214,6 +222,11 @@ func IsPublicKeyPath(id *url.URL) bool { return userPublicKeyPathRegex.MatchString(id.Path) } +// IsBlockPath returns true if the given URL path corresponds to eg /users/example_username/blocks/SOME_ULID_OF_A_BLOCK +func IsBlockPath(id *url.URL) bool { + return blockPathRegex.MatchString(id.Path) +} + // ParseStatusesPath returns the username and ulid from a path such as /users/example_username/statuses/SOME_ULID_OF_A_STATUS func ParseStatusesPath(id *url.URL) (username string, ulid string, err error) { matches := statusesPathRegex.FindStringSubmatch(id.Path) @@ -292,3 +305,15 @@ func ParseLikedPath(id *url.URL) (username string, ulid string, err error) { ulid = matches[2] return } + +// ParseBlockPath returns the username and ulid from a path such as /users/example_username/blocks/SOME_ULID_OF_A_BLOCK +func ParseBlockPath(id *url.URL) (username string, ulid string, err error) { + matches := blockPathRegex.FindStringSubmatch(id.Path) + if len(matches) != 3 { + err = fmt.Errorf("expected 3 matches but matches length was %d", len(matches)) + return + } + username = matches[1] + ulid = matches[2] + return +} |