TelegramSearch/src/search.cpp

143 lines
3.2 KiB
C++
Raw Normal View History

2021-04-07 11:42:35 +02:00
#include "search.h"
#include <fstream>
#include <Log.h>
#include <mrbesen.h>
#include <regex>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
Search::Search() {}
2021-04-07 17:00:19 +02:00
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;
}
2021-04-07 11:42:35 +02:00
Search::~Search() {}
void Search::addFile(const std::string& file) {
//laden den datei
try {
std::ifstream fstream(file);
json j;
fstream >> j;
2021-04-07 13:56:18 +02:00
//single chat export
2021-04-07 11:42:35 +02:00
if(j.contains("messages")) {
chatnames.insert({j["id"], j["name"].get<std::string>()});
loadMessages(j["messages"], j["id"]);
2021-04-07 17:00:19 +02:00
return;
2021-04-07 11:42:35 +02:00
}
2021-04-07 13:56:18 +02:00
//multi chat export
if(j.contains("chats")) {
2021-04-07 20:07:11 +02:00
const json& chatlist = j["chats"]["list"]; //asume that list exists
2021-04-07 13:56:18 +02:00
if(chatlist.is_null()) {
Log::error << "File does not contain a chatlist";
return;
}
for(const json& chat : chatlist) {
2021-04-07 20:07:11 +02:00
int64_t id = chat["id"];
2021-04-07 13:56:18 +02:00
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 << ")";
}
2021-04-07 17:00:19 +02:00
return;
2021-04-07 13:56:18 +02:00
}
2021-04-07 17:00:19 +02:00
2021-04-07 20:07:11 +02:00
//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});
}
}
2021-04-07 17:00:19 +02:00
Log::warn << "File " << file << " could not be parsed! Export as json!";
2021-04-07 11:42:35 +02:00
} catch (nlohmann::detail::parse_error& e) {
Log::error << "Could not load File: " << e.what();
}
}
2021-04-07 17:52:32 +02:00
void Search::finalize() {
msgs.reserve(msgs.size() + deduplicate.size());
for(const auto it : deduplicate) {
msgs.push_back(it);
}
deduplicate.clear();
}
2021-04-11 01:47:55 +02:00
std::list<const Message*> Search::search(const Filter& filter) const {
2021-04-07 11:42:35 +02:00
std::list<const Message*> out;
2021-04-11 01:47:55 +02:00
runsearch(filter, out);
2021-04-07 11:42:35 +02:00
return out;
}
2021-04-07 20:07:11 +02:00
const std::string& Search::getChatname(int64_t id) const {
2021-04-07 11:42:35 +02:00
static const std::string UNKOWNCHAT = "<unknownchat>";
auto it = chatnames.find(id);
if(it == chatnames.end()) return UNKOWNCHAT;
2021-04-07 13:56:18 +02:00
if(it->second.empty()) return UNKOWNCHAT;
2021-04-07 11:42:35 +02:00
return it->second;
}
2021-04-07 20:07:11 +02:00
std::string Search::getShortChatname(int64_t id) const {
2021-04-07 11:42:35 +02:00
std::string chatname = getChatname(id);
if(chatname.size() > 14) {
return chatname.substr(0, 14);
}
return chatname;
}
2021-04-07 13:56:18 +02:00
uint32_t Search::getChatCount() const {
return chatnames.size();
}
uint64_t Search::getMessageCount() const {
return msgs.size();
}
2021-04-11 01:47:55 +02:00
void Search::runsearch(const Filter& filter, std::list<const Message*>& out) const {
2021-04-07 11:42:35 +02:00
for(const Message& m : msgs) {
2021-04-11 01:47:55 +02:00
if(filter.filter(m)) {
2021-04-07 11:42:35 +02:00
out.push_back(&m);
}
}
}
2021-04-07 20:07:11 +02:00
void Search::loadMessages(const json& j, int64_t chatid) {
2021-04-07 11:42:35 +02:00
uint32_t failed = 0;
for(const json& m : j) {
try {
2021-04-11 15:25:34 +02:00
deduplicate.insert({m, chatid}); // Message(const json&) constructor call
2021-04-07 17:00:19 +02:00
} catch(const nlohmann::detail::exception& e) {
Log::warn << "Parse error: " << e.id << " " << e.what();
2021-04-07 11:42:35 +02:00
} catch(...) {
failed ++;
}
}
2021-04-07 13:56:18 +02:00
if(failed != 0) {
2021-04-07 11:42:35 +02:00
Log::warn << failed << " Messages failed to load";
}
}