init
This commit is contained in:
545
src/app_state.cpp
Normal file
545
src/app_state.cpp
Normal file
@@ -0,0 +1,545 @@
|
||||
#include "app.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
namespace telegram_tui {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string format_download_progress(std::int64_t downloaded_size, std::int64_t size_bytes, bool is_downloaded) {
|
||||
if (is_downloaded) {
|
||||
return "100%";
|
||||
}
|
||||
if (size_bytes > 0) {
|
||||
const auto downloaded = std::min(downloaded_size, size_bytes);
|
||||
return std::to_string(static_cast<int>((downloaded * 100) / size_bytes)) + "% " +
|
||||
format_file_size(downloaded) + "/" + format_file_size(size_bytes);
|
||||
}
|
||||
return format_file_size(downloaded_size);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool App::process_updates() {
|
||||
bool changed = false;
|
||||
while (true) {
|
||||
auto update = td_.receive(0.0);
|
||||
if (!update.has_value()) {
|
||||
break;
|
||||
}
|
||||
changed = true;
|
||||
handle_td_object(*update);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
void App::handle_td_object(const json& object) {
|
||||
const std::string type = safe_string(object, "@type");
|
||||
if (type == "updateAuthorizationState") {
|
||||
authorization_state_ = object.value("authorization_state", json::object());
|
||||
handle_authorization_state();
|
||||
return;
|
||||
}
|
||||
if (type == "updateConnectionState") {
|
||||
status_line_ = "Connection: " + safe_string(object.at("state"), "@type");
|
||||
return;
|
||||
}
|
||||
if (type == "updateChatOnlineMemberCount") {
|
||||
ChatInfo& chat = chats_[safe_i64(object, "chat_id")];
|
||||
chat.id = safe_i64(object, "chat_id");
|
||||
chat.online_member_count = safe_i32(object, "online_member_count");
|
||||
chat.has_online_member_count = true;
|
||||
return;
|
||||
}
|
||||
if (type == "updateUser") {
|
||||
upsert_user(object.value("user", json::object()));
|
||||
return;
|
||||
}
|
||||
if (type == "updateUserStatus") {
|
||||
update_user_status(safe_i64(object, "user_id"), object.value("status", json::object()));
|
||||
return;
|
||||
}
|
||||
if (type == "updateBasicGroup" || type == "basicGroup") {
|
||||
upsert_basic_group(type == "basicGroup" ? object : object.value("basic_group", json::object()));
|
||||
return;
|
||||
}
|
||||
if (type == "updateSupergroup" || type == "supergroup") {
|
||||
upsert_supergroup(type == "supergroup" ? object : object.value("supergroup", json::object()));
|
||||
return;
|
||||
}
|
||||
if (type == "updateNewChat") {
|
||||
upsert_chat(object.value("chat", json::object()));
|
||||
return;
|
||||
}
|
||||
if (type == "updateChatTitle") {
|
||||
ChatInfo& chat = chats_[safe_i64(object, "chat_id")];
|
||||
chat.id = safe_i64(object, "chat_id");
|
||||
chat.title = safe_string(object, "title");
|
||||
resort_chats();
|
||||
return;
|
||||
}
|
||||
if (type == "updateChatPosition") {
|
||||
ChatInfo& chat = chats_[safe_i64(object, "chat_id")];
|
||||
chat.id = safe_i64(object, "chat_id");
|
||||
apply_chat_position(chat, object.value("position", json::object()));
|
||||
resort_chats();
|
||||
return;
|
||||
}
|
||||
if (type == "updateChatLastMessage") {
|
||||
ChatInfo& chat = chats_[safe_i64(object, "chat_id")];
|
||||
chat.id = safe_i64(object, "chat_id");
|
||||
chat.last_message_preview = preview_message(object.value("last_message", json::object()));
|
||||
if (object.contains("positions") && object.at("positions").is_array()) {
|
||||
chat.in_main_list = false;
|
||||
chat.main_order = 0;
|
||||
for (const auto& position : object.at("positions")) {
|
||||
apply_chat_position(chat, position);
|
||||
}
|
||||
}
|
||||
resort_chats();
|
||||
return;
|
||||
}
|
||||
if (type == "updateChatReadInbox") {
|
||||
ChatInfo& chat = chats_[safe_i64(object, "chat_id")];
|
||||
chat.id = safe_i64(object, "chat_id");
|
||||
chat.unread_count = safe_i32(object, "unread_count");
|
||||
chat.last_read_inbox_message_id = safe_i64(object, "last_read_inbox_message_id");
|
||||
return;
|
||||
}
|
||||
if (type == "updateChatReadOutbox") {
|
||||
ChatInfo& chat = chats_[safe_i64(object, "chat_id")];
|
||||
chat.id = safe_i64(object, "chat_id");
|
||||
chat.last_read_outbox_message_id = safe_i64(object, "last_read_outbox_message_id");
|
||||
return;
|
||||
}
|
||||
if (type == "updateNewMessage") {
|
||||
const json message = object.value("message", json::object());
|
||||
const std::int64_t chat_id = safe_i64(message, "chat_id");
|
||||
if (chat_id == open_chat_id_ && message_scroll_ > 0) {
|
||||
++message_scroll_;
|
||||
}
|
||||
const MessageInfo parsed = parse_message(message);
|
||||
append_message(chat_id, parsed);
|
||||
chats_[chat_id].last_message_preview = preview_message(message);
|
||||
if (chat_id == open_chat_id_ && !parsed.is_outgoing) {
|
||||
mark_message_as_read(chat_id, parsed.id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (type == "updateMessageContent") {
|
||||
const std::int64_t chat_id = safe_i64(object, "chat_id");
|
||||
const std::int64_t message_id = safe_i64(object, "message_id");
|
||||
auto chat_it = chats_.find(chat_id);
|
||||
if (chat_it == chats_.end()) {
|
||||
return;
|
||||
}
|
||||
for (auto& message : chat_it->second.messages) {
|
||||
if (message.id == message_id) {
|
||||
const json content = object.value("new_content", json::object());
|
||||
message.text = content_to_text(content, true);
|
||||
if (const auto attachment = parse_attachment(content); attachment.has_value()) {
|
||||
message.has_attachment = true;
|
||||
message.attachment = *attachment;
|
||||
} else {
|
||||
message.has_attachment = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (type == "updateDeleteMessages") {
|
||||
const std::int64_t chat_id = safe_i64(object, "chat_id");
|
||||
auto chat_it = chats_.find(chat_id);
|
||||
if (chat_it == chats_.end()) {
|
||||
return;
|
||||
}
|
||||
std::set<std::int64_t> deleted;
|
||||
for (const auto& id : object.value("message_ids", json::array())) {
|
||||
if (id.is_number_integer()) {
|
||||
deleted.insert(id.get<std::int64_t>());
|
||||
}
|
||||
}
|
||||
std::vector<MessageInfo> kept;
|
||||
kept.reserve(chat_it->second.messages.size());
|
||||
for (const auto& message : chat_it->second.messages) {
|
||||
if (deleted.find(message.id) == deleted.end()) {
|
||||
kept.push_back(message);
|
||||
}
|
||||
}
|
||||
chat_it->second.messages = std::move(kept);
|
||||
return;
|
||||
}
|
||||
if (type == "updateFile") {
|
||||
update_attachment_file(object.value("file", json::object()));
|
||||
return;
|
||||
}
|
||||
if (type == "updateOption" || type == "ok" || type == "userFullInfo" ||
|
||||
type == "updateHavePendingNotifications" || type == "updateUnreadMessageCount" ||
|
||||
type == "updateUnreadChatCount") {
|
||||
return;
|
||||
}
|
||||
if (type == "error") {
|
||||
const std::string extra = safe_string(object, "@extra");
|
||||
const std::string message = safe_string(object, "message");
|
||||
if (extra.rfind("history:", 0) == 0) {
|
||||
const auto chat_id = static_cast<std::int64_t>(std::stoll(extra.substr(8)));
|
||||
chats_[chat_id].history_loading = false;
|
||||
status_line_ = "History unavailable: " + message;
|
||||
return;
|
||||
}
|
||||
status_line_ = "TDLib error: " + message;
|
||||
return;
|
||||
}
|
||||
if (type == "user") {
|
||||
upsert_user(object);
|
||||
return;
|
||||
}
|
||||
if (type == "chat") {
|
||||
upsert_chat(object);
|
||||
return;
|
||||
}
|
||||
if (type == "chats") {
|
||||
const std::string extra = safe_string(object, "@extra");
|
||||
if (extra == "main_chats") {
|
||||
sync_chat_ids_from_response(object);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (type == "messages") {
|
||||
const std::string extra = safe_string(object, "@extra");
|
||||
if (extra.rfind("history:", 0) == 0) {
|
||||
const auto chat_id = static_cast<std::int64_t>(std::stoll(extra.substr(8)));
|
||||
merge_history(chat_id, object.value("messages", json::array()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (type == "updateMessageSendSucceeded") {
|
||||
const json message = object.value("message", json::object());
|
||||
const std::int64_t chat_id = safe_i64(message, "chat_id");
|
||||
remove_message(chat_id, safe_i64(object, "old_message_id"));
|
||||
append_message(chat_id, parse_message(message));
|
||||
chats_[chat_id].last_message_preview = preview_message(message);
|
||||
status_line_ = "Message sent.";
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string extra = safe_string(object, "@extra");
|
||||
if (extra == "getMe" && object.is_object()) {
|
||||
my_user_id_ = safe_i64(object, "id");
|
||||
}
|
||||
}
|
||||
|
||||
void App::request_more_chats() {
|
||||
td_.send({
|
||||
{"@type", "getChats"},
|
||||
{"chat_list", {{"@type", "chatListMain"}}},
|
||||
{"limit", kChatPageSize},
|
||||
{"@extra", "main_chats"},
|
||||
});
|
||||
status_line_ = "Loading chats...";
|
||||
}
|
||||
|
||||
bool App::request_chat_history(std::int64_t chat_id, bool force) {
|
||||
ChatInfo& chat = chats_[chat_id];
|
||||
chat.id = chat_id;
|
||||
if (chat.history_loading && !force) {
|
||||
return false;
|
||||
}
|
||||
chat.history_loading = true;
|
||||
td_.send({
|
||||
{"@type", "getChatHistory"},
|
||||
{"chat_id", chat_id},
|
||||
{"from_message_id", 0},
|
||||
{"offset", 0},
|
||||
{"limit", kHistoryBatchSize},
|
||||
{"only_local", false},
|
||||
{"@extra", "history:" + std::to_string(chat_id)},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
bool App::request_open_chat_history(bool force) {
|
||||
const auto chat_id = open_chat_id();
|
||||
if (!chat_id.has_value()) {
|
||||
return false;
|
||||
}
|
||||
request_chat_details(*chat_id);
|
||||
const bool requested = request_chat_history(*chat_id, force);
|
||||
if (requested) {
|
||||
status_line_ = force ? "Reloading chat history..." : "Loading chat history...";
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
|
||||
void App::mark_chat_messages_as_read(std::int64_t chat_id) {
|
||||
auto chat_it = chats_.find(chat_id);
|
||||
if (chat_it == chats_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::int64_t> unread_message_ids;
|
||||
unread_message_ids.reserve(chat_it->second.messages.size());
|
||||
for (const auto& message : chat_it->second.messages) {
|
||||
if (message.is_outgoing || message.id == 0) {
|
||||
continue;
|
||||
}
|
||||
if (message.id <= chat_it->second.last_read_inbox_message_id) {
|
||||
continue;
|
||||
}
|
||||
unread_message_ids.push_back(message.id);
|
||||
}
|
||||
|
||||
if (!unread_message_ids.empty()) {
|
||||
td_.send({
|
||||
{"@type", "viewMessages"},
|
||||
{"chat_id", chat_id},
|
||||
{"message_ids", unread_message_ids},
|
||||
{"source", {{"@type", "messageSourceChatHistory"}}},
|
||||
{"force_read", true},
|
||||
});
|
||||
}
|
||||
|
||||
td_.send({
|
||||
{"@type", "toggleChatIsMarkedAsUnread"},
|
||||
{"chat_id", chat_id},
|
||||
{"is_marked_as_unread", false},
|
||||
});
|
||||
}
|
||||
|
||||
void App::mark_message_as_read(std::int64_t chat_id, std::int64_t message_id) {
|
||||
if (chat_id == 0 || message_id == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
td_.send({
|
||||
{"@type", "viewMessages"},
|
||||
{"chat_id", chat_id},
|
||||
{"message_ids", json::array({message_id})},
|
||||
{"source", {{"@type", "messageSourceChatHistory"}}},
|
||||
{"force_read", true},
|
||||
});
|
||||
}
|
||||
|
||||
void App::set_open_chat(std::int64_t chat_id) {
|
||||
if (chat_id == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool changed_chat = open_chat_id_ != chat_id;
|
||||
ChatInfo& chat = chats_[chat_id];
|
||||
chat.id = chat_id;
|
||||
if (tdlib_open_chat_id_ != 0 && tdlib_open_chat_id_ != chat_id) {
|
||||
td_.send({
|
||||
{"@type", "closeChat"},
|
||||
{"chat_id", tdlib_open_chat_id_},
|
||||
});
|
||||
}
|
||||
|
||||
open_chat_id_ = chat_id;
|
||||
message_scroll_ = 0;
|
||||
|
||||
if (tdlib_open_chat_id_ != chat_id) {
|
||||
td_.send({
|
||||
{"@type", "openChat"},
|
||||
{"chat_id", chat_id},
|
||||
});
|
||||
tdlib_open_chat_id_ = chat_id;
|
||||
}
|
||||
|
||||
mark_chat_messages_as_read(chat_id);
|
||||
|
||||
if (changed_chat) {
|
||||
request_chat_details(chat_id);
|
||||
const bool should_request_history =
|
||||
auto_reload_chat_history_ || !chat.history_loaded || chat.messages.empty();
|
||||
const bool requested = should_request_history &&
|
||||
request_chat_history(chat_id, auto_reload_chat_history_);
|
||||
if (requested) {
|
||||
status_line_ = auto_reload_chat_history_ && chat.history_loaded
|
||||
? "Reloading chat history..."
|
||||
: "Loading chat history...";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void App::sync_chat_ids_from_response(const json& response) {
|
||||
if (!response.contains("chat_ids") || !response.at("chat_ids").is_array()) {
|
||||
status_line_ = "Chat list response missing chat ids.";
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::int64_t> chat_ids;
|
||||
for (const auto& entry : response.at("chat_ids")) {
|
||||
if (!entry.is_number_integer()) {
|
||||
continue;
|
||||
}
|
||||
const auto chat_id = entry.get<std::int64_t>();
|
||||
chat_ids.push_back(chat_id);
|
||||
if (chats_.find(chat_id) == chats_.end()) {
|
||||
td_.send({
|
||||
{"@type", "getChat"},
|
||||
{"chat_id", chat_id},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!chat_ids.empty()) {
|
||||
sorted_chat_ids_ = std::move(chat_ids);
|
||||
if (selected_chat_index_ >= static_cast<int>(sorted_chat_ids_.size())) {
|
||||
selected_chat_index_ = std::max(0, static_cast<int>(sorted_chat_ids_.size()) - 1);
|
||||
}
|
||||
if (!open_chat_id().has_value()) {
|
||||
const auto chat_id = highlighted_chat_id();
|
||||
if (chat_id.has_value()) {
|
||||
set_open_chat(*chat_id);
|
||||
}
|
||||
}
|
||||
request_open_chat_history(false);
|
||||
status_line_ = "Chats loaded.";
|
||||
return;
|
||||
}
|
||||
|
||||
status_line_ = "No chats loaded.";
|
||||
}
|
||||
|
||||
void App::append_message(std::int64_t chat_id, MessageInfo message) {
|
||||
ChatInfo& chat = chats_[chat_id];
|
||||
chat.id = chat_id;
|
||||
auto existing = std::find_if(chat.messages.begin(), chat.messages.end(),
|
||||
[&](const MessageInfo& item) { return item.id == message.id; });
|
||||
if (existing != chat.messages.end()) {
|
||||
*existing = std::move(message);
|
||||
} else {
|
||||
chat.messages.push_back(std::move(message));
|
||||
}
|
||||
|
||||
std::sort(chat.messages.begin(), chat.messages.end(),
|
||||
[](const MessageInfo& lhs, const MessageInfo& rhs) {
|
||||
if (lhs.date != rhs.date) {
|
||||
return lhs.date < rhs.date;
|
||||
}
|
||||
return lhs.id < rhs.id;
|
||||
});
|
||||
|
||||
if (kMaxMessagesPerChat > 0 && chat.messages.size() > kMaxMessagesPerChat) {
|
||||
chat.messages.erase(
|
||||
chat.messages.begin(),
|
||||
chat.messages.begin() +
|
||||
static_cast<std::ptrdiff_t>(chat.messages.size() - kMaxMessagesPerChat));
|
||||
}
|
||||
}
|
||||
|
||||
void App::remove_message(std::int64_t chat_id, std::int64_t message_id) {
|
||||
if (message_id == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto chat_it = chats_.find(chat_id);
|
||||
if (chat_it == chats_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& messages = chat_it->second.messages;
|
||||
messages.erase(
|
||||
std::remove_if(
|
||||
messages.begin(),
|
||||
messages.end(),
|
||||
[&](const MessageInfo& item) { return item.id == message_id; }),
|
||||
messages.end());
|
||||
}
|
||||
|
||||
void App::merge_history(std::int64_t chat_id, const json& messages) {
|
||||
ChatInfo& chat = chats_[chat_id];
|
||||
chat.history_loading = false;
|
||||
chat.history_loaded = true;
|
||||
for (const auto& message : messages) {
|
||||
append_message(chat_id, parse_message(message));
|
||||
}
|
||||
if (chat_id == open_chat_id_) {
|
||||
mark_chat_messages_as_read(chat_id);
|
||||
}
|
||||
status_line_ = "History loaded.";
|
||||
}
|
||||
|
||||
void App::update_attachment_file(const json& file) {
|
||||
const std::int32_t file_id = safe_i32(file, "id");
|
||||
if (file_id == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::int64_t size_bytes = std::max(safe_i64(file, "size"), safe_i64(file, "expected_size"));
|
||||
const json local = file.value("local", json::object());
|
||||
const std::string local_path = safe_string(local, "path");
|
||||
const std::int64_t downloaded_size = safe_i64(local, "downloaded_size");
|
||||
const bool is_downloading_active = local.value("is_downloading_active", false);
|
||||
const bool is_downloaded = local.value("is_downloading_completed", false);
|
||||
const bool can_be_downloaded = local.value("can_be_downloaded", false);
|
||||
const bool can_be_deleted = local.value("can_be_deleted", false);
|
||||
|
||||
for (auto& [chat_id, chat] : chats_) {
|
||||
(void) chat_id;
|
||||
for (auto& message : chat.messages) {
|
||||
if (!message.has_attachment || message.attachment.file_id != file_id) {
|
||||
continue;
|
||||
}
|
||||
if (size_bytes > 0) {
|
||||
message.attachment.size_bytes = size_bytes;
|
||||
}
|
||||
message.attachment.downloaded_size = downloaded_size;
|
||||
message.attachment.local_path = local_path;
|
||||
message.attachment.is_downloading_active = is_downloading_active;
|
||||
message.attachment.is_downloaded = is_downloaded;
|
||||
message.attachment.can_be_downloaded = can_be_downloaded;
|
||||
message.attachment.can_be_deleted = can_be_deleted;
|
||||
}
|
||||
}
|
||||
|
||||
if (pending_attachment_open_.has_value() && pending_attachment_open_->file_id == file_id) {
|
||||
pending_attachment_open_->size_bytes = size_bytes > 0 ? size_bytes : pending_attachment_open_->size_bytes;
|
||||
pending_attachment_open_->downloaded_size = downloaded_size;
|
||||
pending_attachment_open_->local_path = local_path;
|
||||
pending_attachment_open_->is_downloading_active = is_downloading_active;
|
||||
pending_attachment_open_->is_downloaded = is_downloaded;
|
||||
pending_attachment_open_->can_be_downloaded = can_be_downloaded;
|
||||
pending_attachment_open_->can_be_deleted = can_be_deleted;
|
||||
if (!is_downloaded && is_downloading_active) {
|
||||
status_line_ = "Downloading attachment to open " +
|
||||
format_download_progress(
|
||||
pending_attachment_open_->downloaded_size,
|
||||
pending_attachment_open_->size_bytes,
|
||||
pending_attachment_open_->is_downloaded);
|
||||
}
|
||||
if (is_downloaded && !local_path.empty()) {
|
||||
open_attachment(*pending_attachment_open_);
|
||||
pending_attachment_open_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
if (pending_attachment_download_.has_value() && pending_attachment_download_->file_id == file_id) {
|
||||
pending_attachment_download_->size_bytes =
|
||||
size_bytes > 0 ? size_bytes : pending_attachment_download_->size_bytes;
|
||||
pending_attachment_download_->downloaded_size = downloaded_size;
|
||||
pending_attachment_download_->local_path = local_path;
|
||||
pending_attachment_download_->is_downloading_active = is_downloading_active;
|
||||
pending_attachment_download_->is_downloaded = is_downloaded;
|
||||
pending_attachment_download_->can_be_downloaded = can_be_downloaded;
|
||||
pending_attachment_download_->can_be_deleted = can_be_deleted;
|
||||
if (!is_downloaded && is_downloading_active) {
|
||||
status_line_ = "Downloading attachment to ~/Downloads " +
|
||||
format_download_progress(
|
||||
pending_attachment_download_->downloaded_size,
|
||||
pending_attachment_download_->size_bytes,
|
||||
pending_attachment_download_->is_downloaded);
|
||||
}
|
||||
if (is_downloaded && !local_path.empty()) {
|
||||
export_attachment_to_downloads(*pending_attachment_download_);
|
||||
pending_attachment_download_.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace telegram_tui
|
||||
Reference in New Issue
Block a user