Implement server-side notification count

This commit is contained in:
Konstantinos Sideris 2018-02-15 21:58:57 +02:00
parent 6d08e67abd
commit 86280098b4
11 changed files with 22 additions and 82 deletions

View File

@ -128,6 +128,8 @@ private:
void updateUserMetadata(const std::vector<Collection> &collection);
void retryInitialSync();
//! Update the room with the new notification count.
void updateRoomNotificationCount(const QString &room_id, uint16_t notification_count);
QHBoxLayout *topLayout_;
Splitter *splitter;

View File

@ -68,7 +68,7 @@ public:
RoomInfoListItem(QString room_id, mtx::responses::InvitedRoom room, QWidget *parent = 0);
void updateUnreadMessageCount(int count);
void clearUnreadMessageCount();
void clearUnreadMessageCount() { updateUnreadMessageCount(0); };
void setState(QSharedPointer<RoomState> state)
{
state_ = state;

View File

@ -76,7 +76,6 @@ public slots:
void updateUnreadMessageCount(const QString &roomid, int count);
void updateRoomDescription(const QString &roomid, const DescInfo &info);
void closeJoinRoomDialog(bool isJoining, QString roomAlias);
void clearRoomMessageCount(const QString &room_id);
protected:
void paintEvent(QPaintEvent *event) override;

View File

@ -83,7 +83,7 @@ public:
QWidget *parent = 0);
// Add new events at the end of the timeline.
int addEvents(const mtx::responses::Timeline &timeline);
void addEvents(const mtx::responses::Timeline &timeline);
void addUserMessage(mtx::events::MessageType ty, const QString &msg);
template<class Widget, mtx::events::MessageType MsgType>
@ -113,7 +113,6 @@ private slots:
signals:
void updateLastTimelineMessage(const QString &user, const DescInfo &info);
void clearUnreadMessageCount(const QString &room_id);
protected:
void paintEvent(QPaintEvent *event) override;

View File

@ -57,7 +57,6 @@ public:
signals:
void clearRoomMessageCount(QString roomid);
void unreadMessages(QString roomid, int count);
void updateRoomsLastMessage(const QString &user, const DescInfo &info);
public slots:

View File

