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

Signed-off-by: Alexey Andreyev <[email protected]>
  • Loading branch information
a-andreyev committed Apr 22, 2020
1 parent 8e60a8d commit cd3c1f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/messageschannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -179,6 +197,13 @@ void MatrixMessagesChannel::processMessageEvent(const Quotient::RoomMessageEvent
if (event->isRedacted())
header[QStringLiteral("delivery-status")] = QDBusVariant(Tp::DeliveryStatusDeleted);

/* Read markers */
QList<Quotient::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 @@ -238,5 +263,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, 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);
Expand Down

0 comments on commit cd3c1f2

Please sign in to comment.