From 97f4e98e1da6a52d3b93bbdfb84e0fc4cb769297 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 29 Nov 2016 17:03:42 +0000 Subject: [PATCH] Start fleshing out basic Client types from Go-NEB --- README.md | 2 ++ client.go | 42 ++++++++++++++++++++++++++++++++++++++++++ sync.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 client.go create mode 100644 sync.go diff --git a/README.md b/README.md index 7ed87f4..4f6df02 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # gomatrix +[![GoDoc](https://godoc.org/github.com/matrix-org/gomatrix?status.svg)](https://godoc.org/github.com/matrix-org/gomatrix) + A Golang Matrix client diff --git a/client.go b/client.go new file mode 100644 index 0000000..b5cfbe7 --- /dev/null +++ b/client.go @@ -0,0 +1,42 @@ +package gomatrix + +import ( + "net/http" + "net/url" + "sync" +) + +// Client represents a Matrix client. +type Client struct { + HomeserverURL *url.URL // The base homeserver URL + Prefix string // The API prefix eg '/_matrix/client/r0' + UserID string // The user ID of the client. Used for forming HTTP paths which use the client's user ID. + AccessToken string // The access_token for the client. + syncingMutex sync.Mutex // protects syncingID + syncingID uint32 // Identifies the current Sync. Only one Sync can be active at any given time. + Client *http.Client // The underlying HTTP client which will be used to make HTTP requests. + FilterStorer FilterStorer // Interface for saving and loading the filter ID for sync. + NextBatchStorer NextBatchStorer // Interface for saving and loading the "next_batch" sync token. + // TODO: Worker and Rooms +} + +// NewClient creates a new Matrix Client ready for syncing +func NewClient(httpClient *http.Client, homeserverURL *url.URL, accessToken, userID string) *Client { + cli := Client{ + AccessToken: accessToken, + HomeserverURL: homeserverURL, + UserID: userID, + Prefix: "/_matrix/client/r0", + } + // By default, use a no-op next_batch storer which will never save tokens and always + // "load" the empty string as a token. The client will work with this storer: it just won't + // remember the token across restarts. In practice, a database backend should be used. + cli.NextBatchStorer = NopNextBatchStore{} + // By default, use a no-op filter storer which will never save the filter ID and always + // "load" nothing. The client will work with this storer: it just won't remember the filter + // ID across restarts and hence request a new one. In practice, a database backend should be used. + cli.FilterStorer = NopFilterStore{} + cli.Client = httpClient + + return &cli +} diff --git a/sync.go b/sync.go new file mode 100644 index 0000000..03c2a2e --- /dev/null +++ b/sync.go @@ -0,0 +1,35 @@ +package gomatrix + +// NextBatchStorer controls loading/saving of next_batch tokens for users +type NextBatchStorer interface { + // Save a next_batch token for a given user. Best effort. + Save(userID, nextBatch string) + // Load a next_batch token for a given user. Return an empty string if no token exists. + Load(userID string) string +} + +// NopNextBatchStore does not load or save next_batch tokens. +type NopNextBatchStore struct{} + +// Save does nothing +func (s NopNextBatchStore) Save(userID, nextBatch string) {} + +// Load does nothing +func (s NopNextBatchStore) Load(userID string) string { return "" } + +// FilterStorer controls loading/saving of filter IDs for users +type FilterStorer interface { + // Save a filter ID for a given user. Best effort. + Save(userID, filterID string) + // Load a filter ID for a given user. Return an empty string if no token exists. + Load(userID string) string +} + +// NopFilterStore does not load or save filter IDs. +type NopFilterStore struct{} + +// Save does nothing +func (s NopFilterStore) Save(userID, filterID string) {} + +// Load does nothing +func (s NopFilterStore) Load(userID string) string { return "" }