~aleteoryx/muditaos

ref: 521cadc0bca7d9e984018f472487114b8be2554d muditaos/module-db/Interface/ThreadRecord.cpp -rw-r--r-- 8.0 KiB
521cadc0 — kkleczkowski [EGD-3768] Fixed items sizes and positions in Date Time window. (#728) 5 years ago
                                                                                
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "ThreadRecord.hpp"
#include "SMSRecord.hpp"
#include "ContactRecord.hpp"

#include <queries/sms/QueryThreadGetByID.hpp>
#include <queries/sms/QueryThreadGetByNumber.hpp>
#include <queries/sms/QueryThreadGetByContactID.hpp>
#include <queries/sms/QueryThreadRemove.hpp>

#include <cassert>
#include <log/log.hpp>

ThreadRecordInterface::ThreadRecordInterface(SmsDB *smsDb, ContactsDB *contactsDb)
    : smsDB(smsDb), contactsDB(contactsDb)
{}

bool ThreadRecordInterface::Add(const ThreadRecord &rec)
{
    auto ret = smsDB->threads.add(ThreadsTableRow{{.ID = rec.ID},
                                                  .date           = rec.date,
                                                  .msgCount       = rec.msgCount,
                                                  .unreadMsgCount = rec.unreadMsgCount,
                                                  .contactID      = rec.contactID,
                                                  .numberID       = rec.numberID,
                                                  .snippet        = rec.snippet,
                                                  .type           = rec.type});

    return ret;
}

bool ThreadRecordInterface::RemoveByID(uint32_t id)
{
    auto ret = smsDB->threads.removeById(id);
    if (ret == false) {
        return false;
    }

    SMSRecordInterface smsRecordInterface(smsDB, contactsDB);
    return smsRecordInterface.RemoveByField(SMSRecordField::ThreadID, std::to_string(id).c_str());
}

bool ThreadRecordInterface::Update(const ThreadRecord &rec)
{
    return smsDB->threads.update(ThreadsTableRow{{.ID = rec.ID},
                                                 .date           = rec.date,
                                                 .msgCount       = rec.msgCount,
                                                 .unreadMsgCount = rec.unreadMsgCount,
                                                 .contactID      = rec.contactID,
                                                 .numberID       = rec.numberID,
                                                 .snippet        = rec.snippet,
                                                 .type           = rec.type

    });
}

uint32_t ThreadRecordInterface::GetCount()
{
    return smsDB->threads.count();
}

uint32_t ThreadRecordInterface::GetCount(EntryState state)
{
    return smsDB->threads.count(state);
}

std::unique_ptr<std::vector<ThreadRecord>> ThreadRecordInterface::GetLimitOffset(uint32_t offset, uint32_t limit)
{
    auto records = std::make_unique<std::vector<ThreadRecord>>();

    auto ret = smsDB->threads.getLimitOffset(offset, limit);

    for (const auto &w : ret) {
        records->push_back(w);
    }

    return records;
}

std::unique_ptr<std::vector<ThreadRecord>> ThreadRecordInterface::GetLimitOffsetByField(uint32_t offset,
                                                                                        uint32_t limit,
                                                                                        ThreadRecordField field,
                                                                                        const char *str)
{
    auto records = std::make_unique<std::vector<ThreadRecord>>();

    ThreadsTableFields threadsField;
    switch (field) {
    case ThreadRecordField::ContactID: {
        threadsField = ThreadsTableFields::ContactID;
    } break;
    case ThreadRecordField::NumberID: {
        threadsField = ThreadsTableFields::NumberID;
    } break;
    default:
        LOG_ERROR("Invalid field type %u", static_cast<unsigned>(field));
        return records;
    }

    auto ret = smsDB->threads.getLimitOffsetByField(offset, limit, threadsField, str);
    for (const auto &w : ret) {
        records->push_back(w);
    }

    return records;
}

ThreadRecord ThreadRecordInterface::GetByID(uint32_t id)
{
    auto rec = smsDB->threads.getById(id);
    if (!rec.isValid()) {
        return ThreadRecord();
    }

    return ThreadRecord(rec);
}

ThreadRecord ThreadRecordInterface::GetByContact(uint32_t contact_id)
{
    auto ret =
        smsDB->threads.getLimitOffsetByField(0, 1, ThreadsTableFields::ContactID, std::to_string(contact_id).c_str());
    if (ret.size() == 0) {
        ThreadRecord re;
        re.contactID = contact_id;
        if (!Add(re)) {
            LOG_ERROR("There is no thread but we cant add it");
            return ThreadRecord();
        }

        ret = smsDB->threads.getLimitOffsetByField(
            0, 1, ThreadsTableFields::ContactID, std::to_string(contact_id).c_str());
        if (ret.size() == 0) {
            return ThreadRecord();
        }
    }

    return ThreadRecord(ret[0]);
}

ThreadRecord ThreadRecordInterface::GetByNumber(const utils::PhoneNumber::View &phoneNumber)
{
    auto contactInterface = ContactRecordInterface(contactsDB);
    auto match            = contactInterface.MatchByNumber(phoneNumber);

    if (!match.has_value()) {
        return ThreadRecord();
    }

    auto threadRec = GetLimitOffsetByField(0, 1, ThreadRecordField::NumberID, std::to_string(match->numberId).c_str());

    if (threadRec->size() == 0) {
        return ThreadRecord();
    }

    return threadRec->at(0);
}

std::unique_ptr<db::QueryResult> ThreadRecordInterface::runQuery(std::shared_ptr<db::Query> query)
{
    if (const auto localQuery = dynamic_cast<const db::query::SMSSearch *>(query.get())) {
        auto dbResult = smsDB->threads.getBySMSQuery(localQuery->text, localQuery->starting_postion, localQuery->depth);

        auto response = std::make_unique<db::query::SMSSearchResult>(dbResult.first, dbResult.second);
        response->setRequestQuery(query);
        return response;
    }

    if (const auto localQuery = dynamic_cast<const db::query::SMSThreadsGet *>(query.get())) {
        auto dbResult = smsDB->threads.getLimitOffset(localQuery->offset, localQuery->limit);

        auto response = std::make_unique<db::query::SMSThreadsGetResults>(dbResult);
        response->setRequestQuery(query);
        return response;
    }

    if (const auto local_query = dynamic_cast<const db::query::smsthread::MarkAsRead *>(query.get())) {
        auto response = runQueryImpl(local_query);
        response->setRequestQuery(query);
        return response;
    }

    if (const auto local_query = dynamic_cast<const db::query::ThreadGetByID *>(query.get())) {
        const auto ret = GetByID(local_query->id);
        auto response  = std::make_unique<db::query::ThreadGetByIDResult>(
            ret.isValid() ? std::optional<ThreadRecord>{ret} : std::nullopt);
        response->setRequestQuery(query);
        return response;
    }

    if (const auto local_query = dynamic_cast<const db::query::ThreadGetByNumber *>(query.get())) {
        auto response = std::make_unique<db::query::ThreadGetByNumberResult>(GetByNumber(local_query->getNumber()));
        response->setRequestQuery(query);
        return response;
    }

    if (const auto local_query = dynamic_cast<const db::query::ThreadGetByContactID *>(query.get())) {
        const auto thread = GetByContact(local_query->id);
        auto response     = std::make_unique<db::query::ThreadGetByContactIDResult>(
            thread.isValid() ? std::optional<ThreadRecord>(thread) : std::nullopt);
        response->setRequestQuery(query);
        return response;
    }

    if (const auto local_query = dynamic_cast<const db::query::ThreadRemove *>(query.get())) {
        const auto ret = RemoveByID(local_query->id);
        auto response  = std::make_unique<db::query::ThreadRemoveResult>(ret);
        response->setRequestQuery(query);
        return response;
    }

    return nullptr;
}

std::unique_ptr<db::query::smsthread::MarkAsReadResult> ThreadRecordInterface::runQueryImpl(
    const db::query::smsthread::MarkAsRead *query)
{
    using namespace db::query::smsthread;
    auto ret = false;

    auto record = GetByID(query->id);
    if (record.isValid()) {
        LOG_FATAL("query-read %d", static_cast<int>(query->read));
        record.unreadMsgCount = query->read == MarkAsRead::Read::True ? 0 : 1;
        ret                   = Update(record);
    }
    return std::make_unique<MarkAsReadResult>(ret);
}