Exemplo n.º 1
0
void parseAttachments(FacebookProto *proto, std::string *message_text, const JSONNode &it, const std::string &thread_id, std::string other_user_fbid)
{
	// Process attachements and stickers
	const JSONNode &has_attachment = it["has_attachment"];
	if (!has_attachment || !has_attachment.as_bool())
		return;

	// Append attachements
	std::string type;
	std::string attachments_text;
	const JSONNode &attachments = it["attachments"];
	for (auto itAttachment = attachments.begin(); itAttachment != attachments.end(); ++itAttachment) {
		const JSONNode &attach_type = (*itAttachment)["attach_type"]; // "sticker", "photo", "file", "share"
		if (attach_type) {
			// Get attachment type - "file" has priority over other types
			if (type.empty() || type != "file")
				type = attach_type.as_string();
		}

		if (type == "photo") {			
			std::string filename = (*itAttachment)["name"].as_string();
			std::string link = (*itAttachment)["hires_url"].as_string();

			const JSONNode &metadata = (*itAttachment)["metadata"];
			if (metadata) {
				std::string id = metadata["fbid"].as_string();
				const JSONNode &data = it["attachment_map"][id.c_str()];
				filename = data["filename"].as_string();
				link = data["image_data"]["url"].as_string();
			}

			if (!link.empty()) {
				attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n";
			}
		}
		else if (type == "file") {
			std::string filename = (*itAttachment)["name"].as_string();
			std::string link = (*itAttachment)["url"].as_string();

			if (!link.empty()) {
				attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n";
			}
		}
		else if (type == "share") {
			const JSONNode &share = (*itAttachment)["share"];
			if (share) {
				std::string title = share["title"].as_string();
				std::string description = share["description"].as_string();
				std::string link = share["uri"].as_string();

				if (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 if (type == "sticker") {
			std::string link = (*itAttachment)["url"].as_string();
			if (!link.empty()) {
				attachments_text += "\n" + absolutizeUrl(link) + "\n";
			}

			// Stickers as smileys
			if (proto->getByte(FACEBOOK_KEY_CUSTOM_SMILEYS, DEFAULT_CUSTOM_SMILEYS)) {
				const JSONNode &metadata = (*itAttachment)["metadata"];
				if (metadata) {
					const JSONNode &stickerId_ = metadata["stickerID"];
					if (stickerId_) {
						std::string sticker = "[[sticker:" + stickerId_.as_string() + "]]\n";
						attachments_text += sticker;

						if (other_user_fbid.empty() && !thread_id.empty())
							other_user_fbid = proto->ThreadIDToContactID(thread_id);

						// FIXME: rewrite smileyadd to use custom smileys per protocol and not per contact and then remove this ugliness
						if (!other_user_fbid.empty()) {
							MCONTACT hContact = proto->ContactIDToHContact(other_user_fbid);
							proto->StickerAsSmiley(sticker, link, hContact);
						}
					}
				}
			}
		}
	}

	// TODO: have this as extra event, not replace or append message content
	if (!message_text->empty())
		*message_text += "\n\n";

	if (!attachments_text.empty()) {
		// we can't use this as offline messages doesn't have it
		/* const JSONNode &admin_snippet = it["admin_snippet");
		if (admin_snippet != NULL) {
		*message_text += admin_snippet);
		} */

		std::tstring newText;
		if (type == "sticker")
			newText = TranslateT("a sticker");
		else if (type == "share")
			newText = TranslateT("a link");
		else if (type == "file")
			newText = (attachments.size() > 1) ? TranslateT("files") : TranslateT("a file");
		else if (type == "photo")
			newText = (attachments.size() > 1) ? TranslateT("photos") : TranslateT("a photo");
		else
			newText = _A2T(type.c_str());

		TCHAR title[200];
		mir_sntprintf(title, _countof(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."));
	}
}
Exemplo n.º 2
0
void parseAttachments(FacebookProto *proto, std::string *message_text, const JSONNode &delta_, std::string other_user_fbid, bool legacy)
{
	std::string attachments_text;
	std::string type;

	const JSONNode &attachments_ = delta_["attachments"];
	if (!attachments_ || attachments_.empty())
		return;
		
	for (auto itAttachment = attachments_.begin(); itAttachment != attachments_.end(); ++itAttachment) {
		const JSONNode &attach_ = legacy ? (*itAttachment) : (*itAttachment)["mercury"];

		type = attach_["attach_type"].as_string();  // "sticker", "photo", "file", "share", "animated_image", "video"

		if (type == "photo") {
			std::string filename = attach_["name"].as_string();
			std::string link = attach_["hires_url"].as_string();

			if (!link.empty()) {
				attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n";
			}
		}
		else if (type == "file" || type == "video") {
			std::string filename = attach_["name"].as_string();
			std::string link = attach_["url"].as_string();

			if (!link.empty()) {
				attachments_text += "\n" + (!filename.empty() ? "<" + filename + "> " : "") + absolutizeUrl(link) + "\n";
			}
		}
		else if (type == "share") {
			const JSONNode &share = attach_["share"];
			if (share) {
				std::string title = share["title"] ? share["title"].as_string() : "";
				std::string description = share["description"] ? share["description"].as_string() : "";
				std::string link = share["uri"].as_string();

				// shorten long descriptions
				if (description.length() > MAX_LINK_DESCRIPTION_LEN)
					description = description.substr(0, MAX_LINK_DESCRIPTION_LEN) + TEXT_ELLIPSIS;

				if (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 if (type == "sticker") {
			std::string link = attach_["url"].as_string();
			if (!link.empty()) {
				attachments_text += "\n" + absolutizeUrl(link) + "\n";
			}

			const JSONNode &metadata = attach_["metadata"];
			if (metadata) {
				const JSONNode &stickerId_ = metadata["stickerID"];
				if (stickerId_) {
					std::string sticker = "[[sticker:" + stickerId_.as_string() + "]]\n";
					attachments_text += sticker;

					// Stickers as smileys
					if (proto->getByte(FACEBOOK_KEY_CUSTOM_SMILEYS, DEFAULT_CUSTOM_SMILEYS)) {
						// FIXME: rewrite smileyadd to use custom smileys per protocol and not per contact and then remove this ugliness
						if (!other_user_fbid.empty()) {
							MCONTACT hContact = proto->ContactIDToHContact(other_user_fbid);
							proto->StickerAsSmiley(sticker, link, hContact);
						}
					}
				}
			}
		}
		else if (type == "animated_image") {
			std::string link = attach_["hires_url"].as_string();

			if (!link.empty()) {
				attachments_text += "\n" + absolutizeUrl(link) + "\n";
			}
		}
		else {
			proto->debugLogA("json::parseAttachments (%s) - Unknown attachment type '%s'", legacy ? "legacy" : "not legacy", type.c_str());
		}
	}

	// TODO: have this as extra event, not replace or append message content
	if (!message_text->empty())
		*message_text += "\n\n";

	if (!attachments_text.empty()) {
		std::tstring newText;
		if (type == "sticker")
			newText = TranslateT("a sticker");
		else if (type == "share")
			newText = TranslateT("a link");
		else if (type == "file")
			newText = (attachments_.size() > 1) ? TranslateT("files") : TranslateT("a file");
		else if (type == "photo")
			newText = (attachments_.size() > 1) ? TranslateT("photos") : TranslateT("a photo");
		else if (type == "video")
			newText = TranslateT("a video");
		else if (type == "animated_image")
			newText = TranslateT("a GIF");
		else
			newText = _A2T(type.c_str());

		TCHAR title[200];
		mir_sntprintf(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."));
	}
}