Compare commits

..

1 Commits

Author SHA1 Message Date
tastytea a25f088bec
Changed default values for strings from 0 to "".
the build was successful Details
2019-03-02 12:57:51 +01:00
176 changed files with 6059 additions and 11969 deletions

View File

@ -1,269 +1,158 @@
kind: pipeline
name: x86_64
pipeline:
gcc5:
image: debian:stretch-slim
pull: true
when:
event: [push, pull_request]
environment:
- LANG=C.utf8
- CXX=g++-5
- CXXFLAGS=-pipe -O2
commands:
- rm /etc/apt/apt.conf.d/docker-clean
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
- apt-get install -qy gnupg
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
- apt-get update -q
- apt-get install -qy build-essential cmake pkg-config
- apt-get install -qy -t xenial g++-5
- apt-get install -qy libcurl4-openssl-dev libjsoncpp-dev
- apt-get install -qy -t sid libcurlpp-dev
- apt-get install -qy rpm file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=ON -DWITH_TESTS=ON ..
- make VERBOSE=1
- make install DESTDIR=install
volumes:
- /var/cache/debian-package-cache:/var/cache/apt/archives
platform:
os: linux
arch: amd64
gcc6:
image: debian:stretch-slim
pull: true
environment:
- LANG=C.utf8
- CXX=g++-6
- CXXFLAGS=-pipe -O2
commands:
- rm /etc/apt/apt.conf.d/docker-clean
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
- apt-get update -q
- apt-get install -qy build-essential cmake pkg-config
- apt-get install -qy libcurl4-openssl-dev libjsoncpp-dev
- apt-get install -qy -t sid libcurlpp-dev
- apt-get install -qy rpm file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=ON -DWITH_TESTS=ON ..
- make VERBOSE=1
- make install DESTDIR=install
- ctest ..
- make package
- cmake -DWITH_DEB=ON ..
- make package
- cmake -DWITH_DEB=OFF -DWITH_RPM=ON ..
- make package
volumes:
- /var/cache/debian-package-cache:/var/cache/apt/archives
volumes:
- name: debian-package-cache
host:
path: /var/cache/debian-package-cache
gcc7:
image: debian:stretch-slim
pull: true
when:
event: [push, pull_request]
environment:
- LANG=C.utf8
- CXX=g++-7
- CXXFLAGS=-pipe -O2
commands:
- rm /etc/apt/apt.conf.d/docker-clean
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
- apt-get install -qy gnupg
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
- apt-get update -q
- apt-get install -qy build-essential cmake pkg-config
- apt-get install -qy -t xenial g++-7
- apt-get install -qy libcurl4-openssl-dev libjsoncpp-dev
- apt-get install -qy -t sid libcurlpp-dev
- apt-get install -qy rpm file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=ON -DWITH_TESTS=ON ..
- make VERBOSE=1
- make install DESTDIR=install
volumes:
- /var/cache/debian-package-cache:/var/cache/apt/archives
trigger:
event:
exclude:
- tag
gcc8:
image: debian:stretch-slim
pull: true
when:
event: [push, pull_request]
environment:
- LANG=C.utf8
- CXX=g++-8
- CXXFLAGS=-pipe -O2
commands:
- rm /etc/apt/apt.conf.d/docker-clean
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
- apt-get install -qy gnupg
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
- apt-get update -q
- apt-get install -qy build-essential cmake pkg-config
- apt-get install -qy -t xenial g++-8
- apt-get install -qy libcurl4-openssl-dev libjsoncpp-dev
- apt-get install -qy -t sid libcurlpp-dev
- apt-get install -qy rpm file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=ON -DWITH_TESTS=ON ..
- make VERBOSE=1
- make install DESTDIR=install
volumes:
- /var/cache/debian-package-cache:/var/cache/apt/archives
steps:
- name: gcc6
image: debian:stretch-slim
pull: true
environment:
CXX: g++-6
CXXFLAGS: -pipe -O2
MASTODON_CPP_ACCESS_TOKEN:
from_secret: mastodon_cpp_access_token
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- apt-get install -qy build-essential cmake
- apt-get install -qy libpoco-dev libjsoncpp-dev doxygen catch
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=YES -DWITH_TESTS=YES -DEXTRA_TEST_ARGS="~[api]" ..
- make VERBOSE=1
- make install DESTDIR=install
- cd tests && ctest
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
prepare_release:
image: debian:stretch-slim
pull: true
when:
event: tag
commands:
- cp -v build/mastodon-cpp-${DRONE_TAG}_x86_64.tar.gz .
- cp -v build/libmastodon-cpp_${DRONE_TAG}-0_amd64.deb .
- cp -v build/libmastodon-cpp-${DRONE_TAG}-0.x86_64.rpm .
- name: gcc9
image: debian:stretch-slim
pull: true
environment:
CXX: g++-9
CXXFLAGS: -pipe -O2
MASTODON_CPP_ACCESS_TOKEN:
from_secret: mastodon_cpp_access_token
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
- apt-get install -qy gnupg
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
- apt-get update -q
- apt-get install -qy build-essential cmake
- apt-get install -qy -t xenial g++-9
- apt-get install -qy libpoco-dev libjsoncpp-dev doxygen catch
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=YES ..
- make VERBOSE=1
- make install DESTDIR=install
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: clang5
image: debian:stretch-slim
pull: true
environment:
CXX: clang++-5.0
CXXFLAGS: -pipe -O2
MASTODON_CPP_ACCESS_TOKEN:
from_secret: mastodon_cpp_access_token
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://deb.debian.org/debian stretch-backports main" >> /etc/apt/sources.list.d/stretch.list
- apt-get update -q
- apt-get install -qy build-essential cmake
- apt-get install -qy -t stretch-backports clang-5.0
- apt-get install -qy libpoco-dev libjsoncpp-dev doxygen catch
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=YES ..
- make VERBOSE=1
- make install DESTDIR=install
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: clang7
image: debian:buster-slim
pull: true
environment:
LANG: C.utf8
CXX: clang++
CXXFLAGS: -pipe -O2
MASTODON_CPP_ACCESS_TOKEN:
from_secret: mastodon_cpp_access_token
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- apt-get update -q
- apt-get install -qy build-essential cmake clang
- apt-get install -qy libpoco-dev libjsoncpp-dev doxygen catch
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_EXAMPLES=YES ..
- make VERBOSE=1
- make install DESTDIR=install
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: notification
image: drillster/drone-email
pull: always
settings:
host: cryptoparty-celle.de
from: drone@tzend.de
username:
from_secret: email_username
password:
from_secret: email_password
when:
status: [ changed, failure ]
---
kind: pipeline
name: packages x86_64
platform:
os: linux
arch: amd64
volumes:
- name: debian-package-cache
host:
path: /var/cache/debian-package-cache
- name: centos-package-cache
host:
path: /var/cache/centos-package-cache
trigger:
event:
- tag
steps:
- name: Debian stretch
image: debian:stretch-slim
pull: always
environment:
CXXFLAGS: -pipe -O2
LANG: C.utf8
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- apt-get install -qy build-essential cmake lsb-release
- apt-get install -qy libpoco-dev libjsoncpp-dev file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_DEB=YES -DWITH_DOC=NO ..
- make package
- cp -v libmastodon-cpp_${DRONE_TAG}-0_stretch_amd64.deb ..
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: Debian buster
image: debian:buster-slim
pull: always
environment:
CXXFLAGS: -pipe -O2
LANG: C.utf8
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- apt-get install -qy build-essential cmake lsb-release
- apt-get install -qy libpoco-dev libjsoncpp-dev file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_DEB=YES -DWITH_DOC=NO ..
- make package
- cp -v libmastodon-cpp_${DRONE_TAG}-0_buster_amd64.deb ..
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: Ubuntu bionic
image: ubuntu:bionic
pull: always
environment:
CXXFLAGS: -pipe -O2
LANG: C.utf8
commands:
- rm -f /etc/apt/apt.conf.d/docker-clean
- rm -f /var/cache/apt/archives/lock
- apt-get update -q
- apt-get install -qy build-essential cmake lsb-release
- apt-get install -qy libpoco-dev libjsoncpp-dev file
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_DEB=YES -DWITH_DOC=NO ..
- make package
- cp -v libmastodon-cpp_${DRONE_TAG}-0_bionic_amd64.deb ..
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: CentOS 7
image: centos:7
pull: always
environment:
CXX: /opt/rh/devtoolset-6/root/usr/bin/g++
CXXFLAGS: -pipe -O2
LANG: C.utf8
commands:
- sed -i 's/keepcache=0/keepcache=1/' /etc/yum.conf
- curl -s -o /var/cache/yum/epel-release-latest-7.noarch.rpm http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
- rpm -i /var/cache/yum/epel-release-latest-7.noarch.rpm
- yum install -qy centos-release-scl
- yum install -qy devtoolset-6
- scl enable devtoolset-6 bash
- yum install -qy poco-devel openssl-devel doxygen rpm-build
- yum --enablerepo=epel install -qy cmake3 jsoncpp-devel
- rm -rf build && mkdir -p build && cd build
- cmake3 -DWITH_RPM=YES -DWITH_DOC=NO ..
- make package
- cp -v libmastodon-cpp-${DRONE_TAG}-0.x86_64.rpm ..
volumes:
- name: centos-package-cache
path: /var/cache/yum
- name: gitea_release
image: plugins/gitea-release
pull: always
settings:
gitea_release:
image: plugins/gitea-release
pull: true
when:
event: tag
base_url: https://schlomp.space
api_key:
from_secret: gitea_token
secrets: [ gitea_token ]
title: ${DRONE_TAG}
prerelease: true
files:
- libmastodon-cpp_${DRONE_TAG}-0_stretch_amd64.deb
- libmastodon-cpp_${DRONE_TAG}-0_buster_amd64.deb
- libmastodon-cpp_${DRONE_TAG}-0_bionic_amd64.deb
- mastodon-cpp-${DRONE_TAG}_x86_64.tar.gz
- libmastodon-cpp_${DRONE_TAG}-0_amd64.deb
- libmastodon-cpp-${DRONE_TAG}-0.x86_64.rpm
checksum:
- sha256
- sha512
- name: notification
image: drillster/drone-email
pull: always
settings:
notify:
when:
status: [ changed, failure ]
image: drillster/drone-email
pull: true
host: cryptoparty-celle.de
secrets: [ email_username, email_password ]
from: drone@tzend.de
username:
from_secret: email_username
password:
from_secret: email_password
when:
status: [ changed, failure ]

View File

