From 54cdf9dca636ade5a476d9a056d04c5e37188ace Mon Sep 17 00:00:00 2001 From: Sergey Bolhovskoy Date: Fri, 26 Sep 2014 06:03:01 +0000 Subject: =?UTF-8?q?VKontakte:=20work=20with=20friends.getRequests=20part?= =?UTF-8?q?=202=20=E2=80=93=20complete=20add=20Authorize=20and=20AuthDeny?= =?UTF-8?q?=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.miranda-ng.org/main/trunk@10595 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/misc.cpp | 20 +++++++++ protocols/VKontakte/src/vk_proto.cpp | 30 +++++++++----- protocols/VKontakte/src/vk_proto.h | 1 + protocols/VKontakte/src/vk_thread.cpp | 78 ++++++++++++++++++----------------- 4 files changed, 81 insertions(+), 48 deletions(-) (limited to 'protocols') diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index da01c8dbcc..845706004f 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -388,4 +388,24 @@ void CVkProto::DBAddAuthRequest(const MCONTACT hContact) debugLogA("CVkProto::DBAddAuthRequest '%s'", szNick); mir_free(szNick); +} + +MCONTACT CVkProto::MContactFromDbEvent(HANDLE hDbEvent) +{ + if (!hDbEvent || !IsOnline()) + return (MCONTACT)-1; + + DWORD body[2]; + DBEVENTINFO dbei = { sizeof(dbei) }; + dbei.cbBlob = sizeof(DWORD) * 2; + dbei.pBlob = (PBYTE)&body; + + if (db_event_get(hDbEvent, &dbei)) + return (MCONTACT)-1; + if ((dbei.eventType != EVENTTYPE_AUTHREQUEST) || (strcmp(dbei.szModule, m_szModuleName))) + return (MCONTACT)-1; + + MCONTACT hContact = DbGetAuthEventContact(&dbei); + db_unset(hContact, m_szModuleName, "ReqAuth"); + return hContact; } \ No newline at end of file diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp index 3f19416433..54196d9dff 100644 --- a/protocols/VKontakte/src/vk_proto.cpp +++ b/protocols/VKontakte/src/vk_proto.cpp @@ -436,9 +436,9 @@ MCONTACT CVkProto::AddToList(int flags, PROTOSEARCHRESULT* psr) if (!uid) return NULL; - MCONTACT hConnact = FindUser(uid, true); + MCONTACT hContact = FindUser(uid, true); RetrieveUserInfo(uid); - return hConnact; + return hContact; } int CVkProto::AuthRequest(MCONTACT hContact,const PROTOCHAR* message) @@ -448,11 +448,12 @@ int CVkProto::AuthRequest(MCONTACT hContact,const PROTOCHAR* message) return 1; bool bIsFriend = getByte(hContact, "Auth", -1)==0; LONG userID = getDword(hContact, "ID", -1); - if (bIsFriend || (userID == -1) || !hContact) + if ((userID == -1) || !hContact) return 1; - TCHAR msg[501]; - _tcsncpy_s(msg, 500, message, _TRUNCATE); + TCHAR msg[501] = {0}; + if (message) + _tcsncpy_s(msg, 500, message, _TRUNCATE); Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/friends.add.json", true, &CVkProto::OnReceiveAuthRequest) << INT_PARAM("user_id", userID) @@ -471,10 +472,9 @@ void CVkProto::OnReceiveAuthRequest(NETLIBHTTPREQUEST *reply, AsyncHttpRequest * JSONNODE *pResponse = CheckJsonResponse(pReq, reply, pRoot); if (pResponse != NULL) { int iRet = json_as_int(pResponse); - if (iRet == 2){ - setByte(param->hContact, "Auth", 0); - MsgPopup(param->hContact, TranslateT("User added as friend"), _T("")); - } + setByte(param->hContact, "Auth", 0); + if (iRet == 2) + MsgPopup(param->hContact, TranslateT("User added as friend"), _T("")); } else{ switch (param->iCount){ @@ -495,12 +495,20 @@ void CVkProto::OnReceiveAuthRequest(NETLIBHTTPREQUEST *reply, AsyncHttpRequest * int CVkProto::Authorize(HANDLE hDbEvent) { - return 1; + MCONTACT hContact = MContactFromDbEvent(hDbEvent); + if (hContact == -1) + return 1; + + return AuthRequest(hContact, NULL); } int CVkProto::AuthDeny(HANDLE hDbEvent, const PROTOCHAR *reason) { - return 1; + MCONTACT hContact = MContactFromDbEvent(hDbEvent); + if (hContact == -1) + return 1; + + return SvcDeleteFriend(hContact,(LPARAM)true); } int CVkProto::UserIsTyping(MCONTACT hContact, int type) diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index 6d70fdfa65..29250d16db 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -375,4 +375,5 @@ private: void MsgPopup(MCONTACT hContact, const TCHAR *szMsg, const TCHAR *szTitle); void DBAddAuthRequest(const MCONTACT hContact); + MCONTACT MContactFromDbEvent(HANDLE hDbEvent); }; diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index bb448a6de0..a52c333277 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -240,18 +240,22 @@ static char fieldsName[] = "id, first_name, last_name, photo_100, bdate, sex, ti void CVkProto::RetrieveUserInfo(LONG userID) { debugLogA("CVkProto::RetrieveUserInfo %d", userID); - Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/users.get.json", true, &CVkProto::OnReceiveUserInfo) - << INT_PARAM("user_ids", userID) - << CHAR_PARAM("fields", fieldsName) - << CHAR_PARAM("name_case", "nom") - << VER_API)->pUserInfo = new CVkSendMsgParam(NULL); + CMString userIDs, code; + userIDs.AppendFormat(L"%i", userID); + CMString codeformat("var userIDs=\"%s\";" + "return{\"users\":API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"})};"); + + code.AppendFormat(codeformat, userIDs.GetBuffer(), CMString(fieldsName).GetBuffer()); + Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/execute.json", true, &CVkProto::OnReceiveUserInfo) + << TCHAR_PARAM("code", code) + << VER_API); } void CVkProto::RetrieveUsersInfo(bool flag) { debugLogA("CVkProto::RetrieveUsersInfo"); - CMString userIDs; + CMString userIDs, code; for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)){ LONG userID = getDword(hContact, "ID", -1); if (userID == -1) @@ -260,21 +264,18 @@ void CVkProto::RetrieveUsersInfo(bool flag) userIDs.AppendChar(','); userIDs.AppendFormat(L"%i", userID); } - if (flag){ - CMString codeformat("var userIDs=\"%s\";" - "return{\"users\":API.users.get({\"user_ids\":userIDs,\"fields\":\"online,status\",\"name_case\":\"nom\"})," - "\"requests\":API.friends.getRequests({\"extended\":0,\"need_mutual\":0,\"out\":1})};"), code; - code.AppendFormat(codeformat, userIDs.GetBuffer()); - Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/execute.json", true, &CVkProto::OnReceiveUserInfo) - << TCHAR_PARAM("code", code) - << VER_API)->pUserInfo = new CVkSendMsgParam(NULL, flag); - } + + CMString codeformat("var userIDs=\"%s\";" + "return{\"users\":API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"})"); + if (flag) + codeformat += CMString(",\"requests\":API.friends.getRequests({\"extended\":0,\"need_mutual\":0,\"out\":0})};"); else - Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/users.get.json", true, &CVkProto::OnReceiveUserInfo) - << TCHAR_PARAM("user_ids", userIDs) - << CHAR_PARAM("fields", fieldsName) - << CHAR_PARAM("name_case", "nom") - << VER_API)->pUserInfo = new CVkSendMsgParam(NULL, flag); + codeformat += CMString("};"); + code.AppendFormat(codeformat, userIDs.GetBuffer(), CMString(flag ? "online,status" : fieldsName).GetBuffer()); + + Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/execute.json", true, &CVkProto::OnReceiveUserInfo) + << TCHAR_PARAM("code", code) + << VER_API); } void CVkProto::OnReceiveUserInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) @@ -283,15 +284,13 @@ void CVkProto::OnReceiveUserInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe if ((reply->resultCode != 200) || !IsOnline()) return; - bool bIsReqInfo = ((CVkSendMsgParam*)pReq->pUserInfo)->iMsgID!=0; - delete pReq->pUserInfo; - + JSONROOT pRoot; JSONNODE *pResponse = CheckJsonResponse(pReq, reply, pRoot); if (pResponse == NULL) return; - JSONNODE *pUsers = bIsReqInfo ? json_get(pResponse, "users") : pResponse; + JSONNODE *pUsers = json_get(pResponse, "users"); if (pUsers == NULL) return; @@ -384,23 +383,27 @@ void CVkProto::OnReceiveUserInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe setTString(hContact, "domain", szValue); } - return; // tempory - - if (!bIsReqInfo) + JSONNODE *pRequests = json_get(pResponse, "requests"); + if (pRequests == NULL) return; - JSONNODE *pRequests = json_get(pResponse, "requests"); int iCount = json_as_int(json_get(pRequests, "count")); JSONNODE *pItems = json_get(pRequests, "items"), *pInfo; + if (!iCount||(pItems == NULL)) + return; + debugLogA("CVkProto::OnReceiveUserInfo AuthRequests"); for (int i = 0; (pInfo = json_at(pItems, i)) != NULL; i++) { LONG userid = json_as_int(pInfo); if (userid == 0) break; MCONTACT hContact = FindUser(userid, true); - RetrieveUserInfo(userid); - Sleep(1000); - DBAddAuthRequest(hContact); + if (!getBool(hContact, "ReqAuth", false)){ + RetrieveUserInfo(userid); + setByte(hContact, "ReqAuth", 1); + Sleep(1000); + DBAddAuthRequest(hContact); + } } } @@ -431,6 +434,7 @@ void CVkProto::OnReceiveFriends(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)){ db_unset(hContact, m_szModuleName, "Auth"); + db_unset(hContact, m_szModuleName, "ReqAuth"); if (bCleanContacts&&!isChatRoom(hContact)) arContacts.insert((HANDLE)hContact); } @@ -889,7 +893,7 @@ INT_PTR __cdecl CVkProto::SvcAddAsFriend(WPARAM hContact, LPARAM) return 0; } -INT_PTR __cdecl CVkProto::SvcDeleteFriend(WPARAM hContact, LPARAM) +INT_PTR __cdecl CVkProto::SvcDeleteFriend(WPARAM hContact, LPARAM flag) { debugLogA("CVkProto::SvcDeleteFriend"); LONG userID = getDword(hContact, "ID", -1); @@ -899,11 +903,11 @@ INT_PTR __cdecl CVkProto::SvcDeleteFriend(WPARAM hContact, LPARAM) CMString formatstr = TranslateT("Are you sure to delete %s from your friend list?"), tszNick = db_get_tsa(hContact, m_szModuleName, "Nick"), ptszMsg; - - ptszMsg.AppendFormat(formatstr, tszNick.IsEmpty() ? TranslateT("(Unknown contact)") : tszNick); - if (IDNO == MessageBox(NULL, ptszMsg.GetBuffer(), TranslateT("Attention!"), MB_ICONWARNING | MB_YESNO)) - return 1; - + if (flag==0){ + ptszMsg.AppendFormat(formatstr, tszNick.IsEmpty() ? TranslateT("(Unknown contact)") : tszNick); + if (IDNO == MessageBox(NULL, ptszMsg.GetBuffer(), TranslateT("Attention!"), MB_ICONWARNING | MB_YESNO)) + return 1; + } Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/friends.delete.json", true, &CVkProto::OnReceiveDeleteFriend) << INT_PARAM("user_id", userID) << VER_API)->pUserInfo = new CVkSendMsgParam(hContact); -- cgit v1.2.3