From d7c141d997abcc91a6be550a07f38b1c8b32de4a Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Fri, 1 Mar 2019 04:18:31 +0300 Subject: [PATCH] Implement read delivery status support Conributing to issue #18 --- src/messageschannel.cpp | 46 +++++++++++++++++++++++++++++++---------- src/messageschannel.hpp | 2 ++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/messageschannel.cpp b/src/messageschannel.cpp index 973b72d..17a68c4 100644 --- a/src/messageschannel.cpp +++ b/src/messageschannel.cpp @@ -93,6 +93,22 @@ MatrixMessagesChannel::MatrixMessagesChannel(MatrixConnection *connection, QMatr connect(m_room, &QMatrixClient::Room::pendingEventChanged, this, &MatrixMessagesChannel::onPendingEventChanged); connect(m_room, &QMatrixClient::Room::typingChanged, this, &MatrixMessagesChannel::onTypingChanged); + connect(m_room, &QMatrixClient::Room::readMarkerForUserMoved, this, &MatrixMessagesChannel::onReadMarkerForUserMoved); +} + +void MatrixMessagesChannel::sendDeliveryReport(Tp::DeliveryStatus tpDeliveryStatus, const QString &deliveryToken) +{ + Tp::MessagePartList partList; + + Tp::MessagePart header; + header[QStringLiteral("message-sender")] = QDBusVariant(m_targetHandle); + header[QStringLiteral("message-sender-id")] = QDBusVariant(m_targetId); + header[QStringLiteral("message-type")] = QDBusVariant(Tp::ChannelTextMessageTypeDeliveryReport); + header[QStringLiteral("delivery-status")] = QDBusVariant(tpDeliveryStatus); + header[QStringLiteral("delivery-token")] = QDBusVariant(deliveryToken); + partList << header; + + addReceivedMessage(partList); } void MatrixMessagesChannel::onPendingEventChanged(int pendingEventIndex) @@ -115,17 +131,7 @@ void MatrixMessagesChannel::onPendingEventChanged(int pendingEventIndex) break; } - Tp::MessagePartList partList; - - Tp::MessagePart header; - header[QStringLiteral("message-sender")] = QDBusVariant(m_targetHandle); - header[QStringLiteral("message-sender-id")] = QDBusVariant(m_targetId); - header[QStringLiteral("message-type")] = QDBusVariant(Tp::ChannelTextMessageTypeDeliveryReport); - header[QStringLiteral("delivery-status")] = QDBusVariant(tpDeliveryStatus); - header[QStringLiteral("delivery-token")] = QDBusVariant(pendingEvent.event()->id()); - partList << header; - - addReceivedMessage(partList); + sendDeliveryReport(tpDeliveryStatus,pendingEvent.event()->id()); } void MatrixMessagesChannel::sendChatStateNotification(uint state) @@ -179,6 +185,13 @@ void MatrixMessagesChannel::processMessageEvent(const QMatrixClient::RoomMessage if (event->isRedacted()) header[QStringLiteral("delivery-status")] = QDBusVariant(Tp::DeliveryStatusDeleted); + /* Read markers */ + QList usersAtEventId = m_room->usersAtEventId(event->id()); + if ((usersAtEventId.contains(m_connection->matrix()->user()) && usersAtEventId.count()>1) || + (!usersAtEventId.contains(m_connection->matrix()->user()) && usersAtEventId.count()>0)) { + header[QStringLiteral("delivery-status")] = QDBusVariant(Tp::DeliveryStatusRead); + } + /* Text message */ Tp::MessagePartList body; Tp::MessagePart text; @@ -220,6 +233,14 @@ void MatrixMessagesChannel::onTypingChanged() } } +void MatrixMessagesChannel::onReadMarkerForUserMoved(QMatrixClient::User* user, QString fromEventId, QString toEventId) +{ + Q_UNUSED(user); + for (auto eventIt = m_room->findInTimeline(fromEventId); eventIt < m_room->findInTimeline(toEventId); ++eventIt) { + sendDeliveryReport(Tp::DeliveryStatusRead,eventIt->event()->id()); + } +} + void MatrixMessagesChannel::reactivateLocalTyping() { sendChatStateNotification(Tp::ChannelChatStateComposing); @@ -241,5 +262,8 @@ void MatrixMessagesChannel::setChatState(uint state, Tp::DBusError *error) } else { m_localTypingTimer->stop(); } + + m_room->markAllMessagesAsRead(); + sendChatStateNotification(state); } diff --git a/src/messageschannel.hpp b/src/messageschannel.hpp index 013b6eb..af343c5 100644 --- a/src/messageschannel.hpp +++ b/src/messageschannel.hpp @@ -72,7 +72,9 @@ class MatrixMessagesChannel : public Tp::BaseChannelTextType private: MatrixMessagesChannel(MatrixConnection *connection, QMatrixClient::Room *room, Tp::BaseChannel *baseChannel); + void sendDeliveryReport(Tp::DeliveryStatus tpDeliveryStatus, const QString &deliveryToken); void onPendingEventChanged(int pendingEventIndex); + void onReadMarkerForUserMoved(QMatrixClient::User* user, QString fromEventId, QString toEventId); void onTypingChanged(); void reactivateLocalTyping(); void sendChatStateNotification(uint state);