@ -1,20 +0,0 @@
# Configuration file for EditorConfig.
# More information is available under <https://editorconfig.org/>.
root = true
[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 80
[*.?pp]
indent_size = 4
tab_width = 4
[{CMakeLists.txt,*.cmake}]
indent_size = 2
tab_width = 2

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
/build/
/doc/
/update_gh-pages.sh
/update_doc.sh
/examples/example99*

View File

@ -1,86 +1,185 @@
# Support version 3.6 and above, but use policy settings up to 3.14.
# 3.6 is needed because of IMPORTED_TARGET in pkg_check_modules().
cmake_minimum_required(VERSION 3.6...3.14)
# Ranges are supported from 3.12, set policy to current for < 3.12.
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
cmake_minimum_required (VERSION 3.6)
project (mastodon-cpp
VERSION 0.30.2
LANGUAGES CXX
)
include(GNUInstallDirs)
project (mastodon-cpp
VERSION 0.111.5
LANGUAGES CXX)
# DESCRIPTION was introduced in version 3.9.
if(NOT (${CMAKE_VERSION} VERSION_LESS 3.9))
set(PROJECT_DESCRIPTION
"C++ wrapper for the Mastodon API.")
find_package(PkgConfig REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CURLPP REQUIRED curlpp)
if(NOT WITHOUT_EASY)
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
endif()
option(WITH_EASY "Compile Easy interface." YES)
option(WITH_EXAMPLES "Compile examples." NO)
option(WITH_TESTS "Compile tests." NO)
option(WITH_DOC "Generate HTML documentation." YES)
option(WITH_DEB "Prepare for the building of .deb packages." NO)
option(WITH_RPM "Prepare for the building of .rpm packages." NO)
option(BUILD_SHARED_LIBS "Build shared libraries." YES)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type, Release or Debug.")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(DEBUG_CXXFLAGS
"-Wall"
"-Wextra"
"-Wpedantic"
"-ftrapv"
"-fsanitize=undefined"
"-g"
"-Og"
"-fno-omit-frame-pointer"
"-Wno-deprecated-declarations")
set(DEBUG_LDFLAGS
"-fsanitize=undefined")
add_compile_options("$<$<CONFIG:Debug>:${DEBUG_CXXFLAGS}>")
# add_link_options was introduced in version 3.13.
if(${CMAKE_VERSION} VERSION_LESS 3.13)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${DEBUG_LDFLAGS}")
else()
add_link_options("$<$<CONFIG:Debug>:${DEBUG_LDFLAGS}>")
set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wpedantic -Wextra -g -Og")
# Do not complain about compatibility-wrapper
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
endif()
# Turn on debug output.
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_BINARY_DIR})
include_directories(${CURL_INCLUDE_DIRS})
include_directories(${CURLPP_INCLUDE_DIRS})
include_directories(${JSONCPP_INCLUDE_DIRS})
link_directories(${CURL_LIBRARY_DIRS})
link_directories(${CURLPP_LIBRARY_DIRS})
link_directories(${JSONCPP_LIBRARY_DIRS})
# Write version in header
configure_file (
"${PROJECT_SOURCE_DIR}/src/version.hpp.in"
"${PROJECT_BINARY_DIR}/version.hpp"
)
# Announce that we are compiling mastodon-cpp (used to figure out where the
# headers are)
add_definitions(-DMASTODON_CPP=1)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions("-DDEBUG=1")
add_definitions(-DDEBUG=1)
endif()
if(NOT WITH_EASY)
add_definitions("-DWITHOUT_EASY=1")
if(WITHOUT_EASY)
add_definitions(-DWITHOUT_EASY=1)
endif()
add_subdirectory("src")
# Compile library
if(WITHOUT_EASY)
file(GLOB sources src/*.cpp src/api/*.cpp)
else()
file(GLOB sources src/*.cpp src/api/*.cpp
src/easy/*.cpp src/easy/entities/*.cpp)
endif()
add_library(mastodon-cpp SHARED ${sources})
set_target_properties(mastodon-cpp PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${mastodon-cpp_VERSION_MAJOR}
)
if(WITHOUT_EASY)
target_link_libraries(mastodon-cpp ${CURLPP_LIBRARIES})
else()
target_link_libraries(mastodon-cpp ${CURLPP_LIBRARIES} ${JSONCPP_LIBRARIES})
endif()
if(WITH_STATIC)
add_library(mastodon-cpp_static STATIC ${sources})
set_target_properties(mastodon-cpp_static PROPERTIES
OUTPUT_NAME mastodon-cpp)
endif()
# Compile examples
if(WITH_EXAMPLES)
add_subdirectory("examples")
file(GLOB sources_examples examples/*.cpp)
foreach(src ${sources_examples})
get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} ${src})
target_link_libraries(${bin} pthread ${JSONCPP_LIBRARIES} mastodon-cpp)
endforeach()
endif()
# Compile tests
if(WITH_TESTS)
add_subdirectory("tests")
include(CTest)
file(GLOB sources_tests tests/test_*.cpp)
foreach(src ${sources_tests})
get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} ${src})
target_link_libraries(${bin} mastodon-cpp)
add_test(${bin} ${bin})
endforeach()
endif()
# Install library and header files
install(TARGETS mastodon-cpp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(WITH_STATIC)
install(TARGETS mastodon-cpp_static LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(FILES src/mastodon-cpp.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp)
if(NOT WITHOUT_EASY)
file(GLOB easy_header src/easy/*.hpp)
install(FILES ${easy_header}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp/easy)
file(GLOB easy_entities_header src/easy/entities/*.hpp)
install(FILES ${easy_entities_header}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mastodon-cpp/easy/entities)
endif()
# Compile & install documentation
if(WITH_DOC)
add_custom_command(OUTPUT ${PROJECT_SOURCE_DIR}/doc/html
COMMAND "./build_doc.sh" WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
add_custom_target(doc DEPENDS doc/html)
add_dependencies(${PROJECT_NAME} doc)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/doc/html
DESTINATION "${CMAKE_INSTALL_DOCDIR}/${PROJECT_NAME}")
execute_process(COMMAND ./build_doc.sh
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
install(DIRECTORY ${PROJECT_SOURCE_DIR}/doc/html
DESTINATION ${CMAKE_INSTALL_DOCDIR}/mastodon-cpp-${PROJECT_VERSION})
file(GLOB examples examples/example*.cpp)
install(FILES ${examples}
DESTINATION ${CMAKE_INSTALL_DOCDIR}/mastodon-cpp-${PROJECT_VERSION}/examples)
endif()
add_subdirectory("cmake")
add_subdirectory("pkg-config")
# Packages
set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
set(CPACK_PACKAGE_VERSION_MAJOR ${mastodon-cpp_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${mastodon-cpp_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${mastodon-cpp_VERSION_PATCH})
set(CPACK_PACKAGE_VERSION ${mastodon-cpp_VERSION})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "C++ wrapper for the Mastodon API")
set(CPACK_PACKAGE_CONTACT "tastytea <tastytea@tastytea.de>")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${PROJECT_SOURCE_DIR}/README.md")
list(APPEND CPACK_SOURCE_IGNORE_FILES "/\\\\.git"
"/.gitignore"
"/build/"
"/doc/"
"\\\\.sublime-"
"/update_doc.sh"
"/.drone.yml"
"/ISSUE_TEMPLATE.md$")
execute_process(COMMAND uname -m
OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}_${CPACK_PACKAGE_ARCHITECTURE}")
set(CPACK_GENERATOR "TGZ")
set(CPACK_SOURCE_GENERATOR "TGZ")
include("cmake/packages.cmake")
if (WITH_DEB)
set(CPACK_PACKAGE_NAME "lib${CMAKE_PROJECT_NAME}")
set(CPACK_GENERATOR "DEB")
set(CPACK_SOURCE_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://schlomp.space/tastytea/mastodon-cpp")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
execute_process(COMMAND dpkg --print-architecture
OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}-0_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}-0_src")
endif()
if (WITH_RPM)
set(CPACK_PACKAGE_NAME "lib${CMAKE_PROJECT_NAME}")
set(CPACK_GENERATOR "RPM")
set(CPACK_SOURCE_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_LICENSE "GPL-3")
set(CPACK_RPM_PACKAGE_URL "https://schlomp.space/tastytea/mastodon-cpp")
set(CPACK_RPM_PACKAGE_REQUIRES "curlpp >= 0.8.1 jsoncpp, >= 1.7.4")
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-0.${CPACK_PACKAGE_ARCHITECTURE}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-0.src")
endif()
include(CPack)

View File

@ -1,49 +0,0 @@
:contact-coc: tastytea@tastytea.de
== Code of Conduct
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, education, ethnicity, gender identity and expression, level of
experience, nationality, personal appearance, race, religion, sex
characteristics, sexual identity and orientation or socio-economic status.
=== Examples
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language.
* Being respectful of differing viewpoints and experiences.
* Gracefully accepting constructive criticism.
* Focusing on what is best for the community.
* Showing empathy towards other community members.
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances.
* Trolling, insulting/derogatory comments, and personal attacks.
* Public or private harassment.
* Publishing others private information, such as a physical or electronic
address, without explicit permission.
=== Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at {contact-coc}.
All complaints will be reviewed and investigated and will result in a response
that is deemed necessary and appropriate to the circumstances. The project team
is obligated to maintain confidentiality with regard to the reporter of an
incident.
=== Attribution
This Code of Conduct is adapted from the Contributor Covenant, version 1.4,
available at
https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq

View File

@ -1,29 +0,0 @@
:project: mastodon-cpp
:uri-base: https://schlomp.space/tastytea/{project}
:uri-coc: {uri-base}/src/branch/master/CODE_OF_CONDUCT.adoc
:contact-email: tastytea@tastytea.de
:contact-xmpp: {contact-email}
:contact-fediverse: https://likeable.space/users/tastytea
== How to contribute
Read the link:{uri-coc}[Code of Conduct].
=== Reporting bugs or suggesting enhancements
Before reporting a bug, please
https://schlomp.space/tastytea/{project}/issues[perform a search] to see if the
problem has already been reported. If it has, add a comment to the existing
issue instead of opening a new one. Same for enhancements.
You can also contact me via mailto:{contact-email}[E-Mail],
link:xmpp:{contact-xmpp}[XMPP] or the {contact-fediverse}[Fediverse] if you
don't want to open an account.
=== Pull requests
Please use similar coding conventions as the rest of the project. The basic rule
to remember is to write code in the same style as the existing/surrounding code.
You can also send me your patches via mailto:{contact-email}[E-Mail], ideally
using `git format-patch` or `git send-email`.

16
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,16 @@
# How to contribute
## Reporting bugs or suggesting enhancements
Before reporting a bug , please
[perform a search](https://schlomp.space/tastytea/mastodon-cpp/issues)
to see if the problem has already been reported. If it has, add a comment to the
existing issue instead of opening a new one. Same for enhancements.
You can also contact me via the Fediverse if you don't want to open an account.
My account is [@tastytea@soc.ialis.me](https://soc.ialis.me/@tastytea).
## Pull requests
Please use similar coding conventions as the rest of the project. The basic rule
to remember is to write code in the same style as the existing/surrounding code.

View File

@ -1,7 +1,8 @@
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = "mastodon-cpp"
PROJECT_NUMBER = 0.0.0
INPUT = src/ src/api/ src/easy/ src/easy/entities/
INPUT = README.md src/ src/api/ src/easy/ src/easy/entities/
USE_MDFILE_AS_MAINPAGE = README.md
CREATE_SUBDIRS = NO
ALLOW_UNICODE_NAMES = YES
OUTPUT_LANGUAGE = English

147
LICENSE
View File

@ -1,21 +1,23 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
@ -24,34 +26,44 @@ them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
@ -60,7 +72,7 @@ modification follow.
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
@ -537,45 +549,35 @@ to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
@ -633,29 +635,40 @@ the "copyright" line and a pointer to where the full notice is found.
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 Affero General Public License for more details.
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

View File

@ -1,494 +0,0 @@
= mastodon-cpp
:toc: preamble
:project: mastodon-cpp
:uri-base: https://schlomp.space/tastytea/{project}
:uri-branch-main: {uri-base}/src/branch/master
:uri-mastodonpp: https://schlomp.space/tastytea/mastodonpp
[IMPORTANT]
This project has become too complicated to maintain by myself and I can't find
the energy to add features. **You can find a complete rewrite, with cleaner and
much easier to understand code at link:{uri-mastodonpp}[mastodonpp].** I
recommend switching to it. You can still report bugs here and I will respond,
but I won't put too much work in it. If you want to take over maintainership,
get in touch.
*{project}* is a C++ wrapper for the Mastodon API. You submit an API call
and get the raw JSON that you can then transform into easy to use abstractions.
== Usage
The HTML reference can be generated with `build_doc.sh`, if doxygen is
installed. It is also available at
https://doc.schlomp.space/{project}/annotated.html[doc.schlomp.space/{project}/].
There are more {uri-branch-main}/examples[examples] in `examples/`.
=== Examples
.Print own account as JSON to stdout.
====
[source,c++]
----
#include <iostream>
#include <mastodon-cpp/mastodon-cpp.hpp>
int main()
{
Mastodon::API masto("social.example.com", "auth_token");
std::cout << masto.get(Mastodon::API::v1::accounts_verify_credentials);
std::cout << std::endl;
}
----
====
.Use the `Easy` interface to get the last posts from the federated timeline.
====
[source,c++]
----
#include <iostream>
#include <string>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
using Mastodon;
int main()
{
Easy::API masto("social.example", "");
return_call ret = masto.get(API::v1::timelines_public);
for (const std::string &str : Easy::json_array_to_vector(ret.answer))
{
Easy::Status status(str);
std::cout << " " << status.account().acct() << " wrote:\n";
std::cout << status.content() << '\n';
}
}
----
====
=== Compiling your project
A project consisting of one file can be compiled as follows:
[source,shell]
----
g++ --std=c++14 -lmastodon-cpp example.cpp
----
=== List of types
Learn more at <https://doc.schlomp.space/mastodon-cpp/namespaceMastodon.html>
and <https://doc.schlomp.space/mastodon-cpp/namespaceMastodon_1_1Easy.html>.
Not included in this list are entities.
==== Return types
* `Mastodon::return_call`: Contains the response from `Mastodon::API` calls.
==== Other types
* `Mastodon::parameters`: Vector of `Mastodon::param` with custom `find()`, for
specifying parameters to an `Mastodon::API` call.
* `Mastodon::http_method`: HTTP method of an `Mastodon::API` call.
* `Mastodon::Easy::event_type`: Event types returned in streams.
* `Mastodon::Easy::visibility_type`: Describes the visibility of a post.
* `Mastodon::Easy::attachment_type`: Describes the type of attachment.
* `Mastodon::Easy::card_type`: Describes the type of card.
* `Mastodon::Easy::notification_type`: The type of the notification.
* `Mastodon::Easy::context_type`: Describes the context of a filter.
* `Mastodon::Easy::stream_event_type`: Type and data of an events returned in
streams.
* `Mastodon::Easy::alert_type`, used for push subscriptions.
* `Mastodon::Easy::time_type`: Type for time, can be converted to `time_point`
and `string`.
* `Mastodon::Easy::account_field_type`: Type for fields in accounts.
* `Mastodon::Easy::urls_type`: Type for URLs returned by `Instance::urls()`.
* `Mastodon::Easy::stats_type`: Type for statistics returned by
`Instance::stats()`.
* `Mastodon::Easy::poll_options_type`: Type for poll options returned by
`Poll::options()`.
=== Error codes
[options="header",cols=">,<"]
|===================================================
| Code | Explanation
| 0 | No error
| 1 | Invalid argument
| 10 | URL changed (HTTP 301 or 308)
| 11 | Connection timed out
| 12 | Connection refused (check http_error_code)
| 13 | No route to host / Could not resolve host
| 14 | Encryption error
| 127 | Unknown error
|===================================================
If you use a debug build, you get more verbose error messages.
=== Useful links
* https://docs.joinmastodon.org/[Mastodon documentation]
* https://git.pleroma.social/pleroma/pleroma/tree/develop/docs/api[Pleroma documentation]
* https://glitch-soc.github.io/docs/#whats-different[glitch-soc documentation]
== Install
=== Upgrading from versions below 0.100.0
Starting with version `0.100.0`, large parts of the library have been rewritten.
Upgrading from previous versions will require extensive code changes.
=== Packages
Every https://schlomp.space/tastytea/{project}/releases[release] includes
packages for Debian and Centos. Gentoo packages are available in my overlay.
==== Gentoo
Add my https://schlomp.space/tastytea/overlay[repository] and
install it from there.
[source,shell]
----
eselect repository enable tastytea
echo 'dev-cpp/mastodon-cpp ~amd64' >> /etc/portage/package.accept_keywords/mastodon-cpp
emaint sync -r tastytea
emerge -a dev-cpp/mastodon-cpp
----
==== DEB and RPM
Prebuilt DEB and RPM packages for x86_64(amd64) are provided with each release.
`.deb` packages are built on Debian stretch and `.rpm` packages are built on
CentOS 7. These packages are automatically built and not tested.
=== From source
==== Dependencies
* Tested OS: Linux
* C++ compiler (tested: https://gcc.gnu.org/[gcc] 6/8/9,
https://llvm.org/[clang] 5/7)
* https://cmake.org/[cmake] (at least: 3.6)
* https://pocoproject.org/[POCO] (tested: 1.9 / 1.7)
* Optional
** Easy interface & Examples:
https://github.com/open-source-parsers/jsoncpp[jsoncpp] (tested: 1.8 / 1.7)
** Documentation: https://www.stack.nl/~dimitri/doxygen/[doxygen] (tested: 1.8)
** DEB package: https://packages.qa.debian.org/dpkg[dpkg] (tested: 1.18)
** RPM package: http://www.rpm.org[rpm-build] (tested: 4.11)
** Tests: https://github.com/catchorg/Catch2[catch] (tested: 2.5 / 1.2)
.Install dependencies in Debian stretch.
====
[source,shell]
----
apt-get install build-essential cmake libpoco-dev libjsoncpp-dev doxygen
----
====
==== Get sourcecode
===== Release
Download the current release at
https://schlomp.space/tastytea/{project}/releases[schlomp.space].
===== Development version
[source,shell]
----
git clone https://schlomp.space/tastytea/mastodon-cpp.git
----
==== Compile
[source,shell]
----
mkdir build
cd build/
cmake ..
cmake --build . -- -j$(nproc --ignore=1)
----
.cmake options:
* `-DCMAKE_BUILD_TYPE=Debug` for a debug build.
* `-DWITH_EASY=NO` to not build the Easy abstractions and to get rid of the
jsoncpp-dependency (not recommended).
* `-DWITH_EXAMPLES=YES` if you want to compile the examples.
* `-DWITH_TESTS=YES` if you want to compile the tests.
* `-DEXTRA_TEST_ARGS` to run only some tests
(https://github.com/catchorg/Catch2/blob/master/docs/command-line.md#specifying-which-tests-to-run[format]).
** Possible tags: `[api]`, `[auth]`, `[mastodon]`, `[glitch-soc]`,
`[pleroma]`, `[upload]`, `[entity]`.
* `-DWITH_DOC=NO` if you don't want to compile the HTML reference.
* One of:
** `-DWITH_DEB=YES` if you want to be able to generate a deb-package.
** `-DWITH_RPM=YES` if you want to be able to generate an rpm-package.
.Run only tests for glitch-soc features that don't upload any files.
====
[source,shell]
----
cmake -DWITH_TESTS=YES -DEXTRA_TEST_ARGS=[glitch-soc]~[upload] ..
----
====
==== Tests
You can run the tests with `ctest` inside the build directory. You need to set
the environment variable `MASTODON_CPP_ACCESS_TOKEN` to an access token with the
scopes _read_, _write_ and _follow_ for tests tagged with `[auth]`.
You can select the instance to use with `MASTODON_CPP_INSTANCE`, the default is
_likeable.space_. You can select the user ID with `MASTODON_CPP_USER_ID`, the
default is _9hnrrVPriLiLVAhfVo_. You can select the status ID with
`MASTODON_CPP_STATUS_ID`, the default is _9hwnuJMq3eTdO4s1PU_. You can select
the filter ID with `MASTODON_CPP_FILTER_ID`. You can select the list ID with
`MASTODON_CPP_LIST_ID`, the default is _2_. You can select the media ID with
`MASTODON_CPP_MEDIA_ID`, the default is _2127742613_.
.Requirements for the test-user:
* Have at least 1 follower.
* Follow at least 1 account.
* Have at least 1 account endorsed.
* Have at least 1 public or unlisted status.
* Have at least 1 post favourited.
* Have no follow requests.
* Have at least 1 list with at least one account in it.
* have at least 1 account muted.
include::{uri-base}/raw/branch/master/CONTRIBUTING.adoc[]
== Status of implementation
You can still use unsupported calls by using `API::get` and the others with
strings and you can use unsupported fields in an `Entity` by converting it to
`Json::Value`.
=== Mastodon API
==== Calls
* Accounts
** [x] GET /api/v1/accounts/:id
** [x] POST /api/v1/accounts
** [x] GET /api/v1/accounts/verify_credentials
** [x] PATCH /api/v1/accounts/update_credentials
** [x] GET /api/v1/accounts/:id/followers
** [x] GET /api/v1/accounts/:id/following
** [x] GET /api/v1/accounts/:id/statuses
** [x] POST /api/v1/accounts/:id/follow
** [x] POST /api/v1/accounts/:id/unfollow
** [x] GET /api/v1/accounts/relationships
** [x] GET /api/v1/accounts/search
* Apps
** [x] POST /api/v1/apps
** [x] GET /api/v1/apps/verify_credentials
* Blocks
** [x] GET /api/v1/blocks
** [x] POST /api/v1/accounts/:id/block
** [x] POST /api/v1/accounts/:id/unblock
* Custom emoji
** [x] GET /api/v1/custom_emojis
* Domain blocks
** [x] GET /api/v1/domain_blocks
** [x] POST /api/v1/domain_blocks
** [x] DELETE /api/v1/domain_blocks
* Endorsements
** [x] GET /api/v1/endorsements
** [x] POST /api/v1/accounts/:id/pin
** [x] POST /api/v1/accounts/:id/unpin
* Favourites
** [x] GET /api/v1/favourites
** [x] POST /api/v1/statuses/:id/favourite
** [x] POST /api/v1/statuses/:id/unfavourite
* Filters
** [x] GET /api/v1/filters
** [x] POST /api/v1/filters
** [x] GET /api/v1/filters/:id
** [x] PUT /api/v1/filters/:id
** [x] DELETE /api/v1/filters/:id
* Follow requests
** [x] GET /api/v1/follow_requests
** [x] POST /api/v1/follow_requests/:id/authorize
** [x] POST /api/v1/follow_requests/:id/reject
* Follow suggestions
** [x] GET /api/v1/suggestions
** [x] DELETE /api/v1/suggestions/:account_id
* Instances
** [x] GET /api/v1/instance
* Lists
** [x] GET /api/v1/lists
** [x] GET /api/v1/accounts/:id/lists
** [x] GET /api/v1/lists/:id/accounts
** [x] GET /api/v1/lists/:id
** [x] POST /api/v1/lists
** [x] PUT /api/v1/lists/:id
** [x] DELETE /api/v1/lists/:id
** [x] POST /api/v1/lists/:id/accounts
** [x] DELETE /api/v1/lists/:id/accounts
* Media attachments
** [x] POST /api/v1/media
** [x] PUT /api/v1/media/:id
* Mutes
** [x] GET /api/v1/mutes
** [x] POST /api/v1/accounts/:id/mute
** [x] POST /api/v1/accounts/:id/unmute
** [x] POST /api/v1/statuses/:id/mute
** [x] POST /api/v1/statuses/:id/unmute
* Notifications
** [x] GET /api/v1/notifications
** [x] GET /api/v1/notifications/:id
** [x] POST /api/v1/notifications/clear
** [x] POST /api/v1/notifications/dismiss
** [x] POST /api/v1/push/subscription
** [x] GET /api/v1/push/subscription
** [x] PUT /api/v1/push/subscription
** [x] DELETE /api/v1/push/subscription
* Polls
** [x] GET /api/v1/polls/:id
** [x] POST /api/v1/polls/:id/votes
* Reports
** [x] POST /api/v1/reports
* Scheduled Statuses
** [ ] GET /api/v1/scheduled_statuses
** [ ] GET /api/v1/scheduled_statuses/:id
** [ ] PUT /api/v1/scheduled_statuses/:id
** [ ] DELETE /api/v1/scheduled_statuses/:id
* Search
** [x] GET /api/v2/search
* Statuses
** [x] GET /api/v1/statuses/:id
** [x] GET /api/v1/statuses/:id/context
** [x] GET /api/v1/statuses/:id/card
** [x] GET /api/v1/statuses/:id/reblogged_by
** [x] GET /api/v1/statuses/:id/favourited_by
** [x] POST /api/v1/statuses
** [x] DELETE /api/v1/statuses/:id
** [x] POST /api/v1/statuses/:id/reblog
** [x] POST /api/v1/statuses/:id/unreblog
** [x] POST /api/v1/statuses/:id/pin
** [x] POST /api/v1/statuses/:id/unpin
* Timelines
** [x] GET /api/v1/timelines/home
** [x] GET /api/v1/conversations
** [x] GET /api/v1/timelines/public
** [x] GET /api/v1/timelines/tag/:hashtag
** [x] GET /api/v1/timelines/list/:list_id
* Streaming API
** [x] GET /api/v1/streaming/health
** [x] GET /api/v1/streaming/user
** [x] GET /api/v1/streaming/public
** [x] GET /api/v1/streaming/public/local
** [x] GET /api/v1/streaming/hashtag
** [x] GET /api/v1/streaming/hashtag/local
** [x] GET /api/v1/streaming/list
** [x] GET /api/v1/streaming/direct
==== Entities
* [x] Account
* [x] Application
* [x] Attachment
* [x] Card
* [x] Context
* [x] Conversation
* [x] Emoji
* [x] Filter
* [x] Instance
* [x] List
* [x] Mention
* [x] Notification
* [x] Poll
* [x] PushSubscription
* [x] Relationship
* [x] Results
* [x] Status
* [ ] ScheduledStatus
* [x] Tag
* [ ] Token
=== glitch-soc API
==== Calls
* [x] GET /api/v1/bookmarks
* [x] POST /api/v1/statuses/:id/bookmark
* [x] POST /api/v1/statuses/:id/unbookmark
==== Entities
* [x] `max_toot_chars` in Instance
=== Pleroma API
==== Calls
* [ ] `preview` and `content_type` in POST /api/v1/statuses
* [ ] `no_rich_text`, `hide_followers`, `hide_follows`, `hide_favorites` and
`show_role` in /api/v1/update_credentials
* [ ] GET /api/pleroma/emoji
* [ ] POST /api/pleroma/follow_import
* [ ] GET /api/pleroma/captcha
* [ ] POST /api/pleroma/delete_account
* [ ] POST /api/account/register
* [ ] POST /api/v1/pleroma/flavour/:flavour
* [ ] GET /api/v1/pleroma/flavour
* [ ] POST /api/pleroma/notifications/read
* [ ] POST /api/v1/pleroma/accounts/:id/subscribe
* [ ] POST /api/v1/pleroma/accounts/:id/unsubscribe
* [ ] GET /api/v1/pleroma/accounts/:id/favourites
* [ ] PUT /api/pleroma/notification_settings
* [ ] GET /api/pleroma/healthcheck
* Admin API
** [ ] GET /api/pleroma/admin/users
** [ ] DELETE /api/pleroma/admin/user
** [ ] POST /api/pleroma/admin/user
** [ ] POST /api/pleroma/admin/user/follow
** [ ] POST /api/pleroma/admin/user/unfollow
** [ ] PATCH /api/pleroma/admin/users/:nickname/toggle_activation
** [ ] PUT /api/pleroma/admin/users/tag
** [ ] DELETE /api/pleroma/admin/users/tag
** [ ] GET /api/pleroma/admin/permission_group/:nickname
** [ ] GET /api/pleroma/admin/permission_group/:nickname/:permission_group
** [ ] POST /api/pleroma/admin/permission_group/:nickname/:permission_group
** [ ] DELETE /api/pleroma/admin/permission_group/:nickname/:permission_group
** [ ] PUT /api/pleroma/admin/activation_status/:nickname
** [ ] GET /api/pleroma/admin/users/:nickname
** [ ] POST /api/pleroma/admin/relay
** [ ] DELETE /api/pleroma/admin/relay
** [ ] GET /api/pleroma/admin/invite_token
** [ ] GET /api/pleroma/admin/invites
** [ ] POST /api/pleroma/admin/revoke_invite
** [ ] POST /api/pleroma/admin/email_invite
** [ ] GET /api/pleroma/admin/password_reset
==== Entities
* `pleroma` object in:
** [ ] Status
** [ ] Attachment
** [ ] Account
** [ ] Source
** [ ] Notification
== Blocked instances
Instances that are frequently used to hurt marginalized people are blocked from
using this library.
.List of blocked instances:
* https://en.wikipedia.org/wiki/Gab_(social_network)[Gab]
* https://en.wikipedia.org/wiki/Kiwi_Farms[Kiwi Farms]
== Copyright
[source,text]
----
Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>.
License AGPLv3: <https://www.gnu.org/licenses/agpl-3.0.html>.
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
----

284
README.md Normal file
View File

@ -0,0 +1,284 @@
**mastodon-cpp** is a C++ wrapper for the Mastodon API.
The library takes care of the network stuff. You submit a query and get the raw
JSON. You can then put that JSON into easy to use classes.
[TODO-list](https://schlomp.space/tastytea/mastodon-cpp/milestones)
**The ABI will be unstable in versions < 1.0.0**
**Beginning with 0.30.0, all IDs are `const string` instead of
`uint_fast64_t` as required by the Mastodon API. Sorry for the inconvenience.**
# Breaking API changes
I'm going to rewrite key parts of this library. The new API will be incompatible
with the old one. The new code will start with the version number `0.100.0`. The
old code is archived in the branch "pre-0.100.0" and will get bugfixes for a
while.
# Usage
The HTML reference can be generated with `build_doc.sh`, if doxygen is installed. It is also available at
[doc.schlomp.space/mastodon-cpp/](https://doc.schlomp.space/mastodon-cpp/annotated.html).
There are [examples](https://schlomp.space/tastytea/mastodon-cpp/src/branch/master/examples) in `examples/`.
## Most basic example
```C++
#include <iostream>
#include <string>
#include <mastodon-cpp/mastodon-cpp.hpp>
int main()
{
Mastodon::API masto("social.example.com", "auth_token");
std::string answer;
masto.get(Mastodon::API::v1::accounts_verify_credentials, answer);
std::cout << answer << '\n';
}
```
## Another simple example
Using the `Easy`-class.
```C++
#include <iostream>
#include <string>
#include <vector>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
using Mastodon::Easy;
int main()
{
Easy masto("social.example", "");
std::string answer;
masto.get(Mastodon::API::v1::timelines_public, answer);
for (const std::string &str : Easy::json_array_to_vector(answer))
{
Easy::Status status(str);
std::cout << " " << status.account().acct() << " wrote:\n";
std::cout << status.content() << '\n';
}
}
```
## Compiling your project
A project consisting of one file can be compiled as follows:
```SH
g++ -std=c++14 -lmastodon-cpp example.cpp
```
## Error codes
mastodon-cpp will never use error codes below 11, except 0.
| Code | Explanation |
| --------: |:---------------------------------|
| 0 | No error |
| 11 | Invalid call |
| 12 | Not implemented |
| 13 | URL changed (HTTP 301 or 308) |
| 14 | Aborted by user |
| 15 | Network error (curlpp exception) |
| 16 | Timeout |
| 100 - 999 | HTTP status codes |
| 65535 | Unknown error |
If you use a debug build, you get more verbose error messages.
## Useful links
* [Mastodon documentation](https://docs.joinmastodon.org/)
# Install
## Packages
Every [release](https://schlomp.space/tastytea/mastodon-cpp/releases) includes
packages for the package managers of Debian and Red Hat. Gentoo packages are
available in an overlay.
### Gentoo
Add my [repository](https://schlomp.space/tastytea/overlay) and
install it from there.
```SH
eselect repository enable tastytea
echo 'dev-cpp/mastodon-cpp ~amd64' >> /etc/portage/package.keywords/mastodon-cpp
emaint sync -r tastytea
emerge -a dev-cpp/mastodon-cpp
```
### DEB and RPM
Prebuilt DEB and RPM packages for x86_64(amd64) are provided with each release.
These packages are automatically built and not tested.
Install with `dpkg -i` or `rpm -i`, respectively.
To use the DEB package on stretch, you will need [libcurlpp0](https://packages.debian.org/libcurlpp0) from sid.
## From source
### Dependencies
* Tested OS: Linux
* C++ compiler (tested: gcc 5 / 6 / 7 / 8)
* [cmake](https://cmake.org/) (tested: 3.9 / 3.12)
* [pkgconfig](https://pkgconfig.freedesktop.org/wiki/) (tested: 0.29)
* [curlpp](http://www.curlpp.org/) (tested: 0.8)
* Optional
* Easy interface & Examples: [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8 / 1.7)
* Documentation: [doxygen](https://www.stack.nl/~dimitri/doxygen/) (tested: 1.8)
* DEB package: [dpkg](https://packages.qa.debian.org/dpkg) (tested: 1.19 / 1.18)
* RPM package: [rpm](http://www.rpm.org) (tested: 4.14 / 4.12)
### Get sourcecode
#### Release
Download the current release at [schlomp.space](https://schlomp.space/tastytea/mastodon-cpp/releases).
#### Development version
```SH
git clone https://schlomp.space/tastytea/mastodon-cpp.git
```
### Compile
```SH
mkdir build
cd build/
cmake ..
make
```
cmake options:
* `-DCMAKE_BUILD_TYPE=Debug` for a debug build
* `-DWITHOUT_EASY=ON` to not build the Easy abstractions and to get rid of the jsoncpp-dependency (not recommended)
* `-DWITH_EXAMPLES=ON` if you want to compile the examples
* `-DWITH_TESTS=ON` if you want to compile the tests
* `-DWITH_STATIC=ON` If you want a static library along with the dynamic one
* `-DWITH_DOC=ON` if you want to compile the HTML reference
* `-DWITH_DEB=ON` if you want to be able to generate a deb-package
* `-DWITH_RPM=ON` if you want to be able to generate an rpm-package
You can run the tests with `ctest ..` inside the build directory.
To install, run `make install`.
### Packages
#### DEB and RPM
Compile with `-DWITH_DEB=ON` or `-DWITH_RPM=ON`.
Run `make package` from the build directory to generate a DEB/RPM package.
#### Other
Run `make package` from the build directory to generate a tar.gz archive.
# Status of implementation
Feature complete as of Mastodon 2.6.1
* [x] GET /api/v1/accounts/:id
* [x] GET /api/v1/accounts/verify_credentials
* [x] PATCH /api/v1/accounts/update_credentials
* [x] GET /api/v1/accounts/:id/followers
* [x] GET /api/v1/accounts/:id/following
* [x] GET /api/v1/accounts/:id/statuses
* [x] POST /api/v1/accounts/:id/follow
* [x] POST /api/v1/accounts/:id/unfollow
* [x] POST /api/v1/accounts/:id/block
* [x] POST /api/v1/accounts/:id/unblock
* [x] POST /api/v1/accounts/:id/mute
* [x] POST /api/v1/accounts/:id/unmute
* [x] GET /api/v1/accounts/relationships
* [x] GET /api/v1/accounts/search
* [x] POST /api/v1/apps
* [x] GET /api/v1/blocks
* [x] GET /api/v1/domain_blocks
* [x] POST /api/v1/domain_blocks
* [x] DELETE /api/v1/domain_blocks
* [x] GET /api/v1/endorsements
* [x] POST /api/v1/accounts/:id/pin
* [x] POST /api/v1/accounts/:id/unpin
* [x] GET /api/v1/favourites
* [x] GET /api/v1/follow_requests
* [x] POST /api/v1/follow_requests/:id/authorize
* [x] POST /api/v1/follow_requests/:id/reject
* [x] POST /api/v1/follows
* [x] GET /api/v1/instance
* [x] GET /api/v1/custom_emojis
* [x] GET /api/v1/lists
* [x] GET /api/v1/accounts/:id/lists
* [x] GET /api/v1/lists/:id/accounts
* [x] GET /api/v1/lists/:id
* [x] POST /api/v1/lists
* [x] PUT /api/v1/lists/:id
* [x] DELETE /api/v1/lists/:id
* [x] POST /api/v1/lists/:id/accounts
* [x] DELETE /api/v1/lists/:id/accounts
* [x] POST /api/v1/media
* [x] PUT /api/v1/media/:id
* [x] GET /api/v1/mutes
* [x] GET /api/v1/notifications
* [x] GET /api/v1/notifications/:id
* [x] POST /api/v1/notifications/clear
* [x] POST /api/v1/notifications/dismiss
* [x] GET /api/v1/reports
* [x] POST /api/v1/reports
* [x] GET /api/v1/search
* [x] GET /api/v1/statuses/:id
* [x] GET /api/v1/statuses/:id/context
* [x] GET /api/v1/statuses/:id/card
* [x] GET /api/v1/statuses/:id/reblogged_by
* [x] GET /api/v1/statuses/:id/favourited_by
* [x] POST /api/v1/statuses
* [x] DELETE /api/v1/statuses/:id
* [x] POST /api/v1/statuses/:id/reblog
* [x] POST /api/v1/statuses/:id/unreblog
* [x] POST /api/v1/statuses/:id/favourite
* [x] POST /api/v1/statuses/:id/unfavourite
* [x] POST /api/v1/statuses/:id/pin
* [x] POST /api/v1/statuses/:id/unpin
* [x] POST /api/v1/statuses/:id/mute
* [x] POST /api/v1/statuses/:id/unmute
* [x] GET /api/v1/timelines/home
* [x] GET /api/v1/timelines/public
* [x] GET /api/v1/timelines/tag/:hashtag
* [x] GET /api/v1/timelines/list/:list_id
* [x] GET /api/v1/streaming/user
* [x] GET /api/v1/streaming/public
* [x] GET /api/v1/streaming/public/local
* [x] GET /api/v1/streaming/hashtag
* [x] GET /api/v1/streaming/list
* [x] POST /api/v1/push/subscription
* [x] GET /api/v1/push/subscription
* [x] PUT /api/v1/push/subscription
* [x] DELETE /api/v1/push/subscription
* [x] GET /api/v2/search
## Glitch-Soc support
* [x] max_toot_chars in /api/v1/instance
* [x] GET /api/v1/bookmarks
* [x] POST /api/v1/statuses/:id/bookmark
* [x] POST /api/v1/statuses/:id/unbookmark
# Copyright
```PLAIN
Copyright © 2018 tastytea <tastytea@tastytea.de>.
License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
```

10
build.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
mkdir -p build && \
cd build/ && \
cmake .. && \
make
if [ "$(basename ${PWD})" == "build" ]; then
cd ..
fi

View File

@ -1,19 +0,0 @@
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PACKAGE_VERSION}
COMPATIBILITY ExactVersion) # TODO: Set to SameMajorVersion when stable.
install(EXPORT ${PROJECT_NAME}Targets
FILE "${PROJECT_NAME}Targets.cmake"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
configure_file("${PROJECT_NAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

View File

@ -1,7 +0,0 @@
include(CMakeFindDependencyMacro)
include(GNUInstallDirs)
find_dependency(jsoncpp CONFIG REQUIRED)
find_package(Poco COMPONENTS Foundation Net NetSSL CONFIG REQUIRED)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")

View File

@ -1,46 +0,0 @@
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
set(CPACK_PACKAGE_VERSION_MAJOR ${${PROJECT_NAME}_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${${PROJECT_NAME}_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${${PROJECT_NAME}_VERSION_PATCH})
set(CPACK_PACKAGE_VERSION ${mastodon-cpp_VERSION})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_DESCRIPTION}")
if(CPACK_PACKAGE_DESCRIPTION_SUMMARY STREQUAL "") # CMake < 3.9
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "C++ wrapper for the Mastodon API.")
endif()
set(CPACK_PACKAGE_CONTACT "tastytea <tastytea@tastytea.de>")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${PROJECT_SOURCE_DIR}/README.adoc")
execute_process(COMMAND uname -m
OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}_${CPACK_PACKAGE_ARCHITECTURE}")
set(CPACK_GENERATOR "TGZ")
if (WITH_DEB)
set(CPACK_PACKAGE_NAME "lib${PROJECT_NAME}")
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE
"https://schlomp.space/tastytea/${PROJECT_NAME}")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
execute_process(COMMAND dpkg --print-architecture
OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND lsb_release --codename --short
OUTPUT_VARIABLE DEBIAN_CODENAME
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}-0_${DEBIAN_CODENAME}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
endif()
if (WITH_RPM)
set(CPACK_PACKAGE_NAME "lib${PROJECT_NAME}")
set(CPACK_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_LICENSE "AGPL-3")
set(CPACK_RPM_PACKAGE_URL "https://schlomp.space/tastytea/${PROJECT_NAME}")
set(CPACK_RPM_PACKAGE_REQUIRES "poco-netssl >= 1.6, jsoncpp >= 1.7.4")
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-0.${CPACK_PACKAGE_ARCHITECTURE}")
endif()
include(CPack)

View File

@ -1,11 +0,0 @@
file(GLOB sources_examples *.cpp)
foreach(src ${sources_examples})
get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} ${src})
target_link_libraries(${bin} PRIVATE ${PROJECT_NAME})
endforeach()
if(WITH_DOC)
install(FILES ${sources_examples}
DESTINATION "${CMAKE_INSTALL_DOCDIR}/${PROJECT_NAME}/examples")
endif()

View File

@ -0,0 +1,89 @@
/* This file is part of mastodon-cpp.
* This example dumps the raw JSON of your last toot with media attached
* and your last 2 followers.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::cout << "Your last toot with media attached:\n";
std::string uid = answer.substr(7, answer.find("\"", 7) - 7);
API::parametermap parameters =
{
{ "id", { uid } },
{ "limit", { "1" } },
{ "only_media", { "1" } }
};
ret = masto.get(API::v1::accounts_id_statuses, parameters, answer);
if (ret == 0)
{
std::cout << answer << '\n';
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
std::cout << "\nYour last 2 followers:\n";
parameters =
{
{
"limit", { "2" }
},
{
"exclude_types", { "favourite", "reblog", "mention" }
}
};
ret = masto.get(API::v1::notifications, parameters, answer);
if (ret == 0)
{
std::cout << answer << '\n';
return 0;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,78 +0,0 @@
// This file is part of mastodon-cpp.
// Get the last 4 local posts from the public timeline and display them.
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <cstdint>
#include <regex>
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::uint16_t;
using namespace Mastodon;
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <instance>\n";
return 1;
}
// Construct a Mastodon::Easy object without authorization token.
Easy::API masto(argv[1], "");
// Retrieve the last 4 local statuses.
return_call ret = masto.get(API::v1::timelines_public,
{
{ "local", { "true" }},
{ "limit", { "4" }}
});
// If no error was returned.
if (ret)
{
// Convert answer to vector of strings, loop through vector.
for (const string &strstatus : Easy::json_array_to_vector(ret.answer))
{
// Construct mastodon::Easy::Status from string.
Easy::Status status(strstatus);
string content = status.content();
std::regex re_html("<[^>]+>");
// Remove HTML tags from status content.
content = std::regex_replace(content, re_html, "");
// Print account and content.
cout << " " << status.account().acct() << " wrote:\n";
cout << content << endl << endl;
}
}
else
{
// Print error message.
cerr << ret.error_message << endl;
// Print HTTP status code, if there is one.
if (ret.http_error_code != 0)
{
cerr << "HTTP status code: "
<< std::to_string(ret.http_error_code) << endl;
}
return ret.error_code;
}
return 0;
}
#else
#include <cstdio>
int main()
{
std::printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,75 @@
/* This file is part of mastodon-cpp.
* This example parses your account data and prints it out in a readable way.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#include <jsoncpp/json/json.h>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
using std::cout;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
std::string answer;
std::stringstream ss;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
ss.str(answer);
Json::Value json;
ss >> json;
std::string uid = json["id"].asString();
cout << "Your ID is: " << uid << '\n';
cout << "Your whole acount tree:\n";
for (auto it = json.begin(); it != json.end(); ++it)
{
if (it.name().compare("source") == 0)
{
cout << it.name() << '\n';
for (auto it_s = (*it).begin(); it_s != (*it).end(); ++it_s)
{
cout << '\t' << it_s.name() << ": ";
cout << *it_s << '\n';
}
}
else
{
cout << it.name() << ": ";
cout << *it << '\n';
}
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,109 +0,0 @@
// This file is part of mastodon-cpp.
// Listen to /api/v1/streaming/public and display the events.
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <cstdint>
#include <mutex>
#include <chrono>
#include <vector>
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::uint8_t;
using namespace Mastodon;
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <instance>\n";
return 1;
}
// Construct a Mastodon::Easy object without authorization token.
Easy::API masto(argv[1], "");
// Prepare a pointer to the http object, to cancel the stream later.
std::unique_ptr<API::http> ptr;
// This variable is filled with the stream data.
string stream;
// Get the public timeline. The pointer is set here.
masto.get_stream(API::v1::streaming_public, ptr, stream);
// Listen to the stream for 120 seconds.
for (uint8_t counter = 0; counter < 120; ++counter)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
// Acquire lock for the stream variable to avoid simultaneous access.
std::lock_guard<std::mutex> lock(ptr->get_mutex());
// Parse event stream into a vector.
std::vector<Easy::stream_event_type> events
= Easy::parse_stream(stream);
// Clear the stream buffer.
stream.clear();
for (const Easy::stream_event_type &event : events)
{
// Print out some information about the events.
switch (event.type)
{
case Easy::event_type::Update:
{
Easy::Status status(event.data);
cout << "[" << status.created_at().strtime("%T") << "] ";
cout << "Status from: " << status.account().acct()
<< " (" << status.url() << ")\n";
break;
}
case Easy::event_type::Notification:
{
Easy::Notification notification(event.data);
cout << "Notification involving: "
<< notification.account().acct()
<< " (" << notification.id() << ")\n";
break;
}
case Easy::event_type::Delete:
{
cout << "Deleted: " << event.data << endl;
break;
}
case Easy::event_type::Error:
{
// Errors are reported in-stream. Print error and exit.
cerr << "Error: " << event.data << endl;
ptr->cancel_stream();
return 1;
}
default:
{
cout << "Something undefined happened. 😱\n";
cout << event.data << endl;
}
}
}
}
// Close connection.
ptr->cancel_stream();
return 0;
}
#else
#include <cstdio>
int main()
{
std::printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -1,91 +0,0 @@
// This file is part of mastodon-cpp.
// Get 2 pages of 5 favourites each.
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <vector>
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using namespace Mastodon;
void print_favs(Easy::API &masto, const string &max_id = "")
{
// Set up parameters.
Mastodon::parameters params =
{
{ "limit", { "5" } },
{ "exclude_types", { "follow", "reblog", "mention" } }
};
// Set max_id if given.
if (!max_id.empty())
{
params.push_back({ "max_id", { max_id } });
}
// Retrieve the last 5 favourites.
return_call ret = masto.get(API::v1::notifications, params);
// If no error was returned.
if (ret)
{
// Convert answer to vector of strings, loop through vector.
for (const string &str : Easy::json_array_to_vector(ret.answer))
{
// Construct Mastodon::Easy::Notification from string.
const Easy::Notification notif(str);
cout << notif.created_at().strtime("%F %T: ");
cout << notif.account().display_name() << " favourited a status.\n";
}
}
else
{
// Print error message.
cerr << ret.error_message << endl;
// Print HTTP status code, if there is one.
if (ret.http_error_code != 0)
{
cerr << "HTTP status code: "
<< std::to_string(ret.http_error_code) << endl;
}
}
}
int main(int argc, char *argv[])
{
const vector<string> args(argv, argv + argc);
if (args.size() < 3)
{
std::cerr << "usage: " << args[0] << " <instance> <auth token>\n";
return 1;
}
// Construct a Mastodon::Easy object.
Easy::API masto(args[1], args[2]);
print_favs(masto);
cout << "\nLink header is: " << masto.get_header("link") << "\n\n";
// Get the max_id for pagination.
// See <https://docs.joinmastodon.org/api/rest/notifications/#pagination>
print_favs(masto, masto.get_link().max_id());
// The same: print_favs(masto, masto.get_link().next());
return 0;
}
#else
#include <iostream>
int main()
{
std::cout << "mastodon-cpp was compiled without Easy support.\n";
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,134 @@
/* This file is part of mastodon-cpp.
* Prints the new toots under a hashtag, designed to be used in cronjobs
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <regex>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <jsoncpp/json/json.h>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
using std::cout;
using std::string;
int main(int argc, char *argv[])
{
string limit = "20";
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <hashtag> [limit]\n";
std::cerr << " Default limit is 20, max limit is 40.\n";
return 1;
}
else if (argc == 4)
{
limit = argv[3];
}
Mastodon::API masto(argv[1], "");
string hashtag = argv[2];
string answer;
std::stringstream ss;
std::uint16_t ret;
Json::Value config;
string lastid = "0";
string filename = string(getenv("HOME")) + "/.config/mastocron.json";
// Read config file, get last seen toot-id
try {
std::ifstream file(filename, std::ifstream::binary);
file >> config;
lastid = config.get(hashtag, "0").asString();
}
catch (std::exception &e)
{
// most likely no config file found, ignore
}
// Only get toots we haven't seen yet
API::parametermap params =
{
{ "hashtag", { hashtag } },
{ "limit", { limit } },
{ "since_id", { lastid } }
};
ret = masto.get(API::v1::timelines_tag_hashtag, params, answer);
if (ret == 0)
{
// If answer is empty, there are no new toots,
// if answer is "[]" there are none at all
if (answer != "" && answer != "[]")
{
string ornament = " +++++";
for (std::uint8_t i = hashtag.size(); i > 0; --i)
{
ornament += "+";
}
cout << ornament << '\n';
cout << " + " << hashtag << ": +\n";
cout << ornament << '\n';
Json::Value tree;
ss.str(answer);
ss >> tree;
for (const auto &toot : tree)
{
string content = toot["content"].asString();
std::regex reparagraph("</p><p>");
std::regex restrip("<[^>]*>");
cout << "++++++++\n";
content = std::regex_replace(content, reparagraph, "\n\n");
cout << std::regex_replace(content, restrip, "") << '\n';
cout << " ";
cout << toot["account"]["display_name"].asString()
<< " (" << toot["account"]["acct"] << ") at "
<< toot["created_at"].asString() << "\n";
cout << " " << toot["url"].asString() << '\n';
for (const auto &media : toot["media_attachments"])
{
cout << "Attachment: <" << media["url"].asString() << ">\n";
}
cout << "++++++++\n";
}
// Write the id of the newest toot in the config file
lastid = tree[0]["id"].asString();
config[hashtag] = lastid;
Json::StreamWriterBuilder wbuilder;
const string output = Json::writeString(wbuilder, config);
std::ofstream outfile(filename);
if (outfile.is_open())
{
outfile.write(output.c_str(), output.length());
outfile.close();
}
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,48 +0,0 @@
// This file is part of mastodon-cpp.
// Prints shortcode + URL for each custom emoji.
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <vector>
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
using std::string;
using std::vector;
using namespace Mastodon;
int main(int argc, char *argv[])
{
const vector<string> args(argv, argv + argc);
if (args.size() < 2)
{
std::cerr << "usage: " << args[0] << " <instance>\n";
return 1;
}
// Construct a Mastodon::Easy object.
Easy::API masto(args[1], "");
const return_call ret = masto.get(API::v1::custom_emojis);
// Convert JSON array into vector of strings.
for (const string &str : Easy::json_array_to_vector(ret))
{
// Construct Emoji object from string.
const Easy::Emoji emoji(str);
std::cout << ':' << emoji.shortcode() << ": <" << emoji.url() << ">\n";
}
return 0;
}
#else
#include <iostream>
int main()
{
std::cout << "mastodon-cpp was compiled without Easy support.\n";
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,83 @@
/* This file is part of mastodon-cpp.
* Updates your name, description and avatar.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
// This is the Gimp icon, 24x24px, GPL-3
const std::string avatar =
"data:image/png;base64,"
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz"
"AAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAAMdEVY"
"dFRpdGxlAEZvbGRlcuNZL58AAAAUdEVYdEF1dGhvcgBKYWt1YiBTdGVpbmVy5vv3LwAAACF0RVh0"
"U291cmNlAGh0dHA6Ly9qaW1tYWMubXVzaWNoYWxsLmN6aWbjXgAAAFJ0RVh0Q29weXJpZ2h0AEND"
"IEF0dHJpYnV0aW9uLVNoYXJlQWxpa2UgaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbGljZW5z"
"ZXMvYnktc2EvMy4wL16DWrwAAARFSURBVEiJtZXNbxtFGMafGe+ud9eO7bXbNBsSNx9GSaW2aQgK"
"UDiQUmQJiQsHJETFhVNBKuLAhQviT0Aq6g0heqjUS4UQqhYJhCrxoUZKQakIJTFuTGzXjmN7d+31"
"endnhwNNiEIKRWpeaTSjmdHzm/cZzbwkn8/jIIMeqDoA4VGIVCqVZ0DxUuAFn2Wz2dXda/tmUC6X"
"z21sbGQfRrxarb4Mig8BvEkpHdy7vi+AUvokpfSNUqmk/8fJXyeEvD8xNvpcTFUzlNLI3j37W0Rx"
"WKDC/OnTT72gaRoAIJ4YuJ3OaLpl2ZptWtRnjOsjQ1m3a49kUgmp1TJ5FyAPBwCk+bmZ6ZHRUWly"
"YjISi8WkWv3erOd78tkzL0paSiPVaoX//NMiGvUaOrZJ/qG8F2AYBs3n8yEApBKpiBKLq6dmTglj"
"Y+OQJJEoSjSZyRzG1NQxqKoKXdeJqkRx8+Z36PU6D7Rx5w6i0ehkvdH4qLRR/mpk9LFnj5+YEaem"
"j/Hx8Qly7drnxHF6GDoyBMYYlpaWcPHix2i1TRD85QslhP9rBkktee6t8+df5cDg2todDA8PE0VW"
"cOnSJVy5cgXvXHgbAOA4Dq5evYobN24gNzkBgIA80KBdAOazCcZYYkjX6fXrX3JVpFAVOTI39wRU"
"VUIyNYDVtV+RSKaxcGYBs7OzqNc2sFEqwrYsBCFzGWHhAwEAbl++fHkhlTmk3C3+TkK3jaDfhqYl"
"Ydk9tBoVCIIAKSpDllWwkGHxx+9BwdFxfe55QYsxFhqGIQAIt+9zB0AixFwv3Q3anS7cfh89x0Fz"
"8x563SZ6bgh1IA3f9yH1qoj5W2i4IhyzBzWlo+/58INADdxgAEAMQN8wDD+fz7MdAGf8iyAIT7S3"
"Gq9FBKoFYQjXC8A40Gya8Co1MMZAmYsMNbFwpIn+oQgKbhc9VyYkCFYty4oDyAAwAXQNwwgjuVwO"
"ALC8vNyhsnwnoShxRVGyw/qgeiiTJqIoggAIfB+EAJwIECQZHQ/QxD5mkm2kFBHlXkxumu6iZdtV"
"AP79FuwACoUC6TtOJx6PFwWBmv2ADRNCU4TSiBAREFNVpFIaUuk0lHgagXIEdTtEyQz5b20Z82df"
"ERgRp5LJ5POapvXDMCw6jtPdAeRyORQKBViW1REEsciBdcZ4xbS6TttywqbVxWbTJvWtNq1utru1"
"hrleavSKf1j4utzsf2K7XG+1WpOU0izn/HFZlr/Z3Nys7P0qQgBurVYrO45z3XXcHyRJ0kRRHGCM"
"xQMeKAAID7jnc26HnucxxqxOp7Nle7/cSiQGLoii9HQYhkc9zxsEECH7VTTDMHYe6P2eAJABSAA4"
"gOC+x9uvlwPg09PTR2VZfo9zftyyrA+KxeK3+wL2C8MwKP7+Wvguceyaw8mTJ8cIIe96nvfpysrK"
"rYcG/J+oVqvjAOq6rncfScncG7quF7fHB170DxzwJ8uH9gSkOVdYAAAAAElFTkSuQmCC";
API::parametermap params =
{
{ "display_name", { "Botty McBotface" } },
{ "note", { "Beep Bop." } },
{ "avatar", { avatar } }
};
ret = masto.patch(API::v1::accounts_update_credentials, params, answer);
if (ret == 0)
{
std::cout << answer << '\n';
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -0,0 +1,102 @@
/* This file is part of mastodon-cpp.
* Follow someone, then unfollow.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <chrono>
#include <thread>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string id;
std::cout << "Follow [ID or username@domain]: ";
std::cin >> id;
// If no @ is found, it is presumably an ID
if (id.find('@') == std::string::npos)
{
ret = masto.post(API::v1::accounts_id_follow, {{"id", {id}}}, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
std::cout << "Unfollowing in 30 seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(30));
ret = masto.post(API::v1::accounts_id_unfollow, {{"id", {id}}}, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else
{
API::parametermap params =
{
{ "uri", { id } }
};
ret = masto.post(API::v1::follows, params, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
id = answer.substr(7, answer.find("\"", 7) - 7);
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
std::cout << "Unfollowing " << id << " in 30 seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(30));
ret = masto.post(API::v1::accounts_id_unfollow, {{"id", {id}}}, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
return 0;
}

View File

@ -0,0 +1,74 @@
/* This file is part of mastodon-cpp.
* Post a status, then delete it.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <chrono>
#include <thread>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string toot;
std::string id;
std::cout << "Toot: ";
std::cin >> toot;
API::parametermap parameters =
{
{ "status", { toot } },
{ "visibility", { "unlisted" } },
{ "spoiler_text", { "test" } }
};
ret = masto.post(API::v1::statuses, parameters, answer);
if (ret == 0)
{
id = answer.substr(7, answer.find("\"", 7) - 7);
std::cout << "Deleting in 30 seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(30));
ret = masto.del(API::v1::statuses_id, {{"id", {id}}});
if (ret == 0)
{
std::cout << "Status " << id << " deleted.\n";
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -0,0 +1,88 @@
/* This file is part of mastodon-cpp.
* Register as an application.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <instance>\n";
return 1;
}
Mastodon::API masto(argv[1], "");
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string client_id, client_secret, url;
ret = masto.register_app1("test123",
"urn:ietf:wg:oauth:2.0:oob",
"read follow",
"",
client_id,
client_secret,
url);
if (ret == 0)
{
std::cout << "Visit " << url << " to get the code.\n";
std::string code;
std::cout << "Insert code: ";
std::cin >> code;
std::string access_token;
ret = masto.register_app2(client_id,
client_secret,
"urn:ietf:wg:oauth:2.0:oob",
code,
access_token);
if (ret == 0)
{
std::cout << "Success!\nAccess-token: " << access_token << '\n';
std::cout << "Testing access token...\n";
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::cout << answer;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << url << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -0,0 +1,57 @@
/* This file is part of mastodon-cpp.
* In this example we look at HTTP headers to determine how many calls we are
* allowed to make.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::string remaining = masto.get_header("X-RateLimit-Remaining");
std::string reset = masto.get_header("X-RateLimit-Reset");
std::cout << "You are allowed to make " << remaining
<< " calls until the counter is reset at " << reset << '\n';
if (std::stoi(remaining) <= 1)
{
std::cout << "🔥 Please let the server cool off a bit! 🔥\n";
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -0,0 +1,77 @@
/* This file is part of mastodon-cpp.
* Streaming API example
*/
#include <iostream>
#include <string>
#include <cstdint>
#include <thread>
#include <chrono>
#include <memory>
#include <mutex>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access_token>\n";
return 1;
}
static std::string answer;
static std::unique_ptr<Mastodon::API::http> ptr;
std::cout << "Dumping public timeline...\n";
std::thread pub([=]
{
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
masto.get_stream(API::v1::streaming_public, answer, ptr);
});
std::uint8_t counter = 0;
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
if (ptr != nullptr)
{
std::lock_guard<std::mutex> lock(ptr->get_mutex());
++counter;
std::cout << answer;
answer.clear();
if (counter == 10)
{
std::cerr << "Cancelling...\n";
ptr->cancel_stream();
break;
}
}
}
pub.join();
std::cout << '\n';
std::cout << "Dumping the #np tag...\n";
answer = "";
std::thread tag([=]
{
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
masto.get_stream(API::v1::streaming_hashtag, {{ "hashtag", {"np"} }},
answer, ptr);
});
std::this_thread::sleep_for(std::chrono::seconds(20));
ptr->cancel_stream();
std::cout << answer;
std::cout << '\n';
tag.join();
return 0;
}

View File

@ -0,0 +1,56 @@
/* This file is part of mastodon-cpp.
* Simplify the interface
*/
#include <iostream>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
class EasyToot : public Mastodon::API
{
public:
explicit EasyToot(const std::string &instance, const std::string &access_token)
: API(instance, access_token)
{}
std::uint16_t toot(const std::string &text, const std::string &cw = "")
{
std::string answer;
API::parametermap parameters;
parameters.insert({ "status", { text } } );
if (!cw.empty())
{
parameters.insert({ "spoiler_text", { cw } } );
}
return post(API::v1::statuses, parameters, answer);
}
};
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access_token>\n";
return 1;
}
std::uint16_t ret = 0;
EasyToot masto(argv[1], argv[2]);
ret = masto.toot("Test");
if (ret != 0)
{
std::cerr << "Error: " << ret << '\n';
}
return ret;
}

View File

@ -0,0 +1,82 @@
/* This file is part of mastodon-cpp.
* Post the same media 2 times, with URLs
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#include <jsoncpp/json/json.h>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
std::string answer;
std::stringstream ss;
Json::Value json;
std::uint16_t ret;
std::string filepath;
std::cout << "Absolute filename: ";
std::cin >> filepath;
API::parametermap parameters =
{
{ "file", { filepath } }
};
ret = masto.post(API::v1::media, parameters, answer);
if (ret == 0)
{
ss.str(answer);
ss >> json;
std::string image1_id = json["id"].asString();
std::string image1_url = json["url"].asString();
parameters =
{
{ "file", { filepath } }
};
ret = masto.post(API::v1::media, parameters, answer);
if (ret == 0)
{
ss.str(answer);
ss >> json;
std::string image2_id = json["id"].asString();
std::string image2_url = json["url"].asString();
parameters =
{
{ "status", { image1_url + " \n" + image2_url } },
{ "media_ids", { image1_id, image2_id } },
{ "visibility", { "direct" } }
};
ret = masto.post(API::v1::statuses, parameters, answer);
if (ret == 0)
{
return 0;
}
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
std::cout << answer << '\n';
std::cerr << "Error code: " << ret << '\n';
return ret;
}

View File

@ -0,0 +1,129 @@
/* This file is part of mastodon-cpp.
* Prints some information about your last status.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <cstdint>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
// Include all headers in mastodon-cpp/easy/
#include <mastodon-cpp/easy/all.hpp>
#endif
using Mastodon::API;
using Mastodon::Easy;
using std::cout;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
// Get own account in order to obtain account ID
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
// Construct an Account object using the JSON string from the server
Easy::Account acc(answer);
// Get last status
ret = masto.get(API::v1::accounts_id_statuses,
{
{ "id", { acc.id() } },
{ "limit", { "1" } }
},
answer);
if (ret == 0)
{
// answer contains an array with a single object. This works because
// Easy::Status will turn that into an object, but an array with
// more than 1 object will not work.
Easy::Status status(answer);
// An Entitiy is valid if the JSON was not empty and contained no
// "error":-key
if (status.valid())
{
if (!status.language().empty())
cout << "Language: " << status.language() << '\n';
if (!status.content().empty())
cout << "Content: "
<< status.content().substr(0, 200) << "\n";
if (!status.application().name().empty())
cout << "Application used: "
<< status.application().name() << '\n';
cout << "ID: " << status.id() << '\n';
string acct;
string url;
std::vector<Easy::Attachment> attachments;
std::vector<Easy::Tag> tags;
// If the status is a reblog, print the original author
if (status.reblog().valid())
{
// status.reblog() is an Easy::Status
// status.reblog().account() is an Easy::Account
cout << "Original ID: " << status.reblog().id() << '\n';
acct = status.reblog().account().acct();
url = status.reblog().account().url();
attachments = status.reblog().media_attachments();
tags = status.reblog().tags();
}
else
{
acct = status.account().acct();
url = status.account().url();
attachments = status.media_attachments();
tags = status.tags();
}
cout << "From: " << acct << " ";
cout << "(" << url << ")\n";
// List attachments, if any
for (const Easy::Attachment &attachment : attachments)
{
cout << "Attachment: " << attachment.text_url()
<< " (" << attachment.size() << ")\n";
}
// List hashtags, if any
for (const Easy::Tag &tag : tags)
{
cout << "Hashtag: #" << tag.name()
<< " (" << tag.url() << ")\n";
}
}
return 0;
}
}
std::cout << answer << '\n';
std::cerr << "Error code: " << ret << '\n';
return ret;
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,111 @@
/* This file is part of mastodon-cpp.
* Prints some information from the public timeline.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <thread>
#include <memory>
#include <vector>
#include <chrono>
#include <mutex>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
// Include all headers in mastodon-cpp/easy/
#include <mastodon-cpp/easy/all.hpp>
#endif
using Mastodon::API;
using Mastodon::Easy;
using std::cout;
using std::chrono::system_clock;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
cout << "I'll show you the public timeline. Press CTRL-C to cancel\n";
// These have to be static in order to use them in- and outside the thread
static std::string stream;
// You can cancel the stream with this pointer (ptr->cancel_stream())
static std::unique_ptr<Mastodon::API::http> ptr;
// Start a new thread for the stream
std::thread pub_tl([=]
{
Easy masto(argv[1], argv[2]);
masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr);
});
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
// Skip iteration if ptr points not to the Mastodon::API::http object
if (ptr == nullptr)
{
continue;
}
// Acquire lock for the stream variable
std::lock_guard<std::mutex> lock(ptr->get_mutex());
// Parse event stream and clear it afterwards
std::vector<Easy::stream_event> events = Easy::parse_stream(stream);
stream.clear();
// The contents of the stream are now in a vector of pairs
// { Easy::event_type, std::string }
for (const Easy::stream_event &event : events)
{
Easy::Status status;
Easy::Notification notification;
// Print out some information about the events
switch (event.first)
{
case Easy::event_type::Update:
status.from_string(event.second);
cout << "[" <<
Easy::strtime_local(status.created_at(), "%T") << "] ";
cout << "Status from: " << status.account().acct()
<< " (" << status.url() << ")\n";
break;
case Easy::event_type::Notification:
notification.from_string(event.second);
cout << "Notification involving: "
<< notification.account().acct()
<< " (" << notification.id() << ")\n";
break;
case Easy::event_type::Delete:
cout << "Deleted: " << event.second << '\n';
break;
default:
cout << "Something undefined happened. 😱\n";
}
}
}
pub_tl.join();
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,130 @@
/* This file is part of mastodon-cpp.
* Prints a Mastodon-conversation in a tree.
* Argument has to be in the form: https://<domain>/<user>/<status id>
*
* This example uses a rather wasteful approach in order to use less code and
* therefore be more clear. Please don't use it on long conversations.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <regex>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
// Include all headers in mastodon-cpp/easy/
#include <mastodon-cpp/easy/all.hpp>
#endif
using Mastodon::API;
using Mastodon::Easy;
using std::cout;
using std::chrono::system_clock;
// Print a status to stdout, nicely formatted
void format_status(const Easy::Status &status, const std::uint8_t &level)
{
std::string space(level, ' ');
// Remove HTML-tags
std::regex restrip("<[^>]*>");
std::string content = std::regex_replace(status.content(), restrip, "");
cout << space << "+-----------------------------------------\n";
cout << space << "| " << status.account().acct() << ": \n";
while (!content.empty())
{
cout << space << "| " << content.substr(0, 40) << '\n';
if (content.length() > 40)
{
content = content.substr(40);
}
else
{
content.clear();
}
}
cout << space << "| "
<< Easy::strtime_local(status.created_at(), "%T") << '\n';
cout << space << "+-----------------------------------------" << std::endl;
}
// Fetch status and its descendants, output them
std::uint16_t print_status(Easy &masto, const std::string &id,
const std::uint8_t level)
{
std::uint16_t ret;
std::string answer;
API::parametermap parameters =
{
{ "id", { id }}
};
ret = masto.get(Mastodon::API::v1::statuses_id, parameters, answer);
if (ret == 0)
{
format_status(Easy::Status(answer), level);
ret = masto.get(Mastodon::API::v1::statuses_id_context,
parameters, answer);
if (ret == 0)
{
Easy::Context context(answer);
for (const Easy::Status &s : context.descendants())
{
// Only print descendant if it is a direct reply
// NOTE: This is a wasteful approach, I use it only in the
// interest of more clarity. Don't do this at home. :-)
if (s.in_reply_to_id() == id)
{
print_status(masto, s.id(), level + 4);
}
}
}
}
return ret;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <url>\n";
return 1;
}
// Extract domain name
std::string instance = std::string(argv[1]);
std::size_t pos = instance.find('/') + 2;
instance = instance.substr(pos, instance.find('/', pos) - pos);
// Extract status ID
std::string id = std::string(argv[1]);
id = id.substr(id.rfind('/') + 1);
cout << " Instance: " << instance << "\nStatus ID: " << id << "\n\n";
Easy masto(instance, "");
return print_status(masto, id, 0);
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,40 @@
/* This file is part of mastodon-cpp.
* This example shows how to use a proxy.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using std::cout;
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
// SOCKS5 Proxy. Proxy resolves URL hostname.
masto.set_proxy("socks5h://[fd12::1a]:1080/");
// HTTPS proxy with username and password
masto.set_proxy("https://localhost:3128", "user5:supersecurepassword");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
cout << "Return code: " << ret << '\n';
std::cout << answer << '\n';
return 0;
}

View File

@ -0,0 +1,57 @@
/* This file is part of mastodon-cpp.
* Get fields from own account.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#endif
using std::cout;
using Mastodon::API;
using Mastodon::Easy;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
cout << "Return code: " << ret << '\n';
Easy::Account account(answer);
std::vector<Easy::Account::fields_pair> fields(account.fields());
for (const auto &field : fields)
{
cout << "Name: " << field.first << "\nValue: " << field.second << "\n\n";
}
return 0;
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -0,0 +1,55 @@
/* This file is part of mastodon-cpp.
* Print the first 20 characters from every bookmark (Glitch-Soc only).
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#endif
using std::cout;
using Mastodon::API;
using Mastodon::Easy;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::bookmarks, answer);
cout << "Return code: " << ret << '\n';
std::vector<string> statuses = Easy::json_array_to_vector(answer);
for (const string &json : statuses)
{
cout << Easy::Status(json).content().substr(0, 20) << '\n';
}
return 0;
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -1,7 +0,0 @@
include(GNUInstallDirs)
configure_file("${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

View File

@ -1,12 +0,0 @@
name=@PROJECT_NAME@
prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
Name: ${name}
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@
Cflags: -I${includedir}
Libs: -L${libdir} -l${name} -lpthread -lPocoNet
Requires: jsoncpp
Libs.private: -lPocoFoundation -lPocoNetSSL

View File

@ -1,71 +0,0 @@
include(GNUInstallDirs)
if(WITH_EASY)
find_package(jsoncpp CONFIG REQUIRED)
endif()
# Some distributions do not contain Poco*Config.cmake recipes.
find_package(Poco COMPONENTS Foundation Net NetSSL CONFIG)
if(WITH_EASY)
file(GLOB_RECURSE sources *.cpp *.hpp)
else()
file(GLOB sources *.cpp api/*.cpp *.hpp api/*.hpp)
endif()
# Write version in header
configure_file ("version.hpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/version.hpp")
add_library(${PROJECT_NAME} ${sources})
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR})
target_include_directories(${PROJECT_NAME}
PRIVATE
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
if(WITH_EASY)
target_link_libraries(${PROJECT_NAME}
PUBLIC pthread jsoncpp_lib)
endif()
# If no Poco*Config.cmake recipes are found, look for headers in standard dirs.
if(PocoNetSSL_FOUND)
target_link_libraries(${PROJECT_NAME}
PRIVATE Poco::Foundation Poco::Net Poco::NetSSL)
else()
find_file(Poco_h NAMES "Poco/Poco.h"
PATHS "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
if("${Poco_h}" STREQUAL "Poco_h-NOTFOUND")
message(FATAL_ERROR "Could not find POCO.")
else()
message(WARNING
"Your distribution of POCO doesn't contain the *Config.cmake recipes, "
"but the files seem to be in the standard directories. "
"Let's hope this works.")
target_link_libraries(${PROJECT_NAME}
PRIVATE PocoFoundation PocoNet PocoNetSSL)
endif()
endif()
install(TARGETS ${PROJECT_NAME}
EXPORT "${PROJECT_NAME}Targets"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(FILES mastodon-cpp.hpp return_types.hpp types.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
if(WITH_EASY)
file(GLOB easy_header easy/*.hpp)
install(FILES ${easy_header}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/easy)
file(GLOB easy_entities_header easy/entities/*.hpp)
install(FILES ${easy_entities_header}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/easy/entities)
endif()

View File

@ -1,92 +1,91 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
return_call API::del(const Mastodon::API::v1 &call,
const parameters &params)
uint_fast16_t API::del(const Mastodon::API::v1 &call,
const parametermap &parameters)
{
string strcall = "";
string strid = "";
// The ID is part of the path
const parameters::const_iterator &it_id = params.find("id");
const parameters::const_iterator &it_aid = params.find("accountid");
if (it_id != params.end())
const auto &it = parameters.find("id");
if (it != parameters.end())
{
strid = it_id->values[0];
}
else if (it_aid != params.end())
{
strid = it_aid->values[0];
strid = it->second[0];
}
switch (call)
{
case v1::domain_blocks:
{
strcall = "/api/v1/domain_blocks";
break;
}
case v1::lists_id:
{
strcall = "/api/v1/lists/" + strid;
break;
}
case v1::lists_id_accounts:
{
strcall = "/api/v1/lists/" + strid + "/accounts";
break;
}
case v1::statuses_id:
{
strcall = "/api/v1/statuses/" + strid;
break;
}
case v1::push_subscription:
{
strcall = "/api/v1/push/subscription";
break;
}
case v1::filters_id:
{
strcall = "/api/v1/filters/" + strid;
break;
}
case v1::suggestions_accountid:
{
strcall = "/api/v1/suggestions/" + strid;
break;
}
default:
{
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
}
case v1::domain_blocks:
strcall = "/api/v1/domain_blocks";
break;
case v1::lists_id:
strcall = "/api/v1/lists/" + strid;
break;
case v1::lists_id_accounts:
strcall = "/api/v1/lists/" + strid + "/accounts";
break;
case v1::statuses_id:
strcall = "/api/v1/statuses/" + strid;
break;
case v1::push_subscription:
strcall = "/api/v1/push/subscription";
break;
default:
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
return del(strcall, params);
string answer;
return del(strcall, parameters, answer);
}
return_call API::del(const std::string &call, const parameters &params)
uint_fast16_t API::del(const std::string &call,
const parametermap &parameters, string &answer)
{
return _http.request(http_method::DELETE, call, *maptoformdata(params));
return _http.request(http::method::DELETE, call,
maptoformdata(parameters), answer);
}
// ↓↓ DEPRECATED ↓↓
uint_fast16_t API::del(const Mastodon::API::v1 &call,
const string &argument)
{
const parametermap p = {};
return del(call, argument, p);
}
uint_fast16_t API::del(const Mastodon::API::v1 &call,
const string &argument,
const parametermap &parameters)
{
parametermap newparameters = parameters;
// Emulate old behaviour
newparameters["id"] = { argument };
return del(call, parameters);
}

View File

@ -1,306 +1,251 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <algorithm>
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
using std::cerr;
const return_call API::get(const Mastodon::API::v1 &call,
const parameters &params)
uint_fast16_t API::get(const Mastodon::API::v1 &call,
const parametermap &parameters, string &answer)
{
string strcall = "";
string strid = "";
// The ID is part of the path
const auto &it_id = params.find("id");
if (it_id != params.end())
const auto &it = parameters.find("id");
if (it != parameters.end())
{
strid = it_id->values[0];
strid = it->second[0];
}
switch (call)
{
case v1::accounts_verify_credentials:
{
strcall = "/api/v1/accounts/verify_credentials";
break;
}
case v1::blocks:
{
strcall = "/api/v1/blocks";
break;
}
case v1::domain_blocks:
{
strcall = "/api/v1/domain_blocks";
break;
}
case v1::favourites:
{
strcall = "/api/v1/favourites";
break;
}
case v1::follow_requests:
{
strcall = "/api/v1/follow_requests";
break;
}
case v1::instance:
{
strcall = "/api/v1/instance";
break;
}
case v1::custom_emojis:
{
strcall = "/api/v1/custom_emojis";
break;
}
case v1::lists:
{
strcall = "/api/v1/lists";
break;
}
case v1::mutes:
{
strcall = "/api/v1/mutes";
break;
}
case v1::notifications:
{
strcall = "/api/v1/notifications";
break;
}
case v1::timelines_home:
{
strcall = "/api/v1/timelines/home";
break;
}
case v1::timelines_public:
{
strcall = "/api/v1/timelines/public";
break;
}
case v1::accounts_relationships:
{
strcall = "/api/v1/accounts/relationships";
break;
}
case v1::accounts_id:
{
strcall = "/api/v1/accounts/" + strid;
break;
}
case v1::accounts_id_followers:
{
strcall = "/api/v1/accounts/" + strid + "/followers";
break;
}
case v1::accounts_id_following:
{
strcall = "/api/v1/accounts/" + strid + "/following";
break;
}
case v1::accounts_id_statuses:
{
strcall = "/api/v1/accounts/" + strid + "/statuses";
break;
}
case v1::accounts_search:
{
strcall = "/api/v1/accounts/search";
break;
}
case v1::accounts_id_lists:
{
strcall = "/api/v1/accounts/" + strid + "/lists";
break;
}
case v1::lists_id_accounts:
{
strcall = "/api/v1/lists/" + strid + "/accounts";
break;
}
case v1::lists_id:
{
strcall = "/api/v1/lists/" + strid;
break;
}
case v1::notifications_id:
{
strcall = "/api/v1/notifications/" + strid;
break;
}
case v1::statuses_id:
{
strcall = "/api/v1/statuses/" + strid;
break;
}
case v1::statuses_id_context:
{
strcall = "/api/v1/statuses/" + strid + "/context";
break;
}
case v1::statuses_id_card:
{
strcall = "/api/v1/statuses/" + strid + "/card";
break;
}
case v1::statuses_id_reblogged_by:
{
strcall = "/api/v1/statuses/" + strid + "/reblogged_by";
break;
}
case v1::statuses_id_favourited_by:
{
strcall = "/api/v1/statuses/" + strid + "/favourited_by";
break;
}
case v1::timelines_tag_hashtag:
{
// The tag is part of the path
const auto &it = params.find("tag");
if (it != params.end())
{
strcall = "/api/v1/timelines/tag/" + ::urlencode(it->values[0]);
}
else
{
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
}
break;
}
case v1::timelines_list_list_id:
{
strcall = "/api/v1/timelines/list/" + strid;
break;
}
case v1::push_subscription:
{
strcall = "/api/v1/push/subscription";
break;
}
case v1::endorsements:
{
strcall = "/api/v1/endorsements";
break;
}
case v1::bookmarks:
{
strcall = "/api/v1/bookmarks";
break;
}
case v1::apps_verify_credentials:
{
strcall = "/api/v1/apps/verify_credentials";
break;
}
case v1::filters:
{
strcall = "/api/v1/filters";
break;
}
case v1::filters_id:
{
strcall = "/api/v1/filters/" + strid;
break;
}
case v1::suggestions:
{
strcall = "/api/v1/suggestions";
break;
}
case v1::polls_id:
{
strcall = "/api/v1/polls/" + strid;
break;
}
case v1::conversations:
{
strcall = "/api/v1/conversations";
break;
}
case v1::streaming_health:
{
strcall = "/api/v1/streaming/health";
break;
}
default:
{
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
}
case v1::accounts_verify_credentials:
strcall = "/api/v1/accounts/verify_credentials";
break;
case v1::blocks:
strcall = "/api/v1/blocks";
break;
case v1::domain_blocks:
strcall = "/api/v1/domain_blocks";
break;
case v1::favourites:
strcall = "/api/v1/favourites";
break;
case v1::follow_requests:
strcall = "/api/v1/follow_requests";
break;
case v1::instance:
strcall = "/api/v1/instance";
break;
case v1::custom_emojis:
strcall = "/api/v1/custom_emojis";
break;
case v1::lists:
strcall = "/api/v1/lists";
break;
case v1::mutes:
strcall = "/api/v1/mutes";
break;
case v1::notifications:
strcall = "/api/v1/notifications";
break;
case v1::reports:
strcall = "/api/v1/reports";
break;
case v1::timelines_home:
strcall = "/api/v1/timelines/home";
break;
case v1::timelines_public:
strcall = "/api/v1/timelines/public";
break;
case v1::accounts_relationships:
strcall = "/api/v1/accounts/relationships";
break;
case v1::accounts_id:
strcall = "/api/v1/accounts/" + strid;
break;
case v1::accounts_id_followers:
strcall = "/api/v1/accounts/" + strid + "/followers";
break;
case v1::accounts_id_following:
strcall = "/api/v1/accounts/" + strid + "/following";
break;
case v1::accounts_id_statuses:
strcall = "/api/v1/accounts/" + strid + "/statuses";
break;
case v1::accounts_search:
strcall = "/api/v1/accounts/search";
break;
case v1::accounts_id_lists:
strcall = "/api/v1/accounts/" + strid + "/lists";
break;
case v1::lists_id_accounts:
strcall = "/api/v1/lists/" + strid + "/accounts";
break;
case v1::lists_id:
strcall = "/api/v1/lists/" + strid;
break;
case v1::notifications_id:
strcall = "/api/v1/notifications/" + strid;
break;
case v1::search:
strcall = "/api/v1/search";
break;
case v1::statuses_id:
strcall = "/api/v1/statuses/" + strid;
break;
case v1::statuses_id_context:
strcall = "/api/v1/statuses/" + strid + "/context";
break;
case v1::statuses_id_card:
strcall = "/api/v1/statuses/" + strid + "/card";
break;
case v1::statuses_id_reblogged_by:
strcall = "/api/v1/statuses/" + strid + "/reblogged_by";
break;
case v1::statuses_id_favourited_by:
strcall = "/api/v1/statuses/" + strid + "/favourited_by";
break;
case v1::timelines_tag_hashtag:
{
// The hashtag is part of the path
const auto &it = parameters.find("hashtag");
if (it != parameters.end())
{
strcall = "/api/v1/timelines/tag/" + urlencode(it->second[0]);
}
else
{
ttdebug << "ERROR: Invalid call.\n";
return 11;
}
}
break;
case v1::timelines_list_list_id:
strcall = "/api/v1/timelines/list/" + strid;
break;
case v1::push_subscription:
strcall = "/api/v1/push/subscription";
break;
case v1::endorsements:
strcall = "/api/v1/endorsements";
break;
case v1::bookmarks:
strcall = "/api/v1/bookmarks";
break;
default:
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
if (params.size() > 0)
if (parameters.size() > 0)
{
// Delete the parameters that are already in strcall
const parameters newparams = delete_params(params, { "id", "tag" });
strcall += maptostr(newparams);
parametermap newparameters = parameters;
newparameters.erase("id");
newparameters.erase("hashtag");
strcall += maptostr(newparameters);
}
return get(strcall);
return get(strcall, answer);
}
const return_call API::get(const Mastodon::API::v2 &call,
const parameters &params)
uint_fast16_t API::get(const Mastodon::API::v2 &call,
const parametermap &parameters, string &answer)
{
string strcall = "";
string strid = "";
// The ID is part of the path
const auto &it = params.find("id");
if (it != params.end())
const auto &it = parameters.find("id");
if (it != parameters.end())
{
strid = it->values[0];
strid = it->second[0];
}
switch (call)
{
case v2::search:
{
strcall = "/api/v2/search";
break;
}
default:
{
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
}
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
if (params.size() > 0)
if (parameters.size() > 0)
{
// Delete the parameterss that are already in strcall
const parameters newparams = delete_params(params, { "id", "tag" });
strcall += maptostr(newparams);
// Delete the parameters that are already in strcall
parametermap newparameters = parameters;
newparameters.erase("id");
newparameters.erase("hashtag");
strcall += maptostr(newparameters);
}
return get(strcall);
return get(strcall, answer);
}
const return_call API::get(const Mastodon::API::v1 &call)
uint_fast16_t API::get(const Mastodon::API::v1 &call, string &answer)
{
return get(call, {});
const parametermap p;
return get(call, p, answer);
}
const return_call API::get(const std::string &call)
uint_fast16_t API::get(const std::string &call, string &answer)
{
return _http.request(http_method::GET, call);
return _http.request(http::method::GET, call, answer);
}
// ↓↓ DEPRECATED ↓↓
uint_fast16_t API::get(const Mastodon::API::v1 &call,
const string &argument,
const parametermap &parameters, string &answer)
{
parametermap newparameters = parameters;
// Emulate old behaviour
switch (call)
{
case v1::search:
case v1::accounts_search:
newparameters["q"] = { argument };
break;
case v1::timelines_tag_hashtag:
newparameters["hashtag"] = { argument };
break;
default:
newparameters["id"] = { argument };
break;
}
return get(call, newparameters, answer);
}
uint_fast16_t API::get(const Mastodon::API::v1 &call,
const string &argument, string &answer)
{
const parametermap p;
return get(call, argument, p, answer);
}

View File

@ -1,99 +1,103 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
using std::cerr;
using std::to_string;
void API::get_stream(const Mastodon::API::v1 &call,
const parameters &params,
std::unique_ptr<Mastodon::API::http> &ptr,
string &stream)
uint_fast16_t API::get_stream(const Mastodon::API::v1 &call,
const parametermap &parameters,
string &answer,
std::unique_ptr<Mastodon::API::http> &ptr)
{
string strcall = "";
switch (call)
{
case Mastodon::API::v1::streaming_user:
{
strcall = "/api/v1/streaming/user";
break;
}
case v1::streaming_public:
{
strcall = "/api/v1/streaming/public";
break;
}
case v1::streaming_public_local:
{
strcall = "/api/v1/streaming/public/local";
break;
}
case v1::streaming_hashtag:
{
strcall = "/api/v1/streaming/hashtag";
break;
}
case v1::streaming_hashtag_local:
{
strcall = "/api/v1/streaming/hashtag/local";
break;
}
case v1::streaming_list:
{
strcall = "/api/v1/streaming/list";
break;
}
case v1::streaming_direct:
{
strcall = "/api/v1/streaming/direct";
break;
}
default:
{
const uint8_t err = static_cast<uint8_t>(error::INVALID_ARGUMENT);
ttdebug << "ERROR: Invalid call.\n";
stream = "event: ERROR\ndata: "
"{\"error_code\":" + to_string(err) + "}\n";
return;
}
case v1::streaming_user:
strcall = "/api/v1/streaming/user";
break;
case v1::streaming_public:
strcall = "/api/v1/streaming/public";
break;
case v1::streaming_public_local:
strcall = "/api/v1/streaming/public/local";
break;
case v1::streaming_hashtag:
strcall = "/api/v1/streaming/hashtag";
break;
case v1::streaming_list:
strcall = "/api/v1/streaming/list";
break;
default:
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
if (params.size() > 0)
if (parameters.size() > 0)
{
strcall += maptostr(params);
strcall += maptostr(parameters);
}
return get_stream(strcall, ptr, stream);
return get_stream(strcall, answer, ptr);
}
void API::get_stream(const Mastodon::API::v1 &call,
std::unique_ptr<Mastodon::API::http> &ptr,
string &stream)
uint_fast16_t API::get_stream(const Mastodon::API::v1 &call,
string &answer,
std::unique_ptr<Mastodon::API::http> &ptr)
{
return get_stream(call, {}, ptr, stream);
parametermap p = {};
return get_stream(call, p, answer, ptr);
}
void API::get_stream(const std::string &call, std::unique_ptr<http> &ptr,
string &stream)
uint_fast16_t API::get_stream(const std::string &call, string &answer,
std::unique_ptr<http> &ptr)
{
ptr = std::make_unique<http>(*this, _instance, _access_token);
return ptr->request_stream(call, stream);
return ptr->request(http::method::GET_STREAM, call, answer);
}
// ↓↓ DEPRECATED ↓↓
uint_fast16_t API::get_stream(const Mastodon::API::v1 &call,
const string &argument,
string &answer,
std::unique_ptr<Mastodon::API::http> &ptr)
{
parametermap parameters;
// Emulate old behaviour
switch (call)
{
case v1::streaming_hashtag:
parameters["tag"] = { argument };
break;
case v1::streaming_list:
parameters["list"] = { argument };
break;
default:
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
return get_stream(call, parameters, answer, ptr);
}

View File

@ -1,28 +1,29 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
using std::cerr;
return_call API::patch(const Mastodon::API::v1 &call,
const parameters &params)
uint_fast16_t API::patch(const Mastodon::API::v1 &call,
const parametermap &parameters,
string &answer)
{
string strcall = "";
switch (call)
@ -31,10 +32,11 @@ return_call API::patch(const Mastodon::API::v1 &call,
strcall = "/api/v1/accounts/update_credentials";
break;
default:
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
return _http.request(http_method::PATCH, strcall, *maptoformdata(params));
return _http.request(API::http::method::PATCH,
strcall, maptoformdata(parameters), answer);
}

View File

@ -1,223 +1,174 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
return_call API::post(const Mastodon::API::v1 &call,
const parameters &params)
uint_fast16_t API::post(const Mastodon::API::v1 &call,
const parametermap &parameters, string &answer)
{
string strcall = "";
string strid = "";
// The ID is part of the path
const auto &it = params.find("id");
if (it != params.end())
const auto &it = parameters.find("id");
if (it != parameters.end())
{
strid = it->values[0];
strid = it->second[0];
}
switch (call)
{
case v1::apps:
{
strcall = "/api/v1/apps";
break;
}
case v1::domain_blocks:
{
strcall = "/api/v1/domain_blocks";
break;
}
case v1::lists:
{
strcall = "/api/v1/lists";
break;
}
case v1::media:
{
strcall = "/api/v1/media";
break;
}
case v1::notifications_clear:
{
strcall = "/api/v1/notifications/clear";
break;
}
case v1::notifications_dismiss:
{
strcall = "/api/v1/notifications/dismiss";
break;
}
case v1::reports:
{
strcall = "/api/v1/reports";
break;
}
case v1::statuses:
{
strcall = "/api/v1/statuses";
break;
}
case v1::accounts:
{
strcall = "/api/v1/accounts";
break;
}
case v1::accounts_id_follow:
{
strcall = "/api/v1/accounts/" + strid + "/follow";
break;
}
case v1::accounts_id_unfollow:
{
strcall = "/api/v1/accounts/" + strid + "/unfollow";
break;
}
case v1::accounts_id_block:
{
strcall = "/api/v1/accounts/" + strid + "/block";
break;
}
case v1::accounts_id_unblock:
{
strcall = "/api/v1/accounts/" + strid + "/unblock";
break;
}
case v1::accounts_id_mute:
{
strcall = "/api/v1/accounts/" + strid + "/mute";
break;
}
case v1::accounts_id_unmute:
{
strcall = "/api/v1/accounts/" + strid + "/unmute";
break;
}
case v1::follow_requests_id_authorize:
{
strcall = "/api/v1/folow_requests/" + strid + "/authorize";
break;
}
case v1::follow_requests_id_reject:
{
strcall = "/api/v1/folow_requests/" + strid + "/reject";
break;
}
case v1::lists_id_accounts:
{
strcall = "/api/v1/lists/" + strid + "/accounts";
break;
}
case v1::statuses_id_reblog:
{
strcall = "/api/v1/statuses/" + strid + "/reblog";
break;
}
case v1::statuses_id_unreblog:
{
strcall = "/api/v1/statuses/" + strid + "/unreblog";
break;
}
case v1::statuses_id_favourite:
{
strcall = "/api/v1/statuses/" + strid + "/favourite";
break;
}
case v1::statuses_id_unfavourite:
{
strcall = "/api/v1/statuses/" + strid + "/unfavourite";
break;
}
case v1::statuses_id_pin:
{
strcall = "/api/v1/statuses/" + strid + "/pin";
break;
}
case v1::statuses_id_unpin:
{
strcall = "/api/v1/statuses/" + strid + "/unpin";
break;
}
case v1::statuses_id_mute:
{
strcall = "/api/v1/statuses/" + strid + "/mute";
break;
}
case v1::statuses_id_unmute:
{
strcall = "/api/v1/statuses/" + strid + "/unmute";
break;
}
case v1::push_subscription:
{
strcall = "/api/v1/push/subscription";
break;
}
case v1::accounts_id_pin:
{
strcall = "/api/v1/accounts/" + strid + "/pin";
break;
}
case v1::accounts_id_unpin:
{
strcall = "/api/v1/accounts/" + strid + "/unpin";
break;
}
case v1::statuses_id_bookmark:
{
strcall = "/api/v1/statuses/" + strid + "/bookmark";
break;
}
case v1::statuses_id_unbookmark:
{
strcall = "/api/v1/statuses/" + strid + "/unbookmark";
break;
}
case v1::filters:
{
strcall = "/api/v1/filters";
break;
}
case v1::polls_id_votes:
{
strcall = "/api/v1/polls/" + strid + "/votes";
break;
}
default:
{
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, ""};
}
case v1::apps:
strcall = "/api/v1/apps";
break;
case v1::domain_blocks:
strcall = "/api/v1/domain_blocks";
break;
case v1::follows:
strcall = "/api/v1/follows";
break;
case v1::lists:
strcall = "/api/v1/lists";
break;
case v1::media:
strcall = "/api/v1/media";
break;
case v1::notifications_clear:
strcall = "/api/v1/notifications/clear";
break;
case v1::notifications_dismiss:
strcall = "/api/v1/notifications/dismiss";
break;
case v1::reports:
strcall = "/api/v1/reports";
break;
case v1::statuses:
strcall = "/api/v1/statuses";
break;
case v1::accounts_id_follow:
strcall = "/api/v1/accounts/" + strid + "/follow";
break;
case v1::accounts_id_unfollow:
strcall = "/api/v1/accounts/" + strid + "/unfollow";
break;
case v1::accounts_id_block:
strcall = "/api/v1/accounts/" + strid + "/block";
break;
case v1::accounts_id_unblock:
strcall = "/api/v1/accounts/" + strid + "/unblock";
break;
case v1::accounts_id_mute:
strcall = "/api/v1/accounts/" + strid + "/mute";
break;
case v1::accounts_id_unmute:
strcall = "/api/v1/accounts/" + strid + "/unmute";
break;
case v1::follow_requests_id_authorize:
strcall = "/api/v1/folow_requests/" + strid + "/authorize";
break;
case v1::follow_requests_id_reject:
strcall = "/api/v1/folow_requests/" + strid + "/reject";
break;
case v1::lists_id_accounts:
strcall = "/api/v1/lists/" + strid + "/accounts";
break;
case v1::statuses_id_reblog:
strcall = "/api/v1/statuses/" + strid + "/reblog";
break;
case v1::statuses_id_unreblog:
strcall = "/api/v1/statuses/" + strid + "/unreblog";
break;
case v1::statuses_id_favourite:
strcall = "/api/v1/statuses/" + strid + "/favourite";
break;
case v1::statuses_id_unfavourite:
strcall = "/api/v1/statuses/" + strid + "/unfavourite";
break;
case v1::statuses_id_pin:
strcall = "/api/v1/statuses/" + strid + "/pin";
break;
case v1::statuses_id_unpin:
strcall = "/api/v1/statuses/" + strid + "/unpin";
break;
case v1::statuses_id_mute:
strcall = "/api/v1/statuses/" + strid + "/mute";
break;
case v1::statuses_id_unmute:
strcall = "/api/v1/statuses/" + strid + "/unmute";
break;
case v1::push_subscription:
strcall = "/api/v1/push/subscription";
break;
case v1::accounts_id_pin:
strcall = "/api/v1/accounts/" + strid + "/pin";
break;
case v1::accounts_id_unpin:
strcall = "/api/v1/accounts/" + strid + "/unpin";
break;
case v1::statuses_id_bookmark:
strcall = "/api/v1/statuses/" + strid + "/bookmark";
break;
case v1::statuses_id_unbookmark:
strcall = "/api/v1/statuses/" + strid + "/unbookmark";
break;
default:
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
return post(strcall, params);
return post(strcall, parameters, answer);
}
return_call API::post(const Mastodon::API::v1 &call)
uint_fast16_t API::post(const Mastodon::API::v1 &call, string &answer)
{
const parameters p;
return post(call, p);
const parametermap p;
return post(call, p, answer);
}
return_call API::post(const string &call, const parameters &params)
uint_fast16_t API::post(const string &call,
const parametermap &parameters, string &answer)
{
return _http.request(http_method::POST, call, *maptoformdata(params));
return _http.request(http::method::POST, call,
maptoformdata(parameters), answer);
}
// ↓↓ DEPRECATED ↓↓
uint_fast16_t API::post(const Mastodon::API::v1 &call,
const string &argument,
const parametermap &parameters, string &answer)
{
parametermap newparameters = parameters;
// Emulate old behaviour
newparameters["id"] = { argument };
return post(call, newparameters, answer);
}
uint_fast16_t API::post(const Mastodon::API::v1 &call,
const string &argument, string &answer)
{
const parametermap p;
return post(call, argument, p, answer);
}

View File

@ -1,72 +1,76 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
return_call API::put(const Mastodon::API::v1 &call,
const parameters &params)
uint_fast16_t API::put(const Mastodon::API::v1 &call,
const parametermap &parameters, string &answer)
{
string strcall = "";
string strid = "";
// The ID is part of the path
const auto &it = params.find("id");
if (it != params.end())
const auto &it = parameters.find("id");
if (it != parameters.end())
{
strid = it->values[0];
strid = it->second[0];
}
switch (call)
{
case v1::lists_id:
{
strcall = "/api/v1/lists/" + strid;
break;
}
case v1::media_id:
{
strcall = "/api/v1/media/" + strid;
break;
}
case v1::push_subscription:
{
strcall = "/api/v1/push/subscription";
break;
}
case v1::filters_id:
{
strcall = "/api/v1/filters/" + strid;
break;
}
default:
{
ttdebug << "ERROR: Invalid argument.\n";
return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
}
case v1::lists_id:
strcall = "/api/v1/lists/" + strid;
break;
case v1::media_id:
strcall = "/api/v1/media/" + strid;
break;
case v1::push_subscription:
strcall = "/api/v1/push/subscription";
break;
default:
ttdebug << "ERROR: Invalid call.\n";
return 11;
break;
}
return put(strcall, params);
return put(strcall, parameters, answer);
}
return_call API::put(const string &call, const parameters &params)
uint_fast16_t API::put(const string &call,
const parametermap &parameters, string &answer)
{
return _http.request(http_method::PUT, call, *maptoformdata(params));
return _http.request(http::method::PUT, call,
maptoformdata(parameters), answer);
}
// ↓↓ DEPRECATED ↓↓
uint_fast16_t API::put(const Mastodon::API::v1 &call,
const string &argument,
const parametermap &parameters, string &answer)
{
parametermap newparameters = parameters;
// Emulate old behaviour
newparameters["id"] = { argument };
return put(call, newparameters, answer);
}

View File

@ -1,41 +1,59 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_ALL_HPP
#define MASTODON_CPP_EASY_ALL_HPP
#include "easy.hpp"
#include "entities/account.hpp"
#include "entities/application.hpp"
#include "entities/attachment.hpp"
#include "entities/card.hpp"
#include "entities/context.hpp"
#include "entities/emoji.hpp"
#include "entities/instance.hpp"
#include "entities/list.hpp"
#include "entities/mention.hpp"
#include "entities/notification.hpp"
#include "entities/relationship.hpp"
#include "entities/results.hpp"
#include "entities/status.hpp"
#include "entities/tag.hpp"
#include "entities/token.hpp"
#include "entities/pushsubscription.hpp"
#include "entities/filter.hpp"
#include "entities/poll.hpp"
#include "entities/conversation.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "easy/easy.hpp"
#include "easy/entities/account.hpp"
#include "easy/entities/application.hpp"
#include "easy/entities/attachment.hpp"
#include "easy/entities/card.hpp"
#include "easy/entities/context.hpp"
#include "easy/entities/emoji.hpp"
#include "easy/entities/instance.hpp"
#include "easy/entities/list.hpp"
#include "easy/entities/mention.hpp"
#include "easy/entities/notification.hpp"
#include "easy/entities/relationship.hpp"
#include "easy/entities/report.hpp"
#include "easy/entities/results.hpp"
#include "easy/entities/status.hpp"
#include "easy/entities/tag.hpp"
#include "easy/entities/pushsubscription.hpp"
#else
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/entities/account.hpp>
#include <mastodon-cpp/easy/entities/application.hpp>
#include <mastodon-cpp/easy/entities/attachment.hpp>
#include <mastodon-cpp/easy/entities/card.hpp>
#include <mastodon-cpp/easy/entities/context.hpp>
#include <mastodon-cpp/easy/entities/emoji.hpp>
#include <mastodon-cpp/easy/entities/instance.hpp>
#include <mastodon-cpp/easy/entities/list.hpp>
#include <mastodon-cpp/easy/entities/mention.hpp>
#include <mastodon-cpp/easy/entities/notification.hpp>
#include <mastodon-cpp/easy/entities/relationship.hpp>
#include <mastodon-cpp/easy/entities/report.hpp>
#include <mastodon-cpp/easy/entities/results.hpp>
#include <mastodon-cpp/easy/entities/status.hpp>
#include <mastodon-cpp/easy/entities/tag.hpp>
#include <mastodon-cpp/easy/entities/pushsubscription.hpp>
#endif
#endif // MASTODON_CPP_EASY_ALL_HPP

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -18,14 +18,14 @@
#include <iomanip> // get_time
#include <sstream>
#include <regex>
#include <algorithm>
#include "easy.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using std::string;
Easy::API::API(const string &instance, const string &access_token)
: Mastodon::API(instance, access_token)
Easy::Easy(const string &instance, const string &access_token)
: API(instance, access_token)
{}
const std::vector<string> Easy::json_array_to_vector(const string &json)
@ -36,11 +36,11 @@ const std::vector<string> Easy::json_array_to_vector(const string &json)
if (json_array.isArray())
{
// 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(); });
std::vector<string> vec;
for (const Json::Value &value : json_array)
{
vec.push_back(value.toStyledString());
}
return vec;
}
@ -49,14 +49,13 @@ const std::vector<string> Easy::json_array_to_vector(const string &json)
return {};
}
const vector<Easy::stream_event_type> Easy::parse_stream(
const std::string &streamdata)
const std::vector<Easy::stream_event>
Easy::parse_stream(const std::string &streamdata)
{
string stream = streamdata;
std::regex reevent("event: (update|notification|delete|filters_changed"
"|ERROR)\ndata: (.*)\n");
std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n");
std::smatch match;
std::vector<stream_event_type> vec = {};
std::vector<stream_event> vec = {};
while (std::regex_search(stream, match, reevent))
{
@ -64,45 +63,64 @@ const vector<Easy::stream_event_type> Easy::parse_stream(
const string &data = match[2].str();
event_type type = event_type::Undefined;
if (event == "update")
if (event.compare("update") == 0)
type = event_type::Update;
else if (event == "notification")
else if (event.compare("notification") == 0)
type = event_type::Notification;
else if (event == "delete")
else if (event.compare("delete") == 0)
type = event_type::Delete;
else if (event == "ERROR")
type = event_type::Error;
else if (event == "filters_changed")
type = event_type::Filters_changed;
vec.push_back({ type, data });
vec.push_back(stream_event(type, data));
stream = match.suffix().str();
}
return vec;
}
const Easy::time_type Easy::string_to_time(const string &strtime)
{
std::stringstream sstime(strtime);
struct std::tm tm = {};
tm.tm_isdst = -1; // Detect daylight saving time.
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
std::time_t time = timegm(&tm); // Assume time is UTC.
return { system_clock::from_time_t(time) };
}
const Easy::Link Easy::API::get_link() const
const Easy::Link Easy::get_link() const
{
return Link(get_header("Link"));
}
const string Easy::strtime_utc(const system_clock::time_point &timepoint,
const string &format)
{
return strtime(timepoint, format, true);
}
const string Easy::strtime_local(const system_clock::time_point &timepoint,
const string &format)
{
return strtime(timepoint, format, false);
}
const string Easy::strtime(const system_clock::time_point &timepoint,
const string &format, const bool &utc)
{
constexpr std::uint_fast16_t bufsize = 1024;
std::time_t time = system_clock::to_time_t(timepoint);
std::tm *timeinfo;
if (utc)
{
timeinfo = std::gmtime(&time);
}
else
{
timeinfo = std::localtime(&time);
}
char buffer[bufsize];
std::strftime(buffer, bufsize, format.c_str(), timeinfo);
return buffer;
}
Easy::Link::Link(const string &link_header)
: _next()
, _prev()
{
std::regex renext("max_id=([[:alnum:]]*)");
std::regex reprev("(?:since|min)_id=([[:alnum:]]*)");
std::regex renext("max_id=([[:digit:]]*)");
std::regex reprev("since_id=([[:digit:]]*)");
std::smatch match;
if (std::regex_search(link_header, match, renext))
@ -134,8 +152,3 @@ const string Easy::Link::since_id() const
{
return _prev;
}
const string Easy::Link::min_id() const
{
return _prev;
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,182 +19,467 @@
#include <string>
#include <cstdint>
#include <chrono>
#include <vector>
#include <utility>
#include <functional>
#include <ostream>
#include <jsoncpp/json/json.h>
#include "../mastodon-cpp.hpp"
#include "return_types_easy.hpp"
#include "types_easy.hpp"
#include "entities/notification.hpp"
#include "entities/status.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using std::string;
using std::vector;
using std::uint64_t;
using std::uint16_t;
using std::uint_fast64_t;
using std::uint_fast16_t;
using std::chrono::system_clock;
namespace Mastodon
{
/*!
* @brief Collection of things that make it easier to interface with server
* software that implements the Mastodon API.
* @brief Child of Mastodon::API with abstract methods.
*
* @since before 0.11.0
*/
namespace Easy
class Easy : public API
{
public:
/*!
* @brief Class to hold the `Link`-header.
*
* Extracts max_id and since_id from the `Link`-header
*
* @brief Describes the event type
*
* @since before 0.11.0
*/
enum class event_type
{
Update,
Notification,
Delete,
Undefined
};
/*!
* @brief Describes visibility of toots.
*
* @since before 0.11.0
*/
enum class visibility_type
{
Direct,
Private,
Unlisted,
Public,
Undefined
};
/*!
* @brief Describes the attachment type
*
* @since before 0.11.0
*/
enum class attachment_type
{
Image,
Video,
Gifv,
Unknown,
Undefined
};
/*!
* @brief Describes the card type
*
* @since before 0.11.0
*/
enum class card_type
{
Link,
Photo,
Video,
Rich,
Undefined
};
/*!
* @brief Describes the notification type
*
* @since before 0.11.0
*/
enum class notification_type
{
Mention,
Reblog,
Favourite,
Follow,
Undefined
};
/*!
* @brief Used for stream events.
*
* @since before 0.11.0
*/
typedef std::pair<event_type, string> stream_event;
/*!
* @brief Map of 'notification type' and 'push is requested or not'
*
* Used in PushSubscription::alerts().
*
* @since 0.13.3
*/
typedef std::map<Easy::notification_type, bool> alertmap;
class Account;
class Application;
class Attachment;
class Card;
class Context;
class Emoji;
class Instance;
class List;
class Mention;
class Notification;
class Relationship;
class Report;
class Results;
class Status;
class Tag;
class PushSubscription;
/*!
* @brief Class to hold the `Link`-header.
*
* Extracts max_id and since_id from the `Link`-header
*
* @since before 0.11.0
*/
// TODO: Convert to struct?
class Link
{
public:
/*!
* @param link_header The content of the `Link` header
*
*
* @since before 0.11.0
*/
explicit Link(const string &link_header);
/*!
* @brief Returns max_id
*
*
* @since before 0.11.0
*/
const string next() const;
/*!
* @brief Returns max_id
*
*
* @since before 0.11.0
*/
const string max_id() const;
/*!
* @brief Returns since_id
*
*
* @since before 0.11.0
*/
const string prev() const;
/*!
* @brief Returns since_id
*
*
* @since before 0.11.0
*/
const string since_id() const;
/*!
* @brief Returns min_id.
*
* @since 0.111.0
*/
const string min_id() const;
private:
string _next;
string _prev;
};
/*!
* @brief Constructs a new Easy object.
*
* To register your application, leave access_token blank and call
* register_app1() and register_app2().
*
* @param instance The hostname of your instance
* @param access_token The access token
*
* @since before 0.11.0
*/
explicit Easy(const string &instance, const string &access_token);
/*!
* @brief Turns a JSON array into a vector of strings
*
* @param json JSON string holding the array
*
* @return vector of strings or an empty vector on error
*
*
* @since before 0.11.0
*/
const vector<string> json_array_to_vector(const string &json);
static const std::vector<string> json_array_to_vector(const string &json);
/*!
* @brief Split stream into a vector of events
*
* @param streamdata Data from get_stream()
*
* @return vector of Easy::stream_event
*
* @return vector of stream events
*
* @since before 0.11.0
*/
const vector<stream_event_type> parse_stream(const std::string &streamdata);
static const std::vector<stream_event>
parse_stream(const std::string &streamdata);
/*!
* @brief Convert ISO 8601 time string to Easy::time.
*
* @param strtime Time string as returned by Mastodon.
* @brief Gets the links from the last answer
*
* @since before 0.11.0
*/
const Easy::time_type string_to_time(const string &strtime);
const Link get_link() const;
/*!
* @brief Child of Mastodon::API with abstract methods.
* @brief Converts a time_point to a string
*
* The return value can not exceed 1023 chars.
*
* Provides convenient functions to deal with the responses you get.
* @param timepoint The timepoint
* @param format The format of the string, same as with `strftime`.
*
* Example:
* @code
* auto timepoint = status.created_at();
* cout << Easy::strtime_utc(timepoint, "%F, %T") << '\n';
* @endcode
*
* @return The UTC time as string
*
* @since 0.11.0
*/
class API : public Mastodon::API
static const string strtime_utc(const system_clock::time_point &timepoint,
const string &format);
/*!
* @brief See strtime_utc
*
* @return The local time as string
*
* @since 0.11.0
*/
static const string strtime_local(const system_clock::time_point &timepoint,
const string &format);
// #### simple calls ####
/*!
* @brief Sends a toot.
*
* @param status The status to send
* @param error @ref error "Error code"
*
* @return The new Easy::Status
*
* @since 0.18.1
*/
const Status send_post(const Status &status, uint_fast16_t &error);
/*!
* @brief Alias for send_post()
*
* @since 0.17.0
*/
const Status send_toot(const Status &status, uint_fast16_t &error);
/*!
* @brief Gets notifications.
*
* @param error @ref error "Error code"
* @param limit Maximum number of notifications
* @param since_id Return notifications newer than ID
* @param max_id Return notifications older than ID
*
* @return vector of Easy::Notification.
*
* @since 0.21.0
*/
const vector<Notification> get_notifications(
uint_fast16_t &error, const uint_fast16_t limit = 20,
const string since_id = "", const string max_id = "");
/*!
* @brief Base class for all entities.
*
* @since before 0.11.0
*/
class Entity
{
public:
/*!
* @brief Constructs a new Easy object.
* @brief Constructs an Entity object from a JSON string.
*
* To register your application, leave access_token blank and
* call register_app1() and register_app2().
*
* @param instance The hostname of your instance
* @param access_token The access token
*
* @since 0.100.0
* @param json JSON string
*
* @since before 0.11.0
*/
explicit API(const string &instance, const string &access_token);
explicit Entity(const string &json);
/*!
* @brief Gets the links from the last answer
*
* @since 0.100.0
* @brief Constructs an empty Entity object.
*
* @since before 0.11.0
*/
const Link get_link() const;
Entity();
/*!
* @brief Sends a post.
* @brief Replaces the Entity with a new one from a JSON string.
*
* @param status The status to send
*
* @return The new Easy::Status
*
* @since 0.100.0
* @param json JSON string
*
* @since before 0.11.0
*/
[[deprecated("Will go away in 1.0.0 with no replacement.")]]
const return_entity<Easy::Status> send_post(const Status &status);
void from_string(const string &json);
/*!
* @brief Alias for send_post()
* @brief Returns the JSON object of the Entity
*
* @since 0.100.0
* @return JSON object
*
* @since before 0.11.0
*/
[[deprecated("Will go away in 1.0.0 with no replacement.")]]
const return_entity<Easy::Status> send_toot(const Status &status);
const Json::Value to_object() const;
/*!
* @brief Gets notifications.
*
* @param limit Maximum number of notifications
* @param since_id Return notifications newer than ID
* @param max_id Return notifications older than ID
*
* @return vector of Easy::Notification.
*
* @since 0.100.0
* @brief Returns true if the Entity holds valid data
*
* @since before 0.11.0 (virtual since 0.18.2)
*/
[[deprecated("Will go away in 1.0.0 with no replacement.")]]
const return_entity_vector<Easy::Notification> get_notifications(
const uint16_t limit = 20, const string since_id = "",
const string max_id = "");
virtual bool valid() const = 0;
/*!
* @brief Returns error string sent by the server
*
* @since before 0.11.0
*/
const string error() const;
/*!
* @brief Returns true if the last requested value was set, false if
* it was unset.
*
* Members of Easy::Entity-derived classes return a default
* value depending on its type when the requested value is not
* found in the JSON. "" for strings, false for bools and so
* on. Most of the time this is no problem, but sometimes you
* need to know for sure.
*
* Example:
* @code
* Easy::Account a(jsonstring);
* if (a.note().empty())
* {
* if (a.was_set())
* {
* cout << "Account has an empty description.\n";
* }
* else
* {
* cout << "Account has no description.\n";
* }
* }
* @endcode
*
* @since before 0.11.0
*/
bool was_set() const;
protected:
/*!
* @brief Returns the value of key as Json::Value
*
* Returns an empty object if the value does not exist or is
* null.
*/
const Json::Value get(const string &key) const;
/*!
* @brief Returns the value of key as std::string
*
* returns "" if the value does not exist or is null.
*/
const string get_string(const string &key) const;
/*!
* @brief Returns the value of key as std::uint_fast64_t
*
* Returns 0 if the value does not exist or is null.
*/
uint_fast64_t get_uint64(const string &key) const;
/*!
* @brief Returns the value of key as double
*
* Returns 0.0 if the value does not exist or is null.
*/
double get_double(const string &key) const;
// TODO: Maybe an enum would be better?
/*!
* @brief Returns the value of key as bool
*
* Returns false if the value does not exist or is null.
*/
bool get_bool(const string &key) const;
/*!
* @brief Returns the value of key as time_point
*
* Returns clocks epoch if the value does not exist or is null.
*/
const system_clock::time_point get_time_point(const string &key) const;
/*!
* @brief Returns the value of key as vector
*
* Returns an empty vector if the value does not exist or is
* null.
*/
const std::vector<string> get_vector(const string &key) const;
/*!
* @brief Sets the value of key
*
* @since 0.17.0
*/
void set(const string &key, const Json::Value &value);
std::uint_fast64_t stouint64(const string &str) const;
/*!
* @brief Checks if an Entity is valid
*
* @param attributes The attributes to check
*
* @return true if all attributes are set
*
* @since 0.18.2
*/
bool check_valid(const std::vector<string> &attributes) const;
private:
Json::Value _tree;
mutable bool _was_set;
};
}
protected:
inline static const string strtime
(const system_clock::time_point &timepoint,
const string &format, const bool &utc);
};
}
#endif // MASTODON_EASY_CPP_HPP

