From beb1f6c074d7790bd015229afc0daea22e6fedb2 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Fri, 8 May 2015 15:31:07 +0000 Subject: SkypeWeb: DB utils moved to skype_db.cpp. Messages synchronization refactoring. git-svn-id: http://svn.miranda-ng.org/main/trunk@13484 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/SkypeWeb/src/requests/chatrooms.h | 19 +++++++ protocols/SkypeWeb/src/requests/history.h | 2 +- protocols/SkypeWeb/src/requests/search.h | 6 +- protocols/SkypeWeb/src/skype_chatrooms.cpp | 41 ++++++++++++++ protocols/SkypeWeb/src/skype_history_sync.cpp | 34 ++++------- protocols/SkypeWeb/src/skype_login.cpp | 5 +- protocols/SkypeWeb/src/skype_messages.cpp | 81 +-------------------------- protocols/SkypeWeb/src/skype_options.cpp | 2 +- protocols/SkypeWeb/src/skype_proto.cpp | 1 + protocols/SkypeWeb/src/skype_proto.h | 6 ++ protocols/SkypeWeb/src/skype_search.cpp | 4 +- protocols/SkypeWeb/src/skype_utils.cpp | 12 ---- 12 files changed, 93 insertions(+), 120 deletions(-) diff --git a/protocols/SkypeWeb/src/requests/chatrooms.h b/protocols/SkypeWeb/src/requests/chatrooms.h index 588c0de37b..39f2f5eddb 100644 --- a/protocols/SkypeWeb/src/requests/chatrooms.h +++ b/protocols/SkypeWeb/src/requests/chatrooms.h @@ -18,6 +18,25 @@ along with this program. If not, see . #ifndef _SKYPE_REQUEST_CHATS_H_ #define _SKYPE_REQUEST_CHATS_H_ +class LoadChatsRequest : public HttpRequest +{ +public: + LoadChatsRequest(const char *regToken, const char *server = SKYPE_ENDPOINTS_HOST) : + HttpRequest(REQUEST_GET, FORMAT, "%s/v1/users/ME/conversations", server) + { + Url + << INT_VALUE("startTime", 0) + << INT_VALUE("pageSize", 100) + << CHAR_VALUE("view", "msnp24Equivalent") + << CHAR_VALUE("targetType", "Thread"); + + Headers + << CHAR_VALUE("Accept", "application/json, text/javascript") + << FORMAT_VALUE("RegistrationToken", "registrationToken=%s", regToken) + << CHAR_VALUE("Content-Type", "application/json; charset = UTF-8"); + } +}; + class SendChatMessageRequest : public HttpRequest { public: diff --git a/protocols/SkypeWeb/src/requests/history.h b/protocols/SkypeWeb/src/requests/history.h index 1a1d121686..812df6e927 100644 --- a/protocols/SkypeWeb/src/requests/history.h +++ b/protocols/SkypeWeb/src/requests/history.h @@ -27,7 +27,7 @@ public: << INT_VALUE("startTime", 0) << INT_VALUE("pageSize", pageSize) << CHAR_VALUE("view", "msnp24Equivalent") - << CHAR_VALUE("targetType", "Passport|Skype|Lync|Thread"); + << CHAR_VALUE("targetType", "Passport|Skype|Lync"); Headers << CHAR_VALUE("Accept", "application/json, text/javascript") diff --git a/protocols/SkypeWeb/src/requests/search.h b/protocols/SkypeWeb/src/requests/search.h index 4628dfdd56..64283c1964 100644 --- a/protocols/SkypeWeb/src/requests/search.h +++ b/protocols/SkypeWeb/src/requests/search.h @@ -22,11 +22,13 @@ class GetSearchRequest : public HttpRequest { public: GetSearchRequest(const char *token, const char *string) : - HttpRequest(REQUEST_GET, FORMAT, "api.skype.com/search/users/any?keyWord=%s&contactTypes[]=skype", string) + HttpRequest(REQUEST_GET, "api.skype.com/search/users/any") { + Url + << CHAR_VALUE("keyWord", string) + << CHAR_VALUE("contactTypes[]", "skype"); Headers << CHAR_VALUE("Accept", "application/json") - << CHAR_VALUE("Content-Type", "application/json; charset=UTF-8") << CHAR_VALUE("Connection", "keep-alive") << CHAR_VALUE("X-Skypetoken", token); } diff --git a/protocols/SkypeWeb/src/skype_chatrooms.cpp b/protocols/SkypeWeb/src/skype_chatrooms.cpp index c9d3dca714..1c832afc5c 100644 --- a/protocols/SkypeWeb/src/skype_chatrooms.cpp +++ b/protocols/SkypeWeb/src/skype_chatrooms.cpp @@ -98,6 +98,47 @@ void CSkypeProto::StartChatRoom(const TCHAR *tid, const TCHAR *tname) CallServiceSync(MS_GC_EVENT, SESSION_ONLINE, reinterpret_cast(&gce)); } +void CSkypeProto::OnLoadChats(const NETLIBHTTPREQUEST *response) +{ + if (response == NULL) + return; + + JSONROOT root(response->pData); + + if (root == NULL) + return; + + JSONNODE *metadata = json_get(root, "_metadata"); + JSONNODE *conversations = json_as_array(json_get(root, "conversations")); + + int totalCount = json_as_int(json_get(metadata, "totalCount")); + ptrA syncState(mir_t2a(ptrT(json_as_string(json_get(metadata, "syncState"))))); + + if (totalCount >= 99 || json_size(conversations) >= 99) + PushRequest(new SyncHistoryFirstRequest(syncState, RegToken), &CSkypeProto::OnSyncHistory); + + for (size_t i = 0; i < json_size(conversations); i++) + { + JSONNODE *conversation = json_at(conversations, i); + JSONNODE *lastMessage = json_get(conversation, "lastMessage"); + JSONNODE *threadProperties = json_get(conversation, "threadProperties"); + if (json_empty(lastMessage)) + continue; + + char *conversationLink = mir_t2a(json_as_string(json_get(lastMessage, "conversationLink"))); + + ptrA skypename; + TCHAR *topic; + + if (conversationLink != NULL && strstr(conversationLink, "/19:")) + { + skypename = ChatUrlToName(conversationLink); + topic = json_as_string(json_get(threadProperties, "topic")); + SendRequest(new GetChatInfoRequest(RegToken, skypename, Server), &CSkypeProto::OnGetChatInfo, topic); + } + } +} + /* Hooks */ int CSkypeProto::OnGroupChatEventHook(WPARAM, LPARAM lParam) diff --git a/protocols/SkypeWeb/src/skype_history_sync.cpp b/protocols/SkypeWeb/src/skype_history_sync.cpp index f301fdb1c9..7d0886013c 100644 --- a/protocols/SkypeWeb/src/skype_history_sync.cpp +++ b/protocols/SkypeWeb/src/skype_history_sync.cpp @@ -58,6 +58,10 @@ void CSkypeProto::OnGetServerHistory(const NETLIBHTTPREQUEST *response) MCONTACT hContact = FindContact(ptrA(ContactUrlToName(conversationLink))); + + if (timestamp > db_get_dw(hContact,m_szModuleName, "LastMsgTime", 0)) + db_set_dw(hContact, m_szModuleName, "LastMsgTime", (DWORD)timestamp); + int flags = DBEF_UTF; if (!markAllAsUnread) @@ -202,14 +206,9 @@ INT_PTR CSkypeProto::GetContactHistory(WPARAM hContact, LPARAM) return 0; } -void CSkypeProto::SyncHistory() -{ - PushRequest(new SyncHistoryFirstRequest(RegToken, 100, Server), &CSkypeProto::OnSyncHistory); -} - void CSkypeProto::OnSyncHistory(const NETLIBHTTPREQUEST *response) { - if (response == NULL) + if (response == NULL || response->pData == NULL) return; JSONROOT root(response->pData); @@ -224,41 +223,30 @@ void CSkypeProto::OnSyncHistory(const NETLIBHTTPREQUEST *response) if (totalCount >= 99 || json_size(conversations) >= 99) PushRequest(new SyncHistoryFirstRequest(syncState, RegToken), &CSkypeProto::OnSyncHistory); - - bool autoSyncEnabled = getBool("AutoSync", true); for (size_t i = 0; i < json_size(conversations); i++) { JSONNODE *conversation = json_at(conversations, i); JSONNODE *lastMessage = json_get(conversation, "lastMessage"); - JSONNODE *threadProperties = json_get(conversation, "threadProperties"); if (json_empty(lastMessage)) continue; char *clientMsgId = mir_t2a(json_as_string(json_get(lastMessage, "clientmessageid"))); - //char *skypeEditedId = mir_t2a(json_as_string(json_get(lastMessage, "skypeeditedid"))); char *conversationLink = mir_t2a(json_as_string(json_get(lastMessage, "conversationLink"))); time_t composeTime(IsoToUnixTime(ptrT(json_as_string(json_get(lastMessage, "composetime"))))); ptrA skypename; - TCHAR *topic; if (conversationLink != NULL && strstr(conversationLink, "/8:")) { - if (autoSyncEnabled) - { - skypename = ContactUrlToName(conversationLink); - MCONTACT hContact = AddContact(skypename, true); + skypename = ContactUrlToName(conversationLink); + MCONTACT hContact = AddContact(skypename, true); - if (GetMessageFromDb(hContact, clientMsgId, composeTime) == NULL) - PushRequest(new GetHistoryRequest(RegToken, skypename, 100, false, 0, Server), &CSkypeProto::OnGetServerHistory); + if (/*GetLastMessageTime(hContact) < composeTime || */db_get_dw(hContact, m_szModuleName, "LastMsgTime", 0) < composeTime) + { + PushRequest(new GetHistoryRequest(RegToken, skypename, 100, false, 0, Server), &CSkypeProto::OnGetServerHistory); + HistorySynced = true; } } - else if (conversationLink != NULL && strstr(conversationLink, "/19:")) - { - skypename = ChatUrlToName(conversationLink); - topic = json_as_string(json_get(threadProperties, "topic")); - SendRequest(new GetChatInfoRequest(RegToken, skypename, Server), &CSkypeProto::OnGetChatInfo, topic); - } } } \ No newline at end of file diff --git a/protocols/SkypeWeb/src/skype_login.cpp b/protocols/SkypeWeb/src/skype_login.cpp index bcf1fd804a..e1adef6672 100644 --- a/protocols/SkypeWeb/src/skype_login.cpp +++ b/protocols/SkypeWeb/src/skype_login.cpp @@ -216,7 +216,10 @@ void CSkypeProto::OnCapabilitiesSended(const NETLIBHTTPREQUEST *response) m_hPollingThread = ForkThreadEx(&CSkypeProto::PollingThread, 0, NULL); - SyncHistory(); + //SyncHistory(); + + SendRequest(new LoadChatsRequest(RegToken, Server), &CSkypeProto::OnLoadChats); + PushRequest(new SyncHistoryFirstRequest(RegToken, 100, Server), &CSkypeProto::OnSyncHistory); if (response == NULL || response->pData == NULL) return; diff --git a/protocols/SkypeWeb/src/skype_messages.cpp b/protocols/SkypeWeb/src/skype_messages.cpp index 4655c3775f..35b849c89e 100644 --- a/protocols/SkypeWeb/src/skype_messages.cpp +++ b/protocols/SkypeWeb/src/skype_messages.cpp @@ -17,81 +17,6 @@ along with this program. If not, see . #include "stdafx.h" -MEVENT CSkypeProto::GetMessageFromDb(MCONTACT hContact, const char *messageId, LONGLONG timestamp) -{ - if(messageId == NULL) - return NULL; - - timestamp -= 600; // we check events written 10 minutes ago - size_t messageIdLength = mir_strlen(messageId); - - mir_cslock lock(messageSyncLock); - for (MEVENT hDbEvent = db_event_last(hContact); hDbEvent; hDbEvent = db_event_prev(hContact, hDbEvent)) - { - DBEVENTINFO dbei = { sizeof(dbei) }; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - - if (dbei.cbBlob < messageIdLength) - continue; - - mir_ptr blob((PBYTE)mir_alloc(dbei.cbBlob)); - dbei.pBlob = blob; - db_event_get(hDbEvent, &dbei); - - if (dbei.eventType != EVENTTYPE_MESSAGE && dbei.eventType != SKYPE_DB_EVENT_TYPE_ACTION && dbei.eventType != SKYPE_DB_EVENT_TYPE_CALL_INFO) - continue; - - size_t cbLen = strlen((char*)dbei.pBlob); - if (memcmp(&dbei.pBlob[cbLen+1], messageId, messageIdLength) == 0) - return hDbEvent; - - if (dbei.timestamp < timestamp) - break; - } - - return NULL; -} - -MEVENT CSkypeProto::AddMessageToDb(MCONTACT hContact, DWORD timestamp, DWORD flags, const char *messageId, char *content, int emoteOffset) -{ - if (MEVENT hDbEvent = GetMessageFromDb(hContact, messageId, timestamp)) - return hDbEvent; - size_t messageLength = mir_strlen(&content[emoteOffset]) + 1; - size_t messageIdLength = mir_strlen(messageId); - size_t cbBlob = messageLength + messageIdLength; - PBYTE pBlob = (PBYTE)mir_alloc(cbBlob); - memcpy(pBlob, &content[emoteOffset], messageLength); - memcpy(pBlob + messageLength, messageId, messageIdLength); - - return AddEventToDb(hContact, emoteOffset == 0 ? EVENTTYPE_MESSAGE : SKYPE_DB_EVENT_TYPE_ACTION, timestamp, flags, (DWORD)cbBlob, pBlob); -} - -MEVENT CSkypeProto::AddCallInfoToDb(MCONTACT hContact, DWORD timestamp, DWORD flags, const char *messageId, char *content) -{ - if (MEVENT hDbEvent = GetMessageFromDb(hContact, messageId, timestamp)) - return hDbEvent; - size_t messageLength = mir_strlen(content) + 1; - size_t messageIdLength = mir_strlen(messageId); - size_t cbBlob = messageLength + messageIdLength; - PBYTE pBlob = (PBYTE)mir_alloc(cbBlob); - memcpy(pBlob, content, messageLength); - memcpy(pBlob + messageLength, messageId, messageIdLength); - - return AddEventToDb(hContact,SKYPE_DB_EVENT_TYPE_CALL_INFO, timestamp, flags, (DWORD)cbBlob, pBlob); -} - -MEVENT CSkypeProto::AddCallToDb(MCONTACT hContact, DWORD timestamp, DWORD flags, const char *callId, const char *gp) -{ - size_t callIdLength = mir_strlen(callId); - size_t messageLength = mir_strlen(gp) + 1; - size_t cbBlob = messageLength + callIdLength; - PBYTE pBlob = (PBYTE)mir_alloc(cbBlob); - memcpy(pBlob, gp, messageLength); - memcpy(pBlob + messageLength, callId, callIdLength); - - return AddEventToDb(hContact, SKYPE_DB_EVENT_TYPE_INCOMING_CALL, timestamp, flags, (DWORD)cbBlob, pBlob); -} - /* MESSAGE RECEIVING */ // incoming message flow @@ -247,6 +172,8 @@ void CSkypeProto::OnPrivateMessageEvent(JSONNODE *node) ptrA messageType(mir_t2a(ptrT(json_as_string(json_get(node, "messagetype"))))); MCONTACT hContact = AddContact(skypename, true); + + if (!HistorySynced) db_set_dw(hContact, m_szModuleName, "LastMsgTime", (DWORD)timestamp); if (!mir_strcmpi(messageType, "Control/Typing")) CallService(MS_PROTO_CONTACTISTYPING, hContact, PROTOTYPE_CONTACTTYPING_INFINITE); @@ -400,10 +327,6 @@ void CSkypeProto::MarkMessagesRead(MCONTACT hContact, MEVENT hDbEvent) ptrA username(db_get_sa(hContact, m_szModuleName, SKYPE_SETTINGS_ID)); DBEVENTINFO dbei = { sizeof(dbei) }; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - mir_ptr blob((PBYTE)mir_alloc(dbei.cbBlob)); - dbei.pBlob = blob; - db_event_get(hDbEvent, &dbei); time_t timestamp = dbei.timestamp; diff --git a/protocols/SkypeWeb/src/skype_options.cpp b/protocols/SkypeWeb/src/skype_options.cpp index 882b35cfe7..0322d44d36 100644 --- a/protocols/SkypeWeb/src/skype_options.cpp +++ b/protocols/SkypeWeb/src/skype_options.cpp @@ -49,7 +49,7 @@ void CSkypeOptionsMain::OnApply() { ptrA tszNewSkypename(m_skypename.GetTextA()),tszNewPassword(m_password.GetTextA()), tszOldSkypename(m_proto->getStringA(SKYPE_SETTINGS_ID)),tszOldPassword(m_proto->getStringA("Password")); - if (mir_strcmpi(tszNewSkypename, tszOldSkypename) || mir_strcmpi(tszNewPassword, tszOldPassword)) + if (mir_strcmpi(tszNewSkypename, tszOldSkypename) || mir_strcmp(tszNewPassword, tszOldPassword)) m_proto->delSetting("TokenExpiresIn"); m_proto->setString(SKYPE_SETTINGS_ID, tszNewSkypename); m_proto->setString("Password", tszNewPassword); diff --git a/protocols/SkypeWeb/src/skype_proto.cpp b/protocols/SkypeWeb/src/skype_proto.cpp index 407c9ea566..9e9910f594 100644 --- a/protocols/SkypeWeb/src/skype_proto.cpp +++ b/protocols/SkypeWeb/src/skype_proto.cpp @@ -245,6 +245,7 @@ int CSkypeProto::SetStatus(int iNewStatus) m_iStatus = ID_STATUS_CONNECTING; requestQueue->Start(); int tokenExpires(getDword("TokenExpiresIn", 0)); + HistorySynced = false; if ((tokenExpires - 1800) > time(NULL)) OnLoginSuccess(); else diff --git a/protocols/SkypeWeb/src/skype_proto.h b/protocols/SkypeWeb/src/skype_proto.h index 0d1f403912..3f8d51007e 100644 --- a/protocols/SkypeWeb/src/skype_proto.h +++ b/protocols/SkypeWeb/src/skype_proto.h @@ -101,6 +101,8 @@ private: static INT_PTR CALLBACK PasswordEditorProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + bool HistorySynced; + HANDLE m_hPopupClassCall, m_hPopupClassNotify; @@ -255,6 +257,8 @@ private: void StartChatRoom(const TCHAR *tid, const TCHAR *tname); + void OnLoadChats(const NETLIBHTTPREQUEST *response); + void OnGetChatInfo(const NETLIBHTTPREQUEST *response, void *p); INT_PTR __cdecl OnJoinChatRoom (WPARAM hContact, LPARAM); @@ -316,6 +320,8 @@ private: void CALLBACK SkypeUnsetTimer(void*); void CALLBACK SkypeSetTimer(void*); + time_t GetLastMessageTime(MCONTACT hContact); + //events void CSkypeProto::InitDBEvents(); diff --git a/protocols/SkypeWeb/src/skype_search.cpp b/protocols/SkypeWeb/src/skype_search.cpp index 8153e681c3..0d38e61fb1 100644 --- a/protocols/SkypeWeb/src/skype_search.cpp +++ b/protocols/SkypeWeb/src/skype_search.cpp @@ -28,7 +28,9 @@ void CSkypeProto::SearchBasicThread(void* id) debugLogA("CSkypeProto::OnSearchBasicThread"); if (!IsOnline()) return; - ptrA string(mir_urlEncode(ptrA(mir_utf8encodeT((TCHAR*)id)))); + + TCHAR *idT = (TCHAR *)id; + ptrA string(mir_urlEncode(mir_utf8encodeT(idT))); SendRequest(new GetSearchRequest(TokenSecret, string), &CSkypeProto::OnSearch); } diff --git a/protocols/SkypeWeb/src/skype_utils.cpp b/protocols/SkypeWeb/src/skype_utils.cpp index 00b42fb4f0..6defeed3f5 100644 --- a/protocols/SkypeWeb/src/skype_utils.cpp +++ b/protocols/SkypeWeb/src/skype_utils.cpp @@ -22,18 +22,6 @@ bool CSkypeProto::IsOnline() return m_iStatus > ID_STATUS_OFFLINE && m_hPollingThread; } -MEVENT CSkypeProto::AddEventToDb(MCONTACT hContact, WORD type, DWORD timestamp, DWORD flags, DWORD cbBlob, PBYTE pBlob) -{ - DBEVENTINFO dbei = { sizeof(dbei) }; - dbei.szModule = this->m_szModuleName; - dbei.timestamp = timestamp; - dbei.eventType = type; - dbei.cbBlob = cbBlob; - dbei.pBlob = pBlob; - dbei.flags = flags; - return db_event_add(hContact, &dbei); -} - time_t CSkypeProto::IsoToUnixTime(const TCHAR *stamp) { TCHAR date[9]; -- cgit v1.2.3