Don't send avatars over dbus by default

This should reduce payload size a lot
This commit is contained in:
Nicolas Werner 2022-10-09 00:56:02 +02:00
parent 7e0ea12ebb
commit 30fde1f8ac
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
4 changed files with 61 additions and 65 deletions

View file

@ -35,14 +35,14 @@ apiVersionIsCompatible(const QVersionNumber &clientAppVersion)
RoomInfoItem::RoomInfoItem(const QString &roomId, RoomInfoItem::RoomInfoItem(const QString &roomId,
const QString &alias, const QString &alias,
const QString &title, const QString &title,
const QImage &image, const QString &avatarUrl,
const int unreadNotifications, const int unreadNotifications,
QObject *parent) QObject *parent)
: QObject{parent} : QObject{parent}
, roomId_{roomId} , roomId_{roomId}
, alias_{alias} , alias_{alias}
, roomName_{title} , roomName_{title}
, image_{image} , avatarUrl_{avatarUrl}
, unreadNotifications_{unreadNotifications} , unreadNotifications_{unreadNotifications}
{ {
} }
@ -52,7 +52,7 @@ RoomInfoItem::RoomInfoItem(const RoomInfoItem &other)
, roomId_{other.roomId_} , roomId_{other.roomId_}
, alias_{other.alias_} , alias_{other.alias_}
, roomName_{other.roomName_} , roomName_{other.roomName_}
, image_{other.image_} , avatarUrl_{other.avatarUrl_}
, unreadNotifications_{other.unreadNotifications_} , unreadNotifications_{other.unreadNotifications_}
{ {
} }
@ -63,7 +63,7 @@ RoomInfoItem::operator=(const RoomInfoItem &other)
roomId_ = other.roomId_; roomId_ = other.roomId_;
alias_ = other.alias_; alias_ = other.alias_;
roomName_ = other.roomName_; roomName_ = other.roomName_;
image_ = other.image_; avatarUrl_ = other.avatarUrl_;
unreadNotifications_ = other.unreadNotifications_; unreadNotifications_ = other.unreadNotifications_;
return *this; return *this;
} }
@ -72,7 +72,7 @@ QDBusArgument &
operator<<(QDBusArgument &arg, const RoomInfoItem &item) operator<<(QDBusArgument &arg, const RoomInfoItem &item)
{ {
arg.beginStructure(); arg.beginStructure();
arg << item.roomId_ << item.alias_ << item.roomName_ << item.image_ arg << item.roomId_ << item.alias_ << item.roomName_ << item.avatarUrl_
<< item.unreadNotifications_; << item.unreadNotifications_;
arg.endStructure(); arg.endStructure();
return arg; return arg;
@ -82,10 +82,8 @@ const QDBusArgument &
operator>>(const QDBusArgument &arg, RoomInfoItem &item) operator>>(const QDBusArgument &arg, RoomInfoItem &item)
{ {
arg.beginStructure(); arg.beginStructure();
arg >> item.roomId_ >> item.alias_ >> item.roomName_ >> item.image_ >> arg >> item.roomId_ >> item.alias_ >> item.roomName_ >> item.avatarUrl_ >>
item.unreadNotifications_; item.unreadNotifications_;
if (item.image_.isNull())
item.image_ = QImage{QStringLiteral(":/icons/ui/speech-bubbles.svg")};
arg.endStructure(); arg.endStructure();
return arg; return arg;
@ -121,6 +119,16 @@ rooms()
return {}; return {};
} }
QImage
image(const QString &mxcuri)
{
if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")};
interface.isValid())
return QDBusReply<QImage>{interface.call(QStringLiteral("image"), mxcuri)}.value();
else
return {};
}
void void
activateRoom(const QString &alias) activateRoom(const QString &alias)
{ {

View file

@ -18,7 +18,7 @@ init();
//! The nheko D-Bus API version provided by this file. The API version number follows semantic //! The nheko D-Bus API version provided by this file. The API version number follows semantic
//! versioning as defined by https://semver.org. //! versioning as defined by https://semver.org.
const QVersionNumber dbusApiVersion{0, 0, 1}; const QVersionNumber dbusApiVersion{1, 0, 0};
//! Compare the installed Nheko API to the version that your client app targets to see if they //! Compare the installed Nheko API to the version that your client app targets to see if they
//! are compatible. //! are compatible.
@ -33,7 +33,7 @@ public:
RoomInfoItem(const QString &roomId = QString{}, RoomInfoItem(const QString &roomId = QString{},
const QString &alias = QString{}, const QString &alias = QString{},
const QString &title = QString{}, const QString &title = QString{},
const QImage &image = QImage{}, const QString &avatarUrl = QString{},
const int unreadNotifications = 0, const int unreadNotifications = 0,
QObject *parent = nullptr); QObject *parent = nullptr);
@ -42,7 +42,7 @@ public:
const QString &roomId() const { return roomId_; } const QString &roomId() const { return roomId_; }
const QString &alias() const { return alias_; } const QString &alias() const { return alias_; }
const QString &roomName() const { return roomName_; } const QString &roomName() const { return roomName_; }
const QImage &image() const { return image_; } const QString &avatarUrl() const { return avatarUrl_; }
int unreadNotifications() const { return unreadNotifications_; } int unreadNotifications() const { return unreadNotifications_; }
RoomInfoItem &operator=(const RoomInfoItem &other); RoomInfoItem &operator=(const RoomInfoItem &other);
@ -54,7 +54,7 @@ private:
QString roomId_; QString roomId_;
QString alias_; QString alias_;
QString roomName_; QString roomName_;
QImage image_; QString avatarUrl_;
int unreadNotifications_; int unreadNotifications_;
}; };
@ -67,6 +67,9 @@ nhekoVersion();
//! Call this function to get a list of all joined rooms. //! Call this function to get a list of all joined rooms.
QVector<RoomInfoItem> QVector<RoomInfoItem>
rooms(); rooms();
//! Fetch an image using a matrix URI
QImage
image(const QString &uri);
//! Activates a currently joined room. //! Activates a currently joined room.
void void
activateRoom(const QString &alias); activateRoom(const QString &alias);

View file

@ -32,25 +32,14 @@ struct RoomReplyState
} }
QVector<nheko::dbus::RoomInfoItem> QVector<nheko::dbus::RoomInfoItem>
NhekoDBusBackend::rooms(const QDBusMessage &message) NhekoDBusBackend::rooms() const
{ {
message.setDelayedReply(true);
nhlog::ui()->debug("Rooms requested over D-Bus."); nhlog::ui()->debug("Rooms requested over D-Bus.");
const auto roomListModel = m_parent->models; const auto roomListModel = m_parent->models;
QVector<nheko::dbus::RoomInfoItem> model;
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);
for (const auto &room : roomListModel) { for (const auto &room : roomListModel) {
auto addRoom = [room, roomListModelSize = roomListModel.size(), message, state](
const QImage &image) {
const auto aliases = cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>( const auto aliases = cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(
room->roomId().toStdString()); room->roomId().toStdString());
QString alias; QString alias;
@ -62,37 +51,31 @@ NhekoDBusBackend::rooms(const QDBusMessage &message)
alias = QString::fromStdString(val.alt_aliases.front()); alias = QString::fromStdString(val.alt_aliases.front());
} }
std::lock_guard<std::mutex> childLock(state->m); model.push_back(nheko::dbus::RoomInfoItem{room->roomId(),
state->model.push_back(nheko::dbus::RoomInfoItem{
room->roomId(),
alias, alias,
QString::fromStdString(state->roominfos[room->roomId()].name), room->plainRoomName(),
image, room->roomAvatarUrl(),
room->notificationCount()}); room->notificationCount()});
if (state->model.size() == roomListModelSize) {
nhlog::ui()->debug("Sending {} rooms over D-Bus...", state->model.size());
auto reply = message.createReply();
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);
} }
};
if (state->roominfos[room->roomId()].avatar_url.empty()) nhlog::ui()->debug("Sending {} rooms over D-Bus...", model.size());
addRoom(QImage()); return model;
else }
QImage
NhekoDBusBackend::image(const QString &uri, const QDBusMessage &message) const
{
message.setDelayedReply(true);
nhlog::ui()->debug("Rooms requested over D-Bus.");
MainWindow::instance()->imageProvider()->download( MainWindow::instance()->imageProvider()->download(
QString::fromStdString(state->roominfos[room->roomId()].avatar_url).remove("mxc://"), QString(uri).remove("mxc://"),
{96, 96}, {96, 96},
[addRoom](const QString &, const QSize &, const QImage &image, const QString &) { [message](const QString &, const QSize &, const QImage &image, const QString &) {
addRoom(image); auto reply = message.createReply();
reply << QVariant::fromValue(image);
QDBusConnection::sessionBus().send(reply);
}, },
true); true);
}
return {}; return {};
} }

View file

@ -27,7 +27,9 @@ public slots:
//! Get the nheko version. //! Get the nheko version.
Q_SCRIPTABLE QString nhekoVersion() const { return nheko::version; } Q_SCRIPTABLE QString nhekoVersion() const { return nheko::version; }
//! Call this function to get a list of all joined rooms. //! Call this function to get a list of all joined rooms.
Q_SCRIPTABLE QVector<nheko::dbus::RoomInfoItem> rooms(const QDBusMessage &message); Q_SCRIPTABLE QVector<nheko::dbus::RoomInfoItem> rooms() const;
//! Call this function to convert a URI into an image
Q_SCRIPTABLE QImage image(const QString &uri, const QDBusMessage &message) const;
//! Activates a currently joined room. //! Activates a currently joined room.
Q_SCRIPTABLE void activateRoom(const QString &alias) const; Q_SCRIPTABLE void activateRoom(const QString &alias) const;
//! Joins a room. It is your responsibility to ask for confirmation (if desired). //! Joins a room. It is your responsibility to ask for confirmation (if desired).