Add GUI to change hidden events per room

This adds a dialog to the room settings in which the user can choose
which of these three event types they want to hide (additionally to the
default):

  - m.room.member
  - m.room.power_levels
  - m.sticker

The current state is read when room settings are opened and saved when
new settings are accepted.
This commit is contained in:
tastytea 2022-01-11 07:38:27 +01:00 committed by Nicolas Werner
parent dfb8f9a160
commit 5cd3e61cb0
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
5 changed files with 200 additions and 1 deletions

View file

@ -12,14 +12,19 @@
#include <QMimeDatabase>
#include <QStandardPaths>
#include <QVBoxLayout>
#include <algorithm>
#include <mtx/responses/common.hpp>
#include <mtx/responses/media.hpp>
#include "Cache.h"
#include "Cache_p.h"
#include "Config.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"
#include "mtx/events/event_type.hpp"
#include "mtx/events/nheko_extensions/hidden_events.hpp"
#include "mtxclient/http/client.hpp"
#include "ui/TextField.h"
using namespace mtx::events;
@ -224,6 +229,25 @@ RoomSettings::RoomSettings(QString roomid, QObject *parent)
accessRules_ = 4;
}
emit accessJoinRulesChanged();
// Get room's hidden events and store it in member variable.
using mtx::events::EventType;
if (auto hiddenEvents =
cache::client()->getAccountData(EventType::NhekoHiddenEvents, roomid_.toStdString())) {
if (auto tmp = std::get_if<mtx::events::EphemeralEvent<
mtx::events::account_data::nheko_extensions::HiddenEvents>>(&*hiddenEvents)) {
const auto &types = tmp->content.hidden_event_types;
auto is_hidden{[&types](EventType searchFor) {
return std::find_if(types.begin(), types.end(), [&searchFor](const auto curType) {
return curType == searchFor;
}) != types.end();
}};
hiddenEvents_ = {is_hidden(EventType::RoomMember),
is_hidden(EventType::RoomPowerLevels),
is_hidden(EventType::Sticker)};
}
}
}
QString
@ -294,6 +318,20 @@ RoomSettings::accessJoinRules()
return accessRules_;
}
bool
RoomSettings::eventHidden(int index)
{
try {
// Is empty if there are no preferences stored for this room.
if (!hiddenEvents_.empty()) {
return hiddenEvents_.at(index);
}
} catch (...) {
nhlog::db()->warn("Failed to retrieve hidden event setting at {}", index);
}
return false;
}
void
RoomSettings::enableEncryption()
{
@ -404,6 +442,38 @@ RoomSettings::openEditModal()
});
}
void
RoomSettings::saveHiddenEventsSettings(const bool toggleRoomMember,
const bool toggleRoomPowerLevels,
const bool toggleSticker)
{
const auto roomid = roomid_.toStdString();
nhlog::ui()->debug("Setting events to hidden in room {}: m.room.member={}, "
"m.room.power_levels={}, m.sticker={}",
roomid,
toggleRoomMember,
toggleRoomPowerLevels,
toggleSticker);
mtx::events::account_data::nheko_extensions::HiddenEvents hiddenEvents;
hiddenEvents.hidden_event_types = {
EventType::Reaction, EventType::CallCandidates, EventType::Unsupported};
if (toggleRoomMember) {
hiddenEvents.hidden_event_types.emplace_back(mtx::events::EventType::RoomMember);
}
if (toggleRoomPowerLevels) {
hiddenEvents.hidden_event_types.emplace_back(mtx::events::EventType::RoomPowerLevels);
}
if (toggleSticker) {
hiddenEvents.hidden_event_types.emplace_back(mtx::events::EventType::Sticker);
}
http::client()->put_room_account_data(roomid, hiddenEvents, [&roomid](mtx::http::RequestErr e) {
if (e) {
nhlog::net()->error("Failed to update room account data in {}: {}", roomid, *e);
}
});
}
void
RoomSettings::changeNotifications(int currentIndex)
{

View file

@ -11,6 +11,7 @@
#include <QString>
#include <mtx/events/guest_access.hpp>
#include <vector>
#include "CacheStructs.h"
@ -107,8 +108,11 @@ public:
Q_INVOKABLE void enableEncryption();
Q_INVOKABLE void updateAvatar();
Q_INVOKABLE void openEditModal();
Q_INVOKABLE void
saveHiddenEventsSettings(bool toggleRoomMember, bool toggleRoomPowerLevels, bool toggleSticker);
Q_INVOKABLE void changeAccessRules(int index);
Q_INVOKABLE void changeNotifications(int currentIndex);
Q_INVOKABLE bool eventHidden(int index);
signals:
void loadingChanged();
@ -137,4 +141,5 @@ private:
RoomInfo info_;
int notifications_ = 0;
int accessRules_ = 0;
std::vector<bool> hiddenEvents_;
};