Skip to content

Commit

Permalink
Implement read delivery status support
Browse files Browse the repository at this point in the history
Conributing to issue #18
  • Loading branch information
a-andreyev committed Mar 1, 2019
1 parent 9869614 commit d7c141d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/messageschannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -179,6 +185,13 @@ void MatrixMessagesChannel::processMessageEvent(const QMatrixClient::RoomMessage
if (event->isRedacted())
header[QStringLiteral("delivery-status")] = QDBusVariant(Tp::DeliveryStatusDeleted);

/* Read markers */
QList<QMatrixClient::User*> 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;
Expand Down Expand Up @@ -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);
Expand All @@ -241,5 +262,8 @@ void MatrixMessagesChannel::setChatState(uint state, Tp::DBusError *error)
} else {
m_localTypingTimer->stop();
}

m_room->markAllMessagesAsRead();

sendChatStateNotification(state);
}
2 changes: 2 additions & 0 deletions src/messageschannel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d7c141d

Please sign in to comment.