View File

@ -1,48 +1,56 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <array>
#include "account.hpp"
#include "debug.hpp"
#include "easy/easy.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Account = Easy::Account;
Account::Account(const string &json)
: Entity(json)
{}
Account::Account()
: Entity()
{}
bool Account::valid() const
{
return Entity::check_valid(
{
"id",
"username",
"acct",
"display_name",
"locked",
"created_at",
"followers_count",
"following_count",
"statuses_count",
"note",
"url",
"avatar",
"avatar_static",
"header",
"header_static",
"emojis"
});
const std::vector<string> attributes =
{{
"id",
"username",
"acct",
"display_name",
"locked",
"created_at",
"followers_count",
"following_count",
"statuses_count",
"note",
"url",
"avatar",
"avatar_static",
"header",
"header_static"
}};
return Entity::check_valid(attributes);
}
const string Account::acct() const
@ -55,6 +63,12 @@ const string Account::avatar() const
return get_string("avatar");
}
Account Account::avatar(const string &avatar)
{
set("avatar", Json::Value(avatar));
return *this;
}
const string Account::avatar_static() const
{
return get_string("avatar_static");
@ -65,9 +79,9 @@ bool Account::bot() const
return get_bool("bot");
}
const Easy::time_type Account::created_at() const
const system_clock::time_point Account::created_at() const
{
return get_time("created_at");
return get_time_point("created_at");
}
const string Account::display_name() const
@ -75,54 +89,51 @@ const string Account::display_name() const
return get_string("display_name");
}
const std::vector<Easy::Emoji> Account::emojis()
Account Account::display_name(const string &display_name)
{
const Json::Value &node = get("emojis");
if (node.isArray())
{
std::vector<Easy::Emoji> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{
return Easy::Emoji(value);
});
return vec;
}
return {};
set("display_name", Json::Value(display_name));
return *this;
}
const vector<Easy::account_field_type> Account::fields() const
const std::vector<Account::fields_pair> Account::fields() const
{
const Json::Value &node = get("fields");
if (node.isArray())
{
vector<Easy::account_field_type> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{
return Easy::account_field_type(
{
value["name"].asString(),
value["value"].asString(),
Easy::string_to_time(
value["verified_at"].asString())
});
});
std::vector<Account::fields_pair> vec;
for (const Json::Value &value : node)
{
vec.push_back(Account::fields_pair(value["name"].asString(),
value["value"].asString()));
}
return vec;
}
return {};
}
std::uint64_t Account::followers_count() const
Account Account::fields(std::vector<Account::fields_pair> &fields)
{
Json::Value jsonarray(Json::arrayValue);
for (const fields_pair &field : fields)
{
Json::Value jsonkeyval(Json::objectValue);
jsonkeyval["name"] = field.first;
jsonkeyval["value"] = field.second;
jsonarray.append(jsonkeyval);
}
set("fields", jsonarray);
return *this;
}
std::uint_fast64_t Account::followers_count() const
{
return get_uint64("followers_count");
}
std::uint64_t Account::following_count() const
std::uint_fast64_t Account::following_count() const
{
return get_uint64("following_count");
}
@ -132,6 +143,12 @@ const string Account::header() const
return get_string("header");
}
Account Account::header(const string &header)
{
set("header", Json::Value(header));
return *this;
}
const string Account::header_static() const
{
return get_string("header_static");
@ -147,6 +164,12 @@ bool Account::locked() const
return get_bool("locked");
}
Account Account::locked(const bool &locked)
{
set("locked", Json::Value(locked));
return *this;
}
bool Account::has_moved() const
{
if (get("moved").isObject())
@ -161,7 +184,8 @@ const Account Account::moved() const
{
if (has_moved())
{
return Account(get("moved"));
// TODO: Find an account with moved-node and test
return Account(get("moved").toStyledString());
}
return Account();
@ -172,18 +196,30 @@ const string Account::note() const
return get_string("note");
}
Account Account::note(const string &note)
{
set("note", Json::Value(note));
return *this;
}
const string Account::note_plain() const
{
return get_string("source.note");
}
Easy::visibility_type Account::privacy() const
{
const string strprivacy = get_string("privacy");
if (strprivacy == "public")
const string strprivacy = get_string("source.privacy");
if (strprivacy.compare("public") == 0)
return visibility_type::Public;
else if (strprivacy == "unlisted")
else if (strprivacy.compare("unlisted") == 0)
return visibility_type::Unlisted;
else if (strprivacy == "private")
else if (strprivacy.compare("private") == 0)
return visibility_type::Private;
else if (strprivacy == "direct")
else if (strprivacy.compare("direct") == 0)
return visibility_type::Direct;
ttdebug << "Could not get data: source.privacy\n";
return visibility_type::Undefined;
}
@ -192,12 +228,138 @@ bool Account::sensitive() const
return get_bool("source.sensitive");
}
const Account::Source Account::source() const
Account::Source::Source(const string &json)
: Entity(json)
{}
Account::Source::Source()
: Entity()
{}
bool Account::Source::valid() const
{
return Account::Source(get("source"));
return true;
}
std::uint64_t Account::statuses_count() const
const std::vector<Account::fields_pair> Account::Source::fields() const
{
const Json::Value &node = get("fields");
if (node.isArray())
{
std::vector<Account::fields_pair> vec;
for (const Json::Value &value : node)
{
vec.push_back(Account::fields_pair(value["name"].asString(),
value["value"].asString()));
}
return vec;
}
return {};
}
Account::Source Account::Source::fields
(std::vector<Account::fields_pair> &fields)
{
Json::Value jsonarray(Json::arrayValue);
for (const fields_pair &field : fields)
{
Json::Value jsonkeyval(Json::objectValue);
jsonkeyval["name"] = field.first;
jsonkeyval["value"] = field.second;
jsonarray.append(jsonkeyval);
}
set("fields", jsonarray);
return *this;
}
const string Account::Source::note() const
{
return get_string("note");
}
Account::Source Account::Source::note(const string &note)
{
set("note", Json::Value(note));
return *this;
}
Easy::visibility_type Account::Source::privacy() const
{
const string strprivacy = get_string("privacy");
if (strprivacy.compare("public") == 0)
return visibility_type::Public;
else if (strprivacy.compare("unlisted") == 0)
return visibility_type::Unlisted;
else if (strprivacy.compare("private") == 0)
return visibility_type::Private;
else if (strprivacy.compare("direct") == 0)
return visibility_type::Direct;
ttdebug << "Could not get data: source.privacy\n";
return visibility_type::Undefined;
}
Account::Source Account::Source::privacy(const Easy::visibility_type &privacy)
{
string strprivacy = "";
switch (privacy)
{
case visibility_type::Public:
{
strprivacy = "public";
break;
}
case visibility_type::Unlisted:
{
strprivacy = "unlisted";
break;
}
case visibility_type::Private:
{
strprivacy = "private";
break;
}
case visibility_type::Direct:
{
strprivacy = "direct";
break;
}
default:
{
strprivacy = "undefined";
break;
}
}
set("privacy", Json::Value(strprivacy));
return *this;
}
bool Account::Source::sensitive() const
{
return get_bool("sensitive");
}
Account::Source Account::Source::sensitive(const bool &sensitive)
{
set("source", Json::Value(sensitive));
return *this;
}
const Account::Source Account::source() const
{
return Account::Source(get("source").toStyledString());
}
Account Account::source(const Account::Source &source)
{
set("source", Json::Value(source.to_object()));
return *this;
}
std::uint_fast64_t Account::statuses_count() const
{
return get_uint64("statuses_count");
}
@ -211,66 +373,3 @@ const string Account::username() const
{
return get_string("username");
}
bool Account::Source::valid() const
{
return Entity::check_valid(
{
"note",
"fields"
});
}
const vector<Easy::account_field_type> Account::Source::fields() const
{
const Json::Value &node = get("fields");
if (node.isArray())
{
vector<Easy::account_field_type> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{
return Easy::account_field_type(
{
value["name"].asString(),
value["value"].asString(),
Easy::string_to_time(
value["verified_at"].asString())
});
});
return vec;
}
return {};
}
const string Account::Source::language() const
{
return get_string("language");
}
const string Account::Source::note() const
{
return get_string("note");
}
Easy::visibility_type Account::Source::privacy() const
{
const string strprivacy = get_string("privacy");
if (strprivacy == "public")
return visibility_type::Public;
else if (strprivacy == "unlisted")
return visibility_type::Unlisted;
else if (strprivacy == "private")
return visibility_type::Private;
else if (strprivacy == "direct")
return visibility_type::Direct;
return visibility_type::Undefined;
}
bool Account::Source::sensitive() const
{
return get_bool("sensitive");
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,247 +19,349 @@
#include <string>
#include <cstdint>
#include <chrono>
#include <vector>
#include <utility>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "emoji.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::vector;
using std::uint_fast64_t;
using std::chrono::system_clock;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold accounts.
*
*
* @since before 0.11.0
*/
class Account : public Entity
class Easy::Account : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Describes a field. Format: name, value
*
* @since 0.16.1
*/
using fields_pair = std::pair<const string, const string>;
virtual bool valid() const override;
/*!
* @brief Constructs an Account object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Account(const string &json);
/*!
* @brief Constructs an empty Account object.
*
* @since before 0.11.0
*/
Account();
virtual bool valid() const;
/*!
* @brief Returns username
*
* `username` for users on the same instance, `user@hostname`
* for users on other instances.
*
*
* @since before 0.11.0
*/
const string acct() const;
/*!
* @brief Returns URL of avatar.
*
* @brief Returns URL of avatar
*
* @since before 0.11.0
*/
const string avatar() const;
/*!
* @brief Returns URL of static avatar.
*
* @brief Sets avatar
*
* Filename or base64-encoded
*
* @since 0.18.5
*/
Account avatar(const string &avatar);
/*!
* @brief Returns URL of static avatar
*
* @since before 0.11.0
*/
const string avatar_static() const;
/*!
* @brief Returns true if the account performs automated actions.
*
* @brief Returns true if the account performs automated actions
*
* @since 0.16.0
*/
bool bot() const;
/*!
* @brief Returns time of creation.
*
* @brief Returns time of creation
*
* @since before 0.11.0
*/
const Easy::time_type created_at() const;
const system_clock::time_point created_at() const;
/*!
* @brief Returns display name.
*
* @brief Returns display name
*
* @since before 0.11.0
*/
const string display_name() const;
/*!
* @brief Returns emojis.
*
* @since 0.106.0
* @brief Sets display name
*
* @since 0.18.5
*/
const std::vector<Easy::Emoji> emojis();
Account display_name(const string &display_name);
/*!
* @brief Returns metadata fields.
*
* @brief Returns metadata fields
*
* @since 0.16.1
*/
const vector<Easy::account_field_type> fields() const;
const std::vector<fields_pair> fields() const;
/*!
* @brief Returns number of followers.
*
* @brief Sets metadata fields
*
* @since 0.18.5
*/
Account fields(std::vector<fields_pair> &fields);
/*!
* @brief Returns number of followers
*
* @since before 0.11.0
*/
uint64_t followers_count() const;
uint_fast64_t followers_count() const;
/*!
* @brief Returns number of people this account follows.
*
* @brief Returns number of people this account follows
*
* @since before 0.11.0
*/
uint64_t following_count() const;
uint_fast64_t following_count() const;
/*!
* @brief Returns URL of header image.
*
* @brief Returns URL of header image
*
* @since before 0.11.0
*/
const string header() const;
/*!
* @brief Returns URL of static header image.
*
* @brief Sets header image
*
* Filename or base64-encoded.
*
* @since 0.18.5
*/
Account header(const string &header);
/*!
* @brief Returns URL of static header image
*
* @since before 0.11.0
*/
const string header_static() const;
/*!
* @brief Returns account-ID.
*
* @brief Returns account-ID
*
* @since before 0.11.0
*/
const string id() const;
/*!
* @brief Returns true if the account is locked.
*
* @brief Returns true if the account is locked
*
* @since before 0.11.0
*/
bool locked() const;
/*!
* @brief Returns true if the account has been moved. (Deprecated)
*
* @brief Sets locked state
*
* @since 0.18.5
*/
Account locked(const bool &locked);
/*!
* @brief Returns true if the account has been moved
*
* @since before 0.11.0
*/
bool has_moved() const;
/*!
* @brief If the owner decided to switch accounts, new account is in
* this attribute.
*
* this attribute
*
* @since before 0.11.0
*/
const Account moved() const;
/*!
* @brief Returns account description, or biography.
*
* @brief Returns note
*
* @since before 0.11.0
*/
const string note() const;
/*!
* @brief Returns default privacy of new toots.
*
* @brief Sets note
*
* @since 0.18.5
*/
Account note(const string &note);
/*!
* @brief Returns plaintext version of note
*
* @since before 0.11.0
*/
[[deprecated("Will vanish in 1.0.0. Use source() instead.")]]
const string note_plain() const;
/*!
* @brief Returns default privacy of new toots
*
* @since before 0.11.0
*/
visibility_type privacy() const;
/*!
* @brief Returns if media is marked as sensitive by default.
*
* @brief Returns if media is marked as sensitive by default
*
* @since before 0.11.0
*/
bool sensitive() const;
/*!
* @brief Class to hold source attribute.
*
* @brief Class to hold source attribute
*
* @since 0.18.5
*/
class Source : public Entity
class Source : public Easy::Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Returns metadata fields.
* @brief Constructs an Account::Source object from a JSON string.
*
* @param json JSON string
*
* @since 0.18.5
*/
const vector<Easy::account_field_type> fields() const;
explicit Source(const string &json);
/*!
* @brief Returns the language as ISO 6391 string.
*
* @since 0.106.0
* @brief Constructs an empty Account::Source object.
*
* @since 0.18.5
*/
const string language() const;
Source();
virtual bool valid() const;
/*!
* @brief Returns account description in plain text.
*
* @brief Returns metadata fields
*
* @since 0.18.5
*/
const std::vector<fields_pair> fields() const;
/*!
* @brief Sets metadata fields
*
* @since 0.18.5
*/
Source fields(std::vector<fields_pair> &fields);
/*!
* @brief Returns note in plain text
*
* @since 0.18.5
*/
const string note() const;
/*!
* @brief Returns default privacy of new toots.
*
* @brief Sets note
*
* @since 0.18.5
*/
Source note(const string &note);
/*!
* @brief Returns default privacy of new toots
*
* @since 0.18.5
*/
visibility_type privacy() const;
/*!
* @brief Returns if media is marked as sensitive by default.
*
* @brief Sets default privacy of new toots
*
* @since 0.18.5
*/
Source privacy(const visibility_type &privacy);
/*!
* @brief Returns if media is marked as sensitive by default
*
* @since 0.18.5
*/
bool sensitive() const;
/*!
* @brief Sets if media is marked as sensitive by default
*
* @since 0.18.5
*/
Source sensitive(const bool &sensitive);
};
/*!
* @brief Get source.
*
* @since before 0.100.0
*/
const Source source() const;
Account source(const Source &source);
/*!
* @brief Returns number of statuses
*
*
* @since before 0.11.0
*/
uint64_t statuses_count() const;
uint_fast64_t statuses_count() const;
/*!
* @brief Returns URL of the profile
*
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns username (without \@hostname)
*
*
* @since before 0.11.0
*/
const string username() const;
};
}
}
#endif // MASTODON_CPP_EASY_ACCOUNT_HPP

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,6 +19,14 @@
using namespace Mastodon;
using Application = Easy::Application;
Application::Application(const string &json)
: Entity(json)
{}
Application::Application()
: Entity()
{}
bool Application::valid() const
{
return Entity::check_valid({"name"});

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,42 +19,59 @@
#include <string>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold applications.
*
*
* @since before 0.11.0
*/
class Application : public Entity
class Easy::Application : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs an Application object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Application(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Application object.
*
* @since before 0.11.0
*/
Application();
virtual bool valid() const;
/*!
* @brief Returns the name of the application
*
*
* @since before 0.11.0
*/
const string name() const;
/*!
* @brief Returns the website of the application
*
*
* @since before 0.11.0
*/
const string website() const;
};
}
}
#endif // MASTODON_CPP_EASY_APPLICATION_HPP

