summary refs log tree commit diff
path: root/src/dbus/NhekoDBusBackend.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2022-07-11 15:14:30 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2022-07-11 15:14:30 +0200
commiteeaf16e442bef8a80ed1e6a94d8c2d8e433f8deb (patch)
tree07146f990882a24a619eca130244fc2200904ed2 /src/dbus/NhekoDBusBackend.cpp
parentFix aliases being duplicated in the aliases list in some cases (diff)
downloadnheko-eeaf16e442bef8a80ed1e6a94d8c2d8e433f8deb.tar.xz
Fix race condition in dbus API
Diffstat (limited to '')
-rw-r--r--src/dbus/NhekoDBusBackend.cpp49
1 files changed, 38 insertions, 11 deletions
diff --git a/src/dbus/NhekoDBusBackend.cpp b/src/dbus/NhekoDBusBackend.cpp
index d49ff2a5..ea7a8940 100644
--- a/src/dbus/NhekoDBusBackend.cpp
+++ b/src/dbus/NhekoDBusBackend.cpp
@@ -4,6 +4,9 @@
 
 #include "NhekoDBusBackend.h"
 
+#include <mutex>
+
+#include "Cache.h"
 #include "Cache_p.h"
 #include "ChatPage.h"
 #include "Logging.h"
@@ -18,17 +21,35 @@ NhekoDBusBackend::NhekoDBusBackend(RoomlistModel *parent)
   , m_parent{parent}
 {}
 
+namespace {
+struct RoomReplyState
+{
+    QVector<nheko::dbus::RoomInfoItem> model;
+    std::map<QString, RoomInfo> roominfos;
+    std::recursive_mutex m;
+};
+}
+
 QVector<nheko::dbus::RoomInfoItem>
 NhekoDBusBackend::rooms(const QDBusMessage &message)
 {
     message.setDelayedReply(true);
+    nhlog::ui()->debug("Rooms requested over D-Bus.");
 
     const auto roomListModel = m_parent->models;
-    QSharedPointer<QVector<nheko::dbus::RoomInfoItem>> model{
-      new QVector<nheko::dbus::RoomInfoItem>};
 
+    auto state = QSharedPointer<RoomReplyState>::create();
+
+    std::vector<std::string> roomids;
+    roomids.reserve(roomids.size());
+    for (const auto &room : roomListModel) {
+        roomids.push_back(room->roomId().toStdString());
+    }
+    state->roominfos = cache::getRoomInfo(roomids);
+
+    std::lock_guard<std::recursive_mutex> parentLock(state->m);
     for (const auto &room : roomListModel) {
-        auto addRoom = [room, roomListModelSize = roomListModel.size(), message, model](
+        auto addRoom = [room, roomListModelSize = roomListModel.size(), message, state](
                          const QImage &image) {
             const auto aliases = cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(
               room->roomId().toStdString());
@@ -41,24 +62,30 @@ NhekoDBusBackend::rooms(const QDBusMessage &message)
                     alias = QString::fromStdString(val.alt_aliases.front());
             }
 
-            model->push_back(nheko::dbus::RoomInfoItem{
-              room->roomId(), alias, room->roomName(), image, room->notificationCount()});
+            state->model.push_back(nheko::dbus::RoomInfoItem{
+              room->roomId(),
+              alias,
+              QString::fromStdString(state->roominfos[room->roomId()].name),
+              image,
+              room->notificationCount()});
 
-            if (model->length() == roomListModelSize) {
-                nhlog::ui()->debug("Sending {} rooms over D-Bus...", model->size());
+            std::lock_guard<std::recursive_mutex> childLock(state->m);
+            if (state->model.size() == roomListModelSize) {
+                nhlog::ui()->debug("Sending {} rooms over D-Bus...", state->model.size());
                 auto reply = message.createReply();
-                reply << QVariant::fromValue(*model);
+                reply << QVariant::fromValue(state->model);
                 QDBusConnection::sessionBus().send(reply);
                 nhlog::ui()->debug("Rooms successfully sent to D-Bus.");
+            } else {
+                // nhlog::ui()->debug("DBUS: {}/{}", state->model.size(), roomListModelSize);
             }
         };
 
-        auto avatarUrl = room->roomAvatarUrl();
-        if (avatarUrl.isEmpty())
+        if (state->roominfos[room->roomId()].avatar_url.empty())
             addRoom(QImage());
         else
             MainWindow::instance()->imageProvider()->download(
-              avatarUrl.remove("mxc://"),
+              QString::fromStdString(state->roominfos[room->roomId()].avatar_url).remove("mxc://"),
               {96, 96},
               [addRoom](const QString &, const QSize &, const QImage &image, const QString &) {
                   addRoom(image);