From 62f6aae6c313b2a2a493c5147ec831a66eba01ff Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 22 May 2016 19:21:00 -0400 Subject: Added utility function to generate tweet reply prefill --- src/client.cpp | 16 ++++++++++++++++ src/client.h | 2 ++ src/tweet.cpp | 17 +++++++++++++++++ src/tweet.h | 4 ++++ src/user.cpp | 5 +++++ src/user.h | 1 + 6 files changed, 45 insertions(+) diff --git a/src/client.cpp b/src/client.cpp index bae563f..617af62 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -420,6 +420,22 @@ namespace twitter { _user_stream.stop(); } + std::string client::generateReplyPrefill(tweet _tweet) const + { + std::ostringstream output; + output << "@" << _tweet.getAuthor().getScreenName() << " "; + + for (auto mention : _tweet.getMentions()) + { + if ((mention.first != _tweet.getAuthor().getID()) && (mention.first != _current_user.getID())) + { + output << "@" << mention.second << " "; + } + } + + return output.str(); + } + bool client::performGet(std::string url, long& response_code, std::string& result) { std::ostringstream output; diff --git a/src/client.h b/src/client.h index 750d603..901c7b5 100644 --- a/src/client.h +++ b/src/client.h @@ -84,6 +84,8 @@ namespace twitter { void startUserStream(); void stopUserStream(); + std::string generateReplyPrefill(tweet t) const; + private: friend class stream; diff --git a/src/tweet.cpp b/src/tweet.cpp index 5885b51..bf95bc0 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp @@ -18,6 +18,18 @@ namespace twitter { _text = _data.at("text"); _author = user(_data.at("user").dump()); _retweeted = _data.at("retweeted"); + + if (_data.find("entities") != _data.end()) + { + auto _entities = _data.at("entities"); + if (_entities.find("user_mentions") != _entities.end()) + { + for (auto _mention : _entities.at("user_mentions")) + { + _mentions.push_back(std::make_pair(_mention.at("id"), _mention.at("screen_name").get())); + } + } + } } tweet_id tweet::getID() const @@ -48,6 +60,11 @@ namespace twitter { return _retweeted; } + std::vector> tweet::getMentions() const + { + return _mentions; + } + tweet::operator bool() const { return _valid; diff --git a/src/tweet.h b/src/tweet.h index fba1ced..34a9cd7 100644 --- a/src/tweet.h +++ b/src/tweet.h @@ -3,6 +3,8 @@ #include #include "user.h" +#include +#include namespace twitter { @@ -17,6 +19,7 @@ namespace twitter { std::string getText() const; const user& getAuthor() const; bool isRetweet() const; + std::vector> getMentions() const; operator bool() const; @@ -26,6 +29,7 @@ namespace twitter { std::string _text; user _author; bool _retweeted; + std::vector> _mentions; }; }; diff --git a/src/user.cpp b/src/user.cpp index 9352938..0fa1e39 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -42,5 +42,10 @@ namespace twitter { { return _id == other._id; } + + bool user::operator!=(const user& other) const + { + return _id != other._id; + } }; diff --git a/src/user.h b/src/user.h index 0af40d6..1d8be99 100644 --- a/src/user.h +++ b/src/user.h @@ -18,6 +18,7 @@ namespace twitter { operator bool() const; bool operator==(const user& other) const; + bool operator!=(const user& other) const; private: bool _valid = false; -- cgit 1.4.1