From 7c44fd17bb6be54a2ea4b60761e91053ca988977 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 29 Nov 2016 16:18:25 -0500 Subject: Made tweets, users, and notifications into copyable objects Notifications are now also mutable. Users and tweets no longer have helper methods for interacting with the client. Fixed a bug (possibly introduced by a change to the Twitter API) that caused non-reply tweets to be marked as unknown notifications. --- src/notification.cpp | 518 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 434 insertions(+), 84 deletions(-) (limited to 'src/notification.cpp') diff --git a/src/notification.cpp b/src/notification.cpp index 3269a90..0e46112 100644 --- a/src/notification.cpp +++ b/src/notification.cpp @@ -28,16 +28,11 @@ namespace twitter { try { - if (!json["in_reply_to_status_id"].is_null()) - { - _type = type::tweet; - - new(&_tweet) tweet(tclient, data); - } else if (!json["event"].is_null()) + if (!json["event"].is_null()) { std::string event = json["event"]; - user source(tclient, json["source"].dump()); - user target(tclient, json["target"].dump()); + user source(json["source"].dump()); + user target(json["target"].dump()); if (event == "user_update") { @@ -56,7 +51,7 @@ namespace twitter { new(&_user) user(target); } else if (event == "favorite") { - new(&_user_and_tweet._tweet) tweet(tclient, json["target_object"].dump()); + new(&_user_and_tweet._tweet) tweet(json["target_object"].dump()); if (current_user == source) { @@ -70,7 +65,7 @@ namespace twitter { } } else if (event == "unfavorite") { - new(&_user_and_tweet._tweet) tweet(tclient, json["target_object"].dump()); + new(&_user_and_tweet._tweet) tweet(json["target_object"].dump()); if (current_user == source) { @@ -175,7 +170,9 @@ namespace twitter { _type = type::quoted; new(&_user_and_tweet._user) user(source); - new(&_user_and_tweet._tweet) tweet(tclient, json["target_object"].dump()); + new(&_user_and_tweet._tweet) tweet(json["target_object"].dump()); + } else { + _type = type::unknown; } } else if (!json["warning"].is_null()) { @@ -260,7 +257,9 @@ namespace twitter { new(&_direct_message) direct_message(json["direct_message"].dump()); } else { - _type = type::unknown; + _type = type::tweet; + + new(&_tweet) tweet(data); } } catch (const std::domain_error& error) { @@ -268,7 +267,7 @@ namespace twitter { } } - notification::notification(notification&& other) + notification::notification(const notification& other) { _type = other._type; @@ -276,7 +275,7 @@ namespace twitter { { case type::tweet: { - new(&_tweet) tweet(std::move(other._tweet)); + new(&_tweet) tweet(other._tweet); break; } @@ -288,7 +287,7 @@ namespace twitter { case type::followed: case type::unfollow: { - new(&_user) user(std::move(other._user)); + new(&_user) user(other._user); break; } @@ -299,8 +298,8 @@ namespace twitter { case type::unfavorited: case type::quoted: { - new(&_user_and_tweet._user) user(std::move(other._user_and_tweet._user)); - new(&_user_and_tweet._tweet) tweet(std::move(other._user_and_tweet._tweet)); + new(&_user_and_tweet._user) user(other._user_and_tweet._user); + new(&_user_and_tweet._tweet) tweet(other._user_and_tweet._tweet); break; } @@ -309,7 +308,7 @@ namespace twitter { case type::list_destroyed: case type::list_updated: { - new(&_list) list(std::move(other._list)); + new(&_list) list(other._list); break; } @@ -323,8 +322,8 @@ namespace twitter { case type::list_unsubscribe: case type::list_unsubscribed: { - new(&_user_and_list._user) user(std::move(other._user_and_list._user)); - new(&_user_and_list._list) list(std::move(other._user_and_list._list)); + new(&_user_and_list._user) user(other._user_and_list._user); + new(&_user_and_list._list) list(other._user_and_list._list); break; } @@ -333,7 +332,7 @@ namespace twitter { case type::follow_limit: case type::unknown_warning: { - new(&_warning) std::string(std::move(other._warning)); + new(&_warning) std::string(other._warning); break; } @@ -358,7 +357,7 @@ namespace twitter { { _withhold_status._user_id = other._withhold_status._user_id; _withhold_status._tweet_id = other._withhold_status._tweet_id; - new(&_withhold_status._countries) std::vector(std::move(other._withhold_status._countries)); + new(&_withhold_status._countries) std::vector(other._withhold_status._countries); break; } @@ -366,7 +365,7 @@ namespace twitter { case type::withhold_user: { _withhold_user._user_id = other._withhold_user._user_id; - new(&_withhold_user._countries) std::vector(std::move(other._withhold_user._countries)); + new(&_withhold_user._countries) std::vector(other._withhold_user._countries); break; } @@ -380,14 +379,14 @@ namespace twitter { case type::friends: { - new(&_friends) std::set(std::move(other._friends)); + new(&_friends) std::set(other._friends); break; } case type::direct: { - new(&_direct_message) direct_message(std::move(other._direct_message)); + new(&_direct_message) direct_message(other._direct_message); break; } @@ -400,17 +399,25 @@ namespace twitter { } } - notification& notification::operator=(notification&& other) + notification::notification(notification&& other) : notification() { - this->~notification(); - - _type = other._type; + swap(*this, other); + } + + notification& notification::operator=(notification other) + { + swap(*this, other); + return *this; + } + + notification::~notification() + { switch (_type) { case type::tweet: { - new(&_tweet) tweet(std::move(other._tweet)); + _tweet.~tweet(); break; } @@ -422,7 +429,7 @@ namespace twitter { case type::followed: case type::unfollow: { - new(&_user) user(std::move(other._user)); + _user.~user(); break; } @@ -433,8 +440,8 @@ namespace twitter { case type::unfavorited: case type::quoted: { - new(&_user_and_tweet._user) user(std::move(other._user_and_tweet._user)); - new(&_user_and_tweet._tweet) tweet(std::move(other._user_and_tweet._tweet)); + _user_and_tweet._user.~user(); + _user_and_tweet._tweet.~tweet(); break; } @@ -443,7 +450,7 @@ namespace twitter { case type::list_destroyed: case type::list_updated: { - new(&_list) list(std::move(other._list)); + _list.~list(); break; } @@ -457,8 +464,8 @@ namespace twitter { case type::list_unsubscribe: case type::list_unsubscribed: { - new(&_user_and_list._user) user(std::move(other._user_and_list._user)); - new(&_user_and_list._list) list(std::move(other._user_and_list._list)); + _user_and_list._user.~user(); + _user_and_list._list.~list(); break; } @@ -467,65 +474,192 @@ namespace twitter { case type::follow_limit: case type::unknown_warning: { - new(&_warning) std::string(std::move(other._warning)); + using string_type = std::string; + _warning.~string_type(); break; } - case type::deletion: - case type::scrub_location: + case type::withhold_status: { - _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; - + using list_type = std::vector; + _withhold_status._countries.~list_type(); + break; } - case type::limit: + case type::withhold_user: { - _limit = other._limit; + using list_type = std::vector; + _withhold_user._countries.~list_type(); break; } - - case type::withhold_status: + + case type::friends: { - _withhold_status._user_id = other._withhold_status._user_id; - _withhold_status._tweet_id = other._withhold_status._tweet_id; - new(&_withhold_status._countries) std::vector(std::move(other._withhold_status._countries)); + using list_type = std::set; + _friends.~list_type(); break; } - case type::withhold_user: + case type::direct: { - _withhold_user._user_id = other._withhold_user._user_id; - new(&_withhold_user._countries) std::vector(std::move(other._withhold_user._countries)); + _direct_message.~direct_message(); break; } + case type::deletion: + case type::scrub_location: + case type::limit: case type::disconnect: + case type::unknown: + case type::invalid: { - _disconnect = other._disconnect; + break; + } + } + } + + void swap(notification& first, notification& second) + { + using type = notification::type; + + type tempType = first._type; + tweet tempTweet; + user tempUser; + list tempList; + std::string tempWarning; + user_id tempUserId; + tweet_id tempTweetId; + int tempLimit; + std::vector tempCountries; + disconnect_code tempDisconnectCode; + std::set tempFriends; + direct_message tempDirectMessage; + + switch (first._type) + { + case type::tweet: + { + tempTweet = std::move(first._tweet); break; } - case type::friends: + case type::update_user: + case type::block: + case type::unblock: + case type::follow: + case type::followed: + case type::unfollow: { - new(&_friends) std::set(std::move(other._friends)); - + tempUser = std::move(first._user); + break; } + + case type::favorite: + case type::favorited: + case type::unfavorite: + case type::unfavorited: + case type::quoted: + { + tempTweet = std::move(first._user_and_tweet._tweet); + tempUser = std::move(first._user_and_tweet._user); - case type::direct: + break; + } + + case type::list_created: + case type::list_destroyed: + case type::list_updated: { - new(&_direct_message) direct_message(std::move(other._direct_message)); - + tempList = std::move(first._list); + + break; + } + + case type::list_add: + case type::list_added: + case type::list_remove: + case type::list_removed: + case type::list_subscribe: + case type::list_subscribed: + case type::list_unsubscribe: + case type::list_unsubscribed: + { + 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: { @@ -533,16 +667,19 @@ namespace twitter { } } - return *this; - } - - notification::~notification() - { - switch (_type) + 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: { - _tweet.~tweet(); + new(&first._tweet) tweet(std::move(second._tweet)); break; } @@ -554,7 +691,7 @@ namespace twitter { case type::followed: case type::unfollow: { - _user.~user(); + new(&first._user) user(std::move(second._user)); break; } @@ -565,8 +702,8 @@ namespace twitter { case type::unfavorited: case type::quoted: { - _user_and_tweet._user.~user(); - _user_and_tweet._tweet.~tweet(); + 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; } @@ -575,7 +712,7 @@ namespace twitter { case type::list_destroyed: case type::list_updated: { - _list.~list(); + new(&first._list) list(std::move(second._list)); break; } @@ -589,8 +726,8 @@ namespace twitter { case type::list_unsubscribe: case type::list_unsubscribed: { - _user_and_list._user.~user(); - _user_and_list._list.~list(); + 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; } @@ -599,49 +736,199 @@ namespace twitter { case type::follow_limit: case type::unknown_warning: { - using string_type = std::string; - _warning.~string_type(); + 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: { - using list_type = std::vector; - _withhold_status._countries.~list_type(); - + 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: { - using list_type = std::vector; - _withhold_user._countries.~list_type(); + 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: { - using list_type = std::set; - _friends.~list_type(); + new(&first._friends) std::set(std::move(second._friends)); break; } case type::direct: { - _direct_message.~direct_message(); + 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: + case type::follow: + case type::followed: + case type::unfollow: + { + new(&second._user) user(std::move(tempUser)); + + break; + } + + case type::favorite: + case type::favorited: + case type::unfavorite: + case type::unfavorited: + case type::quoted: + { + 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: + case type::list_removed: + case type::list_subscribe: + case type::list_subscribed: + case type::list_unsubscribe: + case type::list_unsubscribed: + { + 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: - case type::unknown: + { + 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: { break; } @@ -767,6 +1054,28 @@ namespace twitter { } } + void notification::setTweetID(tweet_id _arg) + { + switch (_type) + { + case type::deletion: + case type::scrub_location: + { + _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) @@ -794,6 +1103,33 @@ 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; + } + + default: + { + assert(false); + } + } + } + const std::vector& notification::getCountries() const { switch (_type) @@ -822,6 +1158,13 @@ namespace twitter { return _disconnect; } + void notification::setDisconnectCode(disconnect_code _arg) + { + assert(_type == type::disconnect); + + _disconnect = _arg; + } + const std::set& notification::getFriends() const { assert(_type == type::friends); @@ -843,6 +1186,13 @@ namespace twitter { return _limit; } + void notification::setLimit(int _arg) + { + assert(_type == type::limit); + + _limit = _arg; + } + const std::string& notification::getWarning() const { switch (_type) -- cgit 1.4.1