View File

@ -1,35 +1,60 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include "attachment.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Attachment = Easy::Attachment;
Attachment::Attachment(const string &json)
: Entity(json)
{}
Attachment::Attachment()
: Entity()
{}
bool Attachment::valid() const
{
return Entity::check_valid(
{
"id",
"type",
"url",
"preview_url"
});
const std::vector<string> attributes =
{{
"id",
"type",
"url",
"preview_url"
}};
return Entity::check_valid(attributes);
}
double Attachment::aspect() const
{
return get_double("meta.original.aspect");
}
double Attachment::aspect_small() const
{
return get_double("meta.small.aspect");
}
uint_fast64_t Attachment::bitrate() const
{
return get_uint64("meta.original.bitrate");
}
const string Attachment::description() const
@ -43,6 +68,13 @@ Attachment Attachment::description(const string &description)
return *this;
}
const std::chrono::duration<double> Attachment::duration() const
{
const double sec = get_double("meta.original.duration");
return std::chrono::duration<double>(sec);
}
const string Attachment::file() const
{
return get_string("file");
@ -78,16 +110,40 @@ Attachment Attachment::focus(const std::array<double, 2> &focus)
return *this;
}
double Attachment::framerate() const
{
string strframes = get_string("meta.original.frame_rate");
if (!strframes.empty())
{
std::size_t pos = strframes.find('/');
if (pos != std::string::npos)
{
std::uint_fast16_t frames = std::stoul(strframes.substr(0, pos));
std::uint_fast16_t divider = std::stoul(strframes.substr(pos + 1));
return frames / divider;
}
}
return 0.0;
}
uint_fast64_t Attachment::height() const
{
return get_uint64("meta.original.height");
}
uint_fast64_t Attachment::height_small() const
{
return get_uint64("meta.small.height");
}
const string Attachment::id() const
{
return get_string("id");
}
const Attachment::Meta Attachment::meta() const
{
return Meta(get("meta"));
}
const string Attachment::preview_url() const
{
return get_string("preview_url");
@ -98,6 +154,16 @@ const string Attachment::remote_url() const
return get_string("remote_url");
}
const string Attachment::size() const
{
return get_string("meta.original.size");
}
const string Attachment::size_small() const
{
return get_string("meta.small.size");
}
const string Attachment::text_url() const
{
return get_string("text_url");
@ -124,78 +190,12 @@ const string Attachment::url() const
return get_string("url");
}
bool Attachment::Meta::valid() const
uint_fast64_t Attachment::width() const
{
return true;
return get_uint64("meta.original.width");
}
double Attachment::Meta::aspect() const
uint_fast64_t Attachment::width_small() const
{
return get_double("original.aspect");
}
double Attachment::Meta::aspect_small() const
{
return get_double("small.aspect");
}
uint64_t Attachment::Meta::bitrate() const
{
return get_uint64("original.bitrate");
}
const std::chrono::duration<double> Attachment::Meta::duration() const
{
const double sec = get_double("original.duration");
return std::chrono::duration<double>(sec);
}
double Attachment::Meta::frame_rate() const
{
string strframes = get_string("original.frame_rate");
if (!strframes.empty())
{
std::size_t pos = strframes.find('/');
if (pos != std::string::npos)
{
std::uint16_t frames = std::stoul(strframes.substr(0, pos));
std::uint16_t divider = std::stoul(strframes.substr(pos + 1));
return frames / divider;
}
}
return 0.0;
}
uint64_t Attachment::Meta::height() const
{
return get_uint64("original.height");
}
uint64_t Attachment::Meta::height_small() const
{
return get_uint64("small.height");
}
const string Attachment::Meta::size() const
{
return get_string("original.size");
}
const string Attachment::Meta::size_small() const
{
return get_string("small.size");
}
uint64_t Attachment::Meta::width() const
{
return get_uint64("original.width");
}
uint64_t Attachment::Meta::width_small() const
{
return get_uint64("small.width");
return get_uint64("meta.small.width");
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -22,142 +22,98 @@
#include <chrono>
#include <array>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold attachments
*
*
* @since before 0.11.0
*/
class Attachment : public Entity
class Easy::Attachment : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs an Attachment object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Attachment(const string &json);
/*!
* @brief Metadata for attachments.
*
* @since 0.106.0
* @brief Constructs an empty Attachment object.
*
* @since before 0.11.0
*/
class Meta : public Entity
{
public:
using Entity::Entity;
Attachment();
virtual bool valid() const override;
virtual bool valid() const;
/*!
* @brief Aspect of original image.
*
* @since 0.106.0
*/
double aspect() const;
/*!
* @brief Aspect of original image
*
* @since before 0.11.0
*/
double aspect() const;
/*!
* @brief Aspect of preview image.
*
* @since 0.106.0
*/
double aspect_small() const;
/*!
* @brief Aspect of preview image
*
* @since before 0.11.0
*/
double aspect_small() const;
/*!
* @brief Returns the bitrate of a video.
*
* @since 0.106.0
*/
uint64_t bitrate() const;
/*!
* @brief Returns the duration of a video in seconds.
*
* @since 0.106.0
*/
const std::chrono::duration<double> duration() const;
/*!
* @brief Returns the framerate of a video in frames per second.
*
* @since 0.106.0
*/
double frame_rate() const;
/*!
* @brief Returns the height of the original image.
*
* @since 0.106.0
*/
uint64_t height() const;
/*!
* @brief Returns the height of the preview image.
*
* @since 0.106.0
*/
uint64_t height_small() const;
/*!
* @brief Returns the size of the original image.
*
* @since 0.106.0
*/
const string size() const;
/*!
* @brief Returns the size of the preview image.
*
* @since 0.106.0
*/
const string size_small() const;
/*!
* @brief Returns the width of the original image.
*
* @since 0.106.0
*/
uint64_t width() const;
/*!
* @brief Returns the width of the preview image
*
* @since 0.106.0
*/
uint64_t width_small() const;
};
virtual bool valid() const override;
/*!
* @brief Returns the bitrate of a video
*
* @since before 0.11.0
*/
uint_fast64_t bitrate() const;
/*!
* @brief Returns the image description
*
*
* @since before 0.11.0
*/
const string description() const;
/*!
* @brief Sets the image description
*
*
* @since 0.17.0
*/
Attachment description(const string &description);
/*!
* @brief Returns the duration of a video in seconds
*
* @since before 0.11.0
*/
const std::chrono::duration<double> duration() const;
/*!
* @brief Gets file to upload
*
*
* @since 0.17.0
*/
const string file() const;
/*!
* @brief Sets file to upload
*
*
* @since 0.17.0
*
* @param file Filename
@ -166,72 +122,115 @@ namespace Easy
/*!
* @brief Returns the focus point (x, y)
*
*
* Values are between -1.0 and 1.0.
*
*
* @since before 0.11.0
*/
const std::array<double, 2> focus() const;
/*!
* @brief Sets the focus point (x, y)
*
*
* Values are between -1.0 and 1.0.
*
*
* @since 0.17.0
*/
Attachment focus(const std::array<double, 2> &focus);
/*!
* @brief Returns the framerate of a video in frames per second
*
* @since before 0.11.0
*/
double framerate() const;
/*!
* @brief Returns the height of the original image
*
* @since before 0.11.0
*/
uint_fast64_t height() const;
/*!
* @brief Returns the height of the preview image
*
* @since before 0.11.0
*/
uint_fast64_t height_small() const;
/*!
* @brief Returns the ID of the attachment
*
*
* @since before 0.11.0
*/
const string id() const;
/*!
* @brief Returns metadata about the attachment.
*
* @since 0.106.0
*/
const Meta meta() const;
/*!
* @brief Returns the URL of the preview image
*
*
* @since before 0.11.0
*/
const string preview_url() const;
/*!
* @brief Returns the remote URL of the original image
*
*
* @since before 0.11.0
*/
const string remote_url() const;
/*!
* @brief Returns the size of the original image
*
* @since before 0.11.0
*/
const string size() const;
/*!
* @brief Returns the size of the preview image
*
* @since before 0.11.0
*/
const string size_small() const;
/*!
* @brief Returns shorter URL for the image
*
*
* @since before 0.11.0
*/
const string text_url() const;
/*!
* @brief Returns attachment type
*
*
* @since before 0.11.0
*/
attachment_type type() const;
/*!
* @brief Returns URL of the locally hosted version of the image
*
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns the width of the original image
*
* @since before 0.11.0
*/
uint_fast64_t width() const;
/*!
* @brief Returns the width of the preview image
*
* @since before 0.11.0
*/
uint_fast64_t width_small() const;
};
}
}
#endif // MASTODON_CPP_EASY_ATTACHMENT_HPP

View File

