diff options
author | George Hazan <ghazan@miranda.im> | 2022-12-26 15:49:21 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2022-12-26 15:49:21 +0300 |
commit | 2c566472e4d0fd77f33a873d1898d4cf094dc95c (patch) | |
tree | e5def2ab911051c1ed439775f19addd52bba554d /protocols/Telegram | |
parent | 815d356584371b59545f5a2918a48cebd3a3b396 (diff) |
Telegram: contact status processing
Diffstat (limited to 'protocols/Telegram')
-rw-r--r-- | protocols/Telegram/src/mt_proto.cpp | 1 | ||||
-rw-r--r-- | protocols/Telegram/src/mt_proto.h | 22 | ||||
-rw-r--r-- | protocols/Telegram/src/server.cpp | 36 | ||||
-rw-r--r-- | protocols/Telegram/src/stdafx.h | 1 |
4 files changed, 60 insertions, 0 deletions
diff --git a/protocols/Telegram/src/mt_proto.cpp b/protocols/Telegram/src/mt_proto.cpp index e3dd7b6d29..998e84c733 100644 --- a/protocols/Telegram/src/mt_proto.cpp +++ b/protocols/Telegram/src/mt_proto.cpp @@ -18,6 +18,7 @@ static int CompareUsers(const TG_USER *p1, const TG_USER *p2) CMTProto::CMTProto(const char* protoName, const wchar_t* userName) : PROTO<CMTProto>(protoName, userName), + m_impl(*this), m_pClientMmanager(std::make_unique<td::ClientManager>()), m_arUsers(10, CompareUsers), m_arRequests(10, CompareRequests), diff --git a/protocols/Telegram/src/mt_proto.h b/protocols/Telegram/src/mt_proto.h index cc07eddd99..22b4fd2654 100644 --- a/protocols/Telegram/src/mt_proto.h +++ b/protocols/Telegram/src/mt_proto.h @@ -65,10 +65,30 @@ struct TG_USER uint64_t id; MCONTACT hContact; bool isGroupChat; + time_t m_timer1 = 0, m_timer2 = 0; }; class CMTProto : public PROTO<CMTProto> { + class CProtoImpl + { + friend class CMTProto; + CMTProto &m_proto; + + CTimer m_keepAlive; + void OnKeepAlive(CTimer *) + { + m_proto.SendKeepAlive(); + } + + CProtoImpl(CMTProto &pro) : + m_proto(pro), + m_keepAlive(Miranda_GetSystemWindow(), UINT_PTR(this)) + { + m_keepAlive.OnEvent = Callback(this, &CProtoImpl::OnKeepAlive); + } + } m_impl; + std::unique_ptr<td::ClientManager> m_pClientMmanager; TD::object_ptr<TD::AuthorizationState> pAuthState; @@ -90,6 +110,7 @@ class CMTProto : public PROTO<CMTProto> void LogOut(void); void OnLoggedIn(void); void ProcessResponse(td::ClientManager::Response); + void SendKeepAlive(void); void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr); void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER_FULL pHandler, void *pUserInfo); @@ -97,6 +118,7 @@ class CMTProto : public PROTO<CMTProto> void ProcessChat(TD::updateNewChat *pObj); void ProcessGroups(TD::updateChatFilters *pObj); void ProcessMessage(TD::updateNewMessage *pObj); + void ProcessStatus(TD::updateUserStatus *pObj); void ProcessUser(TD::updateUser *pObj); void OnSendMessage(td::ClientManager::Response &response, void *pUserInfo); diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index a94fe77e8f..50c6aa7084 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -60,6 +60,25 @@ void CMTProto::OnLoggedIn() /////////////////////////////////////////////////////////////////////////////// +void CMTProto::SendKeepAlive() +{ + time_t now = time(0); + + for (auto &it : m_arUsers) { + if (it->m_timer1 && now - it->m_timer1 > 600) { + it->m_timer1 = 0; + it->m_timer2 = now; + setWord(it->hContact, "Status", ID_STATUS_AWAY); + } + else if (it->m_timer2 && now - it->m_timer2 > 600) { + it->m_timer2 = 0; + setWord(it->hContact, "Status", ID_STATUS_OFFLINE); + } + } +} + +/////////////////////////////////////////////////////////////////////////////// + void CMTProto::ProcessResponse(td::ClientManager::Response response) { if (!response.object) @@ -94,6 +113,10 @@ void CMTProto::ProcessResponse(td::ClientManager::Response response) ProcessMessage((TD::updateNewMessage *)response.object.get()); break; + case TD::updateUserStatus::ID: + ProcessStatus((TD::updateUserStatus *)response.object.get()); + break; + case TD::updateUser::ID: ProcessUser((TD::updateUser *)response.object.get()); break; @@ -226,6 +249,19 @@ void CMTProto::ProcessMessage(TD::updateNewMessage *pObj) ProtoChainRecvMsg(pUser->hContact, &pre); } +void CMTProto::ProcessStatus(TD::updateUserStatus *pObj) +{ + if (auto *pUser = FindUser(pObj->user_id_)) { + if (pObj->status_->get_id() == TD::userStatusOnline::ID) + setWord(pUser->hContact, "Status", ID_STATUS_ONLINE); + else if (pObj->status_->get_id() == TD::userStatusOffline::ID) { + setWord(pUser->hContact, "Status", ID_STATUS_AWAY); + pUser->m_timer1 = time(0); + } + else debugLogA("!!!!! Unknown status packet, report it to the developers"); + } +} + void CMTProto::ProcessUser(TD::updateUser *pObj) { auto *pUser = pObj->user_.get(); diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h index cfbb5fa8b0..ff63a27ac9 100644 --- a/protocols/Telegram/src/stdafx.h +++ b/protocols/Telegram/src/stdafx.h @@ -1,6 +1,7 @@ #ifndef _COMMON_H_ #define _COMMON_H_ +#include <time.h> #include <windows.h> #include <map> |