mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
Unfork
This commit is contained in:
parent
920b154a41
commit
e1d9c9b869
6 changed files with 3 additions and 287 deletions
|
|
@ -1,6 +1,4 @@
|
|||
# gomatrix
|
||||
[](https://godoc.org/github.com/matrix-org/gomatrix)
|
||||
# mautrix-go
|
||||
[](https://godoc.org/maunium.net/go/mautrix)
|
||||
|
||||
A Golang Matrix client.
|
||||
|
||||
**THIS IS UNDER ACTIVE DEVELOPMENT: BREAKING CHANGES ARE FREQUENT.**
|
||||
A Golang Matrix framework.
|
||||
|
|
|
|||
|
|
@ -1,119 +0,0 @@
|
|||
package gomatrix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Example_sync() {
|
||||
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe")
|
||||
cli.Store.SaveFilterID("@example:matrix.org", "2") // Optional: if you know it already
|
||||
cli.Store.SaveNextBatch("@example:matrix.org", "111_222_333_444") // Optional: if you know it already
|
||||
syncer := cli.Syncer.(*DefaultSyncer)
|
||||
syncer.OnEventType("m.room.message", func(ev *Event) {
|
||||
fmt.Println("Message: ", ev)
|
||||
})
|
||||
|
||||
// Blocking version
|
||||
if err := cli.Sync(); err != nil {
|
||||
fmt.Println("Sync() returned ", err)
|
||||
}
|
||||
|
||||
// Non-blocking version
|
||||
go func() {
|
||||
for {
|
||||
if err := cli.Sync(); err != nil {
|
||||
fmt.Println("Sync() returned ", err)
|
||||
}
|
||||
// Optional: Wait a period of time before trying to sync again.
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func Example_customInterfaces() {
|
||||
// Custom interfaces must be set prior to calling functions on the client.
|
||||
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe")
|
||||
|
||||
// anything which implements the Storer interface
|
||||
customStore := NewInMemoryStore()
|
||||
cli.Store = customStore
|
||||
|
||||
// anything which implements the Syncer interface
|
||||
customSyncer := NewDefaultSyncer("@example:matrix.org", customStore)
|
||||
cli.Syncer = customSyncer
|
||||
|
||||
// any http.Client
|
||||
cli.Client = http.DefaultClient
|
||||
|
||||
// Once you call a function, you can't safely change the interfaces.
|
||||
cli.SendText("!foo:bar", "Down the rabbit hole")
|
||||
}
|
||||
|
||||
func ExampleClient_BuildURLWithQuery() {
|
||||
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
|
||||
out := cli.BuildURLWithQuery([]string{"sync"}, map[string]string{
|
||||
"filter_id": "5",
|
||||
})
|
||||
fmt.Println(out)
|
||||
// Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5
|
||||
}
|
||||
|
||||
func ExampleClient_BuildURL() {
|
||||
userID := "@example:matrix.org"
|
||||
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
|
||||
out := cli.BuildURL("user", userID, "filter")
|
||||
fmt.Println(out)
|
||||
// Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456
|
||||
}
|
||||
|
||||
func ExampleClient_BuildBaseURL() {
|
||||
userID := "@example:matrix.org"
|
||||
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
|
||||
out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org")
|
||||
fmt.Println(out)
|
||||
// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456
|
||||
}
|
||||
|
||||
// Retrieve the content of a m.room.name state event.
|
||||
func ExampleClient_StateEvent() {
|
||||
content := struct {
|
||||
Name string `json:"name"`
|
||||
}{}
|
||||
cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456")
|
||||
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Join a room by ID.
|
||||
func ExampleClient_JoinRoom_id() {
|
||||
cli, _ := NewClient("http://localhost:8008", "@example:localhost", "abcdef123456")
|
||||
if _, err := cli.JoinRoom("!uOILRrqxnsYgQdUzar:localhost", "", nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Join a room by alias.
|
||||
func ExampleClient_JoinRoom_alias() {
|
||||
cli, _ := NewClient("http://localhost:8008", "@example:localhost", "abcdef123456")
|
||||
if resp, err := cli.JoinRoom("#test:localhost", "", nil); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
// Use room ID for something.
|
||||
_ = resp.RoomID
|
||||
}
|
||||
}
|
||||
|
||||
// Login to a local homeserver and set the user ID and access token on success.
|
||||
func ExampleClient_Login() {
|
||||
cli, _ := NewClient("http://localhost:8008", "", "")
|
||||
resp, err := cli.Login(&ReqLogin{
|
||||
Type: "m.login.password",
|
||||
User: "alice",
|
||||
Password: "wonderland",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cli.SetCredentials(resp.UserID, resp.AccessToken)
|
||||
}
|
||||
105
client_test.go
105
client_test.go
|
|
@ -1,105 +0,0 @@
|
|||
package gomatrix
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClient_LeaveRoom(t *testing.T) {
|
||||
cli := mockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if req.Method == "POST" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/leave" {
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
|
||||
})
|
||||
|
||||
if _, err := cli.LeaveRoom("!foo:bar"); err != nil {
|
||||
t.Fatalf("LeaveRoom: error, got %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_GetAvatarUrl(t *testing.T) {
|
||||
cli := mockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"avatar_url":"mxc://matrix.org/iJaUjkshgdfsdkjfn"}`)),
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
|
||||
})
|
||||
|
||||
if response, err := cli.GetAvatarURL(); err != nil {
|
||||
t.Fatalf("GetAvatarURL: Got error: %s", err.Error())
|
||||
} else if response == "" {
|
||||
t.Fatal("GetAvatarURL: Got empty response")
|
||||
} else if response != "mxc://matrix.org/iJaUjkshgdfsdkjfn" {
|
||||
t.Fatalf("Unexpected response URL: %s", response)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestClient_SetAvatarUrl(t *testing.T) {
|
||||
cli := mockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if req.Method == "PUT" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
|
||||
})
|
||||
|
||||
if err := cli.SetAvatarURL("https://foo.com/bar.png"); err != nil {
|
||||
t.Fatalf("GetAvatarURL: Got error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_StateEvent(t *testing.T) {
|
||||
cli := mockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/state/m.room.name" {
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"name":"Room Name Goes Here"}`)),
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path)
|
||||
})
|
||||
|
||||
content := struct {
|
||||
Name string `json:"name"`
|
||||
}{}
|
||||
|
||||
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
|
||||
t.Fatalf("StateEvent: error, got %s", err.Error())
|
||||
}
|
||||
if content.Name != "Room Name Goes Here" {
|
||||
t.Fatalf("StateEvent: got %s, want %s", content.Name, "Room Name Goes Here")
|
||||
}
|
||||
}
|
||||
|
||||
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
|
||||
mrt := MockRoundTripper{
|
||||
RT: fn,
|
||||
}
|
||||
|
||||
cli, _ := NewClient("https://test.gomatrix.org", "@user:test.gomatrix.org", "abcdef")
|
||||
cli.Client = &http.Client{
|
||||
Transport: mrt,
|
||||
}
|
||||
return cli
|
||||
}
|
||||
|
||||
type MockRoundTripper struct {
|
||||
RT func(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
func (t MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return t.RT(req)
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
#! /bin/bash
|
||||
|
||||
DOT_GIT="$(dirname $0)/../.git"
|
||||
|
||||
ln -s "../../hooks/pre-commit" "$DOT_GIT/hooks/pre-commit"
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
#! /bin/bash
|
||||
|
||||
set -eu
|
||||
|
||||
golint
|
||||
misspell --error .
|
||||
|
||||
# gofmt doesn't exit with an error code if the files don't match the expected
|
||||
# format. So we have to run it and see if it outputs anything.
|
||||
if gofmt -l -s . 2>&1 | read
|
||||
then
|
||||
echo "Error: not all code had been formatted with gofmt."
|
||||
echo "Fixing the following files"
|
||||
gofmt -s -w -l .
|
||||
echo
|
||||
echo "Please add them to the commit"
|
||||
git status --short
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ineffassign .
|
||||
|
||||
go fmt
|
||||
go tool vet --all --shadow .
|
||||
gocyclo -over 12 .
|
||||
go test -timeout 5s -test.v
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
package gomatrix
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleEncodeUserLocalpart() {
|
||||
localpart := EncodeUserLocalpart("Alph@Bet_50up")
|
||||
fmt.Println(localpart)
|
||||
// Output: _alph=40_bet__50up
|
||||
}
|
||||
|
||||
func ExampleDecodeUserLocalpart() {
|
||||
localpart, err := DecodeUserLocalpart("_alph=40_bet__50up")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(localpart)
|
||||
// Output: Alph@Bet_50up
|
||||
}
|
||||
|
||||
func ExampleExtractUserLocalpart() {
|
||||
localpart, err := ExtractUserLocalpart("@alice:matrix.org")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(localpart)
|
||||
// Output: alice
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue