~aleteoryx/muditaos

ref: sign_test muditaos/module-db/Interface/CalllogRecord.cpp -rw-r--r-- 13.2 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CalllogRecord.hpp"

#include <ContactRecord.hpp>
#include <log/log.hpp>
#include <Tables/CalllogTable.hpp>
#include <PhoneNumber.hpp>
#include <Utils.hpp>
#include "queries/calllog/QueryCalllogGet.hpp"
#include "queries/calllog/QueryCalllogGetCount.hpp"
#include "queries/calllog/QueryCalllogRemove.hpp"
#include "queries/calllog/QueryCalllogDeleteAll.hpp"
#include "queries/calllog/QueryCalllogGetByContactID.hpp"

#include <cassert>
#include <exception>
#include <sstream>
#include <vector>
#include <utility>

CalllogRecord::CalllogRecord(const CalllogTableRow &tableRow)
    : Record{tableRow.ID}, presentation(tableRow.presentation), date(tableRow.date), duration(tableRow.duration),
      type(tableRow.type), name(tableRow.name),
      phoneNumber(utils::PhoneNumber(tableRow.number, tableRow.e164number).getView()), isRead(tableRow.isRead)
{}

CalllogRecord::CalllogRecord(const CallType type, const utils::PhoneNumber::View &number)
    : presentation(PresentationType::PR_UNKNOWN), date(std::time(nullptr)), type(type), phoneNumber(number)
{}

CalllogRecord::CalllogRecord(const CalllogRecord &rhs) : Record(rhs)
{
    operator=(rhs);
}

CalllogRecord &CalllogRecord::operator=(const CalllogRecord &rhs)
{
    ID           = rhs.ID;
    presentation = rhs.presentation;
    date         = rhs.date;
    duration     = rhs.duration;
    type         = rhs.type;
    name         = rhs.name;
    phoneNumber  = rhs.phoneNumber;
    isRead       = rhs.isRead;
    return *this;
}

std::ostream &operator<<(std::ostream &out, const CalllogRecord &rec)
{
    out << " <id> " << rec.ID << " <number> " << rec.phoneNumber.getEntered() << " <e164number> "
        << rec.phoneNumber.getE164() << " <formatted> " << rec.phoneNumber.getFormatted() << " <presentation> "
        << static_cast<uint32_t>(rec.presentation) << " <date> " << rec.date << " <duration> " << rec.duration
        << " <type> " << static_cast<uint32_t>(rec.type) << " <name> " << rec.name << " <isRead> " << rec.isRead;

    return out;
}

[[nodiscard]] std::string CalllogRecord::str() const
{
    std::ostringstream ss;
    ss << *this;
    return ss.str();
}

CalllogRecordInterface::CalllogRecordInterface(CalllogDB *calllogDb, ContactsDB *contactsDb)
    : calllogDB(calllogDb), contactsDB(contactsDb)
{}

bool CalllogRecordInterface::Add(const CalllogRecord &rec)
{
    auto localRec = rec;
    if (!rec.phoneNumber.getFormatted().empty()) {
        ContactRecordInterface contactInterface(contactsDB);
        auto contactMatch =
            contactInterface.MatchByNumber(rec.phoneNumber, ContactRecordInterface::CreateTempContact::True);
        if (!contactMatch) {
            LOG_ERROR("Cannot get contact, for number %s", rec.phoneNumber.getFormatted().c_str());
            return false;
        }
        auto contactRec = getContactByNumber(rec.phoneNumber);
        if (!contactRec.has_value() || !contactRec->isValid()) {
            return false;
        }

        if (localRec.presentation == PresentationType::PR_UNKNOWN) {
            localRec.presentation = PresentationType::PR_ALLOWED;
        }
        LOG_DEBUG("Adding call log record");
    }
    else {
        // private number so do not add contact just call log entry
        localRec.presentation = PresentationType::PR_UNKNOWN;
        LOG_DEBUG("Adding private call entry to call log record.");
    }

    return calllogDB->calls.add(CalllogTableRow{Record(localRec.ID), // this is only to remove warning
                                                .number       = localRec.phoneNumber.getEntered(),
                                                .e164number   = localRec.phoneNumber.getE164(),
                                                .presentation = localRec.presentation,
                                                .date         = localRec.date,
                                                .duration     = localRec.duration,
                                                .type         = localRec.type,
                                                .name         = UTF8{},
                                                .contactId    = DB_ID_NONE,
                                                .isRead       = localRec.isRead});
}

