170 lines
6.3 KiB
C++
170 lines
6.3 KiB
C++
/**
|
|
This file is a part of rexy's matrix client
|
|
Copyright (C) 2019-2020 rexy712
|
|
|
|
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, 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.
|
|
|
|
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 MATRIX_CLIENT_HPP
|
|
#define MATRIX_CLIENT_HPP
|
|
|
|
#include <rexy/string.hpp>
|
|
#include "raii/rjp_string.hpp"
|
|
#include "matrix/session_info.hpp"
|
|
#include "matrix/upload_info.hpp"
|
|
#include "matrix/connection.hpp"
|
|
#include "matrix/roomcxn.hpp"
|
|
#include "matrix/netreturn.hpp"
|
|
#include "matrix/rest/client_url_list.hpp"
|
|
#include "matrix/sync_response.hpp"
|
|
#include <vector> //vector
|
|
#include <memory> //shared_ptr
|
|
#include <cstdlib> //size_t
|
|
|
|
namespace matrix{
|
|
//Class to interact with the homeserver of the logged in user.
|
|
//Shares state with the spawning session.
|
|
class client : public connection
|
|
{
|
|
private:
|
|
std::shared_ptr<matrix::rest::client_url_list> m_urls;
|
|
public:
|
|
client(const std::shared_ptr<session_info>&);
|
|
|
|
//Copy and move ctor
|
|
client(const client& b) = default;
|
|
client(client&& b) = default;
|
|
//Dtor
|
|
~client(void) = default;
|
|
|
|
//Copy and move assignment
|
|
client& operator=(const client&) = default;
|
|
client& operator=(client&&) = default;
|
|
|
|
/*
|
|
* Sets the display name on the homeserver.
|
|
* Note name must be safe to be placed in a json string.
|
|
* Returns: true if the operation was successful.
|
|
*/
|
|
netreturn<void> set_display_name(const rexy::string_base&);
|
|
/*
|
|
* Sets the url of the user's profile picture on the homeserver.
|
|
* Note url must be safe to be placed in a json string.
|
|
* Returns: true if the operation was successful.
|
|
*/
|
|
netreturn<void> set_profile_picture(const rexy::string_base&);
|
|
|
|
netreturn<void> set_presence(const rexy::string_base& status);
|
|
netreturn<raii::rjp_string> get_presence(const rexy::string_base& userid);
|
|
/*
|
|
* Gets the display name of the logged in user from the homeserver.
|
|
* Returns: the display name on success, an empty string on failure.
|
|
*/
|
|
netreturn<raii::rjp_string> get_display_name(void)const;
|
|
netreturn<raii::rjp_string> get_display_name(const rexy::string_base&)const;
|
|
/*
|
|
* Gets the profile picture url of the logged in user from the homeserver.
|
|
* Returns: the profile picture url on success, an empty string on failure.
|
|
*/
|
|
netreturn<raii::rjp_string> get_profile_picture(void)const;
|
|
/*
|
|
* Lookup a room id given a room alias.
|
|
* Returns: the room id of the associated room on success, empty string on failure.
|
|
*/
|
|
netreturn<raii::rjp_string> room_alias_to_id(const rexy::string_base& alias)const;
|
|
|
|
/*
|
|
* Get a list of rooms the logged in user is a member of.
|
|
* Returns: a vector of room id strings.
|
|
*/
|
|
netreturn<std::vector<raii::rjp_string>> list_rooms(void)const;
|
|
|
|
/*
|
|
* Create a new room on the logged in user's homeserver. Assign an alias to the room.
|
|
* Returns: the room id of the newly created room on success, an empty string on failure.
|
|
*/
|
|
netreturn<raii::rjp_string> create_room(const rexy::string_base& name, const rexy::string_base& alias)const;
|
|
|
|
/*
|
|
* Create a new matrix::roomcxn object for the given roomid.
|
|
* The roomcxn will share login information with this client. Note that changing useragent,
|
|
* access token, or homeserver after creating a roomcxn MUST be synchronized between threads.
|
|
* Returns: a new matrix::roomcxn for the given roomid.
|
|
*/
|
|
roomcxn spawn_room(const rexy::string_base& roomid)const;
|
|
roomcxn spawn_room(rexy::string&& roomid)const;
|
|
|
|
|
|
//NOTE alias equivalents just give the uploaded file a name other than the filename.
|
|
/*
|
|
* Upload a file as a raw file.
|
|
* Takes file data as argument.
|
|
* Returns: file_info struct containing the url of the file on the homeserver.
|
|
* Note check the file_info::fileurl field to check if the upload succeeded.
|
|
*/
|
|
netreturn<uploaded_file> upload_file(const file_details& file)const;
|
|
|
|
/*
|
|
* Upload a file as an image.
|
|
* Returns: image_info struct containing the url of the image on the homeserver.
|
|
* Note check the image_info::fileurl field to check if the upload succeeded.
|
|
*/
|
|
netreturn<uploaded_image> upload_image(const image_details& file)const;
|
|
|
|
/*
|
|
* Upload data as a video.
|
|
* Returns: video_info struct containing the url of the image on the homeserver.
|
|
* Note check the video_info::fileurl field to check if the upload succeeded.
|
|
*/
|
|
netreturn<uploaded_video> upload_video(const video_details& file)const;
|
|
/*
|
|
* Upload a file as an audio file.
|
|
* Returns: audio_info struct containing the url of the image on the homeserver.
|
|
* Note check the audio_info::fileurl field to check if the upload succeeded.
|
|
*/
|
|
netreturn<uploaded_audio> upload_audio(const audio_details& file)const;
|
|
|
|
netreturn<void> create_thumbnail(uploaded_image& info)const;
|
|
netreturn<void> create_thumbnail(uploaded_video& video)const;
|
|
|
|
rexy::binary download_file(const rexy::string_base& url);
|
|
template<class DLHandler>
|
|
bool download_file(const rexy::string_base& url, DLHandler&& dl){
|
|
_get_curl_setup(m_urls->file_download(*m_ses, url));
|
|
m_curl.setwritefun(_download_dispatch<DLHandler>);
|
|
m_curl.setwritedata(&dl);
|
|
if(!_perform_curl())
|
|
return false;
|
|
return true;
|
|
}
|
|
template<class DLHandler>
|
|
bool download_file(const rexy::string_base& server, const rexy::string_base& media_id, DLHandler&& dl){
|
|
rexy::string url = "mxc://"_ss + server + "/"_ss + media_id;
|
|
return download_file(url, std::forward<DLHandler>(dl));
|
|
}
|
|
|
|
netreturn<sync::client_response> sync(size_t timeout);
|
|
netreturn<sync::client_response> sync(size_t timeout, const sync::client_response&);
|
|
|
|
private:
|
|
template<class DLHandler>
|
|
static size_t _download_dispatch(char* ptr, size_t size, size_t nmemb, void* userdata){
|
|
return reinterpret_cast<DLHandler*>(userdata)(ptr, size, nmemb, nullptr);
|
|
}
|
|
netreturn<uploaded_file> _upload_file(const file_details& file, const raii::curl_llist& header)const;
|
|
};
|
|
}
|
|
|
|
#endif
|