Send read receipts

Automatically dismiss unread notifications when the window regains
focus.

fixes #111
fixes #68
This commit is contained in:
Konstantinos Sideris 2017-11-24 00:10:58 +02:00
parent c6cf6c2b41
commit 0f363b5f44
11 changed files with 126 additions and 4 deletions

View file

@ -125,6 +125,11 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
connect(
room_list_, &RoomList::roomChanged, view_manager_, &TimelineViewManager::setHistoryView);
connect(view_manager_,
&TimelineViewManager::clearRoomMessageCount,
room_list_,
&RoomList::clearRoomMessageCount);
connect(view_manager_,
&TimelineViewManager::unreadMessages,
this,

View file

@ -847,3 +847,31 @@ MatrixClient::removeTypingNotification(const QString &roomid)
put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
}
void
MatrixClient::readEvent(const QString &room_id, const QString &event_id)
{
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ +
QString("/rooms/%1/receipt/m.read/%2").arg(room_id).arg(event_id));
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
auto reply = post(request, "{}");
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status == 0 || status >= 400) {
qWarning() << reply->errorString();
return;
}
});
}

View file

@ -203,6 +203,18 @@ RoomList::sync(const QMap<QString, RoomState> &states,
}
}
void
RoomList::clearRoomMessageCount(const QString &room_id)
{
if (!rooms_.contains(room_id))
return;
auto room = rooms_[room_id];
room->clearUnreadMessageCount();
calculateUnreadMessageCount();
}
void
RoomList::highlightSelectedRoom(const QString &room_id)
{
@ -213,9 +225,7 @@ RoomList::highlightSelectedRoom(const QString &room_id)
return;
}
// TODO: Send a read receipt for the last event.
auto room = rooms_[room_id];
room->clearUnreadMessageCount();
clearRoomMessageCount(room_id);
calculateUnreadMessageCount();

View file

@ -154,6 +154,8 @@ TimelineItem::TimelineItem(ImageItem *image,
{
init();
event_id_ = event.eventId();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
auto displayName = TimelineViewManager::displayName(event.sender());
@ -193,6 +195,9 @@ TimelineItem::TimelineItem(const events::MessageEvent<msgs::Notice> &event,
: QWidget(parent)
{
init();
event_id_ = event.eventId();
descriptionMsg_ = {TimelineViewManager::displayName(event.sender()),
event.sender(),
" sent a notification",
@ -234,6 +239,8 @@ TimelineItem::TimelineItem(const events::MessageEvent<msgs::Emote> &event,
{
init();
event_id_ = event.eventId();
auto body = event.content().body().trimmed();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
auto displayName = TimelineViewManager::displayName(event.sender());
@ -273,6 +280,8 @@ TimelineItem::TimelineItem(const events::MessageEvent<msgs::Text> &event,
{
init();
event_id_ = event.eventId();
auto body = event.content().body().trimmed();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
auto displayName = TimelineViewManager::displayName(event.sender());

View file

@ -379,6 +379,9 @@ TimelineView::addEvents(const Timeline &timeline)
if (!timeline.events().isEmpty() && scroll_layout_->count() > 1)
notifyForLastEvent();
if (isActiveWindow() && isVisible() && timeline.events().size() > 0)
readLastEvent();
return message_count;
}
@ -648,3 +651,52 @@ TimelineView::paintEvent(QPaintEvent *)
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void
TimelineView::readLastEvent() const
{
const auto eventId = getLastEventId();
if (!eventId.isEmpty())
client_->readEvent(room_id_, eventId);
}
QString
TimelineView::getLastEventId() const
{
auto index = scroll_layout_->count();
// Search backwards for the first event that has a valid event id.
while (index > 0) {
--index;
auto lastItem = scroll_layout_->itemAt(index);
auto *lastTimelineItem = qobject_cast<TimelineItem *>(lastItem->widget());
if (lastTimelineItem && !lastTimelineItem->eventId().isEmpty())
return lastTimelineItem->eventId();
}
return QString("");
}
void
TimelineView::showEvent(QShowEvent *event)
{
readLastEvent();
QWidget::showEvent(event);
}
bool
TimelineView::event(QEvent *event)
{
if (event->type() == QEvent::WindowActivate) {
QTimer::singleShot(1000, this, [=]() {
emit clearUnreadMessageCount(room_id_);
readLastEvent();
});
}
return QWidget::event(event);
}

View file

@ -131,6 +131,10 @@ TimelineViewManager::addRoom(const JoinedRoom &room, const QString &room_id)
&TimelineView::updateLastTimelineMessage,
this,
&TimelineViewManager::updateRoomsLastMessage);
connect(view,
&TimelineView::clearUnreadMessageCount,
this,
&TimelineViewManager::clearRoomMessageCount);
// Add the view in the widget stack.
addWidget(view);
@ -147,6 +151,10 @@ TimelineViewManager::addRoom(const QString &room_id)
&TimelineView::updateLastTimelineMessage,
this,
&TimelineViewManager::updateRoomsLastMessage);
connect(view,
&TimelineView::clearUnreadMessageCount,
this,
&TimelineViewManager::clearRoomMessageCount);
// Add the view in the widget stack.
addWidget(view);