TelegramSearch/src/search.cpp

143 lines
3.2 KiB
C++

#include "search.h"
#include <fstream>
#include <Log.h>
#include <mrbesen.h>
#include <regex>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
Search::Search() {}
Search::Search(const Search& orig, std::list<const Message*>* list) {
//copy messages
if(list) {
for(const Message* m : *list) {
msgs.push_back(*m);
}
} else {
msgs = orig.msgs;
}
//copy chatnames
chatnames = orig.chatnames;
}
Search::~Search() {}
void Search::addFile(const std::string& file) {
//laden den datei
try {
std::ifstream fstream(file);
json j;
fstream >> j;
//single chat export
if(j.contains("messages")) {
chatnames.insert({j["id"], j["name"].get<std::string>()});
loadMessages(j["messages"], j["id"]);
return;
}
//multi chat export
if(j.contains("chats")) {
const json& chatlist = j["chats"]["list"]; //asume that list exists
if(chatlist.is_null()) {
Log::error << "File does not contain a chatlist";
return;
}
for(const json& chat : chatlist) {
int64_t id = chat["id"];
std::string name = "";
if(chat.contains("name") && !chat["name"].is_null())
name = chat.value("name", "");
chatnames.insert({id, name});
loadMessages(chat["messages"], id);
Log::note << "Loaded Chat: " << name << " (" << id << ")";
}
return;
}
//get contact list
if(j.contains("frequent_contacts")) {
const json& contacts = j["frequent_contacts"]["list"];//asume that list exists
for(const json& contact : contacts) {
int64_t id = contact["id"];
std::string name = contact["name"];
chatnames.insert({id, name});
}
}
Log::warn << "File " << file << " could not be parsed! Export as json!";
} catch (nlohmann::detail::parse_error& e) {
Log::error << "Could not load File: " << e.what();
}
}
void Search::finalize() {
msgs.reserve(msgs.size() + deduplicate.size());
for(const auto it : deduplicate) {
msgs.push_back(it);
}
deduplicate.clear();
}
std::list<const Message*> Search::search(const Filter& filter) const {
std::list<const Message*> out;
runsearch(filter, out);
return out;
}
const std::string& Search::getChatname(int64_t id) const {
static const std::string UNKOWNCHAT = "<unknownchat>";
auto it = chatnames.find(id);
if(it == chatnames.end()) return UNKOWNCHAT;
if(it->second.empty()) return UNKOWNCHAT;
return it->second;
}
std::string Search::getShortChatname(int64_t id) const {
std::string chatname = getChatname(id);
if(chatname.size() > 14) {
return chatname.substr(0, 14);
}
return chatname;
}
uint32_t Search::getChatCount() const {
return chatnames.size();
}
uint64_t Search::getMessageCount() const {
return msgs.size();
}
void Search::runsearch(const Filter& filter, std::list<const Message*>& out) const {
for(const Message& m : msgs) {
if(filter.filter(m)) {
out.push_back(&m);
}
}
}
void Search::loadMessages(const json& j, int64_t chatid) {
uint32_t failed = 0;
for(const json& m : j) {
try {
deduplicate.insert({m, chatid}); // Message(const json&) constructor call
} catch(const nlohmann::detail::exception& e) {
Log::warn << "Parse error: " << e.id << " " << e.what();
} catch(...) {
failed ++;
}
}
if(failed != 0) {
Log::warn << failed << " Messages failed to load";
}
}