TelegramSearch/src/search.h

64 lines
1.8 KiB
C++

#pragma once
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <cstdint>
#include <nlohmann/json_fwd.hpp>
using json = nlohmann::json;
struct Message {
std::string text;
uint64_t chatid;
uint64_t messageid;
bool operator==(const Message& m) const;
bool operator!=(const Message& m) const;
bool operator<(const Message& m) const;
};
enum class Searchflags {
NONE = 0,
IGNORECASE = 1,
REGEX = 2,
//ideen: nach sender filtern, nur nachrichten mit medien, nur nachrichten ohne medien, medien Dateinamen, nach datum filtern
};
Searchflags operator|=(Searchflags& lhs, const Searchflags sf);
bool operator&(Searchflags& lhs, const Searchflags sf);
class Search {
public:
Search();
//copy chatnames from orig, and message list either from orig, or - if set - from list
Search(const Search& orig, std::list<const Message*>* list = nullptr);
~Search();
static Searchflags fromString(const std::string&);
void addFile(const std::string& file);
void finalize(); //stop adding files and finalize deduplication, could be called twice, but then a deduplication is not guaranteed
std::list<const Message*> search(std::string text, Searchflags flags = Searchflags::NONE) const;
const std::string& getChatname(uint64_t id) const;
std::string getShortChatname(uint64_t id) const;
uint32_t getChatCount() const;
uint64_t getMessageCount() const;
private:
void searchRegex(const std::string& text, bool ignoreCase, std::list<const Message*>& out) const;
template<typename T>
void runsearch(T st, bool (*checker)(const std::string& msg, T text), std::list<const Message*>& out) const;
void loadMessages(const json& j, uint64_t chatid);
std::vector<Message> msgs;
std::set<Message> deduplicate; //intermediate store, for reading files and deduplicate them
std::map<uint64_t, std::string> chatnames;
};