From 09509db69203537d336a2d2b3451e5bba32d8aec Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 10 Jan 2014 20:12:55 +0000 Subject: VK: nasty clutch to maintain chat history git-svn-id: http://svn.miranda-ng.org/main/trunk@7582 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/vk_chats.cpp | 55 +++++++++++++++++++++++++++--------- protocols/VKontakte/src/vk_proto.h | 10 +++++++ 2 files changed, 52 insertions(+), 13 deletions(-) (limited to 'protocols') diff --git a/protocols/VKontakte/src/vk_chats.cpp b/protocols/VKontakte/src/vk_chats.cpp index 614e5b04a6..3f6c97bf1c 100644 --- a/protocols/VKontakte/src/vk_chats.cpp +++ b/protocols/VKontakte/src/vk_chats.cpp @@ -101,10 +101,8 @@ void CVkProto::RetrieveChatInfo(CVkChatInfo *cc) // retrieve users szQuery.AppendFormat("\"users\": API.messages.getChatUsers({\"chat_id\":%d, \"fields\":\"uid,first_name,last_name\"})", cc->m_chatid); - if (!cc->m_bHistoryRead) { - cc->m_bHistoryRead = true; + if (!cc->m_bHistoryRead) szQuery.AppendFormat(",\"msgs\": API.messages.getHistory({\"chat_id\":%d, \"count\":\"20\", \"rev\":\"1\"})", cc->m_chatid); - } szQuery.Append("};"); @@ -213,7 +211,14 @@ void CVkProto::OnReceiveChatInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe AppendChatMessage(cc->m_chatid, pMsg, true); } + cc->m_bHistoryRead = true; + } + + for (int j = 0; j < cc->m_msgs.getCount(); j++) { + CVkChatMessage &p = cc->m_msgs[j]; + AppendChatMessage(cc, p.m_uid, p.m_date, p.m_tszBody, false); } + cc->m_msgs.destroy(); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -230,25 +235,40 @@ void CVkProto::AppendChatMessage(int id, JSONNODE *pMsg, bool bIsHistory) m_sendIds.insert((void*)mid); int uid = json_as_int(json_get(pMsg, "uid")); - TCHAR tszId[20]; - _itot(uid, tszId, 10); int msgTime = json_as_int(json_get(pMsg, "date")); time_t now = time(NULL); if (!msgTime || msgTime > now) msgTime = now; + ptrT tszBody(json_as_string(json_get(pMsg, "body"))); + JSONNODE *pAttachments = json_get(pMsg, "attachments"); + if (pAttachments != NULL) + tszBody = mir_tstrdup(CMString(tszBody) + GetAttachmentDescr(pAttachments)); + + if (cc->m_bHistoryRead) + AppendChatMessage(cc, uid, msgTime, tszBody, bIsHistory); + else { + CVkChatMessage *cm = new CVkChatMessage(); + cm->m_mid = mid; + cm->m_uid = uid; + cm->m_date = msgTime; + cm->m_tszBody = tszBody.detouch(); + cc->m_msgs.insert(cm); + } +} + +void CVkProto::AppendChatMessage(CVkChatInfo *cc, int uid, int msgTime, LPCTSTR ptszBody, bool bIsHistory) +{ + TCHAR tszId[20]; + _itot(uid, tszId, 10); + CVkChatUser *cu = cc->m_users.find((CVkChatUser*)&uid); if (cu == NULL) { cc->m_users.insert(cu = new CVkChatUser(uid)); cu->m_tszTitle = mir_tstrdup(TranslateT("Unknown")); } - ptrT tszBody(json_as_string(json_get(pMsg, "body"))); - JSONNODE *pAttachments = json_get(pMsg, "attachments"); - if (pAttachments != NULL) - tszBody = mir_tstrdup(CMString(tszBody) + GetAttachmentDescr(pAttachments)); - GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_MESSAGE }; GCEVENT gce = { sizeof(GCEVENT), &gcd }; gce.bIsMe = uid == m_myUserId; @@ -256,12 +276,21 @@ void CVkProto::AppendChatMessage(int id, JSONNODE *pMsg, bool bIsHistory) gce.time = msgTime; gce.dwFlags = (bIsHistory) ? GCEF_NOTNOTIFY : GCEF_ADDTOLOG; gce.ptszNick = cu->m_tszTitle; - gce.ptszText = tszBody; + gce.ptszText = ptszBody; CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce); } ///////////////////////////////////////////////////////////////////////////////////////// +CVkChatInfo* CVkProto::GetChatById(LPCTSTR ptszId) +{ + for (int i = 0; i < m_chats.getCount(); i++) + if (!lstrcmp(m_chats[i].m_tszId, ptszId)) + return &m_chats[i]; + + return NULL; +} + CVkChatUser* CVkChatInfo::GetUserById(LPCTSTR ptszId) { int user_id = _ttoi(ptszId); @@ -291,8 +320,8 @@ int CVkProto::OnChatEvent(WPARAM, LPARAM lParam) if (lstrcmpiA(gch->pDest->pszModule, m_szModuleName)) return 0; - CVkChatInfo *cc = (CVkChatInfo*)gch->dwData; - if (m_chats.getIndex(cc) == -1) + CVkChatInfo *cc = GetChatById(gch->pDest->ptszID); + if (cc == NULL) return 0; switch (gch->pDest->iType) { diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index a6b3988a8f..81b9300b5e 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -31,6 +31,12 @@ struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject void *pUserInfo; }; +struct CVkChatMessage : public MZeroedObject +{ + int m_mid, m_uid, m_date; + ptrT m_tszBody; +}; + struct CVkChatUser : public MZeroedObject { CVkChatUser(int _id) : m_uid(_id) {} @@ -44,6 +50,7 @@ struct CVkChatInfo : public MZeroedObject { CVkChatInfo(int _id) : m_users(10, NumericKeySortT), + m_msgs(10, NumericKeySortT), m_chatid(_id) {} @@ -52,6 +59,7 @@ struct CVkChatInfo : public MZeroedObject ptrT m_tszTitle, m_tszId; HANDLE m_hContact; OBJLIST m_users; + OBJLIST m_msgs; CVkChatUser* GetUserById(LPCTSTR); }; @@ -231,6 +239,7 @@ private: OBJLIST m_chats; CVkChatInfo* AppendChat(int id, JSONNODE *pNode); void AppendChatMessage(int id, JSONNODE *pMsg, bool bIsHistory); + void AppendChatMessage(CVkChatInfo *cc, int uid, int msgTime, LPCTSTR ptszBody, bool bIsHistory); void RetrieveChatInfo(CVkChatInfo*); void OnReceiveChatInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnSendChatMsg(NETLIBHTTPREQUEST*, AsyncHttpRequest*); @@ -239,6 +248,7 @@ private: int __cdecl OnGcMenuHook(WPARAM, LPARAM); void LogMenuHook(CVkChatInfo*, GCHOOK*); void NickMenuHook(CVkChatInfo*, GCHOOK*); + CVkChatInfo* GetChatById(LPCTSTR ptszId); CMString GetAttachmentDescr(JSONNODE*); }; -- cgit v1.2.3