From 80f1df6c6d1579f45d4c549f7577e1633aadd0fc Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 20 Nov 2023 22:11:07 +0100 Subject: [PATCH] basic chat listing --- inc/tgclient.h | 7 +++++-- inc/tgtui.h | 4 ++++ inc/viewchat.h | 7 +++++++ src/tgclient.cpp | 22 +++++++++++++++++++--- src/tgtui.cpp | 7 +++++++ src/viewchat.cpp | 27 +++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 5 deletions(-) diff --git a/inc/tgclient.h b/inc/tgclient.h index 853bf34..f31cef6 100644 --- a/inc/tgclient.h +++ b/inc/tgclient.h @@ -4,13 +4,14 @@ #include #include -#include #include #include #include #include #include +#include "chat.h" +#include "message.h" #include "userstatus.h" #include "user.h" @@ -81,6 +82,8 @@ public: void indexChat(int64_t chatid); void getAllTextMessages(int64_t chatid, std::function)>); + void getLastMessages(int64_t chatid, std::function>&)>); + void downloadFile(int32_t file_id); void sendTextMessageToSelf(const std::string& text); @@ -144,7 +147,7 @@ private: void checkUserCache(); //wrapped requests - void requestMessages(int64_t chatid, int64_t from_message_id = 0, std::function onDone = {}, std::function)> forMessage = {}); + void requestMessages(int64_t chatid, int64_t from_message_id = 0, std::function onDone = {}, std::function)> forMessage = {}, bool recurse = true); //helper void iterate(const td_api::array>& messages, std::function handler); diff --git a/inc/tgtui.h b/inc/tgtui.h index 7a9f672..d1dec3e 100644 --- a/inc/tgtui.h +++ b/inc/tgtui.h @@ -4,6 +4,7 @@ #include "slimchat.h" #include "tgclient.h" +#include "message.h" class View; class ViewChat; @@ -31,6 +32,7 @@ private: void threadLoop(); void handleNewChat(objptr chat); + void handleChatMessages(const std::vector>& msgs); void switchToView(ViewMode vm); @@ -42,6 +44,8 @@ private: ViewChat* viewChat = nullptr; std::vector chats; + std::vector> messages; + bool shouldRun = false; std::thread tuiThread; }; diff --git a/inc/viewchat.h b/inc/viewchat.h index 7a1ea29..000c333 100644 --- a/inc/viewchat.h +++ b/inc/viewchat.h @@ -1,5 +1,9 @@ #pragma once +#include +#include + +#include "message.h" #include "slimchat.h" #include "view.h" @@ -15,7 +19,10 @@ public: virtual int keyIn(int key) override; + void updateMessages(const std::vector>& messages); + private: const SlimChat* chat = nullptr; + std::vector> messages; }; diff --git a/src/tgclient.cpp b/src/tgclient.cpp index d0a096f..165bc83 100644 --- a/src/tgclient.cpp +++ b/src/tgclient.cpp @@ -268,6 +268,18 @@ void TGClient::getAllTextMessages(int64_t chatid, std::function>&)> f) { + std::vector>* vec = new std::vector>(); + requestMessages(chatid, 0, [vec, f](int64_t chat) { + f(*vec); + delete vec; + }, [vec](objptr msg) { + std::shared_ptr msgOut = std::make_shared(); + convertMessage(*msg, *msgOut); + vec->push_back(std::move(msgOut)); + }, false); +} + void TGClient::downloadFile(int32_t file_id) { Log::info << "start download for file: " << file_id; send_staticquery(td::make_tl_object(file_id, /* prio */ 15, 0, 0, false), HANDLER_NULL); @@ -399,9 +411,9 @@ void TGClient::checkUserCache() { } } -void TGClient::requestMessages(int64_t chatid, int64_t from_message_id, std::function onDone, std::function)> forMessage) { +void TGClient::requestMessages(int64_t chatid, int64_t from_message_id, std::function onDone, std::function)> forMessage, bool recurse) { Log::debug << "requestMessages " << chatid << " from: " << from_message_id; - send_wrappedquery(td_api::make_object(chatid, from_message_id, 0, 100, false), [this, chatid, onDone, forMessage](objptr m) { + send_wrappedquery(td_api::make_object(chatid, from_message_id, 0, 100, false), [this, chatid, onDone, forMessage, recurse](objptr m) { //request next chunk td_api::array>& arr = m->messages_; @@ -430,7 +442,7 @@ void TGClient::requestMessages(int64_t chatid, int64_t from_message_id, std::fun } //start next request - if(shouldrun && smallestid != std::numeric_limits::max()) { + if(shouldrun && smallestid != std::numeric_limits::max() && recurse) { requestMessages(chatid, smallestid, onDone, forMessage); } @@ -442,6 +454,10 @@ void TGClient::requestMessages(int64_t chatid, int64_t from_message_id, std::fun } } } + + if(!recurse && onDone) { + onDone(chatid); + } }); } diff --git a/src/tgtui.cpp b/src/tgtui.cpp index 1d8308a..6d07290 100644 --- a/src/tgtui.cpp +++ b/src/tgtui.cpp @@ -120,6 +120,11 @@ void TgTUI::handleNewChat(objptr chat) { chats.push_back({chat->id_, chat->title_}); } +void TgTUI::handleChatMessages(const std::vector>& msgs) { + messages = msgs; + viewChat->updateMessages(msgs); +} + void TgTUI::switchToView(ViewMode to) { // close old mode @@ -138,6 +143,8 @@ void TgTUI::switchToView(ViewMode to) { viewChat->setChat(&*it); tgclient.openChat(chatid); } + + tgclient.getLastMessages(chatid, std::bind(&TgTUI::handleChatMessages, this, pl::_1)); } switch(to) { diff --git a/src/viewchat.cpp b/src/viewchat.cpp index f17581c..36758b7 100644 --- a/src/viewchat.cpp +++ b/src/viewchat.cpp @@ -1,7 +1,14 @@ #include "viewchat.h" +#include + #include +#include + +#include "message.h" +#include "tgtui.h" + ViewChat::ViewChat(TgTUI& tgtui) : View(tgtui) {} ViewChat::~ViewChat() {} @@ -15,7 +22,21 @@ int64_t ViewChat::getChat() const { } void ViewChat::paint() { + getmaxyx(stdscr, maxRows, maxCols); + ::printw("Chat: %s (%li)\n", chat->name.c_str(), chat->chatId); + + Log::info << "messages: " << messages.size() << " maxRows: " << maxRows; + + for(int row = 0; row < maxRows-1 && row < messages.size(); ++row) { + + + Log::debug << "print msg at: " << row; + std::shared_ptr msg = messages.at(row); + const char direction = ( msg->isOutgoing ? '>' : '<' ); + + ::mvprintw(maxRows - row, 0, "[%lu] %c (%li) %*s\n", msg->sendDate, direction, msg->sender, std::min(msg->text.size(), maxCols), msg->text.c_str()); + } } int ViewChat::keyIn(int key) { @@ -27,3 +48,9 @@ int ViewChat::keyIn(int key) { return -1; } + +void ViewChat::updateMessages(const std::vector>& msgs) { + // TODO: force a repaint somehow? + + messages = msgs; +}