matrix.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. #include "scene/main/node.h"
  4. #include "core/io/http_client.h"
  5. #include "core/os/thread.h"
  6. #include "core/bind/core_bind.h"
  7. #include "matrixroom.h"
  8. #include "matrixuser.h"
  9. class MatrixRoom;
  10. class MatrixUser;
  11. #define MATRIX_OK Error::OK //request completed successfully
  12. #define MATRIX_CANT_CONNECT Error::ERR_CANT_CONNECT //not able to connect to homeserver
  13. #define MATRIX_UNAUTHENTICATED Error::ERR_UNCONFIGURED //request requires authentication
  14. #define MATRIX_UNAUTHORIZED Error::ERR_UNAUTHORIZED //not allowed to do this
  15. #define MATRIX_UNABLE Error::FAILED //request is not possible to fulfill
  16. #define MATRIX_RATELIMITED Error::ERR_BUSY //too many requests, you are ratelimited
  17. #define MATRIX_INVALID_REQUEST Error::ERR_INVALID_DATA //request contained invalid data
  18. #define MATRIX_INVALID_RESPONSE Error::ERR_PARSE_ERROR //response contained invalid data
  19. #define MATRIX_NOT_IMPLEMENTED Error::ERR_BUG //action is not yet implemented in this library
  20. class MatrixClient : public Node {
  21. friend class MatrixRoom;
  22. friend class MatrixUser;
  23. GDCLASS(MatrixClient,Node);
  24. HTTPClient sync_client;
  25. String hs_name;
  26. String auth_token;
  27. String sync_token;
  28. String user_id;
  29. Thread listener_thread;
  30. bool should_listen = true;
  31. Dictionary rooms;
  32. HTTPClient::ResponseCode request(HTTPClient &client, String endpoint, String body, HTTPClient::Method method, String &response_body);
  33. HTTPClient::ResponseCode request(HTTPClient &client, String endpoint, String body, HTTPClient::Method method);
  34. HTTPClient::ResponseCode request(String endpoint, String body, HTTPClient::Method method, String &response_body);
  35. HTTPClient::ResponseCode request(String endpoint, String body, HTTPClient::Method method);
  36. HTTPClient::ResponseCode request_json(String endpoint, Dictionary body, HTTPClient::Method method, Variant &response_body, bool auth=true);
  37. HTTPClient::ResponseCode request_json(String endpoint, Dictionary body, HTTPClient::Method method, bool auth=true);
  38. static void _listen_forever(void *m);
  39. protected:
  40. static void _bind_methods();
  41. public:
  42. String get_hs_name() const;
  43. void set_hs_name(String name);
  44. String get_auth_token() const;
  45. void set_auth_token(String token);
  46. String get_sync_token() const;
  47. void set_sync_token(String token);
  48. String get_user_id() const;
  49. void set_user_id(String id);
  50. Dictionary get_rooms() const;
  51. Error register_account(Variant username, String password, bool guest=false);
  52. Error login(String username, String password);
  53. Error logout();
  54. Error start_listening();
  55. bool is_listening();
  56. Error stop_listening();
  57. Error _sync(int timeout_ms=30000);
  58. Error _listen_forever();
  59. Variant create_room(String alias=String(), bool is_public=false, Array invitees = Array());
  60. Variant join_room(String room_id_or_alias);
  61. Variant leave_room(String room_id);
  62. //Variant upload(PoolByteArray data, String content_type); TODO: :-D
  63. Variant get_user(String user_id);
  64. Variant get_me();
  65. MatrixClient();
  66. };
  67. #endif