#include "tweet.h" #include #include "util.h" #include "codes.h" #include "client.h" namespace twitter { tweet::tweet(const client& tclient, std::string data) try : _client(tclient) { auto json = nlohmann::json::parse(data); _id = json["id"].get(); _text = json["text"].get(); _author = make_unique(_client, json["user"].dump()); if (!json["retweeted_status"].is_null()) { _is_retweet = true; _retweeted_status = make_unique(_client, json["retweeted_status"].dump()); } if (!json["entities"].is_null()) { auto entities = json["entities"]; if (!entities["user_mentions"].is_null()) { for (auto mention : entities["user_mentions"]) { _mentions.push_back(std::make_pair(mention["id"].get(), mention["screen_name"].get())); } } } } catch (const std::invalid_argument& error) { std::throw_with_nested(malformed_object("tweet", data)); } catch (const std::domain_error& error) { std::throw_with_nested(malformed_object("tweet", data)); } std::string tweet::generateReplyPrefill() const { std::ostringstream output; output << "@" << _author->getScreenName() << " "; for (auto mention : _mentions) { if ((mention.first != _author->getID()) && (mention.first != _client.getUser().getID())) { output << "@" << mention.second << " "; } } return output.str(); } tweet tweet::reply(std::string message, std::list media_ids) const { return _client.replyToTweet(message, _id, media_ids); } bool tweet::isMyTweet() const { return *_author == _client.getUser(); } };