about summary refs log tree commit diff stats
path: root/src/tweet.cpp
blob: 4abe2f895832a048fe532aea789e51ea31c88a05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "tweet.h"
#include <json.hpp>
#include "util.h"
#include "codes.h"
#include "client.h"

namespace twitter {
  
  tweet::tweet(const client& tclient, std::string data) try
    : _client(tclient)
  {
    auto json = nlohmann::json::parse(data);
    _id = json["id"].get<tweet_id>();
    _text = json["text"].get<std::string>();
    _author = make_unique<user>(_client, json["user"].dump());
    
    if (!json["retweeted_status"].is_null())
    {
      _is_retweet = true;
      
      _retweeted_status = make_unique<tweet>(_client, json["retweeted_status"].dump());
    }
    
    if (!json["entities"].is_null())
    {
      auto entities = json["entities"];
      if (!entities["user_mentions"].is_null())
      {
        for (auto mention : entities["user_mentions"])
        {
          _mentions.push_back(std::make_pair(mention["id"].get<user_id>(), mention["screen_name"].get<std::string>()));
        }
      }
    }
  } catch (const std::invalid_argument& error)
  {
    std::throw_with_nested(malformed_object("tweet", data));
  } catch (const std::domain_error& error)
  {
    std::throw_with_nested(malformed_object("tweet", data));
  }
  
  std::string tweet::generateReplyPrefill() const
  {
    std::ostringstream output;
    output << "@" << _author->getScreenName() << " ";
    
    for (auto mention : _mentions)
    {
      if ((mention.first != _author->getID()) && (mention.first != _client.getUser().getID()))
      {
        output << "@" << mention.second << " ";
      }
    }
    
    return output.str();
  }
  
  tweet tweet::reply(std::string message, std::list<long> media_ids) const
  {
    return _client.replyToTweet(message, _id, media_ids);
  }
  
  bool tweet::isMyTweet() const
  {
    return *_author == _client.getUser();
  }
  
  std::string tweet::getURL() const
  {
    std::ostringstream urlstr;
    urlstr << "https://twitter.com";
    urlstr << _author->getScreenName();
    urlstr << "/statuses/";
    urlstr << _id;
    
    return urlstr.str();
  }
  
};