#include "mologram/Message.hpp" namespace mologram { static const char* kindName(MediaKind k) { switch (k) { case MediaKind::None: return "none"; case MediaKind::Photo: return "photo"; case MediaKind::Video: return "video"; case MediaKind::Voice: return "voice"; case MediaKind::Audio: return "audio"; case MediaKind::Document: return "document"; case MediaKind::Sticker: return "sticker"; case MediaKind::Gif: return "gif"; case MediaKind::Location: return "location"; case MediaKind::Contact: return "contact"; case MediaKind::Poll: return "poll"; case MediaKind::Other: return "other"; } return "other"; } static MediaKind kindFrom(const std::string& s) { for (MediaKind k : {MediaKind::None, MediaKind::Photo, MediaKind::Video, MediaKind::Voice, MediaKind::Audio, MediaKind::Document, MediaKind::Sticker, MediaKind::Gif, MediaKind::Location, MediaKind::Contact, MediaKind::Poll}) if (s == kindName(k)) return k; return MediaKind::Other; } js::Value Reaction::toJson() const { js::Object o; o["emoji"] = js::Value(emoji); o["custom"] = js::id(customEmojiId); o["count"] = js::Value(count); o["mine"] = js::Value(mine); return js::Value(std::move(o)); } Reaction Reaction::fromJson(const js::Value& v) { Reaction r; r.emoji = js::getStr(v, "emoji"); r.customEmojiId = js::getId(v, "custom"); r.count = js::getInt(v, "count"); r.mine = js::getBool(v, "mine"); return r; } js::Value MediaRef::toJson() const { js::Object o; o["kind"] = js::Value(kindName(kind)); o["token"] = js::Value(token); o["file"] = js::Value(fileName); o["mime"] = js::Value(mime); o["thumb"] = js::Value(thumbPath); o["local"] = js::Value(localPath); o["size"] = js::id(size); o["duration"] = js::Value(duration); o["w"] = js::Value(width); o["h"] = js::Value(height); o["animated"] = js::Value(animated); js::Array wf; for (int w : waveform) wf.push_back(js::Value(w)); o["wave"] = js::Value(std::move(wf)); return js::Value(std::move(o)); } MediaRef MediaRef::fromJson(const js::Value& v) { MediaRef m; m.kind = kindFrom(js::getStr(v, "kind", "none")); m.token = js::getStr(v, "token"); m.fileName = js::getStr(v, "file"); m.mime = js::getStr(v, "mime"); m.thumbPath = js::getStr(v, "thumb"); m.localPath = js::getStr(v, "local"); m.size = js::getId(v, "size"); m.duration = js::getInt(v, "duration"); m.width = js::getInt(v, "w"); m.height = js::getInt(v, "h"); m.animated = js::getBool(v, "animated"); if (const js::Array* wf = js::getArr(v, "wave")) for (const auto& e : *wf) m.waveform.push_back(e.asInt()); return m; } js::Value Poll::toJson() const { js::Object o; o["question"] = js::Value(question); o["closed"] = js::Value(closed); o["quiz"] = js::Value(quiz); o["multiple"] = js::Value(multiple); o["voters"] = js::Value(totalVoters); js::Array opts; for (const auto& p : options) { js::Object po; po["text"] = js::Value(p.text); po["data"] = js::Value(p.data); po["votes"] = js::Value(p.votes); po["chosen"] = js::Value(p.chosen); po["correct"] = js::Value(p.correct); opts.push_back(js::Value(std::move(po))); } o["options"] = js::Value(std::move(opts)); return js::Value(std::move(o)); } Poll Poll::fromJson(const js::Value& v) { Poll p; p.question = js::getStr(v, "question"); p.closed = js::getBool(v, "closed"); p.quiz = js::getBool(v, "quiz"); p.multiple = js::getBool(v, "multiple"); p.totalVoters = js::getInt(v, "voters"); if (const js::Array* a = js::getArr(v, "options")) for (const auto& e : *a) { PollOption o; o.text = js::getStr(e, "text"); o.data = js::getStr(e, "data"); o.votes = js::getInt(e, "votes"); o.chosen = js::getBool(e, "chosen"); o.correct = js::getBool(e, "correct"); p.options.push_back(o); } return p; } js::Value Message::toJson() const { js::Object o; o["id"] = js::id(id); o["peer"] = peer.toJson(); o["sender"] = sender.toJson(); o["senderName"] = js::Value(senderName); o["text"] = js::Value(text); o["date"] = js::id(date); o["out"] = js::Value(outgoing); o["edited"] = js::Value(edited); o["pinned"] = js::Value(pinned); o["read"] = js::Value(read); o["replyTo"] = js::id(replyTo); o["forwardFrom"] = js::Value(forwardFrom); o["viaBot"] = js::Value(viaBot); o["effect"] = js::id(effectId); o["media"] = media.toJson(); if (hasPoll) o["poll"] = poll.toJson(); js::Array rs; for (const auto& r : reactions) rs.push_back(r.toJson()); o["reactions"] = js::Value(std::move(rs)); js::Array rows; for (const auto& row : buttons) { js::Array cells; for (const auto& b : row) { js::Object bo; bo["text"] = js::Value(b.text); bo["data"] = js::Value(b.data); bo["url"] = js::Value(b.url); cells.push_back(js::Value(std::move(bo))); } rows.push_back(js::Value(std::move(cells))); } o["buttons"] = js::Value(std::move(rows)); return js::Value(std::move(o)); } Message Message::fromJson(const js::Value& v) { Message m; m.id = js::getId(v, "id"); if (const js::Value* p = v.find("peer")) m.peer = Peer::fromJson(*p); if (const js::Value* p = v.find("sender")) m.sender = Peer::fromJson(*p); m.senderName = js::getStr(v, "senderName"); m.text = js::getStr(v, "text"); m.date = js::getId(v, "date"); m.outgoing = js::getBool(v, "out"); m.edited = js::getBool(v, "edited"); m.pinned = js::getBool(v, "pinned"); m.read = js::getBool(v, "read"); m.replyTo = js::getId(v, "replyTo"); m.forwardFrom = js::getStr(v, "forwardFrom"); m.viaBot = js::getStr(v, "viaBot"); m.effectId = js::getId(v, "effect"); if (const js::Value* mv = v.find("media")) m.media = MediaRef::fromJson(*mv); if (const js::Value* pv = v.find("poll")) { m.hasPoll = true; m.poll = Poll::fromJson(*pv); } if (const js::Array* a = js::getArr(v, "reactions")) for (const auto& e : *a) m.reactions.push_back(Reaction::fromJson(e)); if (const js::Array* rows = js::getArr(v, "buttons")) for (const auto& row : *rows) { std::vector