basic chat listing
This commit is contained in:
parent
d5250093a0
commit
80f1df6c6d
|
@ -4,13 +4,14 @@
|
|||
#include <td/telegram/td_api.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chat.h>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
#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(std::map<int64_t, std::string>)>);
|
||||
|
||||
void getLastMessages(int64_t chatid, std::function<void(const std::vector<std::shared_ptr<Message>>&)>);
|
||||
|
||||
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<void(int64_t)> onDone = {}, std::function<void(td_api::object_ptr<td_api::message>)> forMessage = {});
|
||||
void requestMessages(int64_t chatid, int64_t from_message_id = 0, std::function<void(int64_t)> onDone = {}, std::function<void(td_api::object_ptr<td_api::message>)> forMessage = {}, bool recurse = true);
|
||||
|
||||
//helper
|
||||
void iterate(const td_api::array<td_api::object_ptr<td_api::message>>& messages, std::function<void(const td_api::message&)> handler);
|
||||
|
|
|
@ -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<td_api::chat> chat);
|
||||
void handleChatMessages(const std::vector<std::shared_ptr<Message>>& msgs);
|
||||
|
||||
void switchToView(ViewMode vm);
|
||||
|
||||
|
@ -42,6 +44,8 @@ private:
|
|||
ViewChat* viewChat = nullptr;
|
||||
|
||||
std::vector<SlimChat> chats;
|
||||
std::vector<std::shared_ptr<Message>> messages;
|
||||
|
||||
bool shouldRun = false;
|
||||
std::thread tuiThread;
|
||||
};
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#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<std::shared_ptr<Message>>& messages);
|
||||
|
||||
private:
|
||||
const SlimChat* chat = nullptr;
|
||||
|
||||
std::vector<std::shared_ptr<Message>> messages;
|
||||
};
|
||||
|
|
|
@ -268,6 +268,18 @@ void TGClient::getAllTextMessages(int64_t chatid, std::function<void(std::map<in
|
|||
});
|
||||
}
|
||||
|
||||
void TGClient::getLastMessages(int64_t chatid, std::function<void(const std::vector<std::shared_ptr<Message>>&)> f) {
|
||||
std::vector<std::shared_ptr<Message>>* vec = new std::vector<std::shared_ptr<Message>>();
|
||||
requestMessages(chatid, 0, [vec, f](int64_t chat) {
|
||||
f(*vec);
|
||||
delete vec;
|
||||
}, [vec](objptr<td_api::message> msg) {
|
||||
std::shared_ptr<Message> msgOut = std::make_shared<Message>();
|
||||
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<td_api::downloadFile>(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<void(int64_t)> onDone, std::function<void(td_api::object_ptr<td_api::message>)> forMessage) {
|
||||
void TGClient::requestMessages(int64_t chatid, int64_t from_message_id, std::function<void(int64_t)> onDone, std::function<void(td_api::object_ptr<td_api::message>)> forMessage, bool recurse) {
|
||||
Log::debug << "requestMessages " << chatid << " from: " << from_message_id;
|
||||
send_wrappedquery<td_api::messages>(td_api::make_object<td_api::getChatHistory>(chatid, from_message_id, 0, 100, false), [this, chatid, onDone, forMessage](objptr<td_api::messages> m) {
|
||||
send_wrappedquery<td_api::messages>(td_api::make_object<td_api::getChatHistory>(chatid, from_message_id, 0, 100, false), [this, chatid, onDone, forMessage, recurse](objptr<td_api::messages> m) {
|
||||
//request next chunk
|
||||
td_api::array<td_api::object_ptr<td_api::message>>& 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<int64_t>::max()) {
|
||||
if(shouldrun && smallestid != std::numeric_limits<int64_t>::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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -120,6 +120,11 @@ void TgTUI::handleNewChat(objptr<td_api::chat> chat) {
|
|||
chats.push_back({chat->id_, chat->title_});
|
||||
}
|
||||
|
||||
void TgTUI::handleChatMessages(const std::vector<std::shared_ptr<Message>>& 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) {
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
#include "viewchat.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <ncurses.h>
|
||||
|
||||
#include <Log.h>
|
||||
|
||||
#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<Message> 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<int>(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<std::shared_ptr<Message>>& msgs) {
|
||||
// TODO: force a repaint somehow?
|
||||
|
||||
messages = msgs;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue