89 lines
2.6 KiB
C++
89 lines
2.6 KiB
C++
#ifndef DB_MANAGER_H
|
|
#define DB_MANAGER_H
|
|
|
|
#include <sqlite3.h>
|
|
#include <QString>
|
|
#include <QList>
|
|
#include <QObject>
|
|
#include <cstdint>
|
|
|
|
struct ChannelRow {
|
|
QString channelId;
|
|
QString name;
|
|
bool isDm = false;
|
|
QString lastMessage;
|
|
qint64 lastMsgAt = 0;
|
|
};
|
|
|
|
struct MessageRow {
|
|
qint64 id = 0;
|
|
QString channelId;
|
|
quint64 authorNodeId = 0;
|
|
QString content;
|
|
QString contentType;
|
|
qint64 timestamp = 0;
|
|
QByteArray signature;
|
|
bool isOutgoing = false;
|
|
bool isRead = false;
|
|
};
|
|
|
|
struct AccountRow {
|
|
quint64 nodeId = 0;
|
|
QString displayName;
|
|
QString avatarColor;
|
|
QString avatarLetter;
|
|
bool isContact = true;
|
|
};
|
|
|
|
struct NodeAddr {
|
|
quint64 nodeId = 0;
|
|
int family = 0; // 4=IPv4, 6=IPv6
|
|
QByteArray address;
|
|
quint16 port = 0;
|
|
};
|
|
|
|
class DbManager : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
explicit DbManager(const QString& path, QObject* parent = nullptr);
|
|
~DbManager() override;
|
|
|
|
bool isOpen() const { return m_db != nullptr; }
|
|
quint64 myNodeId() const { return m_myNodeId; }
|
|
|
|
QList<ChannelRow> getChannels() const;
|
|
QList<MessageRow> getMessages(const QString& channelId, int limit = 50) const;
|
|
QList<AccountRow> getAccounts(bool contactsOnly = false) const;
|
|
AccountRow getAccount(quint64 nodeId) const;
|
|
QByteArray getNodeEdPub(quint64 nodeId) const;
|
|
|
|
struct BucketMeta { qint64 day; int hour; int count; QByteArray hash; };
|
|
QList<BucketMeta> getBucketMeta(const QString& channelId) const;
|
|
void setBucketMeta(const QString& channelId, qint64 day, int hour,
|
|
int count, const QByteArray& hash);
|
|
QList<QByteArray> getMessageSigs(const QString& channelId, qint64 day, int hour) const;
|
|
QList<MessageRow> getMessagesBySigs(const QList<QByteArray>& sigs) const;
|
|
QList<quint64> getOnlinePeers() const;
|
|
QByteArray getLocalPubkey() const;
|
|
QList<NodeAddr> getOnlinePeerAddresses(quint64 excludeNodeId) const;
|
|
|
|
void seedStubData();
|
|
|
|
bool insertMessage(const QString& channelId, quint64 authorNodeId,
|
|
const QString& content, qint64 timestamp, int isOutgoing);
|
|
|
|
QString getUiState(const QString& key) const;
|
|
void setUiState(const QString& key, const QString& value);
|
|
|
|
private:
|
|
void initTables();
|
|
sqlite3_stmt* prepareOrNull(const char* sql) const;
|
|
static QString colText(sqlite3_stmt* stmt, int col);
|
|
static QByteArray colBlob(sqlite3_stmt* stmt, int col);
|
|
|
|
sqlite3* m_db = nullptr;
|
|
quint64 m_myNodeId = 0;
|
|
};
|
|
|
|
#endif
|