@ -175,25 +175,6 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
connect(room_list_, &RoomList::acceptInvite, client_.data(), &MatrixClient::joinRoom);
connect(room_list_, &RoomList::declineInvite, client_.data(), &MatrixClient::leaveRoom);
connect(view_manager_,
&TimelineViewManager::clearRoomMessageCount,
room_list_,
&RoomList::clearRoomMessageCount);
connect(view_manager_,
&TimelineViewManager::unreadMessages,
this,
[=](const QString &roomid, int count) {
if (roomSettings_.find(roomid) == roomSettings_.end()) {
qWarning() << "RoomId does not have settings" << roomid;
room_list_->updateUnreadMessageCount(roomid, count);
return;
}
if (roomSettings_[roomid]->isNotificationsEnabled())
room_list_->updateUnreadMessageCount(roomid, count);
});
connect(text_input_, &TextInputWidget::startedTyping, this, [=]() {
if (!userSettings_->isTypingNotificationsEnabled())
return;
@ -844,6 +825,8 @@ ChatPage::updateJoinedRooms(const std::map<std::string, mtx::responses::JoinedRo
const auto roomid = QString::fromStdString(it->first);
updateTypingUsers(roomid, it->second.ephemeral.typing);
updateRoomNotificationCount(roomid,
it->second.unread_notifications.notification_count);
if (it->second.ephemeral.receipts.size() > 0)
QtConcurrent::run(cache_.data(),
@ -988,3 +971,9 @@ ChatPage::retryInitialSync()
client_->initialSync();
initialSyncTimer_->start(INITIAL_SYNC_RETRY_TIMEOUT);
}
void
ChatPage::updateRoomNotificationCount(const QString &room_id, uint16_t notification_count)
{
room_list_->updateUnreadMessageCount(room_id, notification_count);
}

View File

@ -1181,14 +1181,14 @@ MatrixClient::readEvent(const QString &room_id, const QString &event_id)
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.setPath(clientApiUrl_ + QString("/rooms/%1/read_markers").arg(room_id));
endpoint.setQuery(query);
QNetworkRequest request(QString(endpoint.toEncoded()));
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
auto reply = post(request, "{}");
QJsonObject body({{"m.fully_read", event_id}, {"m.read", event_id}});
auto reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
connect(reply, &QNetworkReply::finished, this, [reply]() {
reply->deleteLater();

View File

@ -303,14 +303,7 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
void
RoomInfoListItem::updateUnreadMessageCount(int count)
{
unreadMsgCount_ += count;
update();
}
void
RoomInfoListItem::clearUnreadMessageCount()
{
unreadMsgCount_ = 0;
unreadMsgCount_ = count;
update();
}

View File

@ -213,18 +213,6 @@ RoomList::sync(const std::map<QString, QSharedPointer<RoomState>> &states,
}
}
void
RoomList::clearRoomMessageCount(const QString &room_id)
{
if (!roomExists(room_id))
return;
auto room = rooms_[room_id];
room->clearUnreadMessageCount();
calculateUnreadMessageCount();
}
void
RoomList::highlightSelectedRoom(const QString &room_id)
{
@ -235,10 +223,6 @@ RoomList::highlightSelectedRoom(const QString &room_id)
return;
}
clearRoomMessageCount(room_id);
calculateUnreadMessageCount();
for (auto const &room : rooms_) {
if (room.second.isNull())
continue;

View File

@ -290,11 +290,9 @@ TimelineView::renderTopEvents(const std::vector<TimelineEvent> &events)
lastSender_ = items.at(0)->descriptionMessage().userid;
}
int
void
TimelineView::addEvents(const mtx::responses::Timeline &timeline)
{
int message_count = 0;
if (isInitialSync) {
prev_batch_token_ = QString::fromStdString(timeline.prev_batch);
isInitialSync = false;
@ -306,8 +304,8 @@ TimelineView::addEvents(const mtx::responses::Timeline &timeline)
bottomMessages_.push_back(e);
// Calculate notifications.
if (isNotifiable(e))
message_count += 1;
/* if (isNotifiable(e)) */
/* sendNotification() */
}
if (!bottomMessages_.empty())
@ -324,8 +322,6 @@ TimelineView::addEvents(const mtx::responses::Timeline &timeline)
if (isActiveWindow())
readLastEvent();
}
return message_count;
}
inline bool
@ -685,12 +681,8 @@ TimelineView::showEvent(QShowEvent *event)
bool
TimelineView::event(QEvent *event)
{
if (event->type() == QEvent::WindowActivate) {
QTimer::singleShot(1000, this, [=]() {
emit clearUnreadMessageCount(room_id_);
readLastEvent();
});
}
if (event->type() == QEvent::WindowActivate)
readLastEvent();
return QWidget::event(event);
}

View File

@ -159,10 +159,6 @@ TimelineViewManager::addRoom(const mtx::responses::JoinedRoom &room, const QStri
&TimelineView::updateLastTimelineMessage,
this,
&TimelineViewManager::updateRoomsLastMessage);
connect(view,
&TimelineView::clearUnreadMessageCount,
this,
&TimelineViewManager::clearRoomMessageCount);
// Add the view in the widget stack.
addWidget(view);
@ -179,10 +175,6 @@ 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);
@ -201,16 +193,7 @@ TimelineViewManager::sync(const mtx::responses::Rooms &rooms)
auto view = views_.at(roomid);
int msgs_added = view->addEvents(it->second.timeline);
if (msgs_added > 0) {
// TODO: When the app window gets active the current
// unread count (if any) should be cleared.
auto isAppActive = QApplication::activeWindow() != nullptr;
if (roomid != active_room_ || !isAppActive)
emit unreadMessages(roomid, msgs_added);
}
view->addEvents(it->second.timeline);
}
}