#include "mologram/ui/StoryTools.hpp" #include "mologram/ui/dialogs/DialogUtil.hpp" #include "mologram/ui/dialogs/ExtrasPanels.hpp" namespace mologram::ui { using namespace ttgui; const std::vector& StoryTools::privacyLabels() { static const std::vector v = {"Everyone", "My contacts", "Close friends", "Only me"}; return v; } std::string StoryTools::privacyValue(int i) { static const char* v[] = {"everyone", "contacts", "close_friends", "self"}; return v[std::max(0, std::min(i, 3))]; // an unknown index never widens the audience: it stays within the four values } std::vector StoryTools::periodLabels(const PremiumModel& premium, bool ascii) { return {"6 hours", "12 hours", "24 hours", premium.label("2 days", ascii)}; } int StoryTools::periodSeconds(int i) { static const int v[] = {21600, 43200, 86400, 172800}; return v[std::max(0, std::min(i, 3))]; } const char* StoryTools::keyHelp() { return "v views z stealth r react l link k keep x delete"; } namespace { /// Premium refusal of a story action (FR-64): flip the state and explain; every other error goes to the common error sink. ServiceClient::Failed premiumAware(UiContext& c, const std::string& what) { UiContext* cp = &c; return [cp, what](const RpcError& e) { if (e.code == ErrorCode::PremiumRequired) cp->premium.refused("story_extras"); else if (cp->client.onError) cp->client.onError(what, e); }; } js::Object storyParams(const std::string& peerKey, std::int64_t id) { js::Object p; p["peer"] = js::Value(peerKey); p["id"] = js::Value((int)id); // story ids are 32 bit numbers return p; } } // namespace void StoryTools::showViews(UiContext& c, const std::string& peerKey, std::int64_t id) { UiContext* cp = &c; c.premium.use("story_extras", [cp, peerKey, id]() { js::Object p = storyParams(peerKey, id); cp->client.request("load story views", "stories.view_list", std::move(p), [cp](const RpcResult& r) { auto f = cp->kit.open("Story views", "StoryViews", 60, 18); std::string body = BusinessPanel::describe(r.value); cp->kit.text(f, body == "not set" ? "Nobody has viewed this story yet." : body, 1); Window* w = f.win; auto* row = cp->kit.row(f); cp->kit.rowButton(row, cp->t("btn.close"), [cp, w]() { cp->kit.close(w); }); cp->kit.show(f, nullptr); }, premiumAware(*cp, "load story views")); }); } void StoryTools::stealth(UiContext& c) { UiContext* cp = &c; c.premium.use("story_extras", [cp]() { js::Object p; p["past"] = js::Value(true); p["future"] = js::Value(true); cp->client.request("start stealth mode", "stories.stealth", std::move(p), [cp](const RpcResult&) { if (cp->toast) cp->toast("Stories", "Stealth mode is on: others will not see that you viewed their stories."); }, premiumAware(*cp, "start stealth mode")); }); } void StoryTools::react(UiContext& c, const std::string& peerKey, std::int64_t id, const std::string& emoji) { UiContext* cp = &c; js::Object p = storyParams(peerKey, id); js::Object r; r["emoji"] = js::Value(emoji); p["reaction"] = js::Value(std::move(r)); c.client.request("react to the story", "stories.react", std::move(p), [cp](const RpcResult&) { if (cp->toast) cp->toast("Stories", "Reaction sent."); }); } void StoryTools::copyLink(UiContext& c, const std::string& peerKey, std::int64_t id) { UiContext* cp = &c; c.client.request("get the story link", "stories.link", storyParams(peerKey, id), [cp](const RpcResult& r) { std::string link = js::getStr(r.value, "link"); if (cp->controller.copyToClipboard) cp->controller.copyToClipboard(link); if (cp->toast) cp->toast("Stories", "Link copied: " + link); }); } void StoryTools::keepOnProfile(UiContext& c, const std::string& peerKey, std::int64_t id) { UiContext* cp = &c; js::Object p; p["peer"] = js::Value(peerKey); p["ids"] = js::Value(js::Array{js::Value((int)id)}); p["pinned"] = js::Value(true); c.client.request("keep the story on the profile", "stories.pin", std::move(p), [cp](const RpcResult&) { if (cp->toast) cp->toast("Stories", "The story stays on your profile."); }); } void StoryTools::confirmDelete(UiContext& c, const std::string& peerKey, std::int64_t id) { UiContext* cp = &c; auto f = c.kit.open("Delete story", "StoryDeleteConfirm", 46, 8); c.kit.label(f, "Delete this story for everyone? This cannot be undone."); Window* w = f.win; auto* row = c.kit.row(f); c.kit.rowButton(row, "Delete", [cp, w, peerKey, id]() { cp->kit.close(w); js::Object p; p["peer"] = js::Value(peerKey); p["ids"] = js::Value(js::Array{js::Value((int)id)}); cp->client.request("delete the story", "stories.delete", std::move(p), [cp](const RpcResult&) { if (cp->toast) cp->toast("Stories", "Story deleted."); }); }); c.kit.rowButton(row, c.t("btn.cancel"), [cp, w]() { cp->kit.close(w); }); c.kit.show(f, nullptr); } bool StoryTools::handleKey(UiContext& c, const KeyEventData& k, const media::ViewerItem& it) { if (!k.isChar() || k.ctrl || k.alt || it.peerKey.empty() || it.storyId == 0) return false; if (k.text == "v") { showViews(c, it.peerKey, it.storyId); return true; } if (k.text == "z") { stealth(c); return true; } if (k.text == "r") { react(c, it.peerKey, it.storyId, "\xE2\x9D\xA4"); return true; } if (k.text == "l") { copyLink(c, it.peerKey, it.storyId); return true; } if (k.text == "k") { keepOnProfile(c, it.peerKey, it.storyId); return true; } if (k.text == "x") { confirmDelete(c, it.peerKey, it.storyId); return true; } return false; } } // namespace mologram::ui