76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
#include "message.h"
|
|
|
|
#include <Log.h>
|
|
#include <nlohmann/json.hpp>
|
|
#include <ctime>
|
|
|
|
static void readText(const json& t, std::string& out) {
|
|
if(t.is_null()) return;
|
|
if(t.is_string())
|
|
out = t;
|
|
if(t.is_array()) {
|
|
std::ostringstream buff;
|
|
for(const json& entr : t) {
|
|
if(entr.is_string())
|
|
buff << (const std::string&) entr;
|
|
else if(entr.contains("text"))
|
|
buff << (const std::string&) entr["text"];
|
|
}
|
|
out = buff.str();
|
|
}
|
|
}
|
|
|
|
Message::Message(const json& m, int64_t chatid) : chatid(chatid) {
|
|
if(m.contains("text")) {
|
|
readText(m["text"], text);
|
|
messageid = m["id"];
|
|
if(m.contains("file"))
|
|
filename = m["file"];
|
|
else if(m.contains("photo"))
|
|
filename = m["photo"];
|
|
|
|
if(m.contains("from_id") && m["from_id"].is_number_unsigned())
|
|
senderid = m["from_id"];
|
|
|
|
if(m.contains("forwarded_from") && m["forwarded_from"].is_string())
|
|
fwdFrom = m["forwarded_from"];
|
|
|
|
if(m.contains("date")) {
|
|
tm parsedTime;
|
|
::strptime(m["date"].get_ref<const std::string&>().c_str(), "%Y-%m-%dT%T", &parsedTime);
|
|
date = timegm(&parsedTime);
|
|
}
|
|
isreply = m.contains("reply_to_message_id");
|
|
} else {
|
|
Log::warn << "text less message: " << m;
|
|
}
|
|
}
|
|
|
|
bool Message::operator==(const Message& m) const {
|
|
return (m.chatid == chatid) && (m.messageid == messageid);
|
|
}
|
|
|
|
bool Message::operator!=(const Message& m) const {
|
|
return (m.chatid != chatid) || (m.messageid != messageid);
|
|
}
|
|
|
|
bool Message::operator<(const Message& m) const {
|
|
if (chatid < m.chatid) return true;
|
|
if (chatid > m.chatid) return false;
|
|
// chatid == m.chatid
|
|
return (messageid < m.messageid);
|
|
}
|
|
|
|
bool Message::hasFile() const {
|
|
return !filename.empty();
|
|
}
|
|
|
|
std::string Message::getDate() const {
|
|
struct tm buff;
|
|
static const uint32_t buffsize = 20;
|
|
char outbuff[buffsize];
|
|
gmtime_r(&date, &buff);
|
|
strftime(outbuff, buffsize, "%Y-%m-%d %T", &buff);
|
|
return std::string(outbuff);
|
|
}
|