diff options
author | George Hazan <ghazan@miranda.im> | 2020-01-15 15:49:11 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-01-15 15:49:11 +0300 |
commit | 262f12cd97d52a5a78f0d8de1efd79127901901a (patch) | |
tree | 33127c2312656a4bbfa43ab0d8f62bbeb603a6a1 /protocols/Facebook | |
parent | 1fc999b9fa6d9edfc46e828f197fcdb358b17ace (diff) |
fixes #2179 (Facebook: attachment sometimes doesn't load)
Diffstat (limited to 'protocols/Facebook')
-rw-r--r-- | protocols/Facebook/src/proto.h | 2 | ||||
-rw-r--r-- | protocols/Facebook/src/server.cpp | 46 |
2 files changed, 31 insertions, 17 deletions
diff --git a/protocols/Facebook/src/proto.h b/protocols/Facebook/src/proto.h index 34ca49eb3f..4015244ac6 100644 --- a/protocols/Facebook/src/proto.h +++ b/protocols/Facebook/src/proto.h @@ -444,6 +444,8 @@ class FacebookProto : public PROTO<FacebookProto> return m_users.find((FacebookUser *)&id); } + void FetchAttach(const CMStringA &mid, __int64 fbid, CMStringA &szBody); + void OnLoggedIn(); void OnLoggedOut(); diff --git a/protocols/Facebook/src/server.cpp b/protocols/Facebook/src/server.cpp index 2b47f373ef..d040b47181 100644 --- a/protocols/Facebook/src/server.cpp +++ b/protocols/Facebook/src/server.cpp @@ -428,6 +428,34 @@ static facebookClients[] = { "app_id:256002347743983", "Facebook (Facebook Messenger)" } }; +void FacebookProto::FetchAttach(const CMStringA &mid, __int64 fbid, CMStringA &szBody) +{ + for (int iAttempt = 0; iAttempt < 5; iAttempt++) { + auto *pReq = CreateRequest(FB_API_URL_ATTACH, "getAttachment", "messaging.getAttachment"); + pReq << CHAR_PARAM("mid", mid) << INT64_PARAM("aid", fbid); + pReq->CalcSig(); + + JsonReply reply(ExecuteRequest(pReq)); + switch (reply.error()) { + case 0: + { + std::string uri = reply.data()["redirect_uri"].as_string(); + std::string type = reply.data()["content_type"].as_string(); + if (!uri.empty()) + szBody.AppendFormat("\r\n%s: %s", TranslateU(type.find("image/") != -1 ? "Picture attachment" : "File attachment"), uri.c_str()); + } + return; + + case 509: // attachment isn't ready, wait a bit and retry + ::Sleep(100); + continue; + + default: // shit happened, exiting + return; + } + } +} + void FacebookProto::OnPublishPrivateMessage(const JSONNode &root) { auto &metadata = root["messageMetadata"]; @@ -534,23 +562,7 @@ void FacebookProto::OnPublishPrivateMessage(const JSONNode &root) } // inline attachment, request its description - auto *pReq = CreateRequest(FB_API_URL_ATTACH, "getAttachment", "messaging.getAttachment"); - pReq << CHAR_PARAM("mid", szId) << INT64_PARAM("aid", fbid); - pReq->CalcSig(); - - JsonReply reply(ExecuteRequest(pReq)); - if (!reply.error()) { - CMStringA uri = reply.data()["redirect_uri"].as_mstring(); - CMStringA type = reply.data()["content_type"].as_mstring(); - if (!uri.IsEmpty()) { - if (type.Find("image/") == 0) - szBody.Append("\r\nPicture attachment: "); - else - szBody.Append("\r\nFile attachment: "); - - szBody.Append(uri); - } - } + FetchAttach(szId, fbid, szBody); continue; } JSONROOT nBody(szJson); |