From 38203b745ae1903ba0804ed5532b78d255bea635 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 5 Aug 2018 11:14:39 -0400 Subject: Added timeline polling Because the streaming API will sunset on August 16th, it is essential to implement timeline polling so that the library can still be used to access tweets. This commit breaks the current API even for clients still using the streaming API because the OAuth connection data was pulled from twitter::client into twitter::auth. Other than that, almost everything works the same, and if a client wants to update their libtwitter++ within the next week and a half, they are free to. --- src/notification.cpp | 460 +++++++++++++++++++++++++-------------------------- 1 file changed, 229 insertions(+), 231 deletions(-) (limited to 'src/notification.cpp') diff --git a/src/notification.cpp b/src/notification.cpp index 0e46112..0d09fcf 100644 --- a/src/notification.cpp +++ b/src/notification.cpp @@ -6,18 +6,16 @@ #include "client.h" namespace twitter { - + notification::type notification::getType() const { return _type; } - - notification::notification(const client& tclient, std::string data) + + notification::notification(const user& current_user, std::string data) { - const user& current_user = tclient.getUser(); - nlohmann::json json; - + try { json = nlohmann::json::parse(data); @@ -25,7 +23,7 @@ namespace twitter { { std::throw_with_nested(invalid_response(data)); } - + try { if (!json["event"].is_null()) @@ -33,48 +31,48 @@ namespace twitter { std::string event = json["event"]; user source(json["source"].dump()); user target(json["target"].dump()); - + if (event == "user_update") { _type = type::update_user; - + new(&_user) user(source); } else if (event == "block") { _type = type::block; - + new(&_user) user(target); } else if (event == "unblock") { _type = type::unblock; - + new(&_user) user(target); } else if (event == "favorite") { new(&_user_and_tweet._tweet) tweet(json["target_object"].dump()); - + if (current_user == source) { _type = type::favorite; - + new(&_user_and_tweet._user) user(target); } else { _type = type::favorited; - + new(&_user_and_tweet._user) user(source); } } else if (event == "unfavorite") { new(&_user_and_tweet._tweet) tweet(json["target_object"].dump()); - + if (current_user == source) { _type = type::unfavorite; - + new(&_user_and_tweet._user) user(target); } else { _type = type::unfavorited; - + new(&_user_and_tweet._user) user(source); } } else if (event == "follow") @@ -82,93 +80,93 @@ namespace twitter { if (current_user == source) { _type = type::follow; - + new(&_user) user(target); } else { _type = type::followed; - + new(&_user) user(source); } } else if (event == "unfollow") { _type = type::unfollow; - + new(&_user) user(target); } else if (event == "list_created") { _type = type::list_created; - + new(&_list) list(json["target_object"].dump()); } else if (event == "list_destroyed") { _type = type::list_destroyed; - + new(&_list) list(json["target_object"].dump()); } else if (event == "list_updated") { _type = type::list_updated; - + new(&_list) list(json["target_object"].dump()); } else if (event == "list_member_added") { new(&_user_and_list._list) list(json["target_object"].dump()); - + if (current_user == source) { _type = type::list_add; - + new(&_user_and_list._user) user(target); } else { _type = type::list_added; - + new(&_user_and_list._user) user(source); } } else if (event == "list_member_removed") { new(&_user_and_list._list) list(json["target_object"].dump()); - + if (current_user == source) { _type = type::list_remove; - + new(&_user_and_list._user) user(target); } else { _type = type::list_removed; - + new(&_user_and_list._user) user(source); } } else if (event == "list_member_subscribe") { new(&_user_and_list._list) list(json["target_object"].dump()); - + if (current_user == source) { _type = type::list_subscribe; - + new(&_user_and_list._user) user(target); } else { _type = type::list_subscribed; - + new(&_user_and_list._user) user(source); } } else if (event == "list_member_unsubscribe") { new(&_user_and_list._list) list(json["target_object"].dump()); - + if (current_user == source) { _type = type::list_unsubscribe; - + new(&_user_and_list._user) user(target); } else { _type = type::list_unsubscribed; - + new(&_user_and_list._user) user(source); } } else if (event == "quoted_tweet") { _type = type::quoted; - + new(&_user_and_tweet._user) user(source); new(&_user_and_tweet._tweet) tweet(json["target_object"].dump()); } else { @@ -177,7 +175,7 @@ namespace twitter { } else if (!json["warning"].is_null()) { new(&_warning) std::string(json["warning"]["message"].get()); - + auto warning_code = json["warning"]["code"].get(); if (warning_code == "FALLING_BEHIND") { @@ -191,27 +189,27 @@ namespace twitter { } else if (!json["delete"].is_null()) { _type = type::deletion; - + _user_id_and_tweet_id._tweet_id = json["delete"]["status"]["id"].get(); _user_id_and_tweet_id._user_id = json["delete"]["status"]["user_id"].get(); } else if (!json["scrub_geo"].is_null()) { _type = type::scrub_location; - + _user_id_and_tweet_id._tweet_id = json["scrub_geo"]["up_to_status_id"].get(); _user_id_and_tweet_id._user_id = json["scrub_geo"]["user_id"].get(); } else if (!json["limit"].is_null()) { _type = type::limit; - + _limit = json["limit"]["track"].get(); } else if (!json["status_withheld"].is_null()) { _type = type::withhold_status; - + _withhold_status._user_id = json["status_withheld"]["user_id"].get(); _withhold_status._tweet_id = json["status_withheld"]["id"].get(); - + new(&_withhold_status._countries) std::vector(); for (auto s : json["status_withheld"]["withheld_in_countries"]) { @@ -220,9 +218,9 @@ namespace twitter { } else if (!json["user_withheld"].is_null()) { _type = type::withhold_user; - + _withhold_user._user_id = json["user_withheld"]["id"].get(); - + new(&_withhold_user._countries) std::vector(); for (auto s : json["user_withheld"]["withheld_in_countries"]) { @@ -231,7 +229,7 @@ namespace twitter { } else if (!json["disconnect"].is_null()) { _type = type::disconnect; - + switch (json["disconnect"]["code"].get()) { case 1: _disconnect = disconnect_code::shutdown; break; @@ -249,16 +247,16 @@ namespace twitter { } else if (!json["friends"].is_null()) { _type = type::friends; - + new(&_friends) std::set(std::begin(json["friends"]), std::end(json["friends"])); } else if (!json["direct_message"].is_null()) { _type = type::direct; - + new(&_direct_message) direct_message(json["direct_message"].dump()); } else { _type = type::tweet; - + new(&_tweet) tweet(data); } } catch (const std::domain_error& error) @@ -266,20 +264,20 @@ namespace twitter { std::throw_with_nested(invalid_response(data)); } } - + notification::notification(const notification& other) { _type = other._type; - + switch (_type) { case type::tweet: { new(&_tweet) tweet(other._tweet); - + break; } - + case type::update_user: case type::block: case type::unblock: @@ -288,10 +286,10 @@ namespace twitter { case type::unfollow: { new(&_user) user(other._user); - + break; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -300,19 +298,19 @@ namespace twitter { { new(&_user_and_tweet._user) user(other._user_and_tweet._user); new(&_user_and_tweet._tweet) tweet(other._user_and_tweet._tweet); - + break; } - + case type::list_created: case type::list_destroyed: case type::list_updated: { new(&_list) list(other._list); - + break; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -324,73 +322,73 @@ namespace twitter { { new(&_user_and_list._user) user(other._user_and_list._user); new(&_user_and_list._list) list(other._user_and_list._list); - + break; } - + case type::stall: case type::follow_limit: case type::unknown_warning: { new(&_warning) std::string(other._warning); - + break; } - + case type::deletion: case type::scrub_location: { _user_id_and_tweet_id._user_id = other._user_id_and_tweet_id._user_id; _user_id_and_tweet_id._tweet_id = other._user_id_and_tweet_id._tweet_id; - + break; } - + case type::limit: { _limit = other._limit; - + break; } - + case type::withhold_status: { _withhold_status._user_id = other._withhold_status._user_id; _withhold_status._tweet_id = other._withhold_status._tweet_id; new(&_withhold_status._countries) std::vector(other._withhold_status._countries); - + break; } - + case type::withhold_user: { _withhold_user._user_id = other._withhold_user._user_id; new(&_withhold_user._countries) std::vector(other._withhold_user._countries); - + break; } - + case type::disconnect: { _disconnect = other._disconnect; - + break; } - + case type::friends: { new(&_friends) std::set(other._friends); - + break; } - + case type::direct: { new(&_direct_message) direct_message(other._direct_message); - + break; } - + case type::unknown: case type::invalid: { @@ -398,19 +396,19 @@ namespace twitter { } } } - + notification::notification(notification&& other) : notification() { swap(*this, other); } - + notification& notification::operator=(notification other) { swap(*this, other); - + return *this; } - + notification::~notification() { switch (_type) @@ -418,10 +416,10 @@ namespace twitter { case type::tweet: { _tweet.~tweet(); - + break; } - + case type::update_user: case type::block: case type::unblock: @@ -430,10 +428,10 @@ namespace twitter { case type::unfollow: { _user.~user(); - + break; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -442,19 +440,19 @@ namespace twitter { { _user_and_tweet._user.~user(); _user_and_tweet._tweet.~tweet(); - + break; } - + case type::list_created: case type::list_destroyed: case type::list_updated: { _list.~list(); - + break; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -466,51 +464,51 @@ namespace twitter { { _user_and_list._user.~user(); _user_and_list._list.~list(); - + break; } - + case type::stall: case type::follow_limit: case type::unknown_warning: { using string_type = std::string; _warning.~string_type(); - + break; } - + case type::withhold_status: { using list_type = std::vector; _withhold_status._countries.~list_type(); - + break; } - + case type::withhold_user: { using list_type = std::vector; _withhold_user._countries.~list_type(); - + break; } - + case type::friends: { using list_type = std::set; _friends.~list_type(); - + break; } - + case type::direct: { _direct_message.~direct_message(); - + break; } - + case type::deletion: case type::scrub_location: case type::limit: @@ -522,11 +520,11 @@ namespace twitter { } } } - + void swap(notification& first, notification& second) { using type = notification::type; - + type tempType = first._type; tweet tempTweet; user tempUser; @@ -539,16 +537,16 @@ namespace twitter { disconnect_code tempDisconnectCode; std::set tempFriends; direct_message tempDirectMessage; - + switch (first._type) { case type::tweet: { tempTweet = std::move(first._tweet); - + break; } - + case type::update_user: case type::block: case type::unblock: @@ -557,10 +555,10 @@ namespace twitter { case type::unfollow: { tempUser = std::move(first._user); - + break; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -569,19 +567,19 @@ namespace twitter { { tempTweet = std::move(first._user_and_tweet._tweet); tempUser = std::move(first._user_and_tweet._user); - + break; } - + case type::list_created: case type::list_destroyed: case type::list_updated: { tempList = std::move(first._list); - + break; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -593,97 +591,97 @@ namespace twitter { { tempList = std::move(first._user_and_list._list); tempUser = std::move(first._user_and_list._user); - + break; } - + case type::stall: case type::follow_limit: case type::unknown_warning: { tempWarning = std::move(first._warning); - + break; } - + case type::deletion: case type::scrub_location: { tempUserId = first._user_id_and_tweet_id._user_id; tempTweetId = first._user_id_and_tweet_id._tweet_id; - + break; } - + case type::limit: { tempLimit = first._limit; - + break; } - + case type::withhold_status: { tempUserId = first._withhold_status._user_id; tempTweetId = first._withhold_status._tweet_id; tempCountries = std::move(first._withhold_status._countries); - + break; } - + case type::withhold_user: { tempUserId = first._withhold_user._user_id; tempCountries = std::move(first._withhold_user._countries); - + break; } - + case type::disconnect: { tempDisconnectCode = first._disconnect; - + break; } - + case type::friends: { tempFriends = std::move(first._friends); - + break; } - + case type::direct: { tempDirectMessage = std::move(first._direct_message); - + break; } - + case type::invalid: case type::unknown: { break; } } - + first.~notification(); - + first._type = second._type; - + // Okay now you need to initialize the first with the data from the second // And then destruct the second and initialize it with the data stored in temp // This is hell - + switch (second._type) { case type::tweet: { new(&first._tweet) tweet(std::move(second._tweet)); - + break; } - + case type::update_user: case type::block: case type::unblock: @@ -692,10 +690,10 @@ namespace twitter { case type::unfollow: { new(&first._user) user(std::move(second._user)); - + break; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -704,19 +702,19 @@ namespace twitter { { new(&first._user_and_tweet._user) user(std::move(second._user_and_tweet._user)); new(&first._user_and_tweet._tweet) tweet(std::move(second._user_and_tweet._tweet)); - + break; } - + case type::list_created: case type::list_destroyed: case type::list_updated: { new(&first._list) list(std::move(second._list)); - + break; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -728,94 +726,94 @@ namespace twitter { { new(&first._user_and_list._user) user(std::move(second._user_and_list._user)); new(&first._user_and_list._list) list(std::move(second._user_and_list._list)); - + break; } - + case type::stall: case type::follow_limit: case type::unknown_warning: { new(&first._warning) std::string(std::move(second._warning)); - + break; } - + case type::deletion: case type::scrub_location: { first._user_id_and_tweet_id._user_id = second._user_id_and_tweet_id._user_id; first._user_id_and_tweet_id._tweet_id = second._user_id_and_tweet_id._tweet_id; - + break; } - + case type::limit: { first._limit = second._limit; - + break; } - + case type::withhold_status: { first._withhold_status._user_id = second._withhold_status._user_id; first._withhold_status._tweet_id = second._withhold_status._tweet_id; new(&first._withhold_status._countries) std::vector(std::move(second._withhold_status._countries)); - + break; } - + case type::withhold_user: { first._withhold_user._user_id = second._withhold_user._user_id; new(&first._withhold_user._countries) std::vector(std::move(second._withhold_user._countries)); - + break; } - + case type::disconnect: { first._disconnect = second._disconnect; - + break; } - + case type::friends: { new(&first._friends) std::set(std::move(second._friends)); - + break; } - + case type::direct: { new(&first._direct_message) direct_message(std::move(second._direct_message)); - + break; } - + case type::invalid: case type::unknown: { break; } } - + // Now destruct the second and initialize it with data from the first second.~notification(); - + second._type = tempType; - + switch (tempType) { case type::tweet: { new(&second._tweet) tweet(std::move(tempTweet)); - + break; } - + case type::update_user: case type::block: case type::unblock: @@ -824,10 +822,10 @@ namespace twitter { case type::unfollow: { new(&second._user) user(std::move(tempUser)); - + break; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -836,19 +834,19 @@ namespace twitter { { new(&second._user_and_tweet._user) user(std::move(tempUser)); new(&second._user_and_tweet._tweet) tweet(std::move(tempTweet)); - + break; } - + case type::list_created: case type::list_destroyed: case type::list_updated: { new(&second._list) list(std::move(tempList)); - + break; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -860,73 +858,73 @@ namespace twitter { { new(&second._user_and_list._user) user(std::move(tempUser)); new(&second._user_and_list._list) list(std::move(tempList)); - + break; } - + case type::stall: case type::follow_limit: case type::unknown_warning: { new(&second._warning) std::string(std::move(tempWarning)); - + break; } - + case type::deletion: case type::scrub_location: { second._user_id_and_tweet_id._user_id = tempUserId; second._user_id_and_tweet_id._tweet_id = tempTweetId; - + break; } - + case type::limit: { second._limit = tempLimit; - + break; } - + case type::withhold_status: { second._withhold_status._user_id = tempUserId; second._withhold_status._tweet_id = tempTweetId; new(&second._withhold_status._countries) std::vector(std::move(tempCountries)); - + break; } - + case type::withhold_user: { second._withhold_user._user_id = tempUserId; new(&second._withhold_user._countries) std::vector(std::move(tempCountries)); - + break; } - + case type::disconnect: { second._disconnect = tempDisconnectCode; - + break; } - + case type::friends: { new(&second._friends) std::set(std::move(tempFriends)); - + break; } - + case type::direct: { new(&second._direct_message) direct_message(std::move(tempDirectMessage)); - + break; } - + case type::invalid: case type::unknown: { @@ -934,7 +932,7 @@ namespace twitter { } } } - + const tweet& notification::getTweet() const { switch (_type) @@ -943,7 +941,7 @@ namespace twitter { { return _tweet; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -952,14 +950,14 @@ namespace twitter { { return _user_and_tweet._tweet; } - + default: { assert(false); } } } - + const user& notification::getUser() const { switch (_type) @@ -973,7 +971,7 @@ namespace twitter { { return _user; } - + case type::favorite: case type::favorited: case type::unfavorite: @@ -982,7 +980,7 @@ namespace twitter { { return _user_and_tweet._user; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -994,25 +992,25 @@ namespace twitter { { return _user_and_list._user; } - + default: { assert(false); } } } - + const list& notification::getList() const { switch (_type) - { + { case type::list_created: case type::list_destroyed: case type::list_updated: { return _list; } - + case type::list_add: case type::list_added: case type::list_remove: @@ -1024,14 +1022,14 @@ namespace twitter { { return _user_and_list._list; } - + default: { assert(false); } } } - + tweet_id notification::getTweetID() const { switch (_type) @@ -1041,19 +1039,19 @@ namespace twitter { { return _user_id_and_tweet_id._tweet_id; } - + case type::withhold_status: { return _withhold_status._tweet_id; } - + default: { assert(false); } } } - + void notification::setTweetID(tweet_id _arg) { switch (_type) @@ -1063,34 +1061,34 @@ namespace twitter { { _user_id_and_tweet_id._tweet_id = _arg;; } - + case type::withhold_status: { _withhold_status._tweet_id = _arg; } - + default: { assert(false); } } } - + user_id notification::getUserID() const { switch (_type) - { + { case type::deletion: case type::scrub_location: { return _user_id_and_tweet_id._user_id; } - + case type::withhold_status: { return _withhold_status._user_id; } - + case type::withhold_user: { return _withhold_user._user_id; @@ -1102,22 +1100,22 @@ namespace twitter { } } } - + void notification::setUserID(user_id _arg) { switch (_type) - { + { case type::deletion: case type::scrub_location: { _user_id_and_tweet_id._user_id = _arg; } - + case type::withhold_status: { _withhold_status._user_id = _arg; } - + case type::withhold_user: { _withhold_user._user_id = _arg; @@ -1129,16 +1127,16 @@ namespace twitter { } } } - + const std::vector& notification::getCountries() const { switch (_type) - { + { case type::withhold_status: { return _withhold_status._countries; } - + case type::withhold_user: { return _withhold_user._countries; @@ -1150,49 +1148,49 @@ namespace twitter { } } } - + disconnect_code notification::getDisconnectCode() const { assert(_type == type::disconnect); - + return _disconnect; } - + void notification::setDisconnectCode(disconnect_code _arg) { assert(_type == type::disconnect); - + _disconnect = _arg; } - + const std::set& notification::getFriends() const { assert(_type == type::friends); - + return _friends; } - + const direct_message& notification::getDirectMessage() const { assert(_type == type::direct); - + return _direct_message; } - + int notification::getLimit() const { assert(_type == type::limit); - + return _limit; } - + void notification::setLimit(int _arg) { assert(_type == type::limit); - + _limit = _arg; } - + const std::string& notification::getWarning() const { switch (_type) @@ -1203,12 +1201,12 @@ namespace twitter { { return _warning; } - + default: { assert(false); } } } - + }; -- cgit 1.4.1