Skip to content
Repos

mologram

C++ 4.7 KB107 lines Opens in site

Directory Raw Download

#include "mologram/ui/media/ProfileMedia.hpp"

#include <algorithm>

#include "mologram/ui/TgAdapter.hpp"
#include "mologram/ui/media/HostTools.hpp"
#include "mologram/ui/media/MediaModule.hpp"

namespace mologram::ui::media {

ProfileMedia::ProfileMedia(MologramApp& app, MediaModule& module) : app_(app), module_(module) {
  auto& pf = *app_.shell().profile;
  pf.onTab.connect([this](int) { refresh(); });
  app_.shell().onChatOpened.connect([this](long long) { refresh(); });
  for (size_t i = 0; i < pf.lists.size() && i < 3; i++) {
    int tab = (int)i;
    pf.lists[i]->onActivated.connect([this, tab](int idx) { activate(tab, idx); });
  }
}

std::string ProfileMedia::firstUrl(const std::string& text) {
  size_t at = text.find("http://");
  size_t s = text.find("https://");
  if (at == std::string::npos || (s != std::string::npos && s < at)) at = s;
  if (at == std::string::npos) return "";
  size_t end = text.find_first_of(" \t\r\n)>\"'", at);
  return text.substr(at, end == std::string::npos ? std::string::npos : end - at);
}

std::string ProfileMedia::entryText(int tab, const Message& m) {
  std::string when = TgAdapter::dayOf(m.date, m.date + 1) == "Today" ? TgAdapter::timeOf(m.date) : TgAdapter::timeOf(m.date);
  if (tab == 0) {
    const char* kind = m.media.kind == MediaKind::Video ? "video" : m.media.kind == MediaKind::Gif ? "gif" : "photo";
    return std::string("[") + kind + "] " + (m.text.empty() ? (m.media.fileName.empty() ? when : m.media.fileName) : m.text);
  }
  if (tab == 1) return (m.media.fileName.empty() ? "file" : m.media.fileName) + " (" + TgAdapter::humanSize(m.media.size) + ")";
  std::string url = firstUrl(m.text);
  return url.empty() ? m.text : url;
}

void ProfileMedia::refresh() {
  Peer cur = app_.controller().current();
  if (!cur.valid()) return;
  if (!(cur == loadedFor_)) {
    loadedFor_ = cur;
    for (auto& t : tabs_) t.clear();
    generation_++;
    for (size_t i = 0; i < app_.shell().profile->lists.size() && i < 3; i++) app_.shell().profile->lists[i]->items.clear();
  }
  loadTab(std::clamp(app_.shell().profile->tabs->active, 0, 2));
}

void ProfileMedia::loadTab(int tab) {
  Peer cur = loadedFor_;
  int gen = generation_;
  if (!cur.valid()) return;
  std::vector<std::string> filters = tab == 0 ? std::vector<std::string>{"photos", "videos"} : tab == 1 ? std::vector<std::string>{"files"} : std::vector<std::string>{"links"};
  auto collected = std::make_shared<std::vector<Message>>();
  auto remaining = std::make_shared<int>((int)filters.size());
  for (const auto& f : filters) {
    js::Object p;
    p["peer"] = cur.toJson(); p["query"] = js::Value(""); p["filter"] = js::Value(f);
    app_.client().request("load shared " + f, "messages.search", std::move(p), [this, tab, gen, collected, remaining](const RpcResult& r) {
      if (gen == generation_) {
        if (const js::Array* a = js::getArr(r.value, "messages")) for (const auto& e : *a) collected->push_back(Message::fromJson(e));
      }
      if (--*remaining == 0 && gen == generation_) {
        std::sort(collected->begin(), collected->end(), [](const Message& x, const Message& y) { return x.id > y.id; });
        tabs_[(size_t)tab] = *collected;
        fill(tab);
      }
    }, [this, remaining, gen, tab, collected](const RpcError& e) {
      if (gen == generation_) app_.app().notify("Could not load shared media", e.text);
      if (--*remaining == 0 && gen == generation_) { std::sort(collected->begin(), collected->end(), [](const Message& x, const Message& y) { return x.id > y.id; }); tabs_[(size_t)tab] = *collected; fill(tab); }
    });
  }
}

void ProfileMedia::fill(int tab) {
  auto& lists = app_.shell().profile->lists;
  if ((size_t)tab >= lists.size()) return;
  lists[(size_t)tab]->items.clear();
  for (const auto& m : tabs_[(size_t)tab]) lists[(size_t)tab]->items.push_back(entryText(tab, m));
}

void ProfileMedia::activate(int tab, int index) {
  if (tab < 0 || tab > 2 || index < 0 || index >= (int)tabs_[(size_t)tab].size()) return;
  const Message& m = tabs_[(size_t)tab][(size_t)index];
  if (tab == 2) {
    std::string url = firstUrl(m.text), err;
    if (url.empty()) return;
    if (!HostTools::openExternal(url, err)) app_.app().notify("Cannot open link", err);
    return;
  }
  if (tab == 1) { module_.openMedia(m); return; }
  std::vector<ViewerItem> items;
  size_t at = 0;
  for (size_t i = 0; i < tabs_[0].size(); i++) {
    const Message& mm = tabs_[0][i];
    if (mm.media.token.empty()) continue;
    if (&mm == &m) at = items.size();
    items.push_back({mm.media.token, mm.text.empty() ? mm.media.fileName : mm.text, mm.media.kind == MediaKind::Video || mm.media.kind == MediaKind::Gif, "", 0});
  }
  module_.openViewer(items, at);
}

}  // namespace mologram::ui::media
36.1ms