From 314b63e9001d67df97da34e69006374c4c3b9585 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 2 Sep 2016 10:19:37 +0100 Subject: [PATCH 1/2] Set the desired DisplayName for a client when it is being configured --- .../matrix-org/go-neb/clients/clients.go | 12 ++++++++++++ src/github.com/matrix-org/go-neb/matrix/matrix.go | 14 +++++++++++++- src/github.com/matrix-org/go-neb/types/types.go | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/clients/clients.go b/src/github.com/matrix-org/go-neb/clients/clients.go index 3c9a5b6..71b72de 100644 --- a/src/github.com/matrix-org/go-neb/clients/clients.go +++ b/src/github.com/matrix-org/go-neb/clients/clients.go @@ -115,6 +115,18 @@ func (c *Clients) updateClientInDB(newConfig types.ClientConfig) (new clientEntr return } + // set the new display name if they differ + if old.config.DisplayName != new.config.DisplayName { + if err := new.client.SetDisplayName(new.config.DisplayName); err != nil { + // whine about it but don't stop: this isn't fatal. + log.WithFields(log.Fields{ + log.ErrorKey: err, + "displayname": new.config.DisplayName, + "user_id": new.config.UserID, + }).Error("Failed to set display name") + } + } + if old.config, err = c.db.StoreMatrixClientConfig(new.config); err != nil { new.client.StopSync() return diff --git a/src/github.com/matrix-org/go-neb/matrix/matrix.go b/src/github.com/matrix-org/go-neb/matrix/matrix.go index 3911cf8..14610ad 100644 --- a/src/github.com/matrix-org/go-neb/matrix/matrix.go +++ b/src/github.com/matrix-org/go-neb/matrix/matrix.go @@ -104,6 +104,16 @@ func (cli *Client) JoinRoom(roomIDorAlias, serverName, invitingUserID string) (s return joinRoomResponse.RoomID, nil } +// SetDisplayName sets the user's profile display name +func (cli *Client) SetDisplayName(displayName string) error { + urlPath := cli.buildURL("profile", cli.UserID, "displayname") + s := struct { + DisplayName string `json:"displayname"` + }{displayName} + _, err := cli.sendJSON("PUT", urlPath, &s) + return err +} + // SendMessageEvent sends a message event into a room, returning the event_id on success. // contentJSON should be a pointer to something that can be encoded as JSON using json.Marshal. func (cli *Client) SendMessageEvent(roomID string, eventType string, contentJSON interface{}) (string, error) { @@ -282,11 +292,13 @@ func (cli *Client) sendJSON(method string, httpURL string, contentJSON interface }) logger.Print("Sending JSON request") res, err := cli.httpClient.Do(req) + if res != nil { + defer res.Body.Close() + } if err != nil { logger.WithError(err).Warn("Failed to send JSON request") return nil, err } - defer res.Body.Close() contents, err := ioutil.ReadAll(res.Body) if res.StatusCode >= 300 { logger.WithFields(log.Fields{ diff --git a/src/github.com/matrix-org/go-neb/types/types.go b/src/github.com/matrix-org/go-neb/types/types.go index ae076f2..2583707 100644 --- a/src/github.com/matrix-org/go-neb/types/types.go +++ b/src/github.com/matrix-org/go-neb/types/types.go @@ -18,6 +18,7 @@ type ClientConfig struct { AccessToken string // The matrix access token to authenticate the requests with. Sync bool // True to start a sync stream for this user AutoJoinRooms bool // True to automatically join all rooms for this user + DisplayName string // The display name to set for the matrix client } // Check that the client has the correct fields. From 7bafa40708c6c2fe9856d7b9510af45b4e0d3022 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 2 Sep 2016 10:26:23 +0100 Subject: [PATCH 2/2] Only Sync if told to do so --- src/github.com/matrix-org/go-neb/clients/clients.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/clients/clients.go b/src/github.com/matrix-org/go-neb/clients/clients.go index 71b72de..79fce5c 100644 --- a/src/github.com/matrix-org/go-neb/clients/clients.go +++ b/src/github.com/matrix-org/go-neb/clients/clients.go @@ -219,7 +219,9 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) { }) } - go client.Sync() + if config.Sync { + go client.Sync() + } return client, nil }