Browse Source

Merge pull request #56 from matrix-org/kegan/configure-client-name

Set the desired DisplayName for a client when it is being configured
kegan/gh-explicit-unregister-webhooks
Kegsay 8 years ago
committed by GitHub
parent
commit
635ea03516
  1. 14
      src/github.com/matrix-org/go-neb/clients/clients.go
  2. 14
      src/github.com/matrix-org/go-neb/matrix/matrix.go
  3. 1
      src/github.com/matrix-org/go-neb/types/types.go

14
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) {
})
}
if config.Sync {
go client.Sync()
}
return client, nil
}

14
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{

1
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.

Loading…
Cancel
Save