You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8 KiB

  1. package handlers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/matrix-org/go-neb/api"
  6. "github.com/matrix-org/go-neb/clients"
  7. "github.com/matrix-org/util"
  8. )
  9. // ConfigureClient represents an HTTP handler capable of processing /admin/configureClient requests.
  10. type ConfigureClient struct {
  11. Clients *clients.Clients
  12. }
  13. // OnIncomingRequest handles POST requests to /admin/configureClient. The JSON object provided
  14. // is of type "api.ClientConfig".
  15. //
  16. // If a DisplayName is supplied, this request will set this client's display name
  17. // if the old ClientConfig DisplayName differs from the new ClientConfig DisplayName.
  18. //
  19. // Request:
  20. // POST /admin/configureClient
  21. // {
  22. // "UserID": "@my_bot:localhost",
  23. // "HomeserverURL": "http://localhost:8008",
  24. // "Sync": true,
  25. // "DisplayName": "My Bot"
  26. // }
  27. //
  28. // Response:
  29. // HTTP/1.1 200 OK
  30. // {
  31. // "OldClient": {
  32. // // The old api.ClientConfig
  33. // },
  34. // "NewClient": {
  35. // // The new api.ClientConfig
  36. // }
  37. // }
  38. func (s *ConfigureClient) OnIncomingRequest(req *http.Request) util.JSONResponse {
  39. if req.Method != "POST" {
  40. return util.MessageResponse(405, "Unsupported Method")
  41. }
  42. var body api.ClientConfig
  43. if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
  44. return util.MessageResponse(400, "Error parsing request JSON")
  45. }
  46. if err := body.Check(); err != nil {
  47. return util.MessageResponse(400, "Error parsing client config")
  48. }
  49. oldClient, err := s.Clients.Update(body)
  50. if err != nil {
  51. util.GetLogger(req.Context()).WithError(err).WithField("body", body).Error("Failed to Clients.Update")
  52. return util.MessageResponse(500, "Error storing token")
  53. }
  54. return util.JSONResponse{
  55. Code: 200,
  56. JSON: struct {
  57. OldClient api.ClientConfig
  58. NewClient api.ClientConfig
  59. }{oldClient, body},
  60. }
  61. }