mirror of https://github.com/Nheko-Reborn/nheko
parent
dcb6c00708
commit
ad6e4fef64
@ -0,0 +1,167 @@ |
||||
// SPDX-FileCopyrightText: Nheko Contributors |
||||
// |
||||
// SPDX-License-Identifier: GPL-3.0-or-later |
||||
|
||||
import ".." |
||||
import QtQuick |
||||
import QtQuick.Controls |
||||
import QtQuick.Layouts |
||||
import im.nheko |
||||
|
||||
ApplicationWindow { |
||||
id: dialog |
||||
|
||||
property string roomid: "" |
||||
property string roomName: "" |
||||
property var onAccepted: undefined |
||||
|
||||
modality: Qt.NonModal |
||||
flags: Qt.Dialog | Qt.WindowTitleHint |
||||
width: 275 |
||||
height: 330 |
||||
minimumWidth: 250 |
||||
minimumHeight: 220 |
||||
|
||||
EventExpiry { |
||||
id: eventExpiry |
||||
|
||||
roomid: dialog.roomid |
||||
} |
||||
|
||||
title: { |
||||
if (roomid) { |
||||
return qsTr("Event expiration for %1").arg(roomName); |
||||
} |
||||
else { |
||||
return qsTr("Event expiration"); |
||||
} |
||||
} |
||||
|
||||
Shortcut { |
||||
sequence: StandardKey.Cancel |
||||
onActivated: dbb.rejected() |
||||
} |
||||
|
||||
ColumnLayout { |
||||
spacing: Nheko.paddingMedium |
||||
anchors.margins: Nheko.paddingMedium |
||||
anchors.fill: parent |
||||
|
||||
MatrixText { |
||||
id: promptLabel |
||||
text: { |
||||
if (roomid) { |
||||
return qsTr("You can configure when your messages will be deleted in %1. This only happens when Nheko is open and has permissions to delete messages until Matrix servers support this feature natively. In general 0 means disable.").arg(roomName); |
||||
} |
||||
else { |
||||
return qsTr("You can configure when your messages will be deleted in all rooms unless configured otherwise. This only happens when Nheko is open and has permissions to delete messages until Matrix servers support this feature natively. In general 0 means disable."); |
||||
} |
||||
} |
||||
font.pixelSize: Math.floor(fontMetrics.font.pixelSize * 1.2) |
||||
Layout.fillWidth: true |
||||
Layout.fillHeight: false |
||||
} |
||||
|
||||
GridLayout { |
||||
columns: 2 |
||||
rowSpacing: Nheko.paddingMedium |
||||
Layout.fillWidth: true |
||||
Layout.fillHeight: true |
||||
|
||||
MatrixText { |
||||
text: qsTr("Expire events after X days") |
||||
ToolTip.text: qsTr("Automatically redacts messages after X days, unless otherwise protected. Set to 0 to disable.") |
||||
ToolTip.visible: hh1.hovered |
||||
Layout.fillWidth: true |
||||
|
||||
HoverHandler { |
||||
id: hh1 |
||||
} |
||||
} |
||||
|
||||
SpinBox { |
||||
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter |
||||
from: 0 |
||||
to: 1000 |
||||
stepSize: 1 |
||||
value: eventExpiry.expireEventsAfterDays |
||||
onValueChanged: eventExpiry.expireEventsAfterDays = value |
||||
editable: true |
||||
} |
||||
|
||||
MatrixText { |
||||
text: qsTr("Only keep latest X events") |
||||
ToolTip.text: qsTr("Deletes your events in this room if there are more than X newer messages unless otherwise protected. Set to 0 to disable.") |
||||
ToolTip.visible: hh2.hovered |
||||
Layout.fillWidth: true |
||||
|
||||
HoverHandler { |
||||
id: hh2 |
||||
} |
||||
} |
||||
|
||||
|
||||
SpinBox { |
||||
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter |
||||
from: 0 |
||||
to: 1000 |
||||
stepSize: 1 |
||||
value: eventExpiry.expireEventsAfterCount |
||||
onValueChanged: eventExpiry.expireEventsAfterCount = value |
||||
editable: true |
||||
} |
||||
|
||||
MatrixText { |
||||
text: qsTr("Always keep latest X events") |
||||
ToolTip.text: qsTr("This prevents events to be deleted by the above 2 settings if they are the latest X messages from you in the room.") |
||||
ToolTip.visible: hh3.hovered |
||||
Layout.fillWidth: true |
||||
|
||||
HoverHandler { |
||||
id: hh3 |
||||
} |
||||
} |
||||
|
||||
|
||||
SpinBox { |
||||
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter |
||||
from: 0 |
||||
to: 1000 |
||||
stepSize: 1 |
||||
value: eventExpiry.protectLatestEvents |
||||
onValueChanged: eventExpiry.protectLatestEvents = value |
||||
editable: true |
||||
} |
||||
|
||||
MatrixText { |
||||
text: qsTr("Include state events") |
||||
ToolTip.text: qsTr("If this is turned on, old state events also get redacted. The latest state event of any type+key combination is excluded from redaction to not remove the room name and similar state by accident.") |
||||
ToolTip.visible: hh4.hovered |
||||
Layout.fillWidth: true |
||||
|
||||
HoverHandler { |
||||
id: hh4 |
||||
} |
||||
} |
||||
|
||||
ToggleButton { |
||||
Layout.alignment: Qt.AlignRight |
||||
checked: eventExpiry.expireStateEvents |
||||
onToggled: eventExpiry.expireStateEvents = checked |
||||
} |
||||
} |
||||
} |
||||
|
||||
footer: DialogButtonBox { |
||||
id: dbb |
||||
|
||||
standardButtons: DialogButtonBox.Ok | DialogButtonBox.Cancel |
||||
onAccepted: { |
||||
eventExpiry.save(); |
||||
dialog.close(); |
||||
} |
||||
onRejected: dialog.close(); |
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,124 @@ |
||||
// SPDX-FileCopyrightText: Nheko Contributors
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "EventExpiry.h" |
||||
|
||||
#include "Cache_p.h" |
||||
#include "MainWindow.h" |
||||
#include "MatrixClient.h" |
||||
#include "timeline/TimelineModel.h" |
||||
|
||||
void |
||||
EventExpiry::load() |
||||
{ |
||||
using namespace mtx::events; |
||||
|
||||
this->event = {}; |
||||
|
||||
if (auto temp = cache::client()->getAccountData(mtx::events::EventType::NhekoEventExpiry, "")) { |
||||
auto h = std::get< |
||||
mtx::events::AccountDataEvent<mtx::events::account_data::nheko_extensions::EventExpiry>>( |
||||
*temp); |
||||
this->event = std::move(h.content); |
||||
} |
||||
|
||||
if (!roomid_.isEmpty()) { |
||||
if (auto temp = cache::client()->getAccountData(mtx::events::EventType::NhekoEventExpiry, |
||||
roomid_.toStdString())) { |
||||
auto h = std::get<mtx::events::AccountDataEvent< |
||||
mtx::events::account_data::nheko_extensions::EventExpiry>>(*temp); |
||||
this->event = std::move(h.content); |
||||
} |
||||
} |
||||
|
||||
emit expireEventsAfterDaysChanged(); |
||||
emit expireEventsAfterCountChanged(); |
||||
emit protectLatestEventsChanged(); |
||||
emit expireStateEventsChanged(); |
||||
} |
||||
|
||||
void |
||||
EventExpiry::save() |
||||
{ |
||||
if (roomid_.isEmpty()) |
||||
http::client()->put_account_data(event, [](mtx::http::RequestErr e) { |
||||
if (e) { |
||||
nhlog::net()->error("Failed to set hidden events: {}", *e); |
||||
MainWindow::instance()->showNotification( |
||||
tr("Failed to set hidden events: %1") |
||||
.arg(QString::fromStdString(e->matrix_error.error))); |
||||
} |
||||
}); |
||||
else |
||||
http::client()->put_room_account_data( |
||||
roomid_.toStdString(), event, [](mtx::http::RequestErr e) { |
||||
if (e) { |
||||
nhlog::net()->error("Failed to set hidden events: {}", *e); |
||||
MainWindow::instance()->showNotification( |
||||
tr("Failed to set hidden events: %1") |
||||
.arg(QString::fromStdString(e->matrix_error.error))); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
int |
||||
EventExpiry::expireEventsAfterDays() const |
||||
{ |
||||
return event.expire_after_ms / (1000 * 60 * 60 * 24); |
||||
} |
||||
|
||||
int |
||||
EventExpiry::expireEventsAfterCount() const |
||||
{ |
||||
return event.keep_only_latest; |
||||
} |
||||
|
||||
int |
||||
EventExpiry::protectLatestEvents() const |
||||
{ |
||||
return event.protect_latest; |
||||
} |
||||
|
||||
bool |
||||
EventExpiry::expireStateEvents() const |
||||
{ |
||||
return !event.exclude_state_events; |
||||
} |
||||
|
||||
void |
||||
EventExpiry::setExpireEventsAfterDays(int val) |
||||
{ |
||||
if (val > 0) |
||||
this->event.expire_after_ms = val * (1000 * 60 * 60 * 24); |
||||
else |
||||
this->event.expire_after_ms = 0; |
||||
emit expireEventsAfterDaysChanged(); |
||||
} |
||||
|
||||
void |
||||
EventExpiry::setProtectLatestEvents(int val) |
||||
{ |
||||
if (val > 0) |
||||
this->event.protect_latest = val; |
||||
else |
||||
this->event.expire_after_ms = 0; |
||||
emit protectLatestEventsChanged(); |
||||
} |
||||
|
||||
void |
||||
EventExpiry::setExpireEventsAfterCount(int val) |
||||
{ |
||||
if (val > 0) |
||||
this->event.keep_only_latest = val; |
||||
else |
||||
this->event.keep_only_latest = 0; |
||||
emit expireEventsAfterCountChanged(); |
||||
} |
||||
|
||||
void |
||||
EventExpiry::setExpireStateEvents(bool val) |
||||
{ |
||||
this->event.exclude_state_events = !val; |
||||
emit expireEventsAfterCountChanged(); |
||||
} |
@ -0,0 +1,67 @@ |
||||
// SPDX-FileCopyrightText: Nheko Contributors
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once |
||||
|
||||
#include <QObject> |
||||
#include <QQmlEngine> |
||||
#include <QString> |
||||
#include <QVariantList> |
||||
|
||||
#include <mtx/events/nheko_extensions/event_expiry.hpp> |
||||
|
||||
class EventExpiry : public QObject |
||||
{ |
||||
Q_OBJECT |
||||
QML_ELEMENT |
||||
Q_PROPERTY(QString roomid READ roomid WRITE setRoomid NOTIFY roomidChanged REQUIRED) |
||||
Q_PROPERTY(int expireEventsAfterDays READ expireEventsAfterDays WRITE setExpireEventsAfterDays |
||||
NOTIFY expireEventsAfterDaysChanged) |
||||
Q_PROPERTY(bool expireStateEvents READ expireStateEvents WRITE setExpireStateEvents NOTIFY |
||||
expireStateEventsChanged) |
||||
Q_PROPERTY(int expireEventsAfterCount READ expireEventsAfterCount WRITE |
||||
setExpireEventsAfterCount NOTIFY expireEventsAfterCountChanged) |
||||
Q_PROPERTY(int protectLatestEvents READ protectLatestEvents WRITE setProtectLatestEvents NOTIFY |
||||
protectLatestEventsChanged) |
||||
public: |
||||
explicit EventExpiry(QObject *p = nullptr) |
||||
: QObject(p) |
||||
{ |
||||
} |
||||
|
||||
Q_INVOKABLE void save(); |
||||
|
||||
[[nodiscard]] QString roomid() const { return roomid_; } |
||||
void setRoomid(const QString &r) |
||||
{ |
||||
roomid_ = r; |
||||
emit roomidChanged(); |
||||
|
||||
load(); |
||||
} |
||||
|
||||
[[nodiscard]] int expireEventsAfterDays() const; |
||||
[[nodiscard]] int expireEventsAfterCount() const; |
||||
[[nodiscard]] int protectLatestEvents() const; |
||||
[[nodiscard]] bool expireStateEvents() const; |
||||
void setExpireEventsAfterDays(int); |
||||
void setExpireEventsAfterCount(int); |
||||
void setProtectLatestEvents(int); |
||||
void setExpireStateEvents(bool); |
||||
|
||||
signals: |
||||
void roomidChanged(); |
||||
|
||||
void expireEventsAfterDaysChanged(); |
||||
void expireEventsAfterCountChanged(); |
||||
void protectLatestEventsChanged(); |
||||
void expireStateEventsChanged(); |
||||
|
||||
private: |
||||
QString roomid_; |
||||
mtx::events::account_data::nheko_extensions::EventExpiry event = {}; |
||||
|
||||
void load(); |
||||
}; |
||||
|
Loading…
Reference in new issue