diff --git a/src/messageschannel.cpp b/src/messageschannel.cpp index 695fc01..53ca058 100644 --- a/src/messageschannel.cpp +++ b/src/messageschannel.cpp @@ -93,6 +93,22 @@ MatrixMessagesChannel::MatrixMessagesChannel(MatrixConnection *connection, Quoti connect(m_room, &Quotient::Room::pendingEventChanged, this, &MatrixMessagesChannel::onPendingEventChanged); connect(m_room, &Quotient::Room::typingChanged, this, &MatrixMessagesChannel::onTypingChanged); + connect(m_room, &Quotient::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,19 @@ 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; + sendDeliveryReport(tpDeliveryStatus, pendingEvent.event()->id()); +} - addReceivedMessage(partList); +void MatrixMessagesChannel::onReadMarkerForUserMoved(Quotient::User *user, QString fromEventId, QString toEventId) +{ + Q_UNUSED(user); + QStringList tokens; + for (auto eventIt = m_room->findInTimeline(fromEventId); eventIt < m_room->findInTimeline(toEventId); ++eventIt) { + sendDeliveryReport(Tp::DeliveryStatusRead, eventIt->event()->id()); + tokens.append(eventIt->event()->id()); + } + Tp::DBusError error; + acknowledgePendingMessages(tokens,&error); } void MatrixMessagesChannel::sendChatStateNotification(uint state) @@ -179,6 +197,13 @@ void MatrixMessagesChannel::processMessageEvent(const Quotient::RoomMessageEvent 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; @@ -238,5 +263,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 05d93cf..a829560 100644 --- a/src/messageschannel.hpp +++ b/src/messageschannel.hpp @@ -72,7 +72,9 @@ class MatrixMessagesChannel : public Tp::BaseChannelTextType private: MatrixMessagesChannel(MatrixConnection *connection, Quotient::Room *room, Tp::BaseChannel *baseChannel); + void sendDeliveryReport(Tp::DeliveryStatus tpDeliveryStatus, const QString &deliveryToken); void onPendingEventChanged(int pendingEventIndex); + void onReadMarkerForUserMoved(Quotient::User* user, QString fromEventId, QString toEventId); void onTypingChanged(); void reactivateLocalTyping(); void sendChatStateNotification(uint state);