diff options
Diffstat (limited to 'src/tweet.cpp')
-rw-r--r-- | src/tweet.cpp | 141 |
1 files changed, 41 insertions, 100 deletions
diff --git a/src/tweet.cpp b/src/tweet.cpp index 193df6e..4abe2f8 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp | |||
@@ -1,139 +1,80 @@ | |||
1 | #include "tweet.h" | 1 | #include "tweet.h" |
2 | #include <json.hpp> | 2 | #include <json.hpp> |
3 | #include <cassert> | 3 | #include "util.h" |
4 | 4 | #include "codes.h" | |
5 | using nlohmann::json; | 5 | #include "client.h" |
6 | 6 | ||
7 | namespace twitter { | 7 | namespace twitter { |
8 | 8 | ||
9 | tweet::tweet() : _valid(false) | 9 | tweet::tweet(const client& tclient, std::string data) try |
10 | { | 10 | : _client(tclient) |
11 | |||
12 | } | ||
13 | |||
14 | tweet::tweet(std::string data) : _valid(true) | ||
15 | { | 11 | { |
16 | auto _data = json::parse(data); | 12 | auto json = nlohmann::json::parse(data); |
17 | _id = _data.at("id"); | 13 | _id = json["id"].get<tweet_id>(); |
18 | _text = _data.at("text"); | 14 | _text = json["text"].get<std::string>(); |
19 | _author = user(_data.at("user").dump()); | 15 | _author = make_unique<user>(_client, json["user"].dump()); |
20 | 16 | ||
21 | if (_data.find("retweeted_status") != _data.end()) | 17 | if (!json["retweeted_status"].is_null()) |
22 | { | 18 | { |
23 | _is_retweet = true; | 19 | _is_retweet = true; |
24 | 20 | ||
25 | std::string retweet = _data.at("retweeted_status").dump(); | 21 | _retweeted_status = make_unique<tweet>(_client, json["retweeted_status"].dump()); |
26 | _retweeted_status = new tweet(retweet); | ||
27 | } | 22 | } |
28 | 23 | ||
29 | if (_data.find("entities") != _data.end()) | 24 | if (!json["entities"].is_null()) |
30 | { | 25 | { |
31 | auto _entities = _data.at("entities"); | 26 | auto entities = json["entities"]; |
32 | if (_entities.find("user_mentions") != _entities.end()) | 27 | if (!entities["user_mentions"].is_null()) |
33 | { | 28 | { |
34 | for (auto _mention : _entities.at("user_mentions")) | 29 | for (auto mention : entities["user_mentions"]) |
35 | { | 30 | { |
36 | _mentions.push_back(std::make_pair(_mention.at("id"), _mention.at("screen_name").get<std::string>())); | 31 | _mentions.push_back(std::make_pair(mention["id"].get<user_id>(), mention["screen_name"].get<std::string>())); |
37 | } | 32 | } |
38 | } | 33 | } |
39 | } | 34 | } |
40 | } | 35 | } catch (const std::invalid_argument& error) |
41 | |||
42 | tweet::tweet(const tweet& other) | ||
43 | { | 36 | { |
44 | _valid = other._valid; | 37 | std::throw_with_nested(malformed_object("tweet", data)); |
45 | _id = other._id; | 38 | } catch (const std::domain_error& error) |
46 | _text = other._text; | ||
47 | _author = other._author; | ||
48 | _is_retweet = other._is_retweet; | ||
49 | |||
50 | if (_is_retweet) | ||
51 | { | ||
52 | _retweeted_status = new tweet(*other._retweeted_status); | ||
53 | } | ||
54 | |||
55 | _mentions = other._mentions; | ||
56 | } | ||
57 | |||
58 | tweet::tweet(tweet&& other) : tweet() | ||
59 | { | 39 | { |
60 | swap(*this, other); | 40 | std::throw_with_nested(malformed_object("tweet", data)); |
61 | } | 41 | } |
62 | 42 | ||
63 | tweet::~tweet() | 43 | std::string tweet::generateReplyPrefill() const |
64 | { | 44 | { |
65 | if (_is_retweet) | 45 | std::ostringstream output; |
46 | output << "@" << _author->getScreenName() << " "; | ||
47 | |||
48 | for (auto mention : _mentions) | ||
66 | { | 49 | { |
67 | delete _retweeted_status; | 50 | if ((mention.first != _author->getID()) && (mention.first != _client.getUser().getID())) |
51 | { | ||
52 | output << "@" << mention.second << " "; | ||
53 | } | ||
68 | } | 54 | } |
69 | } | ||
70 | |||
71 | tweet& tweet::operator=(tweet other) | ||
72 | { | ||
73 | swap(*this, other); | ||
74 | 55 | ||
75 | return *this; | 56 | return output.str(); |
76 | } | ||
77 | |||
78 | void swap(tweet& first, tweet& second) | ||
79 | { | ||
80 | std::swap(first._valid, second._valid); | ||
81 | std::swap(first._id, second._id); | ||
82 | std::swap(first._text, second._text); | ||
83 | std::swap(first._author, second._author); | ||
84 | std::swap(first._is_retweet, second._is_retweet); | ||
85 | std::swap(first._retweeted_status, second._retweeted_status); | ||
86 | std::swap(first._mentions, second._mentions); | ||
87 | } | 57 | } |
88 | 58 | ||
89 | tweet_id tweet::getID() const | 59 | tweet tweet::reply(std::string message, std::list<long> media_ids) const |
90 | { | 60 | { |
91 | assert(_valid); | 61 | return _client.replyToTweet(message, _id, media_ids); |
92 | |||
93 | return _id; | ||
94 | } | ||
95 | |||
96 | std::string tweet::getText() const | ||
97 | { | ||
98 | assert(_valid); | ||
99 | |||
100 | return _text; | ||
101 | } | 62 | } |
102 | 63 | ||
103 | const user& tweet::getAuthor() const | 64 | bool tweet::isMyTweet() const |
104 | { | 65 | { |
105 | assert(_valid); | 66 | return *_author == _client.getUser(); |
106 | |||
107 | return _author; | ||
108 | } | ||
109 | |||
110 | bool tweet::isRetweet() const | ||
111 | { | ||
112 | assert(_valid); | ||
113 | |||
114 | return _is_retweet; | ||
115 | } | ||
116 | |||
117 | tweet tweet::getRetweet() const | ||
118 | { | ||
119 | assert(_valid && _is_retweet); | ||
120 | |||
121 | return *_retweeted_status; | ||
122 | } | ||
123 | |||
124 | std::vector<std::pair<user_id, std::string>> tweet::getMentions() const | ||
125 | { | ||
126 | return _mentions; | ||
127 | } | 67 | } |
128 | 68 | ||
129 | std::string tweet::getURL() const | 69 | std::string tweet::getURL() const |
130 | { | 70 | { |
131 | return "https://twitter.com/" + _author.getScreenName() + "/statuses/" + std::to_string(_id); | 71 | std::ostringstream urlstr; |
132 | } | 72 | urlstr << "https://twitter.com"; |
133 | 73 | urlstr << _author->getScreenName(); | |
134 | tweet::operator bool() const | 74 | urlstr << "/statuses/"; |
135 | { | 75 | urlstr << _id; |
136 | return _valid; | 76 | |
77 | return urlstr.str(); | ||
137 | } | 78 | } |
138 | 79 | ||
139 | }; | 80 | }; |