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..79fce5c 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 @@ -207,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 } 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.