libkazv
|
Represent a Matrix client. More...
#include <client.hpp>
Classes | |
struct | InEventLoopTag |
Public Types | |
using | ActionT = ClientAction |
using | DepsT = lager::deps< JobInterface &, EventInterface &, SdkModelCursorKey, RandomInterface & > |
using | ContextT = Context< ActionT > |
using | ContextWithDepsT = Context< ActionT, DepsT > |
using | PromiseT = SingleTypePromise< DefaultRetType > |
Public Member Functions | |
Client (lager::reader< SdkModel > sdk, ContextT ctx, std::nullopt_t) | |
Constructor. More... | |
Client (lager::reader< SdkModel > sdk, ContextWithDepsT ctx) | |
Constructor. More... | |
Client (InEventLoopTag, ContextWithDepsT ctx) | |
Constructor. More... | |
Client (InEventLoopTag, ContextT ctx, DepsT deps) | |
Constructor. More... | |
Client | toEventLoop () const |
Create a Client that is not constructed from a cursor. More... | |
auto | rooms () const |
auto | roomIds () const |
auto | roomIdsUnderTag (std::string tagId) const -> lager::reader< immer::map< std::string, double >> |
auto | roomIdsByTagId () const -> lager::reader< immer::map< std::string, immer::map< std::string, double >>> |
Get the room ids under all tags. More... | |
Room | room (std::string id) const |
Get the room with id . More... | |
Room | roomByCursor (lager::reader< std::string > id) const |
Get the room with id . More... | |
PromiseT | passwordLogin (std::string homeserver, std::string username, std::string password, std::string deviceName) const |
Login using the password. More... | |
PromiseT | tokenLogin (std::string homeserver, std::string username, std::string token, std::string deviceId) const |
Login using token and deviceId . More... | |
auto | shouldSync () const -> lager::reader< bool > |
Get the shouldSync field of current ClientModel. More... | |
PromiseT | logout () const |
Stop syncing and then logout current session. More... | |
PromiseT | autoDiscover (std::string userId) const |
Automatically discover the homeserver for userId . More... | |
PromiseT | createRoom (RoomVisibility v, std::optional< std::string > name={}, std::optional< std::string > alias={}, immer::array< std::string > invite={}, std::optional< bool > isDirect={}, bool allowFederate=true, std::optional< std::string > topic={}, JsonWrap powerLevelContentOverride=json::object(), std::optional< CreateRoomPreset > preset=std::nullopt, immer::array< Event > initialState=immer::array< Event >()) const |
Create a room. More... | |
PromiseT | joinRoomById (std::string roomId) const |
Join a room by its id. More... | |
PromiseT | joinRoom (std::string roomId, immer::array< std::string > serverName) const |
Join a room by its id or alias. More... | |
PromiseT | uploadContent (immer::box< Bytes > content, std::string uploadId, std::optional< std::string > filename=std::nullopt, std::optional< std::string > contentType=std::nullopt) const |
Upload content to the content repository. More... | |
PromiseT | uploadContent (FileDesc file) const |
Upload content to the content repository. More... | |
std::string | mxcUriToHttp (std::string mxcUri) const |
Convert a MXC URI to an HTTP(s) URI. More... | |
PromiseT | downloadContent (std::string mxcUri, std::optional< FileDesc > downloadTo=std::nullopt) const |
Download content from the content repository. More... | |
PromiseT | downloadThumbnail (std::string mxcUri, int width, int height, std::optional< ThumbnailResizingMethod > method=std::nullopt, std::optional< FileDesc > downloadTo=std::nullopt) const |
Download a thumbnail from the content repository. More... | |
PromiseT | getProfile (std::string userId) const |
Fetch the profile of a user. More... | |
PromiseT | setAvatarUrl (std::optional< std::string > avatarUrl) const |
Change the avatar url of the current user. More... | |
PromiseT | setDisplayName (std::optional< std::string > displayName) const |
Change the display name of the current user. More... | |
auto | syncing () const |
PromiseT | startSyncing () const |
Start syncing if the Client is not syncing. More... | |
PromiseT | stopSyncing () const |
Stop the indefinite syncing. More... | |
auto | devicesOfUser (std::string userId) const -> lager::reader< immer::flex_vector< DeviceKeyInfo >> |
Get the info of all devices of user userId that supports encryption. More... | |
PromiseT | setDeviceTrustLevel (std::string userId, std::string deviceId, DeviceTrustLevel trustLevel) const |
Set the trust level of a device. More... | |
auto | trustLevelNeededToSendKeys () const -> lager::reader< DeviceTrustLevel > |
Get the trust level needed to send keys to a device. More... | |
PromiseT | setTrustLevelNeededToSendKeys (DeviceTrustLevel trustLevel) const |
Set the trust level needed to send keys to a device. More... | |
auto | directRoomMap () const -> lager::reader< immer::map< std::string, std::string >> |
Get the map from direct messaging room ids to user ids. More... | |
auto | accountData () const -> lager::reader< immer::map< std::string, Event >> |
Get the account data that is not associated with any room. More... | |
PromiseT | setAccountData (Event accountDataEvent) const |
Set the account data that is not associated with any room. More... | |
NotificationHandler | notificationHandler () const |
Get a notification handler that works on this Client. More... | |
template<class Archive > | |
void | serializeTo (Archive &ar) const |
Serialize the model to a Boost.Serialization archive. More... | |
Represent a Matrix client.
If the Client is constructed from a cursor originated from a root whose event loop is on thread A, then we say that the Client belongs to thread A. If the Client is not constructed from a cursor, then we say that the Client belongs to the thread where the event loop of the context runs.
All methods in this class that take a cursor only take a cursor on the same thread as the Client. All methods in this class that return a cursor will return a cursor on the same thread as the Client.
All methods in this class must be run on the same thread as the the Client. If the Client is not constructed from a cursor, copy-constructing another Client from this is safe from any thread. If the Client is constructed from a cursor, copy-constructing another Client is safe only from the same thread as this Client.
A lot of functions in Client and Room are asynchronous actions. These actions return the result via a Promise. If an API request has failed, the Promise p will satisfy the following:
!p.success()
p.dataStr("error")
will contain the error message from the response.p.dataStr("errorCode")
will contain the matrix error code, if available, or the HTTP status code otherwise.What information is resolved if the API request has succeeded is defined by individual functions.
using Kazv::Client::ActionT = ClientAction |
using Kazv::Client::ContextT = Context<ActionT> |
using Kazv::Client::ContextWithDepsT = Context<ActionT, DepsT> |
using Kazv::Client::DepsT = lager::deps<JobInterface &, EventInterface &, SdkModelCursorKey, RandomInterface & > |
Constructor.
Construct the client. Without Deps support.
Kazv::Client::Client | ( | lager::reader< SdkModel > | sdk, |
ContextWithDepsT | ctx | ||
) |
Constructor.
Construct the client, with Deps support.
This enables startSyncing() to work properly.
Kazv::Client::Client | ( | InEventLoopTag | , |
ContextWithDepsT | ctx | ||
) |
Constructor.
Construct the client, with Deps support.
The constructed Client belongs to the thread of event loop.
Kazv::Client::Client | ( | InEventLoopTag | , |
ContextT | ctx, | ||
DepsT | deps | ||
) |
Constructor.
Construct the client, with Deps support.
The constructed Client belongs to the thread of event loop.
auto Kazv::Client::accountData | ( | ) | const -> lager::reader<immer::map<std::string, Event>> |
Get the account data that is not associated with any room.
auto Kazv::Client::autoDiscover | ( | std::string | userId | ) | const |
Automatically discover the homeserver for userId
.
If the operation succeeds, r.dataStr("homeserverUrl")
will contain the url suitable to pass to tokenLogin()
and passwordLogin()
.
If there is no well-known file (i.e. server responds with 404), r.dataStr("homeserverUrl")
will contain the domain part of the user id (https://example.org
for @foo:example.org
).
userId | The full user id. E.g. @foo:example.org . |
auto Kazv::Client::createRoom | ( | RoomVisibility | v, |
std::optional< std::string > | name = {} , |
||
std::optional< std::string > | alias = {} , |
||
immer::array< std::string > | invite = {} , |
||
std::optional< bool > | isDirect = {} , |
||
bool | allowFederate = true , |
||
std::optional< std::string > | topic = {} , |
||
JsonWrap | powerLevelContentOverride = json::object() , |
||
std::optional< CreateRoomPreset > | preset = std::nullopt , |
||
immer::array< Event > | initialState = immer::array<Event>() |
||
) | const |
Create a room.
v | The visibility of the room. |
name | The name of the room. |
alias | The alias of the room. |
invite | User ids to invite to this room. |
isDirect | Whether this room is a direct chat. |
allowFederate | Whether to allow users from other homeservers to join this room. |
topic | The topic of the room. |
powerLevelContentOverride | The content of the m.room.power_levels state event to override the default. |
preset | The preset to create the room with. |
auto Kazv::Client::devicesOfUser | ( | std::string | userId | ) | const -> lager::reader<immer::flex_vector<DeviceKeyInfo>> |
Get the info of all devices of user userId
that supports encryption.
userId | The id of the user to get the devices of. |
auto Kazv::Client::directRoomMap | ( | ) | const -> lager::reader<immer::map<std::string, std::string>> |
Get the map from direct messaging room ids to user ids.
auto Kazv::Client::downloadContent | ( | std::string | mxcUri, |
std::optional< FileDesc > | downloadTo = std::nullopt |
||
) | const |
Download content from the content repository.
After the returned Promise resolves successfully, if downloadTo
is provided, the content will be available in that file; if it is not provided, r.dataStr("content")
will contain the content of the downloaded file.
mxcUri | The MXC URI of the content. |
downloadTo | The file to write the content to. Must not be an in-memory file. |
auto Kazv::Client::downloadThumbnail | ( | std::string | mxcUri, |
int | width, | ||
int | height, | ||
std::optional< ThumbnailResizingMethod > | method = std::nullopt , |
||
std::optional< FileDesc > | downloadTo = std::nullopt |
||
) | const |
Download a thumbnail from the content repository.
After the returned Promise resolves successfully, if downloadTo
is provided, the content will be available in that file; if it is not provided, r.dataStr("content")
will contain the content of the downloaded file.
mxcUri | The MXC URI of the content. |
width,height | The dimension wanted for the thumbnail |
method | The method to generate the thumbnail. Either Crop or Scale . |
downloadTo | The file to write the content to. Must not be an in-memory file. |
auto Kazv::Client::getProfile | ( | std::string | userId | ) | const |
Fetch the profile of a user.
userId | The id of the user to fetch. |
r.dataStr("avatarUrl")
will contain the avatar url of that user, and r.dataStr("displayName")
will contain the display name of that user. auto Kazv::Client::joinRoom | ( | std::string | roomId, |
immer::array< std::string > | serverName | ||
) | const |
Join a room by its id or alias.
roomId | The id or alias of the room to join. |
serverName | A list of servers to use when joining the room. This corresponds to the via parameter in a matrix.to url. |
auto Kazv::Client::joinRoomById | ( | std::string | roomId | ) | const |
Join a room by its id.
roomId | The id of the room to join. |
auto Kazv::Client::logout | ( | ) | const |
Stop syncing and then logout current session.
Meanwhile, clear the current token and set loggedIn to false.
|
inline |
Convert a MXC URI to an HTTP(s) URI.
The converted URI will be using the homeserver of this Client.
mxcUri | The MXC URI to convert. |
mxcUri
. NotificationHandler Kazv::Client::notificationHandler | ( | ) | const |
auto Kazv::Client::passwordLogin | ( | std::string | homeserver, |
std::string | username, | ||
std::string | password, | ||
std::string | deviceName | ||
) | const |
Login using the password.
This will create a new session on the homeserver.
If the returned Promise resolves successfully, this will call startSyncing()
.
homeserver | The base url of the homeserver. E.g. https://tusooa.xyz . |
username | The username. This can be the full user id or just the local part. E.g. tusooa , @tusooa:tusooa.xyz . |
password | The password. |
deviceName | Optionally, a custom device name. If empty, libkazv will be used. |
Room Kazv::Client::room | ( | std::string | id | ) | const |
Get the room with id
.
This is equivalent to roomByCursor(lager::make_constant(id))
.
id | The room id. |
id
. Room Kazv::Client::roomByCursor | ( | lager::reader< std::string > | id | ) | const |
Get the room with id
.
The Room returned will change as the content in id
changes.
For example, you can have the Room that is always the first alphabetically in all rooms by:
id | A lager::reader<std::string> containing the room id. |
id
.
|
inline |
auto Kazv::Client::roomIdsByTagId | ( | ) | const -> lager::reader<immer::map<std::string, immer::map<std::string, double>>> |
Get the room ids under all tags.
auto Kazv::Client::roomIdsUnderTag | ( | std::string | tagId | ) | const -> lager::reader<immer::map<std::string, double>> |
|
inline |
|
inline |
Serialize the model to a Boost.Serialization archive.
ar | A Boost.Serialization output archive. |
This function can be used to save the model. For loading, you should use the makeSdk function. For example:
auto Kazv::Client::setAccountData | ( | Event | accountDataEvent | ) | const |
Set the account data that is not associated with any room.
auto Kazv::Client::setAvatarUrl | ( | std::optional< std::string > | avatarUrl | ) | const |
Change the avatar url of the current user.
avatarUrl | The url of the new avatar. Should be an MXC URI. If it is std::nullopt, remove the user avatar. |
auto Kazv::Client::setDeviceTrustLevel | ( | std::string | userId, |
std::string | deviceId, | ||
DeviceTrustLevel | trustLevel | ||
) | const |
Set the trust level of a device.
userId | The id of the user to whom the device belongs. |
deviceId | The id of the device. |
auto Kazv::Client::setDisplayName | ( | std::optional< std::string > | displayName | ) | const |
Change the display name of the current user.
displayName | The new display name. If it is std::nullopt, remove the user avatar. |
auto Kazv::Client::setTrustLevelNeededToSendKeys | ( | DeviceTrustLevel | trustLevel | ) | const |
Set the trust level needed to send keys to a device.
trustLevel | The trust level threshold. |
auto Kazv::Client::shouldSync | ( | ) | const -> lager::reader<bool> |
Get the shouldSync
field of current ClientModel.
shouldSync
field of the ClientModel auto Kazv::Client::startSyncing | ( | ) | const |
Start syncing if the Client is not syncing.
Syncing will continue indefinitely, if the preparation of the sync (posting filters and uploading identity keys, if needed) is successful, or until stopSyncing() is called.
auto Kazv::Client::stopSyncing | ( | ) | const |
Stop the indefinite syncing.
After this, no more syncing actions will be dispatched.
|
inline |
Client Kazv::Client::toEventLoop | ( | ) | const |
auto Kazv::Client::tokenLogin | ( | std::string | homeserver, |
std::string | username, | ||
std::string | token, | ||
std::string | deviceId | ||
) | const |
Login using token
and deviceId
.
This will not make a request. Library users should make sure the information is correct and the token and the device id are valid.
If the returned Promise resolves successfully, this will call startSyncing()
.
homeserver | The base url of the homeserver. E.g. https://tusooa.xyz . |
username | The full user id. E.g. @tusooa:tusooa.xyz . |
token | The access token. |
deviceId | The device id that is paired with token . |
auto Kazv::Client::trustLevelNeededToSendKeys | ( | ) | const -> lager::reader<DeviceTrustLevel> |
Get the trust level needed to send keys to a device.
auto Kazv::Client::uploadContent | ( | FileDesc | file | ) | const |
Upload content to the content repository.
file | The file to upload. |
r
, r.dataStr("mxcUri")
will be the MXC URI of the uploaded content. auto Kazv::Client::uploadContent | ( | immer::box< Bytes > | content, |
std::string | uploadId, | ||
std::optional< std::string > | filename = std::nullopt , |
||
std::optional< std::string > | contentType = std::nullopt |
||
) | const |
Upload content to the content repository.
content | The content to upload. |
uploadId | |
filename | The name of the file. |
contentType | The content type of the file. |
r
, r.dataStr("mxcUri")
will be the MXC URI of the uploaded content.