matrixuser.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "matrixuser.h"
  2. #include "core/io/json.h"
  3. String MatrixUser::get_display_name(bool sync) {
  4. if (sync) {
  5. Variant response_v;
  6. client->request_json("/_matrix/client/r0/profile/"+user_id+"/displayname", Dictionary(), HTTPClient::Method::METHOD_GET, response_v);
  7. Dictionary response = response_v;
  8. display_name = response["displayname"];
  9. }
  10. return display_name;
  11. }
  12. String MatrixUser::get_friendly_name(bool sync) {
  13. get_display_name(sync);
  14. if (display_name.length() != 0) {
  15. return display_name;
  16. } else {
  17. return user_id;
  18. }
  19. }
  20. Error MatrixUser::set_display_name(String name) {
  21. Dictionary request_body;
  22. request_body["displayname"] = name;
  23. Variant response_v;
  24. HTTPClient::ResponseCode http_status = client->request_json("/_matrix/client/r0/profile/"+user_id.http_escape()+"/displayname", request_body, HTTPClient::Method::METHOD_PUT, response_v);
  25. if (http_status == 200) {
  26. Dictionary response = response_v;
  27. display_name = name;
  28. return MATRIX_OK;
  29. } else {
  30. return MATRIX_NOT_IMPLEMENTED;
  31. }
  32. }
  33. String MatrixUser::get_avatar_url(bool sync) {
  34. if (sync) {
  35. Variant response_v;
  36. HTTPClient::ResponseCode http_status = client->request_json("/_matrix/client/r0/profile/"+user_id.http_escape()+"/displayname", Dictionary(), HTTPClient::Method::METHOD_GET, response_v);
  37. Dictionary response = response_v;
  38. if (response.has("avatar_url")) {
  39. avatar_url = response["avatar_url"];
  40. } else {
  41. avatar_url = String();
  42. }
  43. }
  44. return "https://"+client->get_hs_name()+"/_matrix/media/r0/download/"+avatar_url.substr(6, avatar_url.length()-6);
  45. }
  46. Error MatrixUser::set_avatar_url(String mxcurl) {
  47. if (!mxcurl.begins_with("mxc://")) {
  48. WARN_PRINT("Avatar URL must begin with \"mxc://\"!");
  49. return MATRIX_INVALID_REQUEST;
  50. }
  51. Dictionary request_body;
  52. request_body["avatar_url"] = mxcurl;
  53. Variant response_v;
  54. HTTPClient::ResponseCode http_status = client->request_json("/_matrix/client/r0/profile/"+user_id+"/avatar_url", request_body, HTTPClient::Method::METHOD_PUT, response_v);
  55. Dictionary response = response_v;
  56. if (http_status == 200) {
  57. avatar_url = mxcurl;
  58. return MATRIX_OK;
  59. } else {
  60. WARN_PRINT(((String)response["error"]).utf8().get_data());
  61. return MATRIX_NOT_IMPLEMENTED;
  62. }
  63. }
  64. MatrixUser::MatrixUser() {
  65. }
  66. void MatrixUser::init(MatrixClient *c, String id) {
  67. client = c;
  68. user_id = id;
  69. }
  70. void MatrixUser::_bind_methods() {
  71. ClassDB::bind_method("get_display_name", &MatrixUser::get_display_name);
  72. ClassDB::bind_method("get_friendly_name", &MatrixUser::get_friendly_name);
  73. ClassDB::bind_method("set_display_name", &MatrixUser::set_display_name);
  74. ClassDB::bind_method("get_avatar_url", &MatrixUser::get_avatar_url);
  75. ClassDB::bind_method("set_avatar_url", &MatrixUser::set_avatar_url);
  76. }