summaryrefslogtreecommitdiffstats
path: root/src/ui/EventExpiry.cpp
blob: ef3f093326a03192fc6278b72396c92ec5c89b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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 = std::uint64_t(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 = std::uint64_t(val);
    else
        this->event.expire_after_ms = 0;
    emit protectLatestEventsChanged();
}

void
EventExpiry::setExpireEventsAfterCount(int val)
{
    if (val > 0)
        this->event.keep_only_latest = std::uint64_t(val);
    else
        this->event.keep_only_latest = 0;
    emit expireEventsAfterCountChanged();
}

void
EventExpiry::setExpireStateEvents(bool val)
{
    this->event.exclude_state_events = !val;
    emit expireEventsAfterCountChanged();
}