From 13abc13adc4dc4ad0cd53678120f54ba426a3dfc Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 7 Mar 2016 15:22:39 +0100 Subject: [PATCH] Add event handler for m.receipt --- .gitignore | 1 + src/Makefile.am | 1 + src/matrix-event-receipt.vala | 88 +++++++++++++++++++++++++++++++++++ src/matrix-event-types.c | 3 ++ 4 files changed, 93 insertions(+) create mode 100644 src/matrix-event-receipt.vala diff --git a/.gitignore b/.gitignore index 8aaa95a..60073a0 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ Makefile.in /src/matrix-event-room-topic.c /src/matrix-event-typing.c /src/matrix-event-room-aliases.c +/src/matrix-event-receipt.c diff --git a/src/Makefile.am b/src/Makefile.am index 96f1f24..eec415e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,6 +32,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-topic.vala \ matrix-event-typing.vala \ matrix-event-room-aliases.vala \ + matrix-event-receipt.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-event-receipt.vala b/src/matrix-event-receipt.vala new file mode 100644 index 0000000..aae3c6a --- /dev/null +++ b/src/matrix-event-receipt.vala @@ -0,0 +1,88 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with matrix-glib-sdk. If not, see + * . + */ + +/** + * Class to hold m.receipt events. + * + * Informs the client of new receipts. + */ +public class Matrix.Event.Receipt : Matrix.Event.Base { + public string? room_id { get; set; default = null; } + + private struct ReceiptData { + string event_id; + string typ; + string user; + } + + private HashTable _receipt_data = null; + + private static bool + _rd_equal(ReceiptData k1, ReceiptData k2) + { + return ((k1.event_id == k2.event_id) + && (k1.typ == k2.typ) + && (k1.user == k2.user)); + } + + private void + _init_receipt_data() + { + _receipt_data = new HashTable( + direct_hash, (EqualFunc)_rd_equal); + } + + protected override void + from_json(Json.Node json_data) + throws Matrix.Error + { + var root = json_data.get_object(); + var content_root = root.get_member("content").get_object(); + Json.Node? node; + + if ((node = root.get_member("room_id")) != null) { + _room_id = node.get_string(); + } else { + warning("room_id is missing from a m.presence event"); + } + + content_root.foreach_member((obj, event_id, event_content) => { + if ((node = event_content.get_object() + .get_member("m.read")) != null) { + var read_obj = node.get_object(); + read_obj.foreach_member((robj, r_user_id, r_content) => { + if (_receipt_data == null) { + _init_receipt_data(); + } + + ReceiptData rd_key = ReceiptData() { + event_id = event_id, + typ = "m.read", + user = r_user_id + }; + + _receipt_data.replace( + rd_key, + (ulong)r_content.get_object().get_member("ts").get_int()); + }); + } else { + warning("content.$event-id.m.read is missing from a m.presence event"); + } + }); + } +} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 1ba7157..fc90c69 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -170,6 +170,9 @@ matrix_event_types_ctor(void) matrix_event_register_type("m.room.aliases", MATRIX_EVENT_TYPE_ROOM_ALIASES, NULL); + matrix_event_register_type("m.receipt", + MATRIX_EVENT_TYPE_RECEIPT, + NULL); } void