diff options
author | matej <matej.vavrek@m2ms.sk> | 2018-01-23 20:25:14 +0100 |
---|---|---|
committer | matej <matej.vavrek@m2ms.sk> | 2018-01-23 20:25:14 +0100 |
commit | 8941c8aa4b1318da024026df27052a2e5a8956fb (patch) | |
tree | 379af476b33b81eec539800ecbb07d63209d4b03 /protocols/FacebookRM/src/json.cpp | |
parent | 80207f75648fad4294a77ff9ce50f0edaedffeca (diff) |
Facebook: attachments seems to work differently, some adjustments
Diffstat (limited to 'protocols/FacebookRM/src/json.cpp')
-rw-r--r-- | protocols/FacebookRM/src/json.cpp | 161 |
1 files changed, 90 insertions, 71 deletions
diff --git a/protocols/FacebookRM/src/json.cpp b/protocols/FacebookRM/src/json.cpp index 616b77c833..79cf6a46ef 100644 --- a/protocols/FacebookRM/src/json.cpp +++ b/protocols/FacebookRM/src/json.cpp @@ -281,38 +281,40 @@ void FacebookProto::ParseAttachments(std::string &message_text, const JSONNode & { std::string attachments_text; std::wstring newText; - - const JSONNode &attachments_ = delta_["attachments"]; - if (!attachments_ || attachments_.empty()) - return; - - for (auto &itAttachment : attachments_) { - const JSONNode &attach_ = legacy ? itAttachment : itAttachment["mercury"]; - if (const JSONNode sticker_ = attach_["sticker_attachment"]) { - newText = TranslateT("a sticker"); - attachments_text += "\n"; - - std::string link = sticker_["url"].as_string(); - if (!link.empty()) - attachments_text += absolutizeUrl(link) + "\n"; - - std::string label = sticker_["label"].as_string(); - if (!label.empty()) - attachments_text += label + "\n"; - - const JSONNode &stickerId_ = sticker_["id"]; - if (stickerId_) { - // Stickers as smileys - if (getByte(FACEBOOK_KEY_CUSTOM_SMILEYS, DEFAULT_CUSTOM_SMILEYS) && !facy.loading_history) { - // FIXME: rewrite smileyadd to use custom smileys per protocol and not per contact and then remove this ugliness - if (!other_user_fbid.empty()) { - std::string sticker = "[[sticker:" + stickerId_.as_string() + "]]"; - StickerAsSmiley(sticker, link, ContactIDToHContact(other_user_fbid)); - } + bool hasAttachement = false; + + // const JSONNode &attach_ = legacy ? itAttachment : itAttachment["mercury"]; + const JSONNode sticker_ = delta_["sticker"]; + if (!sticker_.empty()) { + hasAttachement = true; + newText = TranslateT("a sticker"); + attachments_text += "\n"; + + std::string link = sticker_["url"].as_string(); + if (!link.empty()) + attachments_text += absolutizeUrl(link) + "\n"; + + std::string label = sticker_["label"].as_string(); + if (!label.empty()) + attachments_text += label + "\n"; + + const JSONNode &stickerId_ = sticker_["id"]; + if (stickerId_) { + // Stickers as smileys + if (getByte(FACEBOOK_KEY_CUSTOM_SMILEYS, DEFAULT_CUSTOM_SMILEYS) && !facy.loading_history) { + // FIXME: rewrite smileyadd to use custom smileys per protocol and not per contact and then remove this ugliness + if (!other_user_fbid.empty()) { + std::string sticker = "[[sticker:" + stickerId_.as_string() + "]]"; + StickerAsSmiley(sticker, link, ContactIDToHContact(other_user_fbid)); } } } - else if (const JSONNode blob_ = attach_["blob_attachment"]) { + } + const JSONNode blobs_ = delta_["blob_attachments"]; + if (!blobs_.empty()) { + hasAttachement = true; + newText = (blobs_.size() > 1) ? TranslateT("files") : TranslateT("a file"); + for (auto &blob_ : blobs_) { std::string type = blob_["__typename"].as_string(); if (type == "MessageAnimatedImage") { // a GIF newText = TranslateT("a GIF"); @@ -321,64 +323,81 @@ void FacebookProto::ParseAttachments(std::string &message_text, const JSONNode & if (!link.empty()) attachments_text += "\n" + absolutizeUrl(link) + "\n"; } - else { - newText = (attachments_.size() > 1) ? TranslateT("files") : TranslateT("a file"); + else if (type == "MessageVideo") { // a video attachement + std::string filename = blob_["filename"].as_string(); + // playable_duration_in_ms=;video_type=FILE_ATTACHMENT + std::string link = blob_["playable_url"].as_string(); + if (!link.empty()) + attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n"; + } + else if (type == "MessageImage") { // an image + std::string filename = blob_["filename"].as_string(); + + std::string link = blob_["large_preview"]["uri"].as_string(); + if (!link.empty()) + attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n"; + } + else { // other std::string filename = blob_["filename"].as_string(); - std::string filesize = attach_["fileSize"].as_string(); + /*std::string filesize = attach_["fileSize"].as_string(); if (!filesize.empty()) - filename += ", " + filesize + " bytes"; + filename += ", " + filesize + " bytes";*/ std::string link = blob_["url"].as_string(); if (!link.empty()) attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n"; } } - else if (const JSONNode story_ = attach_["extensible_attachment"]["story_attachment"]) { - std::string type = story_["target"]["__typename"].as_string(); - if (type == "ExternalUrl") { - newText = TranslateT("a link"); - - std::string title = story_["title_with_entities"]["text"].as_string(); - std::string description = story_["description"]["text"].as_string(); - std::string link = story_["url"].as_string(); - - // shorten long descriptions - description = utils::text::truncate_utf8(description, MAX_LINK_DESCRIPTION_LEN); - - if (link.find("//www." FACEBOOK_SERVER_DOMAIN "/l.php") != std::string::npos || link.find("//l." FACEBOOK_SERVER_DOMAIN) != std::string::npos) { - // de-facebook this link - link = utils::url::decode(utils::text::source_get_value2(&link, "l.php?u=", "&", true)); - } + } + const JSONNode story_ = delta_["extensible_attachment"]["story_attachment"]; + if (!story_.empty()) { + hasAttachement = true; + std::string type = story_["target"]["__typename"].as_string(); + if (type == "ExternalUrl" || type == "Story") { + newText = TranslateT("a link"); + + std::string title = story_["title_with_entities"]["text"].as_string(); + std::string description = story_["description"]["text"].as_string(); + std::string link = story_["url"].as_string(); + + // shorten long descriptions + description = utils::text::truncate_utf8(description, MAX_LINK_DESCRIPTION_LEN); + + if (link.find("//www." FACEBOOK_SERVER_DOMAIN "/l.php") != std::string::npos || link.find("//l." FACEBOOK_SERVER_DOMAIN) != std::string::npos) { + // de-facebook this link + link = utils::url::decode(utils::text::source_get_value2(&link, "l.php?u=", "&", true)); + } - if (!link.empty()) { - attachments_text += "\n"; - if (!title.empty()) - attachments_text += title + "\n"; - if (!description.empty()) - attachments_text += description + "\n"; - attachments_text += absolutizeUrl(link) + "\n"; - } + if (!link.empty()) { + attachments_text += "\n"; + if (!title.empty()) + attachments_text += title + "\n"; + if (!description.empty()) + attachments_text += description + "\n"; + attachments_text += absolutizeUrl(link) + "\n"; } - else debugLogA("json::parseAttachments (%s) - Unknown extensible attachment type %s", legacy ? "legacy" : "not legacy", type.c_str()); } - else debugLogA("json::parseAttachments (%s) - Unknown attachment type", legacy ? "legacy" : "not legacy"); + else debugLogA("json::parseAttachments (%s) - Unknown extensible attachment type %s", legacy ? "legacy" : "not legacy", type.c_str()); } + // else debugLogA("json::parseAttachments (%s) - Unknown attachment type", legacy ? "legacy" : "not legacy"); - // TODO: have this as extra event, not replace or append message content - if (!message_text.empty()) - message_text += "\n\n"; + if (hasAttachement) { + // TODO: have this as extra event, not replace or append message content + if (!message_text.empty()) + message_text += "\n\n"; - if (!attachments_text.empty()) { - wchar_t title[200]; - mir_snwprintf(title, TranslateT("User sent %s:"), newText.c_str()); + if (!attachments_text.empty()) { + wchar_t title[200]; + mir_snwprintf(title, TranslateT("User sent %s:"), newText.c_str()); - message_text += T2Utf(title); - message_text += attachments_text; - } - else { - message_text += T2Utf(TranslateT("User sent an unsupported attachment. Open your browser to see it.")); - debugLogA("json::parseAttachments (%s) - Unsupported attachment:\n%s", legacy ? "legacy" : "not legacy", attachments_.as_string().c_str()); + message_text += T2Utf(title); + message_text += attachments_text; + } + else { + message_text += T2Utf(TranslateT("User sent an unsupported attachment. Open your browser to see it.")); + debugLogA("json::parseAttachments (%s) - Unsupported attachment:\n%s", legacy ? "legacy" : "not legacy", /*attachments_*/delta_.as_string().c_str()); + } } } |