Skip to content
Repos

mologram

C++ 4.1 KB115 lines Opens in site

Directory Raw Download

#include "mologram/ui/TgAdapter.hpp"

#include <algorithm>
#include <cstdio>
#include <ctime>

namespace mologram::ui {
using namespace ttgui;

long long TgAdapter::handle(const Peer& p) { return p.id * 4 + (long long)p.kind; }

Peer TgAdapter::peerOf(long long h) {
  Peer p;
  p.kind = (PeerKind)(h & 3);
  p.id = h >> 2;
  return p;
}

static std::tm localTm(std::int64_t t) {
  std::time_t tt = (std::time_t)t;
  std::tm tm{};
  localtime_r(&tt, &tm);
  return tm;
}

std::string TgAdapter::timeOf(std::int64_t t) {
  std::tm tm = localTm(t);
  char buf[16];
  std::snprintf(buf, sizeof(buf), "%02d:%02d", tm.tm_hour, tm.tm_min);
  return buf;
}

std::string TgAdapter::dayOf(std::int64_t t, std::int64_t now) {
  std::tm a = localTm(t), b = localTm(now);
  if (a.tm_year == b.tm_year && a.tm_yday == b.tm_yday) return "Today";
  std::tm y = localTm(now - 86400);
  if (a.tm_year == y.tm_year && a.tm_yday == y.tm_yday) return "Yesterday";
  char buf[48];
  std::strftime(buf, sizeof(buf), "%d %B %Y", &a);
  return buf;
}

std::string TgAdapter::humanSize(std::int64_t n) {
  static const char* unit[] = {"B", "KB", "MB", "GB", "TB"};
  double v = (double)n;
  int u = 0;
  while (v >= 1024 && u < 4) { v /= 1024; u++; }
  char buf[32];
  if (u == 0) std::snprintf(buf, sizeof(buf), "%lld B", (long long)n);
  else std::snprintf(buf, sizeof(buf), "%.1f %s", v, unit[u]);
  return buf;
}

TgChatRow TgAdapter::row(const Dialog& d, std::int64_t now) {
  TgChatRow r;
  r.id = handle(d.peer);
  r.name = d.name;
  if (!d.typing.empty()) r.preview = d.typing + " is typing";
  else if (!d.draft.empty()) r.preview = "Draft: " + d.draft;
  else r.preview = d.preview;
  std::tm a = localTm(d.date), b = localTm(now);
  if (d.date == 0) r.time = "";
  else if (a.tm_year == b.tm_year && a.tm_yday == b.tm_yday) r.time = timeOf(d.date);
  else { char buf[16]; std::strftime(buf, sizeof(buf), "%d %b", &a); r.time = buf; }
  r.unread = d.unread; r.pinned = d.pinned; r.muted = d.muted; r.verified = d.verified; r.online = d.online;
  return r;
}

TgMessage TgAdapter::message(const Message& m, std::int64_t now, const Lookup& byId) {
  TgMessage t;
  t.id = m.id;
  t.senderId = (unsigned)(m.sender.id & 0x7fffffff);
  t.sender = m.senderName;
  t.text = m.text;
  t.time = timeOf(m.date);
  t.day = dayOf(m.date, now);
  t.outgoing = m.outgoing;
  t.state = m.outgoing ? (m.read ? 2 : 1) : 1;
  t.edited = m.edited;
  t.fwdFrom = m.forwardFrom;
  t.replyTo = m.replyTo;
  if (m.replyTo != 0 && byId) {
    if (const Message* q = byId(m.replyTo)) { t.replyName = q->senderName; t.replyText = q->text.empty() ? "[media]" : q->text; }
  }
  for (const auto& r : m.reactions) t.reactions.push_back({r.isCustom() ? std::string("\xE2\xAD\x90") : r.emoji, r.count, r.mine});
  const MediaRef& md = m.media;
  std::string tag;
  switch (md.kind) {
    case MediaKind::Photo: t.mediaKind = 1; t.mediaId = md.token; t.mediaInfo = md.fileName; break;
    case MediaKind::Video: case MediaKind::Gif: t.mediaKind = 2; t.mediaId = md.token; t.mediaInfo = md.fileName + (md.duration ? " " + std::to_string(md.duration) + "s" : ""); break;
    case MediaKind::Voice: case MediaKind::Audio:
      t.voiceSecs = md.duration; t.mediaId = md.token;
      for (int w : md.waveform) t.waveform.push_back(std::clamp(w, 0, 31) * 100 / 31);
      break;
    case MediaKind::Document: tag = "[file] " + md.fileName + " (" + humanSize(md.size) + ")"; break;
    case MediaKind::Sticker: tag = md.animated ? "[animated sticker]" : "[sticker]"; break;
    case MediaKind::Location: tag = "[location]"; break;
    case MediaKind::Contact: tag = "[contact]"; break;
    default: break;
  }
  if (!tag.empty()) t.text = t.text.empty() ? tag : tag + "\n" + t.text;
  if (m.hasPoll) {
    t.pollQuestion = m.poll.question;
    t.pollMulti = m.poll.multiple;
    for (const auto& o : m.poll.options) t.poll.push_back({o.text, o.votes, o.chosen});
  }
  for (const auto& row : m.buttons) {
    std::vector<TgButton> r;
    for (const auto& b : row) r.push_back({b.text, b.data.empty() ? b.url : b.data});
    t.keyboard.push_back(std::move(r));
  }
  return t;
}

} // namespace mologram::ui
35.7ms