@ -1,34 +1,44 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "card.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Card = Easy::Card;
Card::Card(const string &json)
: Entity(json)
{}
Card::Card()
: Entity()
{}
bool Card::valid() const
{
return Entity::check_valid(
{
"url",
"title",
"description",
"type"
});
const std::vector<string> attributes =
{{
"url",
"title",
"description",
"type"
}};
return Entity::check_valid(attributes);
}
const string Card::author_name() const
@ -46,12 +56,7 @@ const string Card::description() const
return get_string("description");
}
const string Card::embed_url() const
{
return get_string("embed_url");
}
uint64_t Card::height() const
uint_fast64_t Card::height() const
{
return get_uint64("height");
}
@ -84,16 +89,17 @@ const string Card::title() const
Easy::card_type Card::type() const
{
const string strtype = get_string("type");
if (strtype == "link")
if (strtype.compare("link") == 0)
return card_type::Link;
else if (strtype == "photo")
else if (strtype.compare("photo") == 0)
return card_type::Photo;
else if (strtype == "video")
else if (strtype.compare("video") == 0)
return card_type::Video;
else if (strtype == "rich")
else if (strtype.compare("rich") == 0)
return card_type::Rich;
else
return card_type::Undefined;
ttdebug << "Could not get data: type\n";
return card_type::Undefined;
}
const string Card::url() const
@ -101,7 +107,7 @@ const string Card::url() const
return get_string("url");
}
uint64_t Card::width() const
uint_fast64_t Card::width() const
{
return get_uint64("width");
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,122 +20,130 @@
#include <string>
#include <cstdint>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold cards
*
*
* @since before 0.11.0
*/
class Card : public Entity
class Easy::Card : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a Card object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Card(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Card object.
*
* @since before 0.11.0
*/
Card();
virtual bool valid() const;
/*!
* @brief Returns the name of the author
*
*
* @since before 0.11.0
*/
const string author_name() const;
/*!
* @brief Returns the URL of the author
*
*
* @since before 0.11.0
*/
const string author_url() const;
/*!
* @brief Returns the description
*
*
* @since before 0.11.0
*/
const string description() const;
/*!
* @brief Returns the embed URL.
*
* This is an undocumented attribute and may vanish at any time.
*
* @since 0.106.0
*/
const string embed_url() const;
/*!
* @brief Returns the height of the card
*
*
* @since before 0.11.0
*/
uint64_t height() const;
uint_fast64_t height() const;
/*!
* @brief Returns the HTML
*
*
* @since before 0.11.0
*/
const string html() const;
/*!
* @brief Returns the URL of the image associated with the card
*
*
* @since before 0.11.0
*/
const string image() const;
/*!
* @brief Returns the name of the provider
*
*
* @since before 0.11.0
*/
const string provider_name() const;
/*!
* @brief Returns the URL of the provider
*
*
* @since before 0.11.0
*/
const string provider_url() const;
/*!
* @brief Returns the title
*
*
* @since before 0.11.0
*/
const string title() const;
/*!
* @brief Returns the type of the card
*
*
* @since before 0.11.0
*/
Easy::card_type type() const;
/*!
* @brief Returns the URL associated with the card
*
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns the width of the card
*
*
* @since before 0.11.0
*/
uint64_t width() const;
uint_fast64_t width() const;
};
}
}
#endif // MASTODON_CPP_EASY_CARD_HPP

View File

@ -1,33 +1,42 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include "context.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Context = Easy::Context;
Context::Context(const string &json)
: Entity(json)
{}
Context::Context()
: Entity()
{}
bool Context::valid() const
{
return Entity::check_valid(
{
"ancestors",
"descendants"
});
const std::vector<string> attributes =
{{
"ancestors",
"descendants"
}};
return Entity::check_valid(attributes);
}
const std::vector<Easy::Status> Context::ancestors() const
@ -36,12 +45,14 @@ const std::vector<Easy::Status> Context::ancestors() const
if (node.isArray())
{
std::vector<Easy::Status> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Status(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Status(value.toStyledString()));
}
return vec;
}
ttdebug << "Could not get data: ancestors\n";
return {};
}
@ -51,11 +62,13 @@ const std::vector<Easy::Status> Context::descendants() const
if (node.isArray())
{
std::vector<Easy::Status> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Status(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Status(value.toStyledString()));
}
return vec;
}
ttdebug << "Could not get data: descendants\n";
return {};
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,43 +20,61 @@
#include <string>
#include <vector>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "status.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#include "easy/entities/status.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/entities/status.hpp>
#endif
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold contexts
*
*
* @since before 0.11.0
*/
class Context : public Entity
class Easy::Context : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a Context object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Context(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Context object.
*
* @since before 0.11.0
*/
Context();
virtual bool valid() const;
/*!
* @brief Returns the ancestors of the Status as vector of Statuses
*
*
* @since before 0.11.0
*/
const std::vector<Status> ancestors() const;
/*!
* @brief Returns the descendants of the Status as vector of Statuses
*
*
* @since before 0.11.0
*/
const std::vector<Status> descendants() const;
};
}
}
#endif // MASTODON_CPP_EASY_CONTEXT_HPP

View File

@ -1,68 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include "conversation.hpp"
#include "debug.hpp"
using namespace Mastodon;
using Conversation = Easy::Conversation;
bool Conversation::valid() const
{
return Entity::check_valid(
{
"id",
"accounts",
"unread"
});
}
const string Conversation::id() const
{
return get_string("id");
}
const std::vector<Easy::Account> Conversation::accounts() const
{
const Json::Value node = get("accounts");
if (node.isArray())
{
std::vector<Easy::Account> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Account(value); });
return vec;
}
return {};
}
const Easy::Status Conversation::last_status() const
{
const Json::Value &node = get("last_status");
if (!node.isNull())
{
return Easy::Status(node);
}
return {};
}
bool Conversation::unread() const
{
return get_bool("unread");
}

View File

@ -1,77 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_CONVERSATION_HPP
#define MASTODON_CPP_EASY_CONVERSATION_HPP
#include <string>
#include <vector>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "account.hpp"
#include "status.hpp"
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold conversations.
*
* @since 0.110.0
*/
class Conversation : public Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Returns the id of the conversation.
*
* @since 0.110.0
*/
const string id() const;
/*!
* @brief Returns the participating accounts.
*
* @since 0.110.0
*/
const std::vector<Account> accounts() const;
/*!
* @brief Returns the last status.
*
* @since 0.110.0
*/
const Status last_status() const;
/*!
* @brief Returns true if unread.
*
* @since 0.110.0
*/
bool unread() const;
};
}
}
#endif // MASTODON_CPP_EASY_CONVERSATION_HPP

View File

@ -1,34 +1,43 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "emoji.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Emoji = Easy::Emoji;
Emoji::Emoji(const string &json)
: Entity(json)
{}
Emoji::Emoji()
: Entity()
{}
bool Emoji::valid() const
{
return Entity::check_valid(
{
"shortcode",
"static_url",
"url",
"visible_in_picker"
});
const std::vector<string> attributes =
{{
"shortcode",
"static_url",
"url"
}};
return Entity::check_valid(attributes);
}
const string Emoji::shortcode() const
@ -45,8 +54,3 @@ const string Emoji::url() const
{
return get_string("url");
}
bool Emoji::visible_in_picker() const
{
return get_bool("visible_in_picker");
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,56 +19,66 @@
#include <string>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold emojis.
*
* @since before 0.11.0
* @brief Class to hold emojis
*
* @since before 0.11.0
*/
class Emoji : public Entity
class Easy::Emoji : public Easy::Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Constructs an Emoji object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Emoji(const string &json);
/*!
* @brief Returns the shortcode of the emoji.
*
* @since before 0.11.0
* @brief Constructs an empty Emoji object.
*
* @since before 0.11.0
*/
Emoji();
virtual bool valid() const;
/*!
* @brief Returns the shortcode of the emoji
*
* @since before 0.11.0
*/
const string shortcode() const;
/*!
* @brief Returns the URL to the emoji static image.
*
* @since before 0.11.0
* @brief Returns the URL to the emoji static image
*
* @since before 0.11.0
*/
const string static_url() const;
/*!
* @brief Returns the URL to the emoji image.
*
* @since before 0.11.0
* @brief Returns the URL to the emoji image
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns if the emoji is visible in the picker.
*
* @since 0.106.0
*/
bool visible_in_picker() const;
};
}
}
#endif // MASTODON_CPP_EASY_EMOJI_HPP

View File

@ -1,87 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <vector>
#include <algorithm>
#include "filter.hpp"
using std::vector;
using namespace Mastodon;
using Filter = Easy::Filter;
bool Filter::valid() const
{
return Entity::check_valid(
{
"id",
"phrase",
"context",
"irreversible",
"whole_word"
});
}
const string Filter::id() const
{
return get_string("id");
}
const string Filter::phrase() const
{
return get_string("phrase");
}
const vector<Easy::context_type> Filter::context() const
{
const Json::Value node = get("context");
if (node.isArray())
{
vector<Easy::context_type> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{
const string strtype = value.asString();
if (strtype == "home")
return Easy::context_type::Home;
else if (strtype == "notifications")
return Easy::context_type::Notifications;
else if (strtype == "public")
return Easy::context_type::Public;
else if (strtype == "thread")
return Easy::context_type::Thread;
else
return context_type::Undefined;
});
return vec;
}
return {};
}
const Easy::time_type Filter::expires_at() const
{
return get_time("expires_at");
}
bool Filter::irreversible() const
{
return get_bool("irreversible");
}
bool Filter::whole_word() const
{
return get_bool("whole_word");
}

View File

@ -1,92 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_FILTER_HPP
#define MASTODON_CPP_EASY_FILTER_HPP
#include <string>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold filters.
*
* @since 0.104.0
*/
class Filter : public Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Returns the id of the filter
*
* @since 0.104.0
*/
const string id() const;
/*!
* @brief Returns the phrase to filter.
*
* @since 0.104.0
*/
const string phrase() const;
/*!
* @brief Returns the contexts in which to filter.
*
* @since 0.104.0
*/
const vector<Easy::context_type> context() const;
/*!
* @brief Returns the expiration time of the filter.
*
* @since 0.104.0
*/
const Easy::time_type expires_at() const;
/*!
* @brief Returns if the phrase should disappear irreversibly.
*
* If set to true, the filtered posts will be hidden even if filter is
* later removed.
*
* @since 0.104.0
*/
bool irreversible() const;
/*!
* @brief Returns if the filter should only be applied if it matches
* the whole word.
*
* @since 0.104.0
*/
bool whole_word() const;
};
}
}
#endif // MASTODON_CPP_EASY_FILTER_HPP

View File

@ -1,40 +1,50 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <jsoncpp/json/json.h>
#include "instance.hpp"
#include "account.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Instance = Easy::Instance;
Instance::Instance(const string &json)
: Entity(json)
{}
Instance::Instance()
: Entity()
{}
bool Instance::valid() const
{
return Entity::check_valid(
{
"uri",
"title",
"description",
"email",
"version",
"urls",
"stats",
"languages"
});
const std::vector<string> attributes =
{{
"uri",
"title",
"description",
"email",
"version",
"urls",
"languages",
"contact_account"
}};
return Entity::check_valid(attributes);
}
const Easy::Account Instance::contact_account() const
@ -42,9 +52,10 @@ const Easy::Account Instance::contact_account() const
const Json::Value node = get("contact_account");
if (node.isObject())
{
return Easy::Account(node);
return Easy::Account(node.toStyledString());
}
ttdebug << "Could not get data: contact_account\n";
return Easy::Account();
}
@ -63,15 +74,6 @@ const std::vector<string> Instance::languages() const
return get_vector("languages");
}
const Easy::stats_type Instance::stats() const
{
Easy::stats_type s;
s.user_count = get_uint64("stats.user_count");
s.status_count = get_uint64("stats.status_count");
s.domain_count = get_uint64("stats.domain_count");
return s;
}
const string Instance::title() const
{
return get_string("title");
@ -82,24 +84,19 @@ const string Instance::uri() const
return get_string("uri");
}
const Easy::urls_type Instance::urls() const
{
return { get_string("urls.streaming_api") };
}
const string Instance::version() const
{
return get_string("version");
}
const string Instance::thumbnail() const
const string Instance::streaming_api() const
{
return get_string("thumbnail");
return get_string("urls.streaming_api");
}
uint64_t Instance::max_toot_chars() const
uint_fast64_t Instance::max_toot_chars() const
{
const uint64_t max_chars = get_uint64("max_toot_chars");
const uint_fast64_t max_chars = get_uint64("max_toot_chars");
if (was_set())
{
return max_chars;

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,102 +20,107 @@
#include <string>
#include <vector>
using std::uint64_t;
using std::uint_fast64_t;
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "account.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#include "easy/entities/account.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/entities/account.hpp>
#endif
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold instances.
*
* @since before 0.11.0
* @brief Class to hold instances
*
* @since before 0.11.0
*/
class Instance : public Entity
class Easy::Instance : public Easy::Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Constructs an Instance object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Instance(const string &json);
/*!
* @brief Returns the Account of the admin or another contact person.
*
* @since before 0.11.0
* @brief Constructs an empty Instance object.
*
* @since before 0.11.0
*/
Instance();
virtual bool valid() const;
/*!
* @brief Returns the Account of the admin or another contact person
*
* @since before 0.11.0
*/
const Account contact_account() const;
/*!
* @brief Returns the description of the instance.
*
* @since before 0.11.0
* @brief Returns the description of the instance
*
* @since before 0.11.0
*/
const string description() const;
/*!
* @brief Returns the email address which can be used to contact the
* instance administrator.
*
* @since before 0.11.0
* instance administrator
*
* @since before 0.11.0
*/
const string email() const;
/*!
* @brief Returns a vector of ISO 6391 language codes the instance has
* chosen to advertise.
*
* @since before 0.11.0
* chosen to advertise
*
* @since before 0.11.0
*/
const vector<string> languages() const;
const std::vector<string> languages() const;
/*!
* @brief Returns the thumbnail of the instance.
*
* @since 0.106.0
*/
const string thumbnail() const;
/*!
* @brief Returns the title of the instance.
*
* @since before 0.11.0
* @brief Returns the title of the instance
*
* @since before 0.11.0
*/
const string title() const;
/*!
* @brief Returns the URI of the instance.
*
* @since before 0.11.0
* @brief Returns the URI of the instance
*
* @since before 0.11.0
*/
const string uri() const;
/*!
* @brief Returns the URL for the streaming API and possibly others.
*
* @since 0.106.0
*/
const Easy::urls_type urls() const;
/*!
* @brief Returns statistics about the instance.
*
* @since 0.106.0
*/
const Easy::stats_type stats() const;
/*!
* @brief Returns the version used by the instance.
*
* @since before 0.11.0
* @brief Returns the version used by the instance
*
* @since before 0.11.0
*/
const string version() const;
/*!
* @brief Returns the URL for the streaming API
*
* @since before 0.11.0
*/
const string streaming_api() const;
/*!
* @brief Returns the maximum chars a post can have
*
@ -124,9 +129,8 @@ namespace Easy
*
* @since 0.20.0
*/
uint64_t max_toot_chars() const;
uint_fast64_t max_toot_chars() const;
};
}
}
#endif // MASTODON_CPP_EASY_INSTANCE_HPP

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,15 +19,25 @@
using namespace Mastodon;
using List = Easy::List;
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
List::List(const string &json)
: Entity(json)
{}
List::List()
: Entity()
{}
bool List::valid() const
{
return Entity::check_valid(
{
"id",
"title"
});
const std::vector<string> attributes =
{{
"id",
"title"
}};
return Entity::check_valid(attributes);
}
const string List::id() const

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -21,43 +21,60 @@
#include <vector>
#include <cstdint>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold lists
*
*
* @since before 0.11.0
*/
class List : public Entity
class Easy::List : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a List object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit List(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty List object.
*
* @since before 0.11.0
*/
List();
virtual bool valid() const;
/*!
* @brief Returns list-ID
*
*
* @since before 0.11.0
*/
const string id() const;
/*!
* @brief Returns title
*
*
* @since before 0.11.0
*/
const string title() const;
};
}
}
#endif // MASTODON_CPP_EASY_LIST_HPP

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,15 +19,25 @@
using namespace Mastodon;
using Mention = Easy::Mention;
Mention::Mention(const string &json)
: Entity(json)
{}
Mention::Mention()
: Entity()
{}
bool Mention::valid() const
{
return Entity::check_valid(
{
"url",
"username",
"acct",
"id"
});
const std::vector<string> attributes =
{{
"url",
"username",
"acct",
"id"
}};
return Entity::check_valid(attributes);
}
const string Mention::acct() const

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,58 +20,75 @@
#include <string>
#include <cstdint>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
using std::chrono::system_clock;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold mentions
*
*
* before 0.11.0
*/
class Mention : public Entity
class Easy::Mention : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a Mention object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Mention(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Mention object.
*
* @since before 0.11.0
*/
Mention();
virtual bool valid() const;
/*!
* @brief Returns acct
*
*
* @since before 0.11.0
*/
const string acct() const;
/*!
* @brief Returns account ID
*
*
* @since before 0.11.0
*/
const string id() const;
/*!
* @brief Returns the URL of user's profile
*
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns the username of the account
*
*
* @since before 0.11.0
*/
const string username() const;
};
}
}
#endif // MASTODON_CPP_EASY_MENTION_HPP

View File

@ -1,34 +1,44 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "notification.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Notification = Easy::Notification;
Notification::Notification(const string &json)
: Entity(json)
{}
Notification::Notification()
: Entity()
{}
bool Notification::valid() const
{
return Entity::check_valid(
{
"id",
"type",
"created_at",
"account"
});
const std::vector<string> attributes =
{{
"id",
"type",
"created_at",
"account"
}};
return Entity::check_valid(attributes);
}
const Easy::Account Notification::account() const
@ -43,9 +53,9 @@ const Easy::Account Notification::account() const
return Easy::Account();
}
const Easy::time_type Notification::created_at() const
const system_clock::time_point Notification::created_at() const
{
return get_time("created_at");
return get_time_point("created_at");
}
const string Notification::id() const

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,48 +19,70 @@
#include <string>
#include <cstdint>
#include <chrono>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "account.hpp"
#include "status.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#include "easy/entities/account.hpp"
#include "easy/entities/status.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/entities/account.hpp>
#include <mastodon-cpp/easy/entities/status.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
using std::chrono::system_clock;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold notifications
*
*
* @since before 0.11.0
*/
class Notification : public Entity
class Easy::Notification : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a Notification object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Notification(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Notification object.
*
* @since before 0.11.0
*/
Notification();
virtual bool valid() const;
/*!
* @brief Returns the Account sending the notification to the user
*
*
* @since before 0.11.0
*/
const Account account() const;
/*!
* @brief Returns time of creation
*
*
* @since before 0.11.0
*/
const Easy::time_type created_at() const;
const system_clock::time_point created_at() const;
/*!
* @brief Returns notification ID
*
*
* @since before 0.11.0
*/
const string id() const;
@ -68,19 +90,18 @@ namespace Easy
/*!
* @brief Returns the Status associated with the notification, if
* applicable
*
*
* @since before 0.11.0
*/
const Status status() const;
/*!
* @brief Returns notification type
*
*
* @since before 0.11.0
*/
Easy::notification_type type() const;
};
}
}
#endif // MASTODON_CPP_EASY_NOTIFICATION_HPP

View File

@ -1,86 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "poll.hpp"
#include "debug.hpp"
using namespace Mastodon;
using Poll = Easy::Poll;
bool Poll::valid() const
{
return Entity::check_valid(
{
"id",
"expired",
"expired",
"multiple",
"votes_count",
"options"
});
}
const string Poll::id() const
{
return get_string("id");
}
const Easy::time_type Poll::expires_at() const
{
return get_time("expires_at");
}
bool Poll::expired() const
{
return get_bool("expired");
}
bool Poll::multiple() const
{
return get_bool("multiple");
}
uint64_t Poll::votes_count() const
{
return get_uint64("votes_count");
}
const vector<Easy::poll_options_type> Poll::options() const
{
const Json::Value &node = get("options");
if (node.isArray())
{
vector<Easy::poll_options_type> vec_options;
std::transform(node.begin(), node.end(), std::back_inserter(vec_options),
[](const Json::Value &value)
{
return Easy::poll_options_type(
{
value["title"].asString(),
value["votes_count"].asUInt64()
});
});
return vec_options;
}
return {};
}
bool Poll::voted() const
{
return get_bool("voted");
}

View File

@ -1,98 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_POLL_HPP
#define MASTODON_CPP_EASY_POLL_HPP
#include <string>
#include <cstdint>
#include <vector>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
using std::string;
using std::uint64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold polls.
*
* @since 0.110.0
*/
class Poll : public Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Returns poll ID.
*
* @since 0.110.0
*/
const string id() const;
/*!
* @brief Returns time when the poll expires.
*
* @since 0.110.0
*/
const Easy::time_type expires_at() const;
/*!
* @brief Returns true if poll has expired.
*
* @since 0.110.0
*/
bool expired() const;
/*!
* @brief Returns true or false.
*
* @since 0.110.0
*/
bool multiple() const;
/*!
* @brief Returns the number of votes.
*
* @since 0.110.0
*/
uint64_t votes_count() const;
/*!
* @brief Returns poll options and their votes count.
*
* @since 0.110.0
*/
const vector<poll_options_type> options() const;
/*!
* @brief Returns whether you voted or not.
*
* @since 0.110.0
*/
bool voted() const;
};
}
}
#endif // MASTODON_CPP_EASY_POLL_HPP

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,20 +19,29 @@
using namespace Mastodon;
using PushSubscription = Easy::PushSubscription;
PushSubscription::PushSubscription(const string &json)
: Entity(json)
{}
const string PushSubscription::id() const
{
return get_string("id");
}
PushSubscription::PushSubscription()
: Entity()
{}
bool PushSubscription::valid() const
{
return Entity::check_valid(
{
"id",
"endpoint",
"server_key",
"alerts"
});
const std::vector<string> attributes =
{{
"id",
"endpoint",
"server_key"
}};
return Entity::check_valid(attributes);
}
const string PushSubscription::endpoint() const
@ -45,9 +54,9 @@ const string PushSubscription::server_key() const
return get_string("server_key");
}
const vector<Easy::alert_type> PushSubscription::alerts() const
const Easy::alertmap PushSubscription::alerts() const
{
vector<Easy::alert_type> alerts;
alertmap alerts;
const Json::Value node = get("alerts");
for (auto it = node.begin(); it != node.end(); ++it)
{
@ -74,7 +83,7 @@ const vector<Easy::alert_type> PushSubscription::alerts() const
type = notification_type::Undefined;
}
alerts.push_back({ type, s_to_b(it->asString()) });
alerts.insert({{ type, s_to_b(it->asString()) }});
}
return alerts;
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -18,59 +18,77 @@
#define MASTODON_CPP_EASY_PUSHSUBSCRIPTION_HPP
#include <string>
#include <vector>
#include <cstdint>
#include <map>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::vector;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold push subscriptions.
*
* @since 0.14.0
*/
class PushSubscription : public Entity
class Easy::PushSubscription : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs an PushSubscription object from a JSON string.
*
* @param json JSON string
*
* @since 0.14.0
*/
explicit PushSubscription(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty PushSubscription object.
*
* @since 0.14.0
*/
PushSubscription();
virtual bool valid() const;
/*!
* @brief Returns push subscription ID
*
*
* @since 0.14.0
*/
const string id() const;
/*!
* @brief Returns the endpoint URL
*
*
* @since 0.14.0
*/
const string endpoint() const;
/*!
* @brief Returns the server public key for signature verification
*
*
* @since 0.14.0
*/
const string server_key() const;
// TODO: Look up what the data looks like
/*!
* @brief Returns a vector of Easy::alert_type.
*
* @since 0.100.0
* @brief Returns a map of 'notification event type' and
* 'push is requested or not'
*
* @since 0.14.0
*/
const vector<Easy::alert_type> alerts() const;
const Easy::alertmap alerts() const;
protected:
/*!
@ -81,6 +99,5 @@ namespace Easy
bool s_to_b(const string &str) const;
};
}
}
#endif // MASTODON_CPP_EASY_PUSHSUBSCRIPTION_HPP

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,21 +19,29 @@
using namespace Mastodon;
using Relationship = Easy::Relationship;
Relationship::Relationship(const string &json)
: Entity(json)
{}
Relationship::Relationship()
: Entity()
{}
bool Relationship::valid() const
{
return Entity::check_valid(
{
"id",
"following",
"followed_by",
"blocking",
"muting",
"muting_notifications",
"requested",
"domain_blocking",
"showing_reblogs",
"endorsed"
});
const std::vector<string> attributes =
{{
"id",
"following",
"followed_by",
"blocking",
"muting",
"muting_notifications",
"requested",
"domain_blocking"
}};
return Entity::check_valid(attributes);
}
bool Relationship::blocking() const
@ -85,8 +93,3 @@ bool Relationship::showing_notifications() const
{
return get_bool("showing_notifications");
}
bool Relationship::showing_reblogs() const
{
return get_bool("showing_reblogs");
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,106 +20,116 @@
#include <string>
#include <cstdint>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold relationships
*
*
* before 0.11.0
*/
class Relationship : public Entity
class Easy::Relationship : public Easy::Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Constructs a Relationship object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Relationship(const string &json);
/*!
* @brief Returns true if the user is blocking the account.
*
* @brief Constructs an empty Relationship object.
*
* @since before 0.11.0
*/
Relationship();
virtual bool valid() const;
/*!
* @brief Returns true if the user is blocking the account
*
* @since before 0.11.0
*/
bool blocking() const;
/*!
* @brief Returns true if the user is blocking the account's domain.
*
* @brief Returns true if the user is blocking the account's domain
*
* @since before 0.11.0
*/
bool domain_blocking() const;
/*!
* @brief Returns true if the account is endorsed by the user.
*
* @brief Returns true if the account is endorsed by the user
*
* @since 0.19.0
*/
bool endorsed() const;
/*!
* @brief Returns true if the user is being followed by the account.
*
* @brief Returns true if the user is being followed by the account
*
* @since before 0.11.0
*/
bool followed_by() const;
/*!
* @brief Returns true if the user is being following the account.
*
* @brief Returns true if the user is being following the account
*
* @since before 0.11.0
*/
bool following() const;
/*!
* @brief Returns the target account ID.
*
* @brief Returns the target account ID
*
* @since before 0.11.0
*/
const string id() const;
/*!
* @brief Returns true if the user is muting the account.
*
* @brief Returns true if the user is muting the account
*
* @since before 0.11.0
*/
bool muting() const;
/*!
* @brief Returns true if the user is also muting notifications.
*
* @brief Returns true if the user is also muting notifications
*
* @since before 0.11.0
*/
bool muting_notifications() const;
/*!
* @brief Returns true if the user has requested to follow the account.
*
* @brief Returns true if the user has requested to follow the account
*
* @since before 0.11.0
*/
bool requested() const;
/*!
* @brief Returns true if the user is showing notifications.
*
* @brief Returns true if the user is showing notifications
*
* @since 0.19.0
*/
bool showing_notifications() const;
/*!
* @brief Returns true if the user is showing reblogs.
*
* @since 0.106.0
*/
bool showing_reblogs() const;
};
}
}
#endif // MASTODON_CPP_EASY_RELATIONSHIP_HPP

View File

@ -0,0 +1,50 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* 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/>.
*/
#include "report.hpp"
using namespace Mastodon;
using Report = Easy::Report;
Report::Report(const string &json)
: Entity(json)
{}
Report::Report()
: Entity()
{}
bool Report::valid() const
{
const std::vector<string> attributes =
{{
"id",
"action_taken"
}};
return Entity::check_valid(attributes);
}
bool Report::action_taken() const
{
return get_bool("action_taken");
}
const string Report::id() const
{
return get_string("id");
}

View File

@ -0,0 +1,79 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* 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/>.
*/
#ifndef MASTODON_CPP_EASY_REPORT_HPP
#define MASTODON_CPP_EASY_REPORT_HPP
#include <string>
#include <cstdint>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
namespace Mastodon
{
/*!
* @brief Class to hold reports
*
* before 0.11.0
*/
class Easy::Report : public Easy::Entity
{
public:
/*!
* @brief Constructs a Report object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Report(const string &json);
/*!
* @brief Constructs an empty Report object.
*
* @since before 0.11.0
*/
Report();
virtual bool valid() const;
/*!
* @brief Returns true if an action was taken in response to the
* report
*
* @since before 0.11.0
*/
bool action_taken() const;
/*!
* @brief Returns the ID of the report
*
* @since before 0.11.0
*/
const string id() const;
};
}
#endif // MASTODON_CPP_EASY_REPORT_HPP

View File

@ -1,34 +1,43 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <jsoncpp/json/json.h>
#include "results.hpp"
using namespace Mastodon;
using Results = Easy::Results;
Results::Results(const string &json)
: Entity(json)
{}
Results::Results()
: Entity()
{}
bool Results::valid() const
{
return Entity::check_valid(
{
"accounts",
"statuses",
"hashtags"
});
const std::vector<string> attributes =
{{
"accounts",
"statuses",
"hashtags"
}};
return Entity::check_valid(attributes);
}
const std::vector<Easy::Account> Results::accounts() const
@ -37,9 +46,10 @@ const std::vector<Easy::Account> Results::accounts() const
if (node.isArray())
{
std::vector<Easy::Account> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Account(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Account(value.toStyledString()));
}
return vec;
}
@ -52,9 +62,10 @@ const std::vector<Easy::Status> Results::statuses() const
if (node.isArray())
{
std::vector<Easy::Status> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Status(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Status(value.toStyledString()));
}
return vec;
}
@ -72,11 +83,17 @@ const std::vector<Easy::Tag> Results::hashtags_v2() const
if (node.isArray())
{
std::vector<Easy::Tag> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Tag(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Tag(value.toStyledString()));
}
return vec;
}
return {};
}
const std::vector<string> Results::hashtags() const
{
return hashtags_v1();
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,47 +20,68 @@
#include <string>
#include <vector>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "account.hpp"
#include "status.hpp"
#include "tag.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#include "easy/entities/account.hpp"
#include "easy/entities/status.hpp"
#include "easy/entities/tag.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/entities/account.hpp>
#include <mastodon-cpp/easy/entities/status.hpp>
#include <mastodon-cpp/easy/entities/tag.hpp>
#endif
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold results
*
*
* @since before 0.11.0
*/
class Results : public Entity
class Easy::Results : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a Results object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Results(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Results object.
*
* @since before 0.11.0
*/
Results();
virtual bool valid() const;
/*!
* @brief Returns an array of matched Accounts
*
*
* @since before 0.11.0
*/
const std::vector<Account> accounts() const;
/*!
* @brief Returns an array of matched Statuses
*
*
* @since before 0.11.0
*/
const std::vector<Status> statuses() const;
/*!
* @brief Returns an array of matched hashtags as string
*
*
* @since 0.16.0
*/
const std::vector<string> hashtags_v1() const;
@ -71,8 +92,15 @@ namespace Easy
* @since 0.16.0
*/
const std::vector<Tag> hashtags_v2() const;
/*!
* @brief Alias for hashtags_v1
*
* @since before 0.11.0
*/
[[deprecated("Will vanish in 1.0.0, use hashtags_v1() instead")]]
const std::vector<string> hashtags() const;
};
}
}
#endif // MASTODON_CPP_EASY_RESULTS_HPP

View File

