| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "EventExpiry.h" |
| 6 | |
| 7 | #include "Cache_p.h" |
| 8 | #include "Logging.h" |
| 9 | #include "MainWindow.h" |
| 10 | #include "MatrixClient.h" |
| 11 | #include "timeline/TimelineModel.h" |
| 12 | |
| 13 | void |
| 14 | EventExpiry::load() |
| 15 | { |
| 16 | using namespace mtx::events; |
| 17 | |
| 18 | this->event = {}; |
| 19 | |
| 20 | if (auto temp = cache::client()->getAccountData(mtx::events::EventType::NhekoEventExpiry, "")) { |
| 21 | auto h = std::get< |
| 22 | mtx::events::AccountDataEvent<mtx::events::account_data::nheko_extensions::EventExpiry>>( |
| 23 | *temp); |
| 24 | this->event = std::move(h.content); |
| 25 | } |
| 26 | |
| 27 | if (!roomid_.isEmpty()) { |
| 28 | if (auto temp = cache::client()->getAccountData(mtx::events::EventType::NhekoEventExpiry, |
| 29 | roomid_.toStdString())) { |
| 30 | auto h = std::get<mtx::events::AccountDataEvent< |
| 31 | mtx::events::account_data::nheko_extensions::EventExpiry>>(*temp); |
| 32 | this->event = std::move(h.content); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | emit expireEventsAfterDaysChanged(); |
| 37 | emit expireEventsAfterCountChanged(); |
| 38 | emit protectLatestEventsChanged(); |
| 39 | emit expireStateEventsChanged(); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | EventExpiry::save() |
| 44 | { |
| 45 | if (roomid_.isEmpty()) |
| 46 | http::client()->put_account_data(event, [](mtx::http::RequestErr e) { |
| 47 | if (e) { |
| 48 | nhlog::net()->error("Failed to set hidden events: {}", *e); |
| 49 | MainWindow::instance()->showNotification( |
| 50 | tr("Failed to set hidden events: %1") |
| 51 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 52 | } |
| 53 | }); |
| 54 | else |
| 55 | http::client()->put_room_account_data( |
| 56 | roomid_.toStdString(), event, [](mtx::http::RequestErr e) { |
| 57 | if (e) { |
| 58 | nhlog::net()->error("Failed to set hidden events: {}", *e); |
| 59 | MainWindow::instance()->showNotification( |
| 60 | tr("Failed to set hidden events: %1") |
| 61 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | int |
| 67 | EventExpiry::expireEventsAfterDays() const |
| 68 | { |
| 69 | return event.expire_after_ms / (1000 * 60 * 60 * 24); |
| 70 | } |
| 71 | |
| 72 | int |
| 73 | EventExpiry::expireEventsAfterCount() const |
| 74 | { |
| 75 | return event.keep_only_latest; |
| 76 | } |
| 77 | |
| 78 | int |
| 79 | EventExpiry::protectLatestEvents() const |
| 80 | { |
| 81 | return event.protect_latest; |
| 82 | } |
| 83 | |
| 84 | bool |
| 85 | EventExpiry::expireStateEvents() const |
| 86 | { |
| 87 | return !event.exclude_state_events; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | EventExpiry::setExpireEventsAfterDays(int val) |
| 92 | { |
| 93 | if (val > 0) |
| 94 | this->event.expire_after_ms = std::uint64_t(val) * (1000 * 60 * 60 * 24); |
| 95 | else |
| 96 | this->event.expire_after_ms = 0; |
| 97 | emit expireEventsAfterDaysChanged(); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | EventExpiry::setProtectLatestEvents(int val) |
| 102 | { |
| 103 | if (val > 0) |
| 104 | this->event.protect_latest = std::uint64_t(val); |
| 105 | else |
| 106 | this->event.expire_after_ms = 0; |
| 107 | emit protectLatestEventsChanged(); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | EventExpiry::setExpireEventsAfterCount(int val) |
| 112 | { |
| 113 | if (val > 0) |
| 114 | this->event.keep_only_latest = std::uint64_t(val); |
| 115 | else |
| 116 | this->event.keep_only_latest = 0; |
| 117 | emit expireEventsAfterCountChanged(); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | EventExpiry::setExpireStateEvents(bool val) |
| 122 | { |
| 123 | this->event.exclude_state_events = !val; |
| 124 | emit expireEventsAfterCountChanged(); |
| 125 | } |
| 126 | |
| 127 | #include "moc_EventExpiry.cpp" |