uint32_t CalllogRecordInterface::GetLastID()
{
    return calllogDB->getLastInsertRowId();
}

std::unique_ptr<std::vector<CalllogRecord>> CalllogRecordInterface::GetLimitOffsetByField(uint32_t offset,
                                                                                          uint32_t limit,
                                                                                          CalllogRecordField field,
                                                                                          const char *str)
{
    // TODO: alek: need proper implementation
    return GetLimitOffset(offset, limit);
}

std::unique_ptr<std::vector<CalllogRecord>> CalllogRecordInterface::GetLimitOffset(uint32_t offset, uint32_t limit)
{
    auto calls = calllogDB->calls.getLimitOffset(offset, limit);

    auto records = std::make_unique<std::vector<CalllogRecord>>();
    for (auto &c : calls) {
        auto number     = utils::PhoneNumber{c.number};
        auto contactRec = getContactByNumber(number.getView());
        if (!contactRec.has_value() || !contactRec->isValid()) {
            LOG_ERROR("Cannot find contact for number %s", number.getFormatted().c_str());
            c.name = number.getFormatted();
        }
        else {
            c.name = contactRec->getFormattedName();
        }
        records->push_back(c);
    }

    return records;
}

bool CalllogRecordInterface::Update(const CalllogRecord &rec)
{
    auto call = calllogDB->calls.getById(rec.ID);
    if (!call.isValid()) {
        return false;
    }

    auto presentation = rec.presentation;
    if (call.presentation == PresentationType::PR_UNKNOWN && rec.phoneNumber.isValid()) {
        // entry added as private was updated with phone number
        ContactRecordInterface contactInterface(contactsDB);
        auto contactMatch =
            contactInterface.MatchByNumber(rec.phoneNumber, ContactRecordInterface::CreateTempContact::True);
        if (!contactMatch) {
            LOG_ERROR("Cannot get or create temporary contact for number %s", rec.phoneNumber.getFormatted().c_str());
            return false;
        }
        if (presentation == PresentationType::PR_UNKNOWN) {
            presentation = PresentationType::PR_ALLOWED;
        }
    }

    return calllogDB->calls.update(CalllogTableRow{Record(rec.ID),
                                                   .number       = rec.phoneNumber.getEntered(),
                                                   .e164number   = rec.phoneNumber.getE164(),
                                                   .presentation = presentation,
                                                   .date         = rec.date,
                                                   .duration     = rec.duration,
                                                   .type         = rec.type,
                                                   .name         = UTF8{},
                                                   .contactId    = DB_ID_NONE,
                                                   .isRead       = rec.isRead});
}

bool CalllogRecordInterface::RemoveByID(uint32_t id)
{

    auto call = calllogDB->calls.getById(id);
    if (call.ID == 0) {
        return false;
    }

    return calllogDB->calls.removeById(id);
}

bool CalllogRecordInterface::RemoveByField(CalllogRecordField field, const char *str)
{

    switch (field) {
    case CalllogRecordField::DATE:
        return calllogDB->calls.removeByField(CalllogTableFields::DATE, str);
    case CalllogRecordField::TYPE:
        return calllogDB->calls.removeByField(CalllogTableFields::TYPE, str);
    default:
        return false;
    }
}

CalllogRecord CalllogRecordInterface::GetByID(uint32_t id)
{
    auto call = calllogDB->calls.getById(id);
    if (!call.isValid()) {
        return {};
    }

    auto number        = utils::PhoneNumber{call.number};
    auto contactRecord = getContactByNumber(number.getView());
    if (!contactRecord.has_value() || !contactRecord->isValid()) {
        call.name = number.getFormatted();
        return call;
    }
    call.name = contactRecord->getFormattedName();
    return call;
}

std::vector<CalllogRecord> CalllogRecordInterface::GetByContactID(uint32_t id)
{
    auto calls = calllogDB->calls.getByContactId(id);

    std::vector<CalllogRecord> records;
    for (auto &call : calls) {
        auto number        = utils::PhoneNumber{call.number};
        auto contactRecord = getContactByNumber(number.getView());
        if (!contactRecord.has_value() || !contactRecord->isValid()) {
            records.push_back(CalllogRecord{});
            continue;
        }
        call.name = contactRecord->getFormattedName();
        records.push_back(call);
    }
    return records;
}

uint32_t CalllogRecordInterface::GetCount(EntryState state)
{
    return calllogDB->calls.count(state);
}

uint32_t CalllogRecordInterface::GetCount()
{
    return GetCount(EntryState::ALL);
}

bool CalllogRecordInterface::SetAllRead()
{
    return calllogDB->calls.SetAllRead();
}

bool CalllogRecordInterface::DeleteAll()
{
    return calllogDB->calls.DeleteAll();
}

std::unique_ptr<db::QueryResult> CalllogRecordInterface::runQuery(std::shared_ptr<db::Query> query)
{
    if (typeid(*query) == typeid(db::query::CalllogGet)) {
        return getQuery(query);
    }
    else if (typeid(*query) == typeid(db::query::CalllogGetByContactID)) {
        return getByContactIDQuery(query);
    }
    else if (typeid(*query) == typeid(db::query::calllog::SetAllRead)) {
        return setAllReadQuery(query);
    }
    else if (typeid(*query) == typeid(db::query::calllog::DeleteAll)) {
        return deleteAllQuery(query);
    }
    else if (typeid(*query) == typeid(db::query::CalllogGetCount)) {
        return getCountQuery(query);
    }
    else if (typeid(*query) == typeid(db::query::CalllogRemove)) {
        return removeQuery(query);
    }
    return nullptr;
}
std::unique_ptr<db::QueryResult> CalllogRecordInterface::getQuery(std::shared_ptr<db::Query> query)
{
    auto getQuery = static_cast<db::query::CalllogGet *>(query.get());
    auto records  = calllogDB->calls.getLimitOffset(getQuery->getOffset(), getQuery->getLimit());
    std::vector<CalllogRecord> recordVector;

    for (auto calllog : records) {
        CalllogRecord record;
        record.isRead       = calllog.isRead;
        record.phoneNumber  = utils::PhoneNumber::parse(calllog.number);
        record.type         = calllog.type;
        record.duration     = calllog.duration;
        record.date         = calllog.date;
        record.presentation = calllog.presentation;
        record.ID           = calllog.ID;
        recordVector.emplace_back(record);
    }

    auto response = std::make_unique<db::query::CalllogGetResult>(std::move(recordVector), GetCount());
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> CalllogRecordInterface::getByContactIDQuery(std::shared_ptr<db::Query> query)
{
    auto getQuery = static_cast<db::query::CalllogGetByContactID *>(query.get());
    auto records  = CalllogRecordInterface::GetByContactID(getQuery->contactId);

    auto response = std::make_unique<db::query::CalllogGetByContactIDResult>(std::move(records));
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> CalllogRecordInterface::setAllReadQuery(std::shared_ptr<db::Query> query)
{
    auto db_result = SetAllRead();
    auto response  = std::make_unique<db::query::calllog::SetAllReadResult>(db_result);
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> CalllogRecordInterface::deleteAllQuery(std::shared_ptr<db::Query> query)
{
    auto db_result = DeleteAll();
    auto response  = std::make_unique<db::query::calllog::DeleteAllResult>(db_result);
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> CalllogRecordInterface::getCountQuery(std::shared_ptr<db::Query> query)
{
    auto localQuery = static_cast<db::query::CalllogGetCount *>(query.get());
    auto count      = GetCount(localQuery->getState());
    auto response   = std::make_unique<db::query::CalllogGetCountResult>(localQuery->getState(), count);
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> CalllogRecordInterface::removeQuery(std::shared_ptr<db::Query> query)
{
    auto removeQuery = static_cast<db::query::CalllogRemove *>(query.get());
    auto ret         = CalllogRecordInterface::RemoveByID(removeQuery->id);
    auto response    = std::make_unique<db::query::CalllogRemoveResult>(ret);
    response->setRequestQuery(query);
    return response;
}

std::optional<ContactRecord> CalllogRecordInterface::getContactByNumber(utils::PhoneNumber::View number)
{
    ContactRecordInterface contactInterface(contactsDB);
    auto contactMatch = contactInterface.MatchByNumber(number, ContactRecordInterface::CreateTempContact::True);
    if (!contactMatch) {
        LOG_ERROR("Cannot get contact, for number %s", number.getEntered().c_str());
        return {};
    }
    return contactMatch->contact;
}