about summary refs log tree commit diff stats
path: root/timeline.cpp
blob: bc343296716f748c456920de97acd1560bfdc1c2 (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
#include "timeline.h"
#include <sstream>
#include <hkutil/string.h>
#include <iostream>

timeline::timeline(mastodonpp::API::endpoint_type endpoint) : endpoint_(endpoint)
{
}

std::list<nlohmann::json> timeline::poll(mastodonpp::Connection& connection)
{
  std::string maxId;
  std::list<nlohmann::json> result;

  for (int i = 0; i < 5; i++)
  {
    mastodonpp::parametermap arguments;

    if (i > 0)
    {
      arguments["max_id"] = maxId;
    }

    if (hasSince_)
    {
      arguments["since_id"] = sinceId_;
    }

    auto answer{connection.get(endpoint_, arguments)};
    if (!answer)
    {
      if (answer.curl_error_code == 0)
      {
        std::cout << "HTTP status: " << answer.http_status << std::endl;
      }
      else
      {
        std::cout << "libcurl error " << std::to_string(answer.curl_error_code)
             << ": " << answer.error_message << std::endl;
      }
      return {};
    }

    nlohmann::json rjs = nlohmann::json::parse(answer.body);

    if (rjs.empty())
    {
      break;
    }

    for (auto& single : rjs)
    {
      result.push_back(single);
    }

    maxId = result.back()["id"].get<std::string>();
  }

  if (!result.empty())
  {
    sinceId_ = result.front()["id"].get<std::string>();
    hasSince_ = true;
  }

  return result;
}