diff options
author | matej <matej.vavrek@m2ms.sk> | 2018-01-31 20:17:37 +0100 |
---|---|---|
committer | matej <matej.vavrek@m2ms.sk> | 2018-01-31 20:18:54 +0100 |
commit | dda0fe5570bf7cb6634eb2a1debc5088433abfdd (patch) | |
tree | a03be99f66e14bfdc552d66d6a6cabc106209db7 /protocols | |
parent | 17a61a8acdaf80d89289f7d5b3989c47d4a8f0c8 (diff) |
reverted and modified some changes to make attachments work in active chat
Diffstat (limited to 'protocols')
-rw-r--r-- | protocols/FacebookRM/src/json.cpp | 278 |
1 files changed, 192 insertions, 86 deletions
diff --git a/protocols/FacebookRM/src/json.cpp b/protocols/FacebookRM/src/json.cpp index 79cf6a46ef..6a70485d92 100644 --- a/protocols/FacebookRM/src/json.cpp +++ b/protocols/FacebookRM/src/json.cpp @@ -283,104 +283,210 @@ void FacebookProto::ParseAttachments(std::string &message_text, const JSONNode & std::wstring newText; 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)); + // TODO refactor + + if (legacy) { + // api graph.. request + 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)); + } } } } - } - 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"); - - std::string link = blob_["animated_image"]["uri"].as_string(); - if (!link.empty()) - attachments_text += "\n" + absolutizeUrl(link) + "\n"; - } - else if (type == "MessageVideo") { // a video attachement - std::string filename = blob_["filename"].as_string(); - // playable_duration_in_ms=;video_type=FILE_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"); + + std::string link = blob_["animated_image"]["uri"].as_string(); + if (!link.empty()) + attachments_text += "\n" + absolutizeUrl(link) + "\n"; + } + 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"; + 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(); + if (!filesize.empty()) + filename += ", " + filesize + " bytes";*/ + + std::string link = blob_["url"].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(); - if (!filesize.empty()) - filename += ", " + filesize + " bytes";*/ - - std::string link = blob_["url"].as_string(); - if (!link.empty()) - attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n"; + } + 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"; + } } + 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"); } - 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)); - } + else { + // pull request, i.e. from chat message + const JSONNode &attachments_ = delta_["attachments"]; + if (!attachments_ || attachments_.empty()) + ; + else { + hasAttachement = true; + for (auto &itAttachment : attachments_) { + const JSONNode &attach_ = 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)); + } + } + } + } + else if (const JSONNode blob_ = attach_["blob_attachment"]) { + std::string type = blob_["__typename"].as_string(); + if (type == "MessageAnimatedImage") { // a GIF + newText = TranslateT("a GIF"); + + std::string link = blob_["animated_image"]["uri"].as_string(); + if (!link.empty()) + attachments_text += "\n" + absolutizeUrl(link) + "\n"; + } + else if (type == "MessageVideo") { // a video attachement + std::string filename = blob_["filename"].as_string(); + // playable_duration_in_ms=;video_type=FILE_ATTACHMENT - 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"; + 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 { + newText = (attachments_.size() > 1) ? TranslateT("files") : TranslateT("a file"); + + std::string filename = blob_["filename"].as_string(); + std::string filesize = attach_["fileSize"].as_string(); + if (!filesize.empty()) + 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" || 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"; + } + } + 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"); if (hasAttachement) { // TODO: have this as extra event, not replace or append message content |