@ -1,48 +1,55 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <algorithm>
#include <jsoncpp/json/json.h>
#include "status.hpp"
using namespace Mastodon;
using Status = Easy::Status;
Status::Status(const string &json)
: Entity(json)
{}
Status::Status()
: Entity()
{}
bool Status::valid() const
{
return Entity::check_valid(
{
"id",
"uri",
"account",
"content",
"created_at",
"emojis",
"replies_count",
"reblogs_count",
"favourites_count",
"sensitive",
"spoiler_text",
"visibility",
"media_attachments",
"mentions",
"tags",
"application"
});
const std::vector<string> attributes =
{{
"id",
"uri",
"account",
"content",
"created_at",
"emojis",
"reblogs_count",
"favourites_count",
"sensitive",
"spoiler_text",
"visibility",
"media_attachments",
"mentions",
"tags"
}};
return Entity::check_valid(attributes);
}
const Easy::Account Status::account() const
@ -78,9 +85,9 @@ const Easy::Card Status::card() const
return Easy::Card();
}
const Easy::time_type Status::created_at() const
const system_clock::time_point Status::created_at() const
{
return get_time("created_at");
return get_time_point("created_at");
}
const string Status::content() const
@ -100,9 +107,10 @@ const std::vector<Easy::Emoji> Status::emojis() const
if (node.isArray())
{
std::vector<Easy::Emoji> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Emoji(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Emoji(value.toStyledString()));
}
return vec;
}
@ -114,7 +122,7 @@ bool Status::favourited() const
return get_bool("favourited");
}
uint64_t Status::favourites_count() const
uint_fast64_t Status::favourites_count() const
{
return get_uint64("favourites_count");
}
@ -157,9 +165,10 @@ const std::vector<Easy::Attachment> Status::media_attachments() const
if (node.isArray())
{
std::vector<Easy::Attachment> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Attachment(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Attachment(value.toStyledString()));
}
return vec;
}
@ -169,6 +178,7 @@ const std::vector<Easy::Attachment> Status::media_attachments() const
Status Status::media_attachments
(const std::vector<Attachment> &media_attachments)
{
// FIXME: Needs writable Easy::Attachment()
Json::Value jsonarray(Json::arrayValue);
for (const Attachment &att : media_attachments)
@ -185,9 +195,10 @@ const std::vector<Easy::Mention> Status::mentions() const
if (node.isArray())
{
std::vector<Easy::Mention> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Mention(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Mention(value.toStyledString()));
}
return vec;
}
@ -220,12 +231,12 @@ bool Status::reblogged() const
return get_bool("reblogged");
}
uint64_t Status::reblogs_count() const
uint_fast64_t Status::reblogs_count() const
{
return get_uint64("reblogs_count");
}
uint64_t Status::replies_count() const
uint_fast64_t Status::replies_count() const
{
return get_uint64("replies_count");
}
@ -258,9 +269,10 @@ const std::vector<Easy::Tag> Status::tags() const
if (node.isArray())
{
std::vector<Easy::Tag> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Tag(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Tag(value.toStyledString()));
}
return vec;
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -19,152 +19,179 @@
#include <string>
#include <cstdint>
#include <chrono>
#include <vector>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
#include "account.hpp"
#include "emoji.hpp"
#include "attachment.hpp"
#include "mention.hpp"
#include "tag.hpp"
#include "application.hpp"
#include "card.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#include "easy/entities/account.hpp"
#include "easy/entities/emoji.hpp"
#include "easy/entities/attachment.hpp"
#include "easy/entities/mention.hpp"
#include "easy/entities/tag.hpp"
#include "easy/entities/application.hpp"
#include "easy/entities/card.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/entities/account.hpp>
#include <mastodon-cpp/easy/entities/emoji.hpp>
#include <mastodon-cpp/easy/entities/attachment.hpp>
#include <mastodon-cpp/easy/entities/mention.hpp>
#include <mastodon-cpp/easy/entities/tag.hpp>
#include <mastodon-cpp/easy/entities/application.hpp>
#include <mastodon-cpp/easy/entities/card.hpp>
#endif
using std::string;
using std::uint64_t;
using std::uint_fast64_t;
using std::chrono::system_clock;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold statuses
*
*
* @since before 0.11.0
*/
class Status : public Entity
class Easy::Status : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs a Status object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Status(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Status object.
*
* @since before 0.11.0
*/
Status();
virtual bool valid() const;
/*!
* @brief Returns an array of matched accounts.
*
*
* @since before 0.11.0
*/
const Account account() const;
/*!
* @brief Returns application from which the status was posted.
*
*
* @since before 0.11.0
*/
const Application application() const;
/*!
* @brief Returns card
*
*
* @since 0.19.0
*/
const Card card() const;
/*!
* @brief Returns time of creation
*
*
* @since before 0.11.0
*/
const Easy::time_type created_at() const;
const system_clock::time_point created_at() const;
/*!
* @brief Returns content of status
*
*
* @since before 0.11.0
*/
const string content() const;
/*!
* @brief Sets content of status
*
*
* @since 0.17.0
*/
Status content(const string &content);
/*!
* @brief Returns an array of emojis
*
*
* @since before 0.11.0
*/
const std::vector<Emoji> emojis() const;
/*!
* @brief Returns true if the user has favourited the status
*
*
* @since before 0.11.0
*/
bool favourited() const;
/*!
* @brief Returns the number of favourites
*
*
* @since before 0.11.0
*/
uint64_t favourites_count() const;
uint_fast64_t favourites_count() const;
/*!
* @brief Returns the ID of the status
*
*
* @since before 0.11.0
*/
const string id() const;
/*!
* @brief Returns the ID of the status it replies to
*
*
* @since before 0.11.0
*/
const string in_reply_to_id() const;
/*!
* @brief Sets the ID of the status it replies to
*
*
* @since 0.17.0
*/
Status in_reply_to_id(const string &in_reply_to_id);
/*!
* @brief Returns the ID of the account it replies to
*
*
* @since before 0.11.0
*/
const string in_reply_to_account_id() const;
/*!
* @brief Returns the language of the status
*
*
* @since before 0.11.0
*/
const string language() const;
/*!
* @brief Overrides the language of the status (ISO 639-2)
*
*
* @since 0.17.0
*/
Status language(const string &language);
/*!
* @brief Returns the attachments
*
*
* @since before 0.11.0
*/
const std::vector<Attachment> media_attachments() const;
/*!
* @brief Sets the attachments
*
*
* @since 0.17.0
*/
Status media_attachments
@ -172,117 +199,116 @@ namespace Easy
/*!
* @brief Returns the mentions
*
*
* @since before 0.11.0
*/
const std::vector<Mention> mentions() const;
/*!
* @brief Returns true if the user muted the conversation
*
*
* @since before 0.11.0
*/
bool muted() const;
/*!
* @brief Returns true if the status is pinned
*
*
* @since before 0.11.0
*/
bool pinned() const;
/*!
* @brief Returns the reblogged Status
*
*
* @since before 0.11.0
*/
const Status reblog() const;
/*!
* @brief Returns true if the user has reblogged the status
*
*
* @since before 0.11.0
*/
bool reblogged() const;
/*!
* @brief Returns the number of reblogs for the status
*
*
* @since before 0.11.0
*/
uint64_t reblogs_count() const;
uint_fast64_t reblogs_count() const;
/*!
* @brief Returns the number of replies for the status
*
*
* @since 0.19.0
*/
uint64_t replies_count() const;
uint_fast64_t replies_count() const;
/*!
* @brief Returns true if the attachments should be hidden by default
*
*
* @since before 0.11.0
*/
bool sensitive() const;
/*!
* @brief Sets sensitive flag for attachments
*
*
* @since 0.17.0
*/
Status sensitive(const bool &sensitive);
/*!
* @brief Returns the spoiler text
*
*
* @since before 0.11.0
*/
const string spoiler_text() const;
/*!
* @brief Sets the spoiler text
*
*
* @since 0.17.0
*/
Status spoiler_text(const string &spoiler_text);
/*!
* @brief Returns the tags
*
*
* @since before 0.11.0
*/
const std::vector<Tag> tags() const;
/*!
* @brief Returns the Fediverse-unique resource ID
*
*
* @since before 0.11.0
*/
const string uri() const;
/*!
* @brief Returns the URL to the status page
*
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns the visibility of the status
*
*
* @since before 0.11.0
*/
visibility_type visibility() const;
/*!
* @brief Sets the visibility of the status
*
*
* @since 0.17.0
*/
Status visibility(const visibility_type &visibility);
};
}
}
#endif // MASTODON_CPP_EASY_STATUS_HPP

View File

@ -1,35 +1,44 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <iomanip>
#include <algorithm>
#include "tag.hpp"
#include "debug.hpp"
#include "macros.hpp"
using namespace Mastodon;
using Tag = Easy::Tag;
Tag::Tag(const string &json)
: Entity(json)
{}
Tag::Tag()
: Entity()
{}
bool Tag::valid() const
{
return Entity::check_valid(
{
"name",
"url"
});
const std::vector<string> attributes =
{{
"name",
"url"
}};
return Entity::check_valid(attributes);
}
const string Tag::name() const
@ -48,9 +57,10 @@ const std::vector<Tag::History> Tag::history() const
if (node.isArray())
{
std::vector<Easy::Tag::History> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return Easy::Tag::History(value); });
for (const Json::Value &value : node)
{
vec.push_back(Easy::Tag::History(value.toStyledString()));
}
return vec;
}
@ -58,6 +68,15 @@ const std::vector<Tag::History> Tag::history() const
return {};
}
Tag::History::History(const string &json)
: Entity(json)
{}
Tag::History::History()
: Entity()
{}
bool Tag::History::valid() const
{
const std::vector<string> attributes =
@ -70,26 +89,26 @@ bool Tag::History::valid() const
return Entity::check_valid(attributes);
}
uint64_t Tag::History::accounts() const
uint_fast64_t Tag::History::accounts()
{
return get_uint64("accounts");
return stouint64(get_string("accounts"));
}
const Easy::time_type Tag::History::day() const
const system_clock::time_point Tag::History::day()
{
const Json::Value node = get("day");
if (node.isString())
{
std::chrono::seconds seconds(stouint64(node.asString()));
return {system_clock::time_point(seconds)};
return system_clock::time_point(seconds);
}
ttdebug << "Could not get data: day\n";
return Easy::time_type();
return system_clock::time_point();
}
uint64_t Tag::History::uses() const
uint_fast64_t Tag::History::uses()
{
return get_uint64("uses");
return stouint64(get_string("uses"));
}

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -18,86 +18,119 @@
#define MASTODON_CPP_EASY_TAG_HPP
#include <string>
#include <chrono>
#include <cstdint>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;
using std::uint64_t;
using std::chrono::system_clock;
using std::uint_fast64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold tags.
*
*
* @since before 0.11.0
*/
class Tag : public Entity
class Easy::Tag : public Easy::Entity
{
public:
/*!
* @brief Class to hold Tag history
*
*
* @since 0.16.0
*/
class History : public Easy::Entity
{
public:
using Entity::Entity;
/*!
* @brief Constructs an Tag::History object from a JSON string.
*
* @param json JSON string
*
* @since 0.16.0
*/
explicit History(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Tag::History object.
*
* @since 0.16.0
*/
History();
virtual bool valid() const;
/*!
* @brief Returns the number of accounts using that hashtag.
*
*
* @since 0.16.0
*/
uint64_t accounts() const;
uint_fast64_t accounts();
/*!
* @brief Returns the day.
*
*
* @since 0.16.0
*/
const Easy::time_type day() const;
const system_clock::time_point day();
/*!
* @brief Returns the number of statuses with that hashtag.
*
* @brief Returns the number of accounts using that hashtag.
*
* @since 0.16.0
*/
uint64_t uses() const;
uint_fast64_t uses();
};
using Entity::Entity;
/*!
* @brief Constructs an Tag object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Tag(const string &json);
virtual bool valid() const override;
/*!
* @brief Constructs an empty Tag object.
*
* @since before 0.11.0
*/
Tag();
virtual bool valid() const;
/*!
* @brief Returns the name of the tag
*
*
* @since before 0.11.0
*/
const string name() const;
/*!
* @brief Returns the URL of the tag
*
*
* @since before 0.11.0
*/
const string url() const;
/*!
* @brief Returns the history of the tag
*
*
* @since 0.16.0
*/
const std::vector<History> history() const;
};
}
}
#endif // MASTODON_CPP_EASY_TAG_HPP

View File

@ -1,51 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "token.hpp"
using namespace Mastodon;
using Token = Easy::Token;
bool Token::valid() const
{
return Entity::check_valid(
{
"access_token",
"token_type",
"scope",
"created_at"
});
}
const string Token::access_token() const
{
return get_string("access_token");
}
const string Token::token_type() const
{
return get_string("token_type");
}
const string Token::scope() const
{
return get_string("scope");
}
const Easy::time_type Token::created_at() const
{
return get_time("created_at");
}

View File

@ -1,74 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_TOKEN_HPP
#define MASTODON_CPP_EASY_TOKEN_HPP
#include <string>
#include "../../mastodon-cpp.hpp"
#include "../entity.hpp"
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Class to hold applications.
*
* @since before 0.11.0
*/
class Token : public Entity
{
public:
using Entity::Entity;
virtual bool valid() const override;
/*!
* @brief Returns the access token.
*
* @since 0.103.0
*/
const string access_token() const;
/*!
* @brief Returns the token type.
*
* @since 0.103.0
*/
const string token_type() const;
/*!
* @brief Returns the scope of the token.
*
* @since 0.103.0
*/
const string scope() const;
/*!
* @brief Returns the date of creation.
*
* @since 0.103.0
*/
const Easy::time_type created_at() const;
};
}
}
#endif // MASTODON_CPP_EASY_TOKEN_HPP

View File

@ -1,28 +1,26 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctime>
#include <iomanip> // get_time
#include <sstream>
#include <chrono>
#include <ctime>
#include <regex>
#include <algorithm>
#include "easy/entity.hpp"
#include "easy/easy.hpp"
#include "debug.hpp"
#include "easy.hpp"
#include "macros.hpp"
using namespace Mastodon;
using std::string;
@ -35,35 +33,10 @@ Easy::Entity::Entity(const string &json)
from_string(json);
}
Easy::Entity::Entity(const Json::Value &object)
: _tree(object)
,_was_set(false)
{}
Easy::Entity::Entity()
: _tree(Json::nullValue)
, _was_set(false)
{}
Easy::Entity::~Entity()
{}
Easy::Entity::operator const Json::Value() const
{
return to_object();
}
void Easy::Entity::from_string(const string &json)
{
if (json.find('{') != std::string::npos)
{
std::stringstream ss(json);
ss >> _tree;
}
else
{
_tree.clear();
}
std::stringstream ss(json);
ss >> _tree;
// If the JSON is a single object encapsulated in an array,
// transform it into an object. If the JSON string is [], transform to null
@ -77,31 +50,25 @@ void Easy::Entity::from_string(const string &json)
ttdebug << "ERROR: JSON string holds no object\n";
ttdebug << "String was: " << json << '\n';
}
else if (!_tree["error"].isNull() || !_tree["errors"].isNull())
else if (!_tree["error"].isNull())
{
ttdebug << "ERROR: Server returned an error\n";
ttdebug << "String was: " << json << '\n';
}
}
const string Easy::Entity::to_string() const
{
return _tree.toStyledString();
}
void Easy::Entity::from_object(const Json::Value &object)
{
_tree = object;
}
const Json::Value Easy::Entity::to_object() const
{
return _tree;
}
Easy::Entity::Entity()
: _was_set(false)
{}
bool Easy::Entity::check_valid(const std::vector<string> &attributes) const
{
for (const string &attribute : attributes)
for (const string &attribute: attributes)
{
get(attribute);
if (!was_set())
@ -115,14 +82,7 @@ bool Easy::Entity::check_valid(const std::vector<string> &attributes) const
const string Easy::Entity::error() const
{
string error = get_string("error");
if (error.empty())
{
// Pleroma uses {"errors":{"detail":"[…]"}} sometimes.
const Json::Value node = get("errors.detail");
error = node.asString();
}
return error;
return get_string("error");
}
bool Easy::Entity::was_set() const
@ -185,7 +145,7 @@ const string Easy::Entity::get_string(const string &key) const
return "";
}
uint64_t Easy::Entity::get_uint64(const string &key) const
uint_fast64_t Easy::Entity::get_uint64(const string &key) const
{
const Json::Value node = get(key);
@ -227,19 +187,24 @@ bool Easy::Entity::get_bool(const string &key) const
return false;
}
const Easy::time_type Easy::Entity::get_time(const string &key) const
const system_clock::time_point
Easy::Entity::get_time_point(const string &key) const
{
const Json::Value node = get(key);
if (node.isString())
{
std::stringstream sstime(node.asString());
struct std::tm tm;
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
std::time_t time = timegm(&tm);
_was_set = true;
return Easy::string_to_time(node.asString());
return system_clock::from_time_t(time);
}
_was_set = false;
// Return clocks epoch
return { system_clock::time_point() };
return system_clock::time_point();
}
const std::vector<string> Easy::Entity::get_vector(const string &key) const
@ -249,9 +214,10 @@ const std::vector<string> Easy::Entity::get_vector(const string &key) const
if (node.isArray())
{
std::vector<string> vec;
std::transform(node.begin(), node.end(), std::back_inserter(vec),
[](const Json::Value &value)
{ return value.asString(); });
for (const Json::Value &value : node)
{
vec.push_back(value.asString());
}
_was_set = true;
return vec;
}
@ -298,7 +264,7 @@ void Easy::Entity::set(const string &key, const Json::Value &value)
ttdebug << "Could not set data: " << key << '\n';
}
std::uint64_t Easy::Entity::stouint64(const string &str) const
std::uint_fast64_t Easy::Entity::stouint64(const string &str) const
{
if (str == "")
{

View File

@ -1,240 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_ENTITY_HPP
#define MASTODON_CPP_EASY_ENTITY_HPP
#include <string>
#include <jsoncpp/json/json.h>
#include "types_easy.hpp"
using std::string;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Base class for all entities.
*
* @since before 0.11.0
*/
class Entity
{
public:
/*!
* @brief Constructs an Entity object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Entity(const string &json);
/*!
* @brief Constructs an Entity object from a JSON object.
*
* @param object JSON object
*
* @since 0.100.0
*/
explicit Entity(const Json::Value &object);
/*!
* @brief Constructs an empty Entity object.
*
* @since before 0.11.0
*/
Entity();
/*!
* @brief Destroys the object.
*
* @since 0.100.0
*/
virtual ~Entity();
/*!
* Returns the JSON object of the Entity
*
* @since 0.100.0
*/
operator const Json::Value() const;
/*!
* @brief Replaces the Entity with a new one from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
void from_string(const string &json);
/*!
* @brief Returns the JSON of the Entity as formatted string.
*
* @return JSON string
*
* @since before 0.11.0
*/
const string to_string() const;
/*!
* @brief Replaces the Entity with a new one from a JSON object.
*
* @param object JSON object
*
* @since 0.100.0
*/
void from_object(const Json::Value &object);
/*!
* @brief Returns the JSON object of the Entity
*
* @return JSON object
*
* @since before 0.11.0
*/
const Json::Value to_object() const;
/*!
* @brief Returns true if the Entity holds valid data
*
* @since before 0.11.0 (virtual since 0.18.2)
*/
virtual bool valid() const = 0;
/*!
* @brief Returns error string sent by the server
*
* @since before 0.11.0
*/
const string error() const;
/*!
* @brief Returns true if the last requested value was set, false if
* it was unset.
*
* Members of Easy::Entity-derived classes return a default
* value depending on its type when the requested value is not
* found in the JSON. "" for strings, false for bools and so
* on. Most of the time this is no problem, but sometimes you
* need to know for sure.
*
* Example:
* @code
* Easy::Account a(jsonstring);
* if (a.note().empty())
* {
* if (a.was_set())
* {
* cout << "Account has an empty description.\n";
* }
* else
* {
* cout << "Account has no description.\n";
* }
* }
* @endcode
*
* @since before 0.11.0
*/
bool was_set() const;
protected:
/*!
* @brief Returns the value of key as Json::Value
*
* Returns an empty object if the value does not exist or is
* null.
*/
const Json::Value get(const string &key) const;
/*!
* @brief Returns the value of key as std::string
*
* returns "" if the value does not exist or is null.
*/
const string get_string(const string &key) const;
/*!
* @brief Returns the value of key as std::uint64_t
*
* Returns 0 if the value does not exist or is null.
*/
uint64_t get_uint64(const string &key) const;
/*!
* @brief Returns the value of key as double
*
* Returns 0.0 if the value does not exist or is null.
*/
double get_double(const string &key) const;
/*!
* @brief Returns the value of key as bool
*
* Returns false if the value does not exist or is null.
*/
bool get_bool(const string &key) const;
/*!
* @brief Returns the value of key as Easy::time.
*
* Returns clocks epoch if the value does not exist or is null.
*/
const Easy::time_type get_time(const string &key) const;
/*!
* @brief Returns the value of key as vector
*
* Returns an empty vector if the value does not exist or is
* null.
*/
const std::vector<string> get_vector(const string &key) const;
/*!
* @brief Sets the value of key
*
* @since 0.17.0
*/
void set(const string &key, const Json::Value &value);
/*!
* @brief Returns value of str as uint64_t.
*/
std::uint64_t stouint64(const string &str) const;
/*!
* @brief Checks if an Entity is valid
*
* @param attributes The attributes to check
*
* @return true if all attributes are set
*
* @since 0.18.2
*/
bool check_valid(const std::vector<string> &attributes) const;
private:
Json::Value _tree;
mutable bool _was_set;
};
}
}
#endif // MASTODON_CPP_EASY_ENTITY_HPP

View File

@ -1,142 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "return_types_easy.hpp"
#include "easy/entities/account.hpp"
#include "easy/entities/application.hpp"
#include "easy/entities/attachment.hpp"
#include "easy/entities/card.hpp"
#include "easy/entities/context.hpp"
#include "easy/entities/emoji.hpp"
#include "easy/entities/instance.hpp"
#include "easy/entities/list.hpp"
#include "easy/entities/mention.hpp"
#include "easy/entities/notification.hpp"
#include "easy/entities/relationship.hpp"
#include "easy/entities/results.hpp"
#include "easy/entities/status.hpp"
#include "easy/entities/tag.hpp"
#include "easy/entities/token.hpp"
#include "easy/entities/pushsubscription.hpp"
#include "easy/entities/filter.hpp"
#include "easy/entities/poll.hpp"
#include "easy/entities/conversation.hpp"
using namespace Mastodon;
template<typename T>
Easy::return_entity<T>::return_entity()
: entity()
{}
template<typename T>
Easy::return_entity<T>::return_entity(const uint8_t ec, const string &em,
const uint16_t hec, const T &ent)
: entity(ent)
{
error_code = ec;
error_message = em;
http_error_code = hec;
}
template<typename T>
Easy::return_entity<T>::return_entity(const error ec, const string &em,
const uint16_t hec, const T &ent)
: entity(ent)
{
error_code = static_cast<uint8_t>(ec);
error_message = em;
http_error_code = hec;
}
template<typename T>
Easy::return_entity<T>::return_entity::operator const T() const
{
return entity;
}
template<typename T>
Easy::return_entity<T>::return_entity::operator const string() const
{
return entity.to_string();
}
// Explicit instantiations, so it can be used from outside.
template struct Easy::return_entity<Easy::Account>;
template struct Easy::return_entity<Easy::Application>;
template struct Easy::return_entity<Easy::Attachment>;
template struct Easy::return_entity<Easy::Card>;
template struct Easy::return_entity<Easy::Context>;
template struct Easy::return_entity<Easy::Emoji>;
template struct Easy::return_entity<Easy::Instance>;
template struct Easy::return_entity<Easy::List>;
template struct Easy::return_entity<Easy::Mention>;
template struct Easy::return_entity<Easy::Notification>;
template struct Easy::return_entity<Easy::Relationship>;
template struct Easy::return_entity<Easy::Results>;
template struct Easy::return_entity<Easy::Status>;
template struct Easy::return_entity<Easy::Tag>;
template struct Easy::return_entity<Easy::Token>;
template struct Easy::return_entity<Easy::PushSubscription>;
template struct Easy::return_entity<Easy::Filter>;
template struct Easy::return_entity<Easy::Poll>;
template struct Easy::return_entity<Easy::Conversation>;
template<typename T>
Easy::return_entity_vector<T>::
return_entity_vector::return_entity_vector()
: entities()
{}
template<typename T>
Easy::return_entity_vector<T>::return_entity_vector::return_entity_vector(
const uint8_t ec, const string &em,
const uint16_t hec, const vector<T> &vec)
: entities(vec)
{
error_code = ec;
error_message = em;
http_error_code = hec;
}
template<typename T>
Easy::return_entity_vector<T>::return_entity_vector::operator const vector<T>()
const
{
return entities;
}
// Explicit instantiations, so it can be used from outside.
template struct Easy::return_entity_vector<Easy::Account>;
template struct Easy::return_entity_vector<Easy::Application>;
template struct Easy::return_entity_vector<Easy::Attachment>;
template struct Easy::return_entity_vector<Easy::Card>;
template struct Easy::return_entity_vector<Easy::Context>;
template struct Easy::return_entity_vector<Easy::Emoji>;
template struct Easy::return_entity_vector<Easy::Instance>;
template struct Easy::return_entity_vector<Easy::List>;
template struct Easy::return_entity_vector<Easy::Mention>;
template struct Easy::return_entity_vector<Easy::Notification>;
template struct Easy::return_entity_vector<Easy::Relationship>;
template struct Easy::return_entity_vector<Easy::Results>;
template struct Easy::return_entity_vector<Easy::Status>;
template struct Easy::return_entity_vector<Easy::Tag>;
template struct Easy::return_entity_vector<Easy::Token>;
template struct Easy::return_entity_vector<Easy::PushSubscription>;
template struct Easy::return_entity_vector<Easy::Filter>;
template struct Easy::return_entity_vector<Easy::Poll>;
template struct Easy::return_entity_vector<Easy::Conversation>;

View File

@ -1,136 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_RETURN_TYPES_EASY_HPP
#define MASTODON_CPP_EASY_RETURN_TYPES_EASY_HPP
#include <string>
#include <vector>
#include <ostream>
#include <cstdint>
#include "../mastodon-cpp.hpp"
using std::string;
using std::vector;
using std::uint8_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Return types for calls that return a single `Easy::Entity`.
*
* @since 0.100.0
*/
template <typename T> struct
[[deprecated("Will go away in 1.0.0 with no replacement.")]]
return_entity : return_base
{
/*!
* @brief Mastodon::Easy::Entity
*
* @since 0.100.0
*/
T entity;
return_entity();
/*!
* @brief Return type for easy Mastodon::Easy::API.
*
* @param ec Error code
* @param em Error message
* @param hec HTTP error code
* @param ent Answer
*
* @since 0.100.0
*/
return_entity(const uint8_t ec, const string &em,
const uint16_t hec, const T &ent);
/*!
* @brief Return type for easy Mastodon::Easy::API.
*
* @param ec Error code
* @param em Error message
* @param hec HTTP error code
* @param ent Answer
*
* @since 0.110.0
*/
return_entity(const error ec, const string &em,
const uint16_t hec, const T &ent);
/*!
* @brief Same as return_entity::entity.
*
* @since 0.100.0
*/
operator const T() const;
/*!
* @brief Mastodon::Easy::Entity as formatted string.
*
* @since 0.100.0
*/
operator const string() const;
/*!
* @brief Mastodon::Easy::Entity as formatted string.
*
* @since 0.100.0
*/
friend std::ostream &operator <<(std::ostream &out,
const return_entity<T> &ret)
{
// Could only get it to work by implementing it here.
out << ret.entity.to_string();
return out;
}
};
/*!
* @brief Return types for calls that return multiple `Easy::Entity`s.
*
* @since 0.100.0
*/
template <typename T> struct
[[deprecated("Will go away in 1.0.0 with no replacement.")]]
return_entity_vector : return_base
{
/*!
* @brief std::vector of Mastodon::Easy::Entity.
*
* @since 0.100.0
*/
vector<T> entities;
return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em,
const uint16_t hec, const vector<T> &vec);
/*!
* @brief Same es return_entity_vector::entities.
*
* @since 0.100.0
*/
operator const vector<T>() const;
};
}
}
#endif // MASTODON_CPP_EASY_RETURN_TYPES_EASY_HPP

View File

@ -1,60 +1,62 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include "easy.hpp"
#include "debug.hpp"
#include "macros.hpp"
#include "easy/entities/status.hpp"
#include "easy/entities/attachment.hpp"
#include "easy/entities/notification.hpp"
using namespace Mastodon::Easy;
using namespace Mastodon;
const return_entity<Status> API::send_toot(const Status &status)
const Easy::Status Easy::send_toot(const Status &status, uint_fast16_t &error)
{
return send_post(status);
return send_post(status, error);
}
const return_entity<Status> API::send_post(const Status &status)
const Easy::Status Easy::send_post(const Status &status, uint_fast16_t &error)
{
parameters params;
API::parametermap parameters;
string answer;
error = 0;
if (!status.content().empty())
{
params.push_back({ "status", { status.content() }});
parameters.insert({ "status", { status.content() }});
}
else
{
ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
return { error::INVALID_ARGUMENT,
"Easy::Status::content can not be empty", 0, Status() };
error = 11;
return Status();
}
if (!status.in_reply_to_id().empty())
{
params.push_back({ "in_reply_to_id",
{ status.in_reply_to_id() }});
parameters.insert({ "in_reply_to_id",
{ status.in_reply_to_id() }});
}
if (status.sensitive())
{
params.push_back({ "sensitive", { "true" }});
parameters.insert({ "sensitive", { "true" }});
}
if (!status.spoiler_text().empty())
{
params.push_back({ "spoiler_text", { status.spoiler_text() }});
parameters.insert({ "spoiler_text", { status.spoiler_text() }});
}
if (status.visibility() != visibility_type::Undefined)
{
@ -76,96 +78,102 @@ const return_entity<Status> API::send_post(const Status &status)
default:
break;
};
params.push_back({ "visibility", { visibility }});
parameters.insert({ "visibility", { visibility }});
}
if (!status.language().empty())
{
params.push_back({ "language", { status.language() }});
parameters.insert({ "language", { status.language() }});
}
if (!status.media_attachments().empty())
{
std::vector<string> media_ids;
for (const Attachment &att : status.media_attachments())
{
parameters param_att;
API::parametermap param_att;
if (!att.file().empty())
{
param_att.push_back({ "file", { att.file() }});
param_att.insert({ "file", { att.file() }});
}
else
{
ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
return { error::INVALID_ARGUMENT,
"Easy::Attachment::file can not be empty", 0,
Status() };
error = 11;
return Status();
}
if (!att.description().empty())
{
param_att.push_back({ "description", { att.description() }});
param_att.insert({ "description", { att.description() }});
}
if (!att.focus().empty())
{
param_att.push_back({ "focus",
{ std::to_string(att.focus()[0]) + ',' +
std::to_string(att.focus()[1]) }});
param_att.insert({ "focus",
{ std::to_string(att.focus()[0]) + ',' +
std::to_string(att.focus()[1]) }});
}
return_call ret = post(API::v1::media, param_att);
if (ret.error_code == 0)
error = post(API::v1::media, param_att, answer);
if (error == 0)
{
Attachment attachment(ret.answer);
Attachment attachment(answer);
media_ids.push_back(attachment.id());
}
else
{
ttdebug << "ERROR: Could not upload file.\n";
return { ret.error_code, ret.error_message,
ret.http_error_code, Status(ret.answer) };
return Status();
}
}
params.push_back({ "media_ids", media_ids });
parameters.insert({ "media_ids", media_ids });
}
return_call ret = post(API::v1::statuses, params);
return { ret.error_code, ret.error_message, ret.http_error_code,
Status(ret.answer) };
error = post(API::v1::statuses, parameters, answer);
if (error == 0)
{
return Status(answer);
}
else
{
return Status();
}
}
const return_entity_vector<Notification> API::get_notifications(
const uint16_t limit, const string since_id, const string max_id)
const vector<Easy::Notification> Easy::get_notifications(
uint_fast16_t &error, const uint_fast16_t limit,
const string since_id, const string max_id)
{
parameters params;
API::parametermap parameters;
string answer;
error = 0;
params.push_back({ "limit", { std::to_string(limit) } });
parameters.insert({ "limit", { std::to_string(limit) } });
if (!since_id.empty())
{
params.push_back({ "since_id", { since_id } });
parameters.insert({ "since_id", { since_id } });
}
if (!max_id.empty())
{
params.push_back({ "max_id", { max_id } });
parameters.insert({ "max_id", { max_id } });
}
return_call ret = API::get(API::v1::notifications, params);
error = API::get(Mastodon::API::v1::notifications, parameters, answer);
if (ret.error_code == 0)
if (error == 0)
{
const vector<string> &answer_v = json_array_to_vector(ret.answer);
const vector<string> &answer_v = json_array_to_vector(answer);
vector<Notification> notifications;
notifications.resize(answer_v.size());
// Transform vector of strings to vector of Notification.
std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
[](const string &s)
[](const string s)
{ return Notification(s); });
return { ret.error_code, ret.error_message, ret.http_error_code,
notifications };
return notifications;
}
else
{
ttdebug << "ERROR: Could not get notifications.\n";
return { ret.error_code, ret.error_message, ret.http_error_code, {} };
return { Notification() };
}
}

