// GCC 15 reports a false -Wmaybe-uninitialized on moved std::variant values (ttgui::json::Value); silenced for this file only. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // TapiService: dialogs.* handlers (FR-10, FR-11). #include #include "mologram/tapi/TapiCodec.hpp" #include "mologram/tapi/TapiService.hpp" #include "tapi/tl/functions.hpp" #include "tapi/tl/types.hpp" namespace mologram { namespace T = tapi::tl::types; namespace F = tapi::tl::functions; using js::Array; using js::Object; using js::Value; // Every dialog matching `o`, fetched page by page. There is no cap on how many come back (D-02); the pages only exist so a // cancel (user quit, request cancelled) is noticed within one page instead of after the whole account. static std::vector allDialogs(tapi::Dialogs& dialogs, tapi::IterDialogsOptions o, const CancelFlag& cancel, bool& cancelled) { std::vector out; const int pageSize = 100; cancelled = false; for (;;) { if (cancel.cancelled()) { cancelled = true; return out; } tapi::IterDialogsOptions page = o; page.limit = pageSize; auto got = dialogs.get_dialogs(page); for (auto& d : got) out.push_back(d); if ((int)got.size() < pageSize || got.empty()) return out; const tapi::DialogInfo& last = got[got.size() - 1]; if (!last.has_message()) return out; o.offset_date = last.date(); o.offset_id = last.message()->id(); o.offset_peer = tapi::EntityLike(last.input_entity()); } } RpcResult TapiService::hDialogsList(const Value& p, const CancelFlag& cancel) { std::string folder = js::getStr(p, "folder"); int offset = std::max(0, js::getInt(p, "offset", 0)); tapi::IterDialogsOptions o; // no limit: every dialog of the account (the UI scrolls a list, the daemon does not truncate it) if (folder == "archive") o.archived = true; else if (!folder.empty() && folder != "all" && folder.find_first_not_of("0123456789") == std::string::npos) o.filter_id = std::stoi(folder); else o.archived = false; bool stopped = false; auto list = allDialogs(client_->dialogs(), o, cancel, stopped); if (stopped) return RpcResult::failure(ErrorCode::Cancelled, "cancelled"); // folder membership: which user folders hold each dialog (rules applied by tapi's own filter, so they match the server's folders) std::map> member; if (o.archived.has_value() && !*o.archived) { for (const auto& f : client_->dialogs().get_filters()) { if (cancel.cancelled()) return RpcResult::failure(ErrorCode::Cancelled, "cancelled"); Folder fo = TapiCodec::folder(f); if (fo.id == 0) continue; tapi::IterDialogsOptions fo_opt; fo_opt.filter_id = fo.id; auto inFolder = allDialogs(client_->dialogs(), fo_opt, cancel, stopped); if (stopped) return RpcResult::failure(ErrorCode::Cancelled, "cancelled"); for (auto& d : inFolder) member[TapiCodec::peerOfEntity(d.entity()).key()].push_back(fo.id); } } Array a; for (size_t i = (size_t)offset; i < list.size(); i++) { Dialog d = TapiCodec::dialog(list[i]); auto it = member.find(d.peer.key()); if (it != member.end()) d.folders = it->second; tapi::DraftEntry dr = list[i].draft(); if (!dr.is_empty()) d.draft = dr.text(); a.push_back(d.toJson()); } Object r; r["dialogs"] = Value(std::move(a)); return RpcResult::success(Value(std::move(r))); } RpcResult TapiService::hDialogsFolders(const Value&, const CancelFlag&) { Array a; for (const auto& f : client_->dialogs().get_filters()) { Folder fo = TapiCodec::folder(f); if (fo.id != 0) a.push_back(fo.toJson()); } Object r; r["folders"] = Value(std::move(a)); return RpcResult::success(Value(std::move(r))); } RpcResult TapiService::hDialogsPin(const Value& p, const CancelFlag&) { tapi::EntityLike e = entity(p, "peer"); auto req = std::make_shared(); auto dp = std::make_shared(); dp->peer = client_->get_input_entity(e); req->pinned = js::getBool(p, "on"); req->peer = dp; client_->invoke(req); return RpcResult::success(Value(Object{})); } RpcResult TapiService::hDialogsMute(const Value& p, const CancelFlag&) { tapi::EntityLike e = entity(p, "peer"); std::int64_t until = js::getId(p, "until"); client_->account().set_mute(e, (tapi::tl::TlDate)until); return RpcResult::success(Value(Object{})); } RpcResult TapiService::hDialogsMarkRead(const Value& p, const CancelFlag&) { client_->messages().send_read_acknowledge(entity(p, "peer"), std::monostate{}, true, true); return RpcResult::success(Value(Object{})); } RpcResult TapiService::hDialogsArchive(const Value& p, const CancelFlag&) { client_->dialogs().edit_folder(entity(p, "peer"), js::getBool(p, "on") ? 1 : 0); return RpcResult::success(Value(Object{})); } RpcResult TapiService::hDialogsDelete(const Value& p, const CancelFlag&) { client_->dialogs().delete_dialog(entity(p, "peer")); return RpcResult::success(Value(Object{})); } void TapiService::registerDialogs() { reg("dialogs.list", &TapiService::hDialogsList); reg("dialogs.folders", &TapiService::hDialogsFolders); reg("dialogs.pin", &TapiService::hDialogsPin); reg("dialogs.mute", &TapiService::hDialogsMute); reg("dialogs.mark_read", &TapiService::hDialogsMarkRead); reg("dialogs.archive", &TapiService::hDialogsArchive); reg("dialogs.delete", &TapiService::hDialogsDelete); } } // namespace mologram