basic chat listing
This commit is contained in:
parent
d5250093a0
commit
80f1df6c6d
|
@ -4,13 +4,14 @@
|
||||||
#include <td/telegram/td_api.h>
|
#include <td/telegram/td_api.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chat.h>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
|
#include "chat.h"
|
||||||
|
#include "message.h"
|
||||||
#include "userstatus.h"
|
#include "userstatus.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
|
@ -81,6 +82,8 @@ public:
|
||||||
void indexChat(int64_t chatid);
|
void indexChat(int64_t chatid);
|
||||||
void getAllTextMessages(int64_t chatid, std::function<void(std::map<int64_t, std::string>)>);
|
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 downloadFile(int32_t file_id);
|
||||||
|
|
||||||
void sendTextMessageToSelf(const std::string& text);
|
void sendTextMessageToSelf(const std::string& text);
|
||||||
|
@ -144,7 +147,7 @@ private:
|
||||||
void checkUserCache();
|
void checkUserCache();
|
||||||
|
|
||||||
//wrapped requests
|
//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
|
//helper
|
||||||
void iterate(const td_api::array<td_api::object_ptr<td_api::message>>& messages, std::function<void(const td_api::message&)> handler);
|
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 "slimchat.h"
|
||||||
#include "tgclient.h"
|
#include "tgclient.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
class View;
|
class View;
|
||||||
class ViewChat;
|
class ViewChat;
|
||||||
|
@ -31,6 +32,7 @@ private:
|
||||||
void threadLoop();
|
void threadLoop();
|
||||||
|
|
||||||
void handleNewChat(objptr<td_api::chat> chat);
|
void handleNewChat(objptr<td_api::chat> chat);
|
||||||
|
void handleChatMessages(const std::vector<std::shared_ptr<Message>>& msgs);
|
||||||
|
|
||||||
void switchToView(ViewMode vm);
|
void switchToView(ViewMode vm);
|
||||||
|
|
||||||
|
@ -42,6 +44,8 @@ private:
|
||||||
ViewChat* viewChat = nullptr;
|
ViewChat* viewChat = nullptr;
|
||||||
|
|
||||||
std::vector<SlimChat> chats;
|
std::vector<SlimChat> chats;
|
||||||
|
std::vector<std::shared_ptr<Message>> messages;
|
||||||
|
|
||||||
bool shouldRun = false;
|
bool shouldRun = false;
|
||||||
std::thread tuiThread;
|
std::thread tuiThread;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "message.h"
|
||||||
#include "slimchat.h"
|
#include "slimchat.h"
|
||||||
#include "view.h"
|
#include "view.h"
|
||||||
|
|
||||||
|
@ -15,7 +19,10 @@ public:
|
||||||
|
|
||||||
virtual int keyIn(int key) override;
|
virtual int keyIn(int key) override;
|
||||||
|
|
||||||
|
void updateMessages(const std::vector<std::shared_ptr<Message>>& messages);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const SlimChat* chat = nullptr;
|
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) {
|
void TGClient::downloadFile(int32_t file_id) {
|
||||||
Log::info << "start download for file: " << 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);
|
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;
|
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
|
//request next chunk
|
||||||
td_api::array<td_api::object_ptr<td_api::message>>& arr = m->messages_;
|
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
|
//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);
|
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_});
|
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) {
|
void TgTUI::switchToView(ViewMode to) {
|
||||||
|
|
||||||
// close old mode
|
// close old mode
|
||||||
|
@ -138,6 +143,8 @@ void TgTUI::switchToView(ViewMode to) {
|
||||||
viewChat->setChat(&*it);
|
viewChat->setChat(&*it);
|
||||||
tgclient.openChat(chatid);
|
tgclient.openChat(chatid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tgclient.getLastMessages(chatid, std::bind(&TgTUI::handleChatMessages, this, pl::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(to) {
|
switch(to) {
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
#include "viewchat.h"
|
#include "viewchat.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
|
#include <Log.h>
|
||||||
|
|
||||||
|
#include "message.h"
|
||||||
|
#include "tgtui.h"
|
||||||
|
|
||||||
ViewChat::ViewChat(TgTUI& tgtui) : View(tgtui) {}
|
ViewChat::ViewChat(TgTUI& tgtui) : View(tgtui) {}
|
||||||
|
|
||||||
ViewChat::~ViewChat() {}
|
ViewChat::~ViewChat() {}
|
||||||
|
@ -15,7 +22,21 @@ int64_t ViewChat::getChat() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ViewChat::paint() {
|
void ViewChat::paint() {
|
||||||
|
getmaxyx(stdscr, maxRows, maxCols);
|
||||||
|
|
||||||
::printw("Chat: %s (%li)\n", chat->name.c_str(), chat->chatId);
|
::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) {
|
int ViewChat::keyIn(int key) {
|
||||||
|
@ -27,3 +48,9 @@ int ViewChat::keyIn(int key) {
|
||||||
|
|
||||||
return -1;
|
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