Skip to content
Repos

mologram

C++ 4.2 KB85 lines Opens in site

Directory Raw Download

#include "mologram/ui/ServiceClient.hpp"

namespace mologram::ui {

RequestId ServiceClient::request(const std::string& what, const std::string& method, js::Object params, Done ok, Failed failed, const std::string& feature) {
  std::shared_ptr<bool> token = current_ ? current_ : (scopeProvider ? scopeProvider() : nullptr);
  return svc_.call(method, js::Value(std::move(params)), [this, what, feature, token, ok = std::move(ok), failed = std::move(failed)](const RpcResult& r) {
    ui_.post([this, what, feature, token, ok, failed, r]() {
      if (!r.ok && onFailure) onFailure(what, r.error);
      if (!r.ok && r.error.code == ErrorCode::PremiumRequired && onPremiumRefused) onPremiumRefused(feature);  // FR-64: the state flips even if the asker is gone
      if (token && !*token) return;  // the dialog or session that asked no longer exists
      std::shared_ptr<bool> saved = current_;
      current_ = token;  // requests made from this reply belong to the same asker
      if (r.ok) { if (ok) ok(r); }
      else if (failed) failed(r.error);
      else if (onError && !(r.error.code == ErrorCode::PremiumRequired && onPremiumRefused)) onError(what, r.error);
      current_ = saved;
    });
  });
}

void ServiceClient::hello(std::function<void(const std::string&, const std::string&, const std::string&)> ok) {
  request("connect", "hello", {}, [ok](const RpcResult& r) { ok(js::getStr(r.value, "premium"), js::getStr(r.value, "account"), js::getStr(r.value, "auth")); });
}

void ServiceClient::dialogs(const std::string& folder, std::function<void(std::vector<Dialog>)> ok) {
  js::Object p;
  if (!folder.empty()) p["folder"] = js::Value(folder);
  request("load chats", "dialogs.list", std::move(p), [ok](const RpcResult& r) {
    std::vector<Dialog> v;
    if (const js::Array* a = js::getArr(r.value, "dialogs")) for (const auto& e : *a) v.push_back(Dialog::fromJson(e));
    ok(std::move(v));
  });
}

void ServiceClient::folders(std::function<void(std::vector<Folder>)> ok) {
  request("load folders", "dialogs.folders", {}, [ok](const RpcResult& r) {
    std::vector<Folder> v;
    if (const js::Array* a = js::getArr(r.value, "folders")) for (const auto& e : *a) v.push_back(Folder::fromJson(e));
    ok(std::move(v));
  });
}

void ServiceClient::history(const Peer& peer, std::int64_t offsetId, const std::string& direction, int count, std::function<void(std::vector<Message>)> ok) {
  js::Object p;
  p["peer"] = peer.toJson(); p["offset_id"] = js::id(offsetId); p["direction"] = js::Value(direction); p["count"] = js::Value(count);
  request("load messages", "messages.history", std::move(p), [ok](const RpcResult& r) {
    std::vector<Message> v;
    if (const js::Array* a = js::getArr(r.value, "messages")) for (const auto& e : *a) v.push_back(Message::fromJson(e));
    ok(std::move(v));
  });
}

void ServiceClient::send(const Peer& peer, const std::string& text, std::int64_t replyTo, std::function<void(Message)> ok) {
  js::Object p;
  p["peer"] = peer.toJson(); p["text"] = js::Value(text);
  if (replyTo != 0) p["reply_to"] = js::id(replyTo);
  request("send message", "messages.send", std::move(p), [ok](const RpcResult& r) { ok(Message::fromJson(r.value)); });
}

void ServiceClient::edit(const Peer& peer, std::int64_t id, const std::string& text, std::function<void(Message)> ok) {
  js::Object p;
  p["peer"] = peer.toJson(); p["id"] = js::id(id); p["text"] = js::Value(text);
  request("edit message", "messages.edit", std::move(p), [ok](const RpcResult& r) { ok(Message::fromJson(r.value)); });
}

void ServiceClient::remove(const Peer& peer, std::int64_t id, bool revoke, std::function<void()> ok) {
  js::Object p;
  p["peer"] = peer.toJson(); p["ids"] = js::Value(js::Array{js::id(id)}); p["revoke"] = js::Value(revoke);
  request("delete message", "messages.delete", std::move(p), [ok](const RpcResult&) { if (ok) ok(); });
}

void ServiceClient::markRead(const Peer& peer) {
  js::Object p;
  p["peer"] = peer.toJson();
  request("mark read", "dialogs.mark_read", std::move(p), [](const RpcResult&) {});
}

void ServiceClient::peerInfo(const Peer& peer, std::function<void(PeerInfo)> ok) {
  js::Object p;
  p["peer"] = peer.toJson();
  request("load profile", "peer.info", std::move(p), [ok](const RpcResult& r) { ok(PeerInfo::fromJson(r.value)); });
}

} // namespace mologram::ui
35.3ms