View File

@ -1,61 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "types_easy.hpp"
namespace Mastodon
{
namespace Easy
{
time_type::operator const system_clock::time_point() const
{
return timepoint;
}
time_type::operator const string() const
{
return strtime("%FT%T%z", true);
}
const string time_type::strtime(const string &format, const bool &local) const
{
constexpr std::uint16_t bufsize = 1024;
std::time_t time = system_clock::to_time_t(timepoint);
std::tm *tm;
if (local)
{
tm = std::localtime(&time);
}
else
{
tm = std::gmtime(&time);
}
char buffer[bufsize];
std::strftime(buffer, bufsize, format.c_str(), tm);
return static_cast<const string>(buffer);
}
std::ostream &operator <<(std::ostream &out,
const time_type &t)
{
const string s = t; // Converts using operator const string().
out << s;
return out;
}
}
}

View File

@ -1,239 +0,0 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_TYPES_EASY_HPP
#define MASTODON_CPP_EASY_TYPES_EASY_HPP
#include <string>
#include <utility>
#include <chrono>
#include <cstdint>
using std::string;
using std::chrono::system_clock;
using std::uint64_t;
namespace Mastodon
{
namespace Easy
{
/*!
* @brief Describes the event type returned in streams.
*
* @since before 0.11.0
*/
enum class event_type
{
Update,
Notification,
Delete,
Error,
Filters_changed,
Undefined
};
/*!
* @brief Describes visibility of posts.
*
* @since before 0.11.0
*/
// TODO: What about instance-only?
enum class visibility_type
{
Direct,
Private,
Unlisted,
Public,
Undefined
};
/*!
* @brief Describes the attachment type
*
* @since before 0.11.0
*/
// TODO: Look up what Pleroma returns.
enum class attachment_type
{
Image,
Video,
Gifv,
Unknown,
Undefined
};
/*!
* @brief Describes the card type
*
* @since before 0.11.0
*/
enum class card_type
{
Link,
Photo,
Video,
Rich,
Undefined
};
/*!
* @brief Describes the notification type
*
* @since before 0.11.0
*/
enum class notification_type
{
Mention,
Reblog,
Favourite,
Follow,
Undefined
};
/*!
* @brief Describes the context.
*
* @since 0.104.0
*/
enum class context_type
{
Home,
Notifications,
Public,
Thread,
Undefined
};
/*!
* @brief Used for stream events.
*
* @since 0.100.0
*/
typedef struct stream_event_type
{
event_type type = event_type::Undefined;
string data;
} stream_event_type;
[[deprecated("Replaced by Mastodon::Easy::stream_event_type")]]
typedef stream_event_type stream_event;
/*!
* @brief Type of notification and 'push is requested or not'.
*
* @since 0.100.0
*/
typedef struct alert_type
{
Easy::notification_type type = Easy::notification_type::Undefined;
bool pushreq = false;
} alert_type;
/*!
* @brief Type for time. Converts to time_point and string.
*
* @since 0.100.0
*/
struct time_type
{
system_clock::time_point timepoint = system_clock::time_point();
operator const system_clock::time_point() const;
/*!
* @brief Returns local time as string in ISO 8601 format (%FT%T%z).
*
* @since 0.100.0
*/
operator const string() const;
/*!
* @brief Returns local time as string in ISO 8601 format (%FT%T%z).
*
* @since 0.100.0
*/
friend std::ostream &operator <<(std::ostream &out,
const Easy::time_type &t);
/*!
* @brief Converts time to a string.
*
* The return value can not exceed 1023 chars.
*
* @param format The format of the string, same as with
* `strftime`.
* @param local Use local time (default) or UTC.
*
* Example:
* @code
* Mastodon::Easy::time timepoint = status.created_at();
* std::cout << timepoint.strtime("%F, %T UTC", false) << '\n';
* @endcode
*
* @return The time as string.
*
* @since 0.100.0
*/
const string strtime(const string &format,
const bool &local = true) const;
};
[[deprecated("Replaced by Mastodon::Easy::time_type")]]
typedef time_type time;
/*!
* @brief Describes an account-field.
*
* @since 0.106.0
*/
typedef struct account_field_type
{
const string name;
const string value;
Easy::time_type verified_at;
} account_field_type;
/*!
* @brief URLs returned by Instance::urls().
*
* @since 0.106.0
*/
typedef struct urls_type
{
string streaming_api;
} urls_type;
/*!
* @brief Statistics returned by Instance::stats().
*/
typedef struct stats_type
{
uint64_t user_count = 0;
uint64_t status_count = 0;
uint64_t domain_count = 0;
} stats_type;
/*!
* @brief Poll options returned by Poll::options().
*/
typedef struct poll_options_type
{
string title;
uint64_t votes_count = 0;
} poll_options_type;
}
}
#endif // MASTODON_CPP_EASY_TYPES_EASY_HPP

View File

@ -1,50 +1,33 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <functional> // std::bind
#include <list>
#include <cstring> // std::strncmp
#include <exception>
#include <thread>
#include <regex>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/URI.h>
#include <Poco/Environment.h>
#include <Poco/Exception.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/SSLException.h>
#include "debug.hpp"
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#include "macros.hpp"
#include "mastodon-cpp.hpp"
using namespace Mastodon;
namespace curlopts = curlpp::options;
using std::cerr;
using std::istream;
using std::make_unique;
using std::move;
using std::regex;
using std::regex_search;
using std::smatch;
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::StreamCopier;
using Poco::Environment;
API::http::http(const API &api, const string &instance,
const string &access_token)
@ -53,297 +36,163 @@ API::http::http(const API &api, const string &instance,
, _access_token(access_token)
, _cancel_stream(false)
{
Poco::Net::initializeSSL();
try
{
string env_proxy = Environment::get("http_proxy");
size_t pos;
// Only keep text between // and /.
if ((pos = env_proxy.find("//")) != string::npos)
{
env_proxy = env_proxy.substr(pos + 2);
}
if ((pos = env_proxy.find('/')) != string::npos)
{
env_proxy = env_proxy.substr(0, pos);
}
if ((pos = env_proxy.find('@')) != string::npos)
{
string hostport = env_proxy.substr(pos + 1);
string userpw = env_proxy.substr(0, pos);
set_proxy(hostport, userpw);
}
else
{
set_proxy(env_proxy);
}
}
catch (const std::exception &)
{
// No proxy found, no problem.
}
curlpp::initialize();
}
API::http::~http()
{
Poco::Net::uninitializeSSL();
curlpp::terminate();
}
void API::http::set_proxy(const string &hostport, const string &userpw)
uint_fast16_t API::http::request(const method &meth,
const string &path,
string &answer)
{
try
{
HTTPSClientSession::ProxyConfig proxyconfig;
size_t pos = hostport.find(':');
proxyconfig.host = hostport.substr(0, pos);
if (pos != string::npos)
{
proxyconfig.port = std::stoi(hostport.substr(pos + 1));
}
if (!userpw.empty())
{
string username;
pos = userpw.find(':');
Poco::URI::decode(userpw.substr(0, pos), username);
proxyconfig.username = username;
if (pos != string::npos)
{
string password;
Poco::URI::decode(userpw.substr(pos + 1), password);
proxyconfig.password = password;
}
}
HTTPSClientSession::setGlobalProxyConfig(proxyconfig);
ttdebug << "Set proxy to " << hostport << ".\n";
}
catch (const std::exception &e)
{
if (parent.exceptions())
{
std::rethrow_exception(std::current_exception());
}
}
return request(meth, path, curlpp::Forms(), answer);
}
return_call API::http::request(const http_method &meth, const string &path)
uint_fast16_t API::http::request(const method &meth,
const string &path,
const curlpp::Forms &formdata,
string &answer)
{
HTMLForm form;
return request(meth, path, form);
}
using namespace std::placeholders; // _1, _2, _3
return_call API::http::request(const http_method &meth, const string &path,
HTMLForm &formdata)
{
string answer;
return request_common(meth, path, formdata, answer);
}
void API::http::request_stream(const string &path, string &stream)
{
static return_call ret;
_streamthread = std::thread(
[&, path] // path is captured by value because it may be
{ // deleted before we access it.
HTMLForm form;
ret = request_common(http_method::GET_STREAM, path,
form, stream);
ttdebug << "Remaining content of the stream: " << stream << '\n';
if (!ret)
{
// Embed the HTTP status code in stream on error.
stream += "event: ERROR\ndata: {\"error_code\":"
+ std::to_string(ret.error_code) + ",\"http_error\":"
+ std::to_string(ret.http_error_code) + "}\n";
}
});
}
return_call API::http::request_common(const http_method &meth,
const string &path,
HTMLForm &formdata,
string &answer)
{
uint_fast16_t ret = 0;
ttdebug << "Path is: " << path << '\n';
try
{
string method;
curlpp::Easy request;
std::list<string> headers;
switch (meth)
{
case http_method::GET:
case http_method::GET_STREAM:
{
method = HTTPRequest::HTTP_GET;
break;
}
case http_method::PUT:
{
method = HTTPRequest::HTTP_PUT;
break;
}
case http_method::POST:
{
method = HTTPRequest::HTTP_POST;
break;
}
case http_method::PATCH:
{
// POCO in CentOS 7 (1.6.1) doesn't have HTTPRequest::HTTP_PATCH.
method = "PATCH";
break;
}
case http_method::DELETE:
{
method = HTTPRequest::HTTP_DELETE;
break;
}
default:
{
break;
}
}
request.setOpt<curlopts::Url>("https://" + _instance + path);
ttdebug << "User-Agent: " << parent.get_useragent() << "\n";
request.setOpt<curlopts::UserAgent>(parent.get_useragent());
HTTPSClientSession session(_instance);
HTTPRequest request(method, path, HTTPMessage::HTTP_1_1);
request.set("User-Agent", parent.get_useragent());
{
string proxy;
string userpw;
parent.get_proxy(proxy, userpw);
if (!proxy.empty())
{
request.setOpt<curlopts::Proxy>(proxy);
if (!userpw.empty())
{
request.setOpt<curlopts::ProxyUserPwd>(userpw);
}
}
}
if (!_access_token.empty())
{
request.set("Authorization", " Bearer " + _access_token);
headers.push_back("Authorization: Bearer " + _access_token);
}
if (meth != http::method::GET_STREAM)
{
headers.push_back("Connection: close");
// Get headers from server
request.setOpt<curlpp::options::Header>(true);
}
request.setOpt<curlopts::HttpHeader>(headers);
request.setOpt<curlopts::FollowLocation>(true);
request.setOpt<curlopts::WriteFunction>
(std::bind(&http::callback_write, this, _1, _2, _3, &answer));
request.setOpt<curlopts::ProgressFunction>
(std::bind(&http::callback_progress, this, _1, _2, _3, _4));
request.setOpt<curlopts::NoProgress>(0);
if (!formdata.empty())
{
ttdebug << "Size of HTMLForm is " << formdata.size() << '\n';
formdata.prepareSubmit(request);
formdata.write(session.sendRequest(request));
request.setOpt<curlopts::HttpPost>(formdata);
}
switch (meth)
{
case http::method::GET:
break;
case http::method::PATCH:
request.setOpt<curlopts::CustomRequest>("PATCH");
break;
case http::method::POST:
request.setOpt<curlopts::CustomRequest>("POST");
break;
case http::method::PUT:
request.setOpt<curlopts::CustomRequest>("PUT");
break;
case http::method::DELETE:
request.setOpt<curlopts::CustomRequest>("DELETE");
break;
default:
break;
}
//request.setOpt<curlopts::Verbose>(true);
answer.clear();
request.perform();
ret = curlpp::infos::ResponseCode::get(request);
ttdebug << "Response code: " << ret << '\n';
// Work around "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK"
size_t pos = answer.find("\r\n\r\n", 25);
_headers = answer.substr(0, pos);
// Only return body
answer = answer.substr(pos + 4);
if (ret == 200 || ret == 302 || ret == 307)
{ // OK or Found or Temporary Redirect
return 0;
}
else if (ret == 301 || ret == 308)
{ // Moved Permanently or Permanent Redirect
// return new URL
answer = curlpp::infos::EffectiveUrl::get(request);
return 13;
}
else if (ret == 0)
{
return 0xffff;
}
else
{
session.sendRequest(request);
}
HTTPResponse response;
istream &body_stream = session.receiveResponse(response);
const uint16_t http_code = response.getStatus();
ttdebug << "Response code: " << http_code << '\n';
answer.clear();
StreamCopier::copyToString(body_stream, answer);
std::ostringstream headers_stream;
response.write(headers_stream);
_headers = headers_stream.str();
switch (http_code)
{
case HTTPResponse::HTTP_OK:
{
return { error::OK, "", http_code, answer };
}
// Not using the constants because some are too new for Debian stretch.
case 301: // HTTPResponse::HTTP_MOVED_PERMANENTLY
case 308: // HTTPResponse::HTTP_PERMANENT_REDIRECT
case 302: // HTTPResponse::HTTP_FOUND
case 303: // HTTPResponse::HTTP_SEE_OTHER
case 307: // HTTPResponse::HTTP_TEMPORARY_REDIRECT
{
ttdebug << "HTTP redirect.\n";
string location = response.get("Location");
if (location.substr(0, 4) == "http")
{ // Remove protocol and instance from path.
size_t pos1 = location.find("//") + 2;
size_t pos2 = location.find('/', pos1);
if (location.substr(pos1, pos2 - pos1) != _instance)
{ // Return new location if the domain changed.
ttdebug << "New location is on another domain.\n";
return { error::URL_CHANGED, "Remote address changed",
http_code, location };
}
location = location.substr(pos2);
}
if (http_code == 301 || http_code == 308)
{ // Return new location for permanent redirects.
return { error::URL_CHANGED, "Remote address changed",
http_code, location };
}
else
{
ttdebug << "Following temporary redirect: " << location << '\n';
return request_common(meth, location, formdata, answer);
}
}
default:
{
return { error::CONNECTION_REFUSED, "Connection refused",
http_code, answer };
}
return ret;
}
}
catch (const Poco::Net::DNSException &e)
catch (curlpp::RuntimeError &e)
{
const string what = e.what();
// This error is thrown if http.cancel_stream() is used.
if ((what.compare(0, 16, "Callback aborted") == 0) ||
(what.compare(0, 19, "Failed writing body") == 0))
{
ttdebug << "Request was cancelled by user\n";
return 14;
}
else if (what.compare(what.size() - 20, 20, "Connection timed out") == 0)
{
ttdebug << "Timeout\n";
return 16;
}
if (parent.exceptions())
{
e.rethrow();
std::rethrow_exception(std::current_exception());
}
ttdebug << e.displayText() << "\n";
return { error::DNS, e.displayText(), 0, "" };
}
catch (const Poco::Net::ConnectionRefusedException &e)
{
if (parent.exceptions())
else
{
e.rethrow();
ttdebug << "curlpp::RuntimeError: " << e.what() << std::endl;
return 15;
}
ttdebug << e.displayText() << "\n";
return { error::CONNECTION_REFUSED, e.displayText(), 0, "" };
}
catch (const Poco::Net::SSLException &e)
{
if (parent.exceptions())
{
e.rethrow();
}
ttdebug << e.displayText() << "\n";
return { error::ENCRYPTION, e.displayText(), 0, "" };
}
catch (const Poco::Net::NetException &e)
{
if (parent.exceptions())
{
e.rethrow();
}
ttdebug << "Unknown network error: " << e.displayText() << std::endl;
return { error::UNKNOWN, e.displayText(), 0, "" };
}
catch (const std::exception &e)
catch (curlpp::LogicError &e)
{
if (parent.exceptions())
{
std::rethrow_exception(std::current_exception());
}
ttdebug << "Unknown error: " << e.what() << std::endl;
return { error::UNKNOWN, e.what(), 0, "" };
ttdebug << "curlpp::LogicError: " << e.what() << std::endl;
return 15;
}
}
@ -352,10 +201,40 @@ void API::http::get_headers(string &headers) const
headers = _headers;
}
size_t API::http::callback_write(char* data, size_t size, size_t nmemb,
string *str)
{
std::lock_guard<std::mutex> lock(_mutex);
str->append(data, size * nmemb);
// ttdebug << "Received " << size * nmemb << " Bytes\n";
return size * nmemb;
}
size_t API::http::callback(char* data, size_t size, size_t nmemb,
string *str)
{
return callback_write(data, size, nmemb, str);
}
double API::http::callback_progress(double /* dltotal */, double /* dlnow */,
double /* ultotal */, double /* ulnow */)
{
if (_cancel_stream)
{
// This throws the runtime error: Callback aborted
return 1;
}
return 0;
}
void API::http::cancel_stream()
{
_cancel_stream = true;
_streamthread.join();
}
void API::http::abort_stream()
{
cancel_stream();
}
std::mutex &API::http::get_mutex()

View File

@ -1,16 +1,16 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@ -20,8 +20,7 @@
#include <iostream>
#ifdef DEBUG
#define ttdebug \
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] DEBUG: "
#define ttdebug std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] DEBUG: "
#else
#define ttdebug false && std::cerr
#endif

View File

@ -1,35 +1,31 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* 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 Affero General Public License for more details.
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <regex>
#include <locale>
#include <codecvt>
#include <utility>
#include <iostream>
#include <exception>
#include <algorithm>
#include <Poco/Net/FilePartSource.h>
#include <Poco/URI.h>
#include "version.hpp"
#include "debug.hpp"
#include "macros.hpp"
#include "mastodon-cpp.hpp"
#include <iostream>
using namespace Mastodon;
using std::make_unique;
using Poco::Net::FilePartSource;
API::API(const string &instance, const string &access_token)
: _instance(instance)
@ -37,29 +33,12 @@ API::API(const string &instance, const string &access_token)
, _useragent(string("mastodon-cpp/") + global::version)
, _http(*this, instance, access_token)
, _exceptions(false)
, _proxy("")
, _proxy_userpw("")
{
bool fash = false;
const std::regex re_gab("(?:\\.|^)gab\\.[^\\.]+$");
const std::regex re_kiwifarms("(?:\\.|^)kiwifarms\\.[^\\.]+$");
for (const std::regex &re : { re_gab, re_kiwifarms })
{
if (std::regex_search(_instance, re))
{
fash = true;
break;
}
}
if (fash)
{
throw std::runtime_error("Fascist instance detected: " + _instance);
}
//
}
API::~API()
{}
void API::set_useragent(const std::string &useragent)
{
_useragent = useragent;
@ -75,7 +54,7 @@ const string API::get_instance() const
return _instance;
}
const string API::maptostr(const parameters &map, const bool &firstparam)
const string API::maptostr(const parametermap &map, const bool &firstparam)
{
string result = "";
char delim = '?';
@ -86,9 +65,9 @@ const string API::maptostr(const parameters &map, const bool &firstparam)
for (const auto &it : map)
{
if (it.values.size() == 1)
if (it.second.size() == 1)
{
result += (delim + it.key + "=" + ::urlencode(it.values.front()));
result += (delim + it.first + "=" + urlencode(it.second.front()));
if (delim == '?')
{
delim = '&';
@ -96,9 +75,9 @@ const string API::maptostr(const parameters &map, const bool &firstparam)
}
else
{
for (const string &str : it.values)
for (const string &str : it.second)
{
result += (delim + it.key + "[]=" + ::urlencode(str));
result += (delim + it.first + "[]=" + urlencode(str));
if (delim == '?')
{
delim = '&';
@ -111,12 +90,9 @@ const string API::maptostr(const parameters &map, const bool &firstparam)
return result;
}
unique_ptr<HTMLForm> API::maptoformdata(const parameters &map)
const curlpp::Forms API::maptoformdata(const parametermap &map)
{
ttdebug << "Transforming Mastodon::parameters to Poco::Net::HTMLForm.\n";
unique_ptr<HTMLForm> formdata =
make_unique<HTMLForm>(HTMLForm::ENCODING_MULTIPART);
curlpp::Forms formdata;
if (map.size() == 0)
{
@ -125,57 +101,36 @@ unique_ptr<HTMLForm> API::maptoformdata(const parameters &map)
for (const auto &it : map)
{
string key = it.key;
ttdebug << "Processing \"" + key + "\".\n";
// TODO: Test nested parameters.
if (const size_t pos = key.find('.') != string::npos)
{ // Nested parameters.
key.replace(pos, 1, "[");
key += ']';
}
if (it.values.size() == 1)
{ // If the file is not base64-encoded, treat as filename.
if ((key == "avatar" ||
key == "header" ||
key == "file") &&
it.values.front().substr(0, 5) != "data:")
{
ttdebug << key << ": Filename detected.\n";
try
if (it.second.size() == 1)
{ // If the file is not base64-encoded, treat as filename
if ((it.first == "avatar" ||
it.first == "header" ||
it.first == "file") &&
it.second.front().substr(0, 4).compare("data") != 0)
{
ttdebug << it.first << ": Filename detected.\n";
formdata.push_back(
new curlpp::FormParts::File(it.first, it.second.front()));
}
else
{
string key = it.first;
if (key == "account_ids" ||
key == "exclude_types" ||
key == "media_ids")
{
formdata->addPart(key,
new FilePartSource(it.values.front()));
}
catch (const std::exception &e)
{
if (exceptions())
{
std::rethrow_exception(std::current_exception());
}
// TODO: Proper error handling without exceptions.
std::cerr << "Error: Could not open file: "
<< it.values.front() << std::endl;
key += "[]";
}
formdata.push_back(
new curlpp::FormParts::Content(key, it.second.front()));
}
else if (key == "account_ids"
|| key == "exclude_types"
|| key == "media_ids"
|| key == "context")
{
key += "[]";
}
formdata->add(key, it.values.front());
}
else
{
for (const string &value : it.values)
for (const string &str : it.second)
{
formdata->add(key + "[]", value);
formdata.push_back(new curlpp::FormParts::Content(it.first + "[]",
str));
}
}
}
@ -183,15 +138,37 @@ unique_ptr<HTMLForm> API::maptoformdata(const parameters &map)
return formdata;
}
return_call API::register_app1(const string &client_name,
const string &redirect_uri,
const string &scopes,
const string &website,
string &client_id,
string &client_secret,
string &url)
const std::string API::urlencode(const std::string &str)
{
parameters params =
return curlpp::escape(str);
}
const std::string API::urldecode(const std::string &str)
{
return curlpp::unescape(str);
}
uint_fast16_t API::register_app1(const string &instance,
const string &client_name,
const string &redirect_uri,
const string &scopes,
const string &website,
string &client_id,
string &client_secret,
string &url)
{
return register_app1(client_name, redirect_uri, scopes, website,
client_id, client_secret, url);
}
uint_fast16_t API::register_app1(const string &client_name,
const string &redirect_uri,
const string &scopes,
const string &website,
string &client_id,
string &client_secret,
string &url)
{
API::parametermap parameters =
{
{ "client_name", { client_name } },
{ "redirect_uris", { redirect_uri } },
@ -199,47 +176,61 @@ return_call API::register_app1(const string &client_name,
{ "website", { website } }
};
return_call ret = post(API::v1::apps, params);
string answer;
uint_fast16_t ret = post(API::v1::apps, parameters, answer);
if (ret.error_code == 0)
if (ret == 0)
{
std::smatch match;
std::regex reid("client_id\":\"([^\"]+)\"");
std::regex resecret("client_secret\":\"([^\"]+)\"");
std::regex_search(ret.answer, match, reid);
std::regex_search(answer, match, reid);
client_id = match[1].str();
std::regex_search(ret.answer, match, resecret);
std::regex_search(answer, match, resecret);
client_secret = match[1].str();
url = "https://" + _instance + "/oauth/authorize" +
"?scope=" + ::urlencode(scopes) + "&response_type=code" +
"&redirect_uri=" + ::urlencode(redirect_uri) +
"&client_id=" + client_id;
"?scope=" + urlencode(scopes) + "&response_type=code" +
"&redirect_uri=" + urlencode(redirect_uri) +
"&client_id=" + client_id;
if (!website.empty())
{
url += "&website=" + ::urlencode(website);
url += "&website=" + urlencode(website);
}
return 0;
}
else if (ret.error_code == 78)
else if (ret == 13)
{
url = ret.answer;
url = answer;
return ret;
}
else
{
std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return ret;
}
return_call API::register_app2(const string &client_id,
const string &client_secret,
const string &redirect_uri,
const string &code,
string &access_token)
uint_fast16_t API::register_app2(const string &instance,
const string &client_id,
const string &client_secret,
const string &redirect_uri,
const string &code,
string &access_token)
{
parameters params =
return register_app2(client_id, client_secret, redirect_uri, code, access_token);
}
uint_fast16_t API::register_app2(const string &client_id,
const string &client_secret,
const string &redirect_uri,
const string &code,
string &access_token)
{
API::parametermap parameters =
{
{ "client_id", { client_id } },
{ "client_secret", { client_secret } },
@ -248,36 +239,31 @@ return_call API::register_app2(const string &client_id,
{ "code", { code } },
};
return_call ret = post("/oauth/token", params);
if (ret.error_code == 0)
std::string answer;
uint_fast16_t ret = post("/oauth/token", parameters, answer);
if (ret == 0)
{
std::smatch match;
std::regex retoken("access_token\":\"([^\"]+)\"");
std::regex_search(ret.answer, match, retoken);
std::regex_search(answer, match, retoken);
access_token = match[1].str();
_access_token = access_token;
return 0;
}
else
{
std::cerr << "Error code: " << std::to_string(ret.error_code) << '\n';
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return ret;
}
const string API::get_header(std::string header) const
const string API::get_header(const std::string &header) const
{
string headers;
string headers_lower;
_http.get_headers(headers);
headers_lower.resize(headers.size());
std::transform(headers.begin(), headers.end(),
headers_lower.begin(), ::tolower);
std::transform(header.begin(), header.end(), header.begin(), ::tolower);
size_t startpos = headers_lower.find(header + ':');
size_t startpos = headers.find(header + ':');
if (startpos != std::string::npos)
{
startpos = headers.find(':', startpos) + 2;
@ -299,47 +285,7 @@ bool API::exceptions() const
return _exceptions;
}
void API::set_proxy(const string &hostport, const string &userpw)
{
_http.set_proxy(hostport, userpw);
}
const parameters API::delete_params(const parameters &params,
const vector<string> &keys)
{
// Iterate through params. For each item in keys (k), compare to key of
// current parameter (p). Return false if parameter is to be deleted. Copy
// to new list of parameters (newparams) if true is returned.
parameters newparams(params.size());
const auto it =
std::copy_if(params.begin(), params.end(), newparams.begin(),
[&keys](const param &p)
{
return std::all_of(keys.begin(), keys.end(),
[&p](const string &k)
{
return (k != p.key);
});
});
newparams.resize(std::distance(newparams.begin(), it));
return newparams;
}
const string Mastodon::urlencode(const std::string &str)
{
string out;
Poco::URI::encode(str, "", out);
return out;
}
const string Mastodon::urldecode(const std::string &str)
{
string out;
Poco::URI::decode(str, out);
return out;
}
const string Mastodon::unescape_html(const string &html)
const string API::unescape_html(const string &html)
{
string buffer = html;
string output = "";
@ -347,9 +293,9 @@ const string Mastodon::unescape_html(const string &html)
// Used to convert int to utf-8 char
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> u8c;
// Matches numbered entities between 1 and 8 digits, decimal or hexadecimal
std::regex re_entity("&#(x)?([[:alnum:]]{1,8});");
std::regex re_entity("&#(x)?(\\d{1,8});");
std::smatch match;
while (std::regex_search(buffer, match, re_entity))
{
char32_t codepoint = 0;
@ -640,20 +586,20 @@ const string Mastodon::unescape_html(const string &html)
return output;
}
// ↓ DEPRECATED ↓
const string API::urlencode(const std::string &str)
void API::set_proxy(const string &proxy, const string &userpw)
{
return Mastodon::urlencode(str);
_proxy = proxy;
_proxy_userpw = userpw;
}
const string API::urldecode(const std::string &str)
void API::get_proxy(string &proxy, string &userpw) const
{
return Mastodon::urldecode(str);
}
const string API::unescape_html(const string &html)
{
return Mastodon::unescape_html(html);
if (!_proxy.empty())
{
proxy = _proxy;
if (!_proxy_userpw.empty())
{
userpw = _proxy_userpw;
}
}
}

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More