~aleteoryx/muditaos

ref: sign_test muditaos/module-db/Interface/ThreadRecord.hpp -rw-r--r-- 3.6 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "Record.hpp"
#include "module-db/databases/SmsDB.hpp"
#include "module-db/databases/ContactsDB.hpp"
#include "module-db/Common/Common.hpp"
#include "module-db/queries/messages/threads/QueryThreadMarkAsRead.hpp"
#include <PhoneNumber.hpp>

#include <utf8/UTF8.hpp>

#include <cstdint>

struct ThreadRecord : Record
{
    std::uint32_t date           = 0;
    std::uint32_t msgCount       = 0;
    std::uint32_t unreadMsgCount = 0;
    UTF8 snippet;
    SMSType type      = SMSType::UNKNOWN;
    std::uint32_t numberID = DB_ID_NONE;

    ThreadRecord() = default;
    ThreadRecord(const ThreadsTableRow &rec)
        : Record(rec.ID), date(rec.date), msgCount(rec.msgCount), unreadMsgCount(rec.unreadMsgCount),
          snippet(rec.snippet), type(rec.type), numberID(rec.numberID)
    {}

    bool isUnread() const
    {
        return unreadMsgCount > 0;
    }
};

enum class ThreadRecordField
{
    ContactID,
    NumberID,
};

class ThreadRecordInterface : public RecordInterface<ThreadRecord, ThreadRecordField>
{
  public:
    ThreadRecordInterface(SmsDB *smsDb, ContactsDB *contactsDb);
    ~ThreadRecordInterface() = default;

    bool Add(const ThreadRecord &rec) override final;
    bool RemoveByID(std::uint32_t id) override final;
    bool Update(const ThreadRecord &rec) override final;
    ThreadRecord GetByID(std::uint32_t id) override final;
    ThreadRecord GetByNumber(const utils::PhoneNumber::View &phoneNumber);

    std::uint32_t GetCount() override final;
    std::uint32_t GetCount(EntryState state);

    std::unique_ptr<std::vector<ThreadRecord>> GetLimitOffset(std::uint32_t offset, std::uint32_t limit) override final;

    std::unique_ptr<std::vector<ThreadRecord>> GetLimitOffsetByField(std::uint32_t offset,
                                                                     std::uint32_t limit,
                                                                     ThreadRecordField field,
                                                                     const char *str) override final;

    std::unique_ptr<db::QueryResult> runQuery(std::shared_ptr<db::Query> query) override;

  private:
    SmsDB *smsDB           = nullptr;
    ContactsDB *contactsDB = nullptr;

    /// for now implementation between Interface <-> Database
    /// it would only make sense to pass Query from Inteface to multiple databases to get all data we are interested in
    /// or better split it to smaller entities... this could be done with any db high level interface -  left as it is
    std::unique_ptr<db::QueryResult> threadSearchForListQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> markAsReadQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadsGetQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadsGetQueryWithTotalCount(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadsGetForListQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadGetByIDQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadGetByNumberQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadRemoveQuery(const std::shared_ptr<db::Query> &query);
    std::unique_ptr<db::QueryResult> threadsGetCount(const std::shared_ptr<db::Query> &query);

    std::vector<ThreadRecord> getThreads(const std::shared_ptr<db::Query> &query);
};