2018-03-21 16:45:52 +01:00
|
|
|
/* This file is part of mastodon-cpp.
|
2019-03-20 06:15:43 +01:00
|
|
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
2018-03-21 20:29:33 +01:00
|
|
|
*
|
2018-03-21 16:45:52 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-03-25 22:46:38 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip> // get_time
|
|
|
|
#include <sstream>
|
2018-04-01 03:08:22 +02:00
|
|
|
#include <regex>
|
2019-03-11 21:13:34 +01:00
|
|
|
#include <algorithm>
|
2018-03-21 16:45:52 +01:00
|
|
|
#include "easy.hpp"
|
2019-02-22 11:35:06 +01:00
|
|
|
#include "debug.hpp"
|
2018-03-21 16:45:52 +01:00
|
|
|
|
|
|
|
using namespace Mastodon;
|
|
|
|
using std::string;
|
|
|
|
|
2019-02-25 13:55:24 +01:00
|
|
|
return_entity::return_entity()
|
|
|
|
: entity()
|
|
|
|
{}
|
|
|
|
|
|
|
|
return_entity::return_entity(const uint8_t ec, const string &em,
|
|
|
|
const Easy::GenericEntity &ent)
|
|
|
|
: entity(ent)
|
|
|
|
{
|
|
|
|
error_code = ec;
|
|
|
|
error_message = em;
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:23:08 +01:00
|
|
|
return_entity::operator const Easy::GenericEntity() const
|
2019-03-02 20:21:31 +01:00
|
|
|
{
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:23:08 +01:00
|
|
|
return_entity::operator const string() const
|
2019-03-02 20:21:31 +01:00
|
|
|
{
|
|
|
|
return entity.to_string();
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:23:08 +01:00
|
|
|
std::ostream &Mastodon::operator <<(std::ostream &out, const return_entity &ret)
|
2019-03-02 20:21:31 +01:00
|
|
|
{
|
|
|
|
out << ret.entity.to_string();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:55:24 +01:00
|
|
|
return_entity_vector::return_entity_vector()
|
|
|
|
: entities()
|
|
|
|
{}
|
|
|
|
|
|
|
|
return_entity_vector::return_entity_vector(const uint8_t ec, const string &em,
|
|
|
|
const vector<Easy::GenericEntity> &vec)
|
|
|
|
: entities(vec)
|
|
|
|
{
|
|
|
|
error_code = ec;
|
|
|
|
error_message = em;
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:21:31 +01:00
|
|
|
return_entity_vector::operator const vector<Easy::GenericEntity>() const
|
|
|
|
{
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:45:52 +01:00
|
|
|
Easy::Easy(const string &instance, const string &access_token)
|
|
|
|
: API(instance, access_token)
|
2018-03-30 19:01:09 +02:00
|
|
|
{}
|
2018-03-22 00:33:36 +01:00
|
|
|
|
2018-03-31 20:03:31 +02:00
|
|
|
const std::vector<string> Easy::json_array_to_vector(const string &json)
|
|
|
|
{
|
|
|
|
Json::Value json_array;
|
|
|
|
std::stringstream ss(json);
|
|
|
|
ss >> json_array;
|
|
|
|
|
|
|
|
if (json_array.isArray())
|
|
|
|
{
|
2019-03-11 21:13:34 +01:00
|
|
|
// Transform array of Json::Value to vector of string.
|
|
|
|
std::vector<string> vec(json_array.size());
|
|
|
|
std::transform(json_array.begin(), json_array.end(), vec.begin(),
|
|
|
|
[](const Json::Value &j)
|
|
|
|
{ return j.toStyledString(); });
|
2018-03-31 20:03:31 +02:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
ttdebug << "ERROR: JSON string holds no array\n";
|
|
|
|
ttdebug << "String was: " << json << '\n';
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-01 03:08:22 +02:00
|
|
|
const std::vector<Easy::stream_event>
|
|
|
|
Easy::parse_stream(const std::string &streamdata)
|
|
|
|
{
|
|
|
|
string stream = streamdata;
|
|
|
|
std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n");
|
|
|
|
std::smatch match;
|
|
|
|
std::vector<stream_event> vec = {};
|
|
|
|
|
|
|
|
while (std::regex_search(stream, match, reevent))
|
|
|
|
{
|
|
|
|
const string &event = match[1].str();
|
|
|
|
const string &data = match[2].str();
|
|
|
|
event_type type = event_type::Undefined;
|
|
|
|
|
|
|
|
if (event.compare("update") == 0)
|
|
|
|
type = event_type::Update;
|
|
|
|
else if (event.compare("notification") == 0)
|
|
|
|
type = event_type::Notification;
|
|
|
|
else if (event.compare("delete") == 0)
|
|
|
|
type = event_type::Delete;
|
|
|
|
|
|
|
|
vec.push_back(stream_event(type, data));
|
|
|
|
stream = match.suffix().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
2018-04-03 00:04:47 +02:00
|
|
|
|
2018-04-03 01:20:15 +02:00
|
|
|
const Easy::Link Easy::get_link() const
|
|
|
|
{
|
|
|
|
return Link(get_header("Link"));
|
|
|
|
}
|
|
|
|
|
2018-05-05 04:25:22 +02:00
|
|
|
const string Easy::strtime_utc(const system_clock::time_point &timepoint,
|
|
|
|
const string &format)
|
|
|
|
{
|
2018-05-05 04:38:08 +02:00
|
|
|
return strtime(timepoint, format, true);
|
2018-05-05 04:25:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const string Easy::strtime_local(const system_clock::time_point &timepoint,
|
|
|
|
const string &format)
|
2018-05-05 04:38:08 +02:00
|
|
|
{
|
|
|
|
return strtime(timepoint, format, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const string Easy::strtime(const system_clock::time_point &timepoint,
|
|
|
|
const string &format, const bool &utc)
|
2018-05-05 04:25:22 +02:00
|
|
|
{
|
2019-02-22 08:29:54 +01:00
|
|
|
constexpr std::uint16_t bufsize = 1024;
|
2018-05-05 04:25:22 +02:00
|
|
|
std::time_t time = system_clock::to_time_t(timepoint);
|
2018-05-05 04:38:08 +02:00
|
|
|
std::tm *timeinfo;
|
|
|
|
if (utc)
|
|
|
|
{
|
|
|
|
timeinfo = std::gmtime(&time);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeinfo = std::localtime(&time);
|
|
|
|
}
|
2018-05-05 04:25:22 +02:00
|
|
|
char buffer[bufsize];
|
|
|
|
|
|
|
|
std::strftime(buffer, bufsize, format.c_str(), timeinfo);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-04-03 00:04:47 +02:00
|
|
|
Easy::Link::Link(const string &link_header)
|
2019-01-27 03:50:28 +01:00
|
|
|
: _next()
|
|
|
|
, _prev()
|
2018-04-03 00:04:47 +02:00
|
|
|
{
|
|
|
|
std::regex renext("max_id=([[:digit:]]*)");
|
|
|
|
std::regex reprev("since_id=([[:digit:]]*)");
|
|
|
|
std::smatch match;
|
|
|
|
|
|
|
|
if (std::regex_search(link_header, match, renext))
|
|
|
|
{
|
2019-01-27 03:50:28 +01:00
|
|
|
_next = match[1].str();
|
2018-04-03 00:04:47 +02:00
|
|
|
}
|
|
|
|
if (std::regex_search(link_header, match, reprev))
|
|
|
|
{
|
2019-01-27 03:50:28 +01:00
|
|
|
_prev = match[1].str();
|
2018-04-03 00:04:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 03:50:28 +01:00
|
|
|
const string Easy::Link::next() const
|
2018-04-03 00:04:47 +02:00
|
|
|
{
|
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
|
2019-01-27 03:50:28 +01:00
|
|
|
const string Easy::Link::max_id() const
|
2018-04-03 01:20:15 +02:00
|
|
|
{
|
|
|
|
return _next;
|
|
|
|
}
|
|
|
|
|
2019-01-27 03:50:28 +01:00
|
|
|
const string Easy::Link::prev() const
|
2018-04-03 00:04:47 +02:00
|
|
|
{
|
|
|
|
return _prev;
|
|
|
|
}
|
|
|
|
|
2019-01-27 03:50:28 +01:00
|
|
|
const string Easy::Link::since_id() const
|
2018-04-03 00:04:47 +02:00
|
|
|
{
|
2018-04-03 01:20:15 +02:00
|
|
|
return _prev;
|
2018-04-03 00:04:47 +02:00
|
|
|
}
|