mirror of https://github.com/matrix-org/gomatrix
Add user ID handling methods
This commit is contained in:
parent
665b35975b
commit
8ef8c8187f
15
userids.go
15
userids.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const lowerhex = "0123456789abcdef"
|
const lowerhex = "0123456789abcdef"
|
||||||
|
@ -44,6 +45,7 @@ func isValidEscapedChar(b byte) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeUserLocalpart encodes the given string into Matrix-compliant user ID localpart form.
|
// EncodeUserLocalpart encodes the given string into Matrix-compliant user ID localpart form.
|
||||||
|
// See http://matrix.org/docs/spec/intro.html#mapping-from-other-character-sets
|
||||||
//
|
//
|
||||||
// This returns a string with only the characters "a-z0-9._=-". The uppercase range A-Z
|
// This returns a string with only the characters "a-z0-9._=-". The uppercase range A-Z
|
||||||
// are encoded using leading underscores ("_"). Characters outside the aforementioned ranges
|
// are encoded using leading underscores ("_"). Characters outside the aforementioned ranges
|
||||||
|
@ -67,6 +69,7 @@ func EncodeUserLocalpart(str string) string {
|
||||||
|
|
||||||
// DecodeUserLocalpart decodes the given string back into the original input string.
|
// DecodeUserLocalpart decodes the given string back into the original input string.
|
||||||
// Returns an error if the given string is not a valid user ID localpart encoding.
|
// Returns an error if the given string is not a valid user ID localpart encoding.
|
||||||
|
// See http://matrix.org/docs/spec/intro.html#mapping-from-other-character-sets
|
||||||
//
|
//
|
||||||
// This decodes quoted-printable bytes back into UTF8, and unescapes casing. For
|
// This decodes quoted-printable bytes back into UTF8, and unescapes casing. For
|
||||||
// example:
|
// example:
|
||||||
|
@ -113,3 +116,15 @@ func DecodeUserLocalpart(str string) (string, error) {
|
||||||
}
|
}
|
||||||
return outputBuffer.String(), nil
|
return outputBuffer.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExtractUserLocalpart extracts the localpart portion of a user ID.
|
||||||
|
// See http://matrix.org/docs/spec/intro.html#user-identifiers
|
||||||
|
func ExtractUserLocalpart(userID string) (string, error) {
|
||||||
|
if len(userID) == 0 || userID[0] != '@' {
|
||||||
|
return "", fmt.Errorf("%s is not a valid user id")
|
||||||
|
}
|
||||||
|
return strings.TrimPrefix(
|
||||||
|
strings.SplitN(userID, ":", 2)[0], // @foo:bar:8448 => [ "@foo", "bar:8448" ]
|
||||||
|
"@", // remove "@" prefix
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
|
|
@ -62,3 +62,25 @@ func TestDecodeUserLocalpartErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var localparttests = []struct {
|
||||||
|
Input string
|
||||||
|
ExpectOutput string
|
||||||
|
}{
|
||||||
|
{"@foo:bar", "foo"},
|
||||||
|
{"@foo:bar:8448", "foo"},
|
||||||
|
{"@foo.bar:baz.quuz", "foo.bar"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractUserLocalpart(t *testing.T) {
|
||||||
|
for _, u := range localparttests {
|
||||||
|
out, err := ExtractUserLocalpart(u.Input)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("TestExtractUserLocalpart(%s) => Error: %s", u.Input, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if out != u.ExpectOutput {
|
||||||
|
t.Errorf("TestExtractUserLocalpart(%s) => Got: %s, Want %s", u.Input, out, u.ExpectOutput)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue