about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.cpp88
-rw-r--r--src/client.h2
-rw-r--r--src/codes.h3
-rw-r--r--src/notification.cpp375
4 files changed, 270 insertions, 198 deletions
diff --git a/src/client.cpp b/src/client.cpp index cf2b5c4..1b32b8b 100644 --- a/src/client.cpp +++ b/src/client.cpp
@@ -105,7 +105,12 @@ namespace twitter {
105 std::string response_data; 105 std::string response_data;
106 if (performGet(url, response_code, response_data) && (response_code == 200)) 106 if (performGet(url, response_code, response_data) && (response_code == 200))
107 { 107 {
108 _current_user = user(response_data); 108 try {
109 _current_user = user(response_data);
110 } catch (std::invalid_argument e)
111 {
112 // Ignore
113 }
109 } 114 }
110 } 115 }
111 116
@@ -145,8 +150,13 @@ namespace twitter {
145 150
146 if (response_code == 200) 151 if (response_code == 200)
147 { 152 {
148 result = tweet(response_data); 153 try {
149 return response::ok; 154 result = tweet(response_data);
155 return response::ok;
156 } catch (std::invalid_argument e)
157 {
158 return response::invalid_response;
159 }
150 } else { 160 } else {
151 return codeForError(response_code, response_data); 161 return codeForError(response_code, response_data);
152 } 162 }
@@ -186,7 +196,14 @@ namespace twitter {
186 return codeForError(response_code, response_data); 196 return codeForError(response_code, response_data);
187 } 197 }
188 198
189 auto response_json = json::parse(response_data); 199 json response_json;
200 try {
201 response_json = json::parse(response_data);
202 } catch (std::invalid_argument e)
203 {
204 return response::invalid_response;
205 }
206
190 media_id = response_json["media_id"].get<long>(); 207 media_id = response_json["media_id"].get<long>();
191 208
192 curl_httppost* append_form_post = nullptr; 209 curl_httppost* append_form_post = nullptr;
@@ -226,7 +243,13 @@ namespace twitter {
226 return codeForError(response_code, response_data); 243 return codeForError(response_code, response_data);
227 } 244 }
228 245
229 response_json = json::parse(response_data); 246 try {
247 response_json = json::parse(response_data);
248 } catch (std::invalid_argument e)
249 {
250 return response::invalid_response;
251 }
252
230 if (response_json.find("processing_info") != response_json.end()) 253 if (response_json.find("processing_info") != response_json.end())
231 { 254 {
232 std::stringstream datastr; 255 std::stringstream datastr;
@@ -244,7 +267,13 @@ namespace twitter {
244 return codeForError(response_code, response_data); 267 return codeForError(response_code, response_data);
245 } 268 }
246 269
247 response_json = json::parse(response_data); 270 try {
271 response_json = json::parse(response_data);
272 } catch (std::invalid_argument e)
273 {
274 return response::invalid_response;
275 }
276
248 if (response_json["processing_info"]["state"] == "succeeded") 277 if (response_json["processing_info"]["state"] == "succeeded")
249 { 278 {
250 break; 279 break;
@@ -316,9 +345,26 @@ namespace twitter {
316 return unfollow(toUnfollow.getID()); 345 return unfollow(toUnfollow.getID());
317 } 346 }
318 347
319 const user& client::getUser() const 348 response client::getUser(user& result)
320 { 349 {
321 return _current_user; 350 if (!_current_user)
351 {
352 std::string url = "https://api.twitter.com/1.1/account/verify_credentials.json";
353 long response_code;
354 std::string response_data;
355 if (performGet(url, response_code, response_data) && (response_code == 200))
356 {
357 try {
358 _current_user = user(response_data);
359 } catch (std::invalid_argument e)
360 {
361 return response::invalid_response;
362 }
363 }
364 }
365
366 result = _current_user;
367 return response::ok;
322 } 368 }
323 369
324 configuration client::getConfiguration() 370 configuration client::getConfiguration()
@@ -366,7 +412,14 @@ namespace twitter {
366 412
367 if (response_code == 200) 413 if (response_code == 200)
368 { 414 {
369 json rjs = json::parse(response_data); 415 json rjs;
416 try {
417 rjs = json::parse(response_data);
418 } catch (std::invalid_argument e)
419 {
420 return response::invalid_response;
421 }
422
370 cursor = rjs.at("next_cursor"); 423 cursor = rjs.at("next_cursor");
371 result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); 424 result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids")));
372 } else { 425 } else {
@@ -408,7 +461,14 @@ namespace twitter {
408 461
409 if (response_code == 200) 462 if (response_code == 200)
410 { 463 {
411 json rjs = json::parse(response_data); 464 json rjs;
465 try {
466 rjs = json::parse(response_data);
467 } catch (std::invalid_argument e)
468 {
469 return response::invalid_response;
470 }
471
412 cursor = rjs.at("next_cursor"); 472 cursor = rjs.at("next_cursor");
413 result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); 473 result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids")));
414 } else { 474 } else {
@@ -560,7 +620,13 @@ namespace twitter {
560 620
561 response client::codeForError(int response_code, std::string response_data) const 621 response client::codeForError(int response_code, std::string response_data) const
562 { 622 {
563 auto response_json = json::parse(response_data); 623 json response_json;
624 try {
625 response_json = json::parse(response_data);
626 } catch (std::invalid_argument e)
627 {
628 return response::invalid_response;
629 }
564 630
565 std::set<int> error_codes; 631 std::set<int> error_codes;
566 if (response_json.find("errors") != response_json.end()) 632 if (response_json.find("errors") != response_json.end())
diff --git a/src/client.h b/src/client.h index f96022d..6963412 100644 --- a/src/client.h +++ b/src/client.h
@@ -80,7 +80,7 @@ namespace twitter {
80 response getFriends(std::set<user_id>& result); 80 response getFriends(std::set<user_id>& result);
81 response getFollowers(std::set<user_id>& result); 81 response getFollowers(std::set<user_id>& result);
82 82
83 const user& getUser() const; 83 response getUser(user& result);
84 84
85 configuration getConfiguration(); 85 configuration getConfiguration();
86 86
diff --git a/src/codes.h b/src/codes.h index 334f0ce..6767805 100644 --- a/src/codes.h +++ b/src/codes.h
@@ -21,7 +21,8 @@ namespace twitter {
21 write_restricted, 21 write_restricted,
22 bad_length, 22 bad_length,
23 unknown_error, 23 unknown_error,
24 invalid_media 24 invalid_media,
25 invalid_response
25 }; 26 };
26 27
27}; 28};
diff --git a/src/notification.cpp b/src/notification.cpp index 3dcdd90..0397ce5 100644 --- a/src/notification.cpp +++ b/src/notification.cpp
@@ -19,240 +19,245 @@ namespace twitter {
19 19
20 notification::notification(std::string data, const user& current_user) 20 notification::notification(std::string data, const user& current_user)
21 { 21 {
22 auto _data = json::parse(data); 22 try {
23 auto _data = json::parse(data);
23 24
24 if (_data.find("in_reply_to_status_id") != _data.end()) 25 if (_data.find("in_reply_to_status_id") != _data.end())
25 {
26 _type = type::tweet;
27
28 new(&_tweet) tweet(data);
29 } else if (_data.find("event") != _data.end())
30 {
31 std::string event = _data.at("event");
32 user source(_data.at("source").dump());
33 user target(_data.at("target").dump());
34
35 if (event == "user_update")
36 { 26 {
37 _type = type::update_user; 27 _type = type::tweet;
38 28
39 new(&_user) user(source); 29 new(&_tweet) tweet(data);
40 } else if (event == "block") 30 } else if (_data.find("event") != _data.end())
41 { 31 {
42 _type = type::block; 32 std::string event = _data.at("event");
33 user source(_data.at("source").dump());
34 user target(_data.at("target").dump());
35
36 if (event == "user_update")
37 {
38 _type = type::update_user;
43 39
44 new(&_user) user(target); 40 new(&_user) user(source);
45 } else if (event == "unblock") 41 } else if (event == "block")
46 { 42 {
47 _type = type::unblock; 43 _type = type::block;
48 44
49 new(&_user) user(target); 45 new(&_user) user(target);
50 } else if (event == "favorite") 46 } else if (event == "unblock")
51 { 47 {
52 new(&_user_and_tweet._tweet) tweet(_data.at("target_object").dump()); 48 _type = type::unblock;
53 49
54 if (current_user == source) 50 new(&_user) user(target);
51 } else if (event == "favorite")
55 { 52 {
56 _type = type::favorite; 53 new(&_user_and_tweet._tweet) tweet(_data.at("target_object").dump());
54
55 if (current_user == source)
56 {
57 _type = type::favorite;
57 58
58 new(&_user_and_tweet._user) user(target); 59 new(&_user_and_tweet._user) user(target);
59 } else { 60 } else {
60 _type = type::favorited; 61 _type = type::favorited;
61 62
62 new(&_user_and_tweet._user) user(source); 63 new(&_user_and_tweet._user) user(source);
63 } 64 }
64 } else if (event == "unfavorite") 65 } else if (event == "unfavorite")
65 {
66 new(&_user_and_tweet._tweet) tweet(_data.at("target_object").dump());
67
68 if (current_user == source)
69 { 66 {
70 _type = type::unfavorite; 67 new(&_user_and_tweet._tweet) tweet(_data.at("target_object").dump());
68
69 if (current_user == source)
70 {
71 _type = type::unfavorite;
71 72
72 new(&_user_and_tweet._user) user(target); 73 new(&_user_and_tweet._user) user(target);
73 } else { 74 } else {
74 _type = type::unfavorited; 75 _type = type::unfavorited;
75 76
76 new(&_user_and_tweet._user) user(source); 77 new(&_user_and_tweet._user) user(source);
77 } 78 }
78 } else if (event == "follow") 79 } else if (event == "follow")
79 {
80 if (current_user == source)
81 { 80 {
82 _type = type::follow; 81 if (current_user == source)
82 {
83 _type = type::follow;
83 84
84 new(&_user) user(target); 85 new(&_user) user(target);
85 } else { 86 } else {
86 _type = type::followed; 87 _type = type::followed;
87 88
88 new(&_user) user(source); 89 new(&_user) user(source);
89 } 90 }
90 } else if (event == "unfollow") 91 } else if (event == "unfollow")
91 { 92 {
92 _type = type::unfollow; 93 _type = type::unfollow;
93
94 new(&_user) user(target);
95 } else if (event == "list_created")
96 {
97 _type = type::list_created;
98 94
99 new(&_list) list(_data.at("target_object").dump()); 95 new(&_user) user(target);
100 } else if (event == "list_destroyed") 96 } else if (event == "list_created")
101 { 97 {
102 _type = type::list_destroyed; 98 _type = type::list_created;
103 99
104 new(&_list) list(_data.at("target_object").dump()); 100 new(&_list) list(_data.at("target_object").dump());
105 } else if (event == "list_updated") 101 } else if (event == "list_destroyed")
106 { 102 {
107 _type = type::list_updated; 103 _type = type::list_destroyed;
108 104
109 new(&_list) list(_data.at("target_object").dump()); 105 new(&_list) list(_data.at("target_object").dump());
110 } else if (event == "list_member_added") 106 } else if (event == "list_updated")
111 { 107 {
112 new(&_user_and_list._list) list(_data.at("target_object").dump()); 108 _type = type::list_updated;
113 109
114 if (current_user == source) 110 new(&_list) list(_data.at("target_object").dump());
111 } else if (event == "list_member_added")
115 { 112 {
116 _type = type::list_add; 113 new(&_user_and_list._list) list(_data.at("target_object").dump());
114
115 if (current_user == source)
116 {
117 _type = type::list_add;
117 118
118 new(&_user_and_list._user) user(target); 119 new(&_user_and_list._user) user(target);
119 } else { 120 } else {
120 _type = type::list_added; 121 _type = type::list_added;
121 122
122 new(&_user_and_list._user) user(source); 123 new(&_user_and_list._user) user(source);
123 } 124 }
124 } else if (event == "list_member_removed") 125 } else if (event == "list_member_removed")
125 {
126 new(&_user_and_list._list) list(_data.at("target_object").dump());
127
128 if (current_user == source)
129 { 126 {
130 _type = type::list_remove; 127 new(&_user_and_list._list) list(_data.at("target_object").dump());
128
129 if (current_user == source)
130 {
131 _type = type::list_remove;
131 132
132 new(&_user_and_list._user) user(target); 133 new(&_user_and_list._user) user(target);
133 } else { 134 } else {
134 _type = type::list_removed; 135 _type = type::list_removed;
135 136
136 new(&_user_and_list._user) user(source); 137 new(&_user_and_list._user) user(source);
137 } 138 }
138 } else if (event == "list_member_subscribe") 139 } else if (event == "list_member_subscribe")
139 { 140 {
140 new(&_user_and_list._list) list(_data.at("target_object").dump()); 141 new(&_user_and_list._list) list(_data.at("target_object").dump());
141 142
142 if (current_user == source) 143 if (current_user == source)
144 {
145 _type = type::list_subscribe;
146
147 new(&_user_and_list._user) user(target);
148 } else {
149 _type = type::list_subscribed;
150
151 new(&_user_and_list._user) user(source);
152 }
153 } else if (event == "list_member_unsubscribe")
143 { 154 {
144 _type = type::list_subscribe; 155 new(&_user_and_list._list) list(_data.at("target_object").dump());
156
157 if (current_user == source)
158 {
159 _type = type::list_unsubscribe;
145 160
146 new(&_user_and_list._user) user(target); 161 new(&_user_and_list._user) user(target);
147 } else { 162 } else {
148 _type = type::list_subscribed; 163 _type = type::list_unsubscribed;
149 164
150 new(&_user_and_list._user) user(source); 165 new(&_user_and_list._user) user(source);
166 }
167 } else if (event == "quoted_tweet")
168 {
169 _type = type::quoted;
170
171 new(&_user_and_tweet._user) user(source);
172 new(&_user_and_tweet._tweet) tweet(_data.at("target_object").dump());
151 } 173 }
152 } else if (event == "list_member_unsubscribe") 174 } else if (_data.find("warning") != _data.end())
153 { 175 {
154 new(&_user_and_list._list) list(_data.at("target_object").dump()); 176 new(&_warning) std::string(_data.at("warning").at("message").get<std::string>());
155 177
156 if (current_user == source) 178 if (_data.at("warning").at("code") == "FALLING_BEHIND")
157 { 179 {
158 _type = type::list_unsubscribe; 180 _type = type::stall;
159 181 } else if (_data.at("warning").at("code") == "FOLLOWS_OVER_LIMIT")
160 new(&_user_and_list._user) user(target); 182 {
183 _type = type::follow_limit;
161 } else { 184 } else {
162 _type = type::list_unsubscribed; 185 _type = type::unknown_warning;
163
164 new(&_user_and_list._user) user(source);
165 } 186 }
166 } else if (event == "quoted_tweet") 187 } else if (_data.find("delete") != _data.end())
167 { 188 {
168 _type = type::quoted; 189 _type = type::deletion;
169
170 new(&_user_and_tweet._user) user(source);
171 new(&_user_and_tweet._tweet) tweet(_data.at("target_object").dump());
172 }
173 } else if (_data.find("warning") != _data.end())
174 {
175 new(&_warning) std::string(_data.at("warning").at("message").get<std::string>());
176 190
177 if (_data.at("warning").at("code") == "FALLING_BEHIND") 191 _user_id_and_tweet_id._tweet_id = _data.at("delete").at("status").at("id");
192 _user_id_and_tweet_id._user_id = _data.at("delete").at("status").at("user_id");
193 } else if (_data.find("scrub_geo") != _data.end())
178 { 194 {
179 _type = type::stall; 195 _type = type::scrub_location;
180 } else if (_data.at("warning").at("code") == "FOLLOWS_OVER_LIMIT") 196
197 _user_id_and_tweet_id._tweet_id = _data.at("scrub_geo").at("up_to_status_id");
198 _user_id_and_tweet_id._user_id = _data.at("scrub_geo").at("user_id");
199 } else if (_data.find("limit") != _data.end())
181 { 200 {
182 _type = type::follow_limit; 201 _type = type::limit;
183 } else {
184 _type = type::unknown_warning;
185 }
186 } else if (_data.find("delete") != _data.end())
187 {
188 _type = type::deletion;
189 202
190 _user_id_and_tweet_id._tweet_id = _data.at("delete").at("status").at("id"); 203 _limit = _data.at("limit").at("track");
191 _user_id_and_tweet_id._user_id = _data.at("delete").at("status").at("user_id"); 204 } else if (_data.find("status_withheld") != _data.end())
192 } else if (_data.find("scrub_geo") != _data.end()) 205 {
193 { 206 _type = type::withhold_status;
194 _type = type::scrub_location;
195 207
196 _user_id_and_tweet_id._tweet_id = _data.at("scrub_geo").at("up_to_status_id"); 208 _withhold_status._user_id = _data.at("status_withheld").at("user_id");
197 _user_id_and_tweet_id._user_id = _data.at("scrub_geo").at("user_id"); 209 _withhold_status._tweet_id = _data.at("status_withheld").at("id");
198 } else if (_data.find("limit") != _data.end())
199 {
200 _type = type::limit;
201 210
202 _limit = _data.at("limit").at("track"); 211 new(&_withhold_status._countries) std::vector<std::string>();
203 } else if (_data.find("status_withheld") != _data.end()) 212 for (auto s : _data.at("status_withheld").at("withheld_in_countries"))
204 { 213 {
205 _type = type::withhold_status; 214 _withhold_status._countries.push_back(s);
215 }
216 } else if (_data.find("user_withheld") != _data.end())
217 {
218 _type = type::withhold_user;
206 219
207 _withhold_status._user_id = _data.at("status_withheld").at("user_id"); 220 _withhold_user._user_id = _data.at("user_withheld").at("id");
208 _withhold_status._tweet_id = _data.at("status_withheld").at("id");
209 221
210 new(&_withhold_status._countries) std::vector<std::string>(); 222 new(&_withhold_user._countries) std::vector<std::string>();
211 for (auto s : _data.at("status_withheld").at("withheld_in_countries")) 223 for (auto s : _data.at("user_withheld").at("withheld_in_countries"))
224 {
225 _withhold_user._countries.push_back(s);
226 }
227 } else if (_data.find("disconnect") != _data.end())
212 { 228 {
213 _withhold_status._countries.push_back(s); 229 _type = type::disconnect;
214 }
215 } else if (_data.find("user_withheld") != _data.end())
216 {
217 _type = type::withhold_user;
218 230
219 _withhold_user._user_id = _data.at("user_withheld").at("id"); 231 switch (_data.at("disconnect").at("code").get<int>())
232 {
233 case 1: _disconnect = disconnect_code::shutdown; break;
234 case 2: _disconnect = disconnect_code::duplicate; break;
235 case 4: _disconnect = disconnect_code::stall; break;
236 case 5: _disconnect = disconnect_code::normal; break;
237 case 6: _disconnect = disconnect_code::token_revoked; break;
238 case 7: _disconnect = disconnect_code::admin_logout; break;
239 case 9: _disconnect = disconnect_code::limit; break;
240 case 10: _disconnect = disconnect_code::exception; break;
241 case 11: _disconnect = disconnect_code::broker; break;
242 case 12: _disconnect = disconnect_code::load; break;
243 default: _disconnect = disconnect_code::unknown;
244 }
245 } else if (_data.find("friends") != _data.end())
246 {
247 _type = type::friends;
220 248
221 new(&_withhold_user._countries) std::vector<std::string>(); 249 new(&_friends) std::set<user_id>(_data.at("friends").begin(), _data.at("friends").end());
222 for (auto s : _data.at("user_withheld").at("withheld_in_countries")) 250 } else if (_data.find("direct_message") != _data.end())
223 { 251 {
224 _withhold_user._countries.push_back(s); 252 _type = type::direct;
225 }
226 } else if (_data.find("disconnect") != _data.end())
227 {
228 _type = type::disconnect;
229
230 switch (_data.at("disconnect").at("code").get<int>())
231 {
232 case 1: _disconnect = disconnect_code::shutdown; break;
233 case 2: _disconnect = disconnect_code::duplicate; break;
234 case 4: _disconnect = disconnect_code::stall; break;
235 case 5: _disconnect = disconnect_code::normal; break;
236 case 6: _disconnect = disconnect_code::token_revoked; break;
237 case 7: _disconnect = disconnect_code::admin_logout; break;
238 case 9: _disconnect = disconnect_code::limit; break;
239 case 10: _disconnect = disconnect_code::exception; break;
240 case 11: _disconnect = disconnect_code::broker; break;
241 case 12: _disconnect = disconnect_code::load; break;
242 default: _disconnect = disconnect_code::unknown;
243 }
244 } else if (_data.find("friends") != _data.end())
245 {
246 _type = type::friends;
247 253
248 new(&_friends) std::set<user_id>(_data.at("friends").begin(), _data.at("friends").end()); 254 new(&_direct_message) direct_message(_data.at("direct_message").dump());
249 } else if (_data.find("direct_message") != _data.end()) 255 } else {
256 _type = type::unknown;
257 }
258 } catch (std::invalid_argument e)
250 { 259 {
251 _type = type::direct; 260 _type = type::invalid;
252
253 new(&_direct_message) direct_message(_data.at("direct_message").dump());
254 } else {
255 _type = type::unknown;
256 } 261 }
257 } 262 }
258 263