about summary refs log tree commit diff stats
path: root/src/tweet.cpp
blob: 2927018ea74976a0c2317b63265efe56a768e534 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "tweet.h"
#include <json.hpp>
#include <cassert>

using nlohmann::json;

namespace twitter {
  
  tweet::tweet() : _valid(false)
  {
    
  }
  
  tweet::tweet(std::string data) : _valid(true)
  {
    auto _data = json::parse(data);
    _id = _data.at("id");
    _text = _data.at("text");
    _author = user(_data.at("user").dump());
    
    if (_data.find("retweeted_status") != _data.end())
    {
      _is_retweet = true;
      
      std::string retweet = _data.at("retweeted_status").dump();
      _retweeted_status = new tweet(retweet);
    }
    
    if (_data.find("entities") != _data.end())
    {
      auto _entities = _data.at("entities");
      if (_entities.find("user_mentions") != _entities.end())
      {
        for (auto _mention : _entities.at("user_mentions"))
        {
          _mentions.push_back(std::make_pair(_mention.at("id"), _mention.at("screen_name").get<std::string>()));
        }
      }
    }
  }
  
  tweet::tweet(const tweet& other)
  {
    _valid = other._valid;
    _id = other._id;
    _text = other._text;
    _author = other._author;
    _is_retweet = other._is_retweet;
    
    if (_is_retweet)
    {
      _retweeted_status = new tweet(*other._retweeted_status);
    }
    
    _mentions = other._mentions;
  }
  
  tweet::tweet(tweet&& other) : tweet()
  {
    swap(*this, other);
  }
  
  tweet::~tweet()
  {
    if (_is_retweet)
    {
      delete _retweeted_status;
    }
  }
  
  tweet& tweet::operator=(tweet other)
  {
    swap(*this, other);
    
    return *this;
  }

  void swap(tweet& first, tweet& second)
  {
    std::swap(first._valid, second._valid);
    std::swap(first._id, second._id);
    std::swap(first._text, second._text);
    std::swap(first._author, second._author);
    std::swap(first._is_retweet, second._is_retweet);
    std::swap(first._retweeted_status, second._retweeted_status);
    std::swap(first._mentions, second._mentions);
  }
  
  tweet_id tweet::getID() const
  {
    assert(_valid);
    
    return _id;
  }
  
  std::string tweet::getText() const
  {
    assert(_valid);
    
    return _text;
  }
  
  const user& tweet::getAuthor() const
  {
    assert(_valid);
    
    return _author;
  }
  
  bool tweet::isRetweet() const
  {
    assert(_valid);
    
    return _is_retweet;
  }
  
  tweet tweet::getRetweet() const
  {
    assert(_valid && _is_retweet);
    
    return *_retweeted_status;
  }
  
  std::vector<std::pair<user_id, std::string>> tweet::getMentions() const
  {
    return _mentions;
  }
  
  tweet::operator bool() const
  {
    return _valid;
  }
  
};