GLib based library to communicate with Matrix.org homeservers
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.
 
 
 
 

334 lines
10 KiB

  1. /*
  2. * This file is part of matrix-glib-sdk
  3. *
  4. * matrix-glib-sdk is free software: you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation, either
  7. * version 3 of the License, or (at your option) any later version.
  8. *
  9. * matrix-glib-sdk is distributed in the hope that it will be
  10. * useful, but WITHOUT ANY WARRANTY; without even the implied
  11. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with matrix-glib-sdk. If not, see
  16. * <http://www.gnu.org/licenses/>.
  17. */
  18. #include "matrix-event-typing.h"
  19. #include "matrix-types.h"
  20. #include "config.h"
  21. /**
  22. * SECTION:matrix-event-typing
  23. * @short_description: event to inform clients of the typing users
  24. *
  25. * This is the default event handler for `m.typing` events.
  26. *
  27. * Informs the client of the list of users currently typing.
  28. */
  29. enum {
  30. PROP_0,
  31. PROP_ROOM_ID,
  32. PROP_USER_IDS,
  33. NUM_PROPS
  34. };
  35. static GParamSpec* matrix_event_typing_properties[NUM_PROPS];
  36. typedef struct {
  37. gchar* _room_id;
  38. gchar** _user_ids;
  39. gint _user_ids_len;
  40. gint __user_ids_size_;
  41. } MatrixEventTypingPrivate;
  42. /**
  43. * MatrixEventTyping:
  44. */
  45. G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(MatrixEventTyping, matrix_event_typing, MATRIX_EVENT_TYPE_BASE);
  46. static void
  47. set_user_id(JsonArray *ary, guint idx, JsonNode *member_node, gpointer user_data) {
  48. gchar **user_ids = (gchar **)user_data;
  49. user_ids[idx] = g_strdup(json_node_get_string(member_node));
  50. }
  51. static void
  52. matrix_event_typing_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error)
  53. {
  54. MatrixEventTypingPrivate *priv = matrix_event_typing_get_instance_private(MATRIX_EVENT_TYPING(matrix_event_base));
  55. JsonObject *root;
  56. JsonObject *content_root;
  57. JsonNode *content_node;
  58. JsonNode *node;
  59. GError *inner_error = NULL;
  60. root = json_node_get_object(json_data);
  61. content_node = json_object_get_member(root, "content");
  62. content_root = json_node_get_object(content_node);
  63. if ((node = json_object_get_member(root, "room_id")) != NULL) {
  64. g_free(priv->_room_id);
  65. priv->_room_id = g_strdup(json_node_get_string(node));
  66. } else if (DEBUG) {
  67. g_warning("room_id is missing from a m.typing event");
  68. }
  69. if ((node = json_object_get_member(content_root, "user_ids")) != NULL) {
  70. JsonArray *user_ids_ary = json_node_get_array(node);
  71. gint user_ids_len = json_array_get_length(user_ids_ary);
  72. for (int i = 0; i < priv->_user_ids_len; i++) {
  73. g_free(priv->_user_ids[i]);
  74. }
  75. g_free(priv->_user_ids);
  76. priv->_user_ids = g_new(gchar *, user_ids_len);
  77. for (int i = 0; i < user_ids_len; i++) {
  78. json_array_foreach_element(user_ids_ary, set_user_id, priv->_user_ids);
  79. }
  80. priv->_user_ids_len = user_ids_len;
  81. } else if (DEBUG) {
  82. g_warning("content.user_ids is missing from a m.typing event");
  83. }
  84. MATRIX_EVENT_BASE_CLASS(matrix_event_typing_parent_class)->from_json(matrix_event_base, json_data, &inner_error);
  85. if (inner_error != NULL) {
  86. g_propagate_error(error, inner_error);
  87. }
  88. }
  89. static void
  90. matrix_event_typing_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error)
  91. {
  92. MatrixEventTypingPrivate *priv = matrix_event_typing_get_instance_private(MATRIX_EVENT_TYPING(matrix_event_base));
  93. JsonObject *root;
  94. JsonObject *content_root;
  95. JsonArray *user_ids_ary;
  96. JsonNode *content_node;
  97. JsonNode *user_ids_node;
  98. GError *inner_error = NULL;
  99. if (priv->_room_id == NULL) {
  100. g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE,
  101. "Won't generate a m.typing event without room_id");
  102. return;
  103. }
  104. root = json_node_get_object(json_data);
  105. content_node = json_object_get_member(root, "content");
  106. content_root = json_node_get_object(content_node);
  107. json_object_set_string_member(root, "room_id", priv->_room_id);
  108. user_ids_ary = json_array_new();
  109. user_ids_node = json_node_new(JSON_NODE_ARRAY);
  110. json_object_set_member(content_root, "user_ids", user_ids_node);
  111. json_node_set_array(user_ids_node, user_ids_ary);
  112. for (gint i = 0; i < priv->_user_ids_len; i++) {
  113. json_array_add_string_element(user_ids_ary, priv->_user_ids[i]);
  114. }
  115. MATRIX_EVENT_BASE_CLASS(matrix_event_typing_parent_class)->to_json(matrix_event_base, json_data, &inner_error);
  116. if (inner_error != NULL) {
  117. g_propagate_error (error, inner_error);
  118. }
  119. }
  120. MatrixEventTyping *
  121. matrix_event_typing_new(void)
  122. {
  123. return (MatrixEventTyping *)matrix_event_base_construct(MATRIX_EVENT_TYPE_TYPING);
  124. }
  125. const gchar *
  126. matrix_event_typing_get_room_id(MatrixEventTyping *matrix_event_typing)
  127. {
  128. MatrixEventTypingPrivate *priv;
  129. g_return_val_if_fail(matrix_event_typing != NULL, NULL);
  130. priv = matrix_event_typing_get_instance_private(matrix_event_typing);
  131. return priv->_room_id;
  132. }
  133. void
  134. matrix_event_typing_set_room_id(MatrixEventTyping *matrix_event_typing, const gchar *room_id)
  135. {
  136. MatrixEventTypingPrivate *priv;
  137. g_return_if_fail(matrix_event_typing != NULL);
  138. priv = matrix_event_typing_get_instance_private(matrix_event_typing);
  139. if (g_strcmp0(room_id, priv->_room_id) != 0) {
  140. g_free(priv->_room_id);
  141. priv->_room_id = g_strdup(room_id);
  142. g_object_notify_by_pspec((GObject *)matrix_event_typing, matrix_event_typing_properties[PROP_ROOM_ID]);
  143. }
  144. }
  145. /**
  146. * matrix_event_typing_get_user_ids:
  147. * @event: a #MatrixEventTyping
  148. * @n_user_ids: (nullable): placeholder for the length of the returned list
  149. *
  150. * Returns: (transfer none): a list of typing users
  151. */
  152. gchar **
  153. matrix_event_typing_get_user_ids(MatrixEventTyping *matrix_event_typing, int *n_user_ids)
  154. {
  155. MatrixEventTypingPrivate *priv;
  156. g_return_val_if_fail(matrix_event_typing != NULL, NULL);
  157. priv = matrix_event_typing_get_instance_private(matrix_event_typing);
  158. if (n_user_ids != NULL) {
  159. *n_user_ids = priv->_user_ids_len;
  160. }
  161. return priv->_user_ids;
  162. }
  163. void
  164. matrix_event_typing_set_user_ids(MatrixEventTyping *matrix_event_typing, gchar **user_ids, int n_user_ids)
  165. {
  166. MatrixEventTypingPrivate *priv;
  167. g_return_if_fail(matrix_event_typing != NULL);
  168. priv = matrix_event_typing_get_instance_private(matrix_event_typing);
  169. if (priv->_user_ids != user_ids) {
  170. for (gint i = 0; i < priv->_user_ids_len; i++) {
  171. g_free(priv->_user_ids[i]);
  172. }
  173. g_free(priv->_user_ids);
  174. priv->_user_ids = g_new(gchar *, n_user_ids);
  175. for (gint i = 0; i < n_user_ids; i++) {
  176. priv->_user_ids[i] = g_strdup(user_ids[i]);
  177. }
  178. priv->_user_ids_len = n_user_ids;
  179. g_object_notify_by_pspec((GObject *)matrix_event_typing, matrix_event_typing_properties[PROP_USER_IDS]);
  180. }
  181. }
  182. static void
  183. matrix_event_typing_finalize(GObject *gobject)
  184. {
  185. MatrixEventTypingPrivate *priv = matrix_event_typing_get_instance_private(MATRIX_EVENT_TYPING(gobject));
  186. g_free(priv->_room_id);
  187. for (gint i = 0; i < priv->_user_ids_len; i++) {
  188. g_free(priv->_user_ids[i]);
  189. }
  190. priv->_user_ids = (g_free(priv->_user_ids), NULL);
  191. G_OBJECT_CLASS(matrix_event_typing_parent_class)->finalize(gobject);
  192. }
  193. static void
  194. matrix_event_typing_get_property(GObject *gobject, guint property_id, GValue* value, GParamSpec* pspec)
  195. {
  196. MatrixEventTyping *matrix_event_typing = G_TYPE_CHECK_INSTANCE_CAST(gobject, MATRIX_EVENT_TYPE_TYPING, MatrixEventTyping);
  197. switch (property_id) {
  198. case PROP_ROOM_ID:
  199. g_value_set_string(value, matrix_event_typing_get_room_id(matrix_event_typing));
  200. break;
  201. case PROP_USER_IDS:
  202. g_value_set_boxed(value, matrix_event_typing_get_user_ids(matrix_event_typing, NULL));
  203. break;
  204. default:
  205. G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
  206. break;
  207. }
  208. }
  209. static void
  210. matrix_event_typing_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec* pspec)
  211. {
  212. MatrixEventTyping *matrix_event_typing = G_TYPE_CHECK_INSTANCE_CAST(gobject, MATRIX_EVENT_TYPE_TYPING, MatrixEventTyping);
  213. switch (property_id) {
  214. case PROP_ROOM_ID:
  215. matrix_event_typing_set_room_id(matrix_event_typing, g_value_get_string (value));
  216. break;
  217. case PROP_USER_IDS:
  218. {
  219. gpointer boxed = g_value_get_boxed(value);
  220. matrix_event_typing_set_user_ids(matrix_event_typing, boxed, (boxed == NULL) ? 0 : g_strv_length(boxed));
  221. break;
  222. }
  223. default:
  224. G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
  225. break;
  226. }
  227. }
  228. static void
  229. matrix_event_typing_class_init(MatrixEventTypingClass *klass)
  230. {
  231. ((MatrixEventBaseClass *)klass)->from_json = matrix_event_typing_real_from_json;
  232. ((MatrixEventBaseClass *)klass)->to_json = matrix_event_typing_real_to_json;
  233. G_OBJECT_CLASS(klass)->get_property = matrix_event_typing_get_property;
  234. G_OBJECT_CLASS(klass)->set_property = matrix_event_typing_set_property;
  235. G_OBJECT_CLASS(klass)->finalize = matrix_event_typing_finalize;
  236. /**
  237. * MatrixEventTyping:room-id:
  238. *
  239. * The ID of the room this event belongs to.
  240. */
  241. matrix_event_typing_properties[PROP_ROOM_ID] = g_param_spec_string(
  242. "room-id", "room-id", "room-id",
  243. NULL,
  244. G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
  245. g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_ROOM_ID, matrix_event_typing_properties[PROP_ROOM_ID]);
  246. /**
  247. * MatrixEventTyping:user-ids:
  248. *
  249. * The list of user IDs typing in this room, if any.
  250. */
  251. matrix_event_typing_properties[PROP_USER_IDS] = g_param_spec_boxed(
  252. "user-ids", "user-ids", "user-ids",
  253. G_TYPE_STRV,
  254. G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
  255. g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_USER_IDS, matrix_event_typing_properties[PROP_USER_IDS]);
  256. }
  257. static void
  258. matrix_event_typing_init(MatrixEventTyping *matrix_event_typing)
  259. {
  260. MatrixEventTypingPrivate *priv = matrix_event_typing_get_instance_private(matrix_event_typing);
  261. priv->_room_id = NULL;
  262. }