Skip to content
Repos

mologram

C++ 3.1 KB96 lines Opens in site

Directory Raw Download

#include "mologram/ui/CallTracker.hpp"

#include "mologram/ui/dialogs/DialogUtil.hpp"

namespace mologram::ui {
using namespace ttgui;

std::string CallTracker::nameOf(const Peer& p) const {
  for (const auto& d : c_.controller.dialogs()) if (d.peer == p) return d.name;
  return p.key();
}

void CallTracker::closePanel() {
  if (panel_ && alive_ && *alive_) c_.kit.close(panel_);
  panel_ = nullptr;
}

void CallTracker::show() {
  closePanel();
  CallPanel::Actions a;
  std::int64_t id = cur_.id;
  UiContext* cp = &c_;
  a.accept = [this, cp, id]() {
    js::Object p;
    p["call"] = js::id(id);
    cp->client.request("accept call", "calls.accept", std::move(p), [this](const RpcResult& r) { adopt(CallModel::fromJson(r.value)); });
  };
  auto discard = [this, cp, id](const char* reason, const char* label) {
    js::Object p;
    p["call"] = js::id(id);
    p["reason"] = js::Value(reason);
    p["duration"] = js::Value(cur_.duration);
    cp->client.request(label, "calls.discard", std::move(p), [this, reason](const RpcResult&) { markEnded(reason); });
  };
  a.decline = [discard]() { discard("hangup", "decline call"); };
  a.hangup = [discard]() { discard("hangup", "hang up"); };
  a.close = [this]() { closePanel(); };
  a.rate = [this, cp, id](int stars) {
    js::Object p;
    p["call"] = js::id(id);
    p["stars"] = js::Value(stars);
    cp->client.request("rate call", "calls.rate", std::move(p), [this](const RpcResult&) { closePanel(); });
  };
  panel_ = CallPanel::open(c_, cur_, nameOf(cur_.user), a);
  alive_ = dlg::track(panel_);
}

void CallTracker::adopt(const CallModel& m) {
  if (m.id == 0) return;
  cur_ = m;
  have_ = true;
  show();
}

void CallTracker::markEnded(const std::string& reason) {
  if (!have_) return;
  cur_.state = "ended";
  if (cur_.endReason.empty()) cur_.endReason = reason;
  show();
}

void CallTracker::start(const Peer& user, bool video) {
  if (!user.valid() || user.kind != PeerKind::User) { if (c_.toast) c_.toast("Call", "Open a chat with a person to call them."); return; }
  if (have_ && !cur_.ended()) { if (c_.toast) c_.toast("Call", "A call is already running."); show(); return; }
  js::Object p;
  p["user"] = user.toJson();
  p["video"] = js::Value(video);
  c_.client.request("start call", "calls.request", std::move(p), [this](const RpcResult& r) { adopt(CallModel::fromJson(r.value)); });
}

void CallTracker::onEvent(const Event& e) {
  if (e.name != "call.state") return;
  const js::Value* cv = e.data.find("call");
  if (!cv) return;
  CallModel m = CallModel::fromJson(*cv);
  if (m.id == 0) return;
  bool sameCall = have_ && cur_.id == m.id;
  if (!sameCall) {
    if (m.ended()) return;  // the end of a call we never showed
    if (have_ && !cur_.ended() && m.incomingRinging()) {  // busy: refuse the second call
      js::Object p;
      p["call"] = js::id(m.id);
      p["reason"] = js::Value("busy");
      c_.client.request("refuse call", "calls.discard", std::move(p), [](const RpcResult&) {});
      return;
    }
    if (m.incomingRinging()) {
      std::string who = nameOf(m.user);
      if (c_.toast) c_.toast("Incoming call", who);
      c_.app.bell.ring("call");
    }
  }
  adopt(m);
}

}  // namespace mologram::ui
29.6ms