diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-06-01 20:34:33 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-06-01 20:34:33 -0400 |
commit | 442f1ee071152be04c4184473ddfee5040795b76 (patch) | |
tree | 297cffbf4a3728039519541429f68a670d70b98c /src/notification.cpp | |
parent | aceb3cc33ee78cce1077252aff8a8808a3195ff1 (diff) | |
download | libtwittercpp-442f1ee071152be04c4184473ddfee5040795b76.tar.gz libtwittercpp-442f1ee071152be04c4184473ddfee5040795b76.tar.bz2 libtwittercpp-442f1ee071152be04c4184473ddfee5040795b76.zip |
Wrapped JSON parsing in exception handling
Diffstat (limited to 'src/notification.cpp')
-rw-r--r-- | src/notification.cpp | 375 |
1 files changed, 190 insertions, 185 deletions
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 | ||