libkazv
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1/*
2 * This file is part of libkazv.
3 * SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe>
4 * SPDX-License-Identifier: AGPL-3.0-or-later
5 */
6
7#pragma once
8#include <libkazv-config.hpp>
9
10#include <lager/reader.hpp>
11#include <immer/box.hpp>
12#include <immer/map.hpp>
13#include <immer/flex_vector.hpp>
14#include <immer/flex_vector_transient.hpp>
15
16#include "sdk-model.hpp"
21
22#include "room/room.hpp"
24
25namespace Kazv
26{
59 class Client
60 {
61 public:
63
65#ifdef KAZV_USE_THREAD_SAFETY_HELPER
66 , EventLoopThreadIdKeeper &
67#endif
68
69 >;
72
74
75 struct InEventLoopTag {};
84 Client(lager::reader<SdkModel> sdk,
85 ContextT ctx, std::nullopt_t);
86
97 Client(lager::reader<SdkModel> sdk,
99
111 ContextWithDepsT ctx);
112
124
125
136 Client toEventLoop() const;
137
138 /* lager::reader<immer::map<std::string, Room>> */
139 inline auto rooms() const {
140 return clientCursor()
143 }
144
145 /* lager::reader<RangeT<std::string>> */
146 inline auto roomIds() const {
147 return rooms().xform(
148 zug::map([](auto m) {
149 return intoImmer(
150 immer::flex_vector<std::string>{},
151 zug::map([](auto val) { return val.first; }),
152 m);
153 }));
154 }
155
156 auto roomIdsUnderTag(std::string tagId) const -> lager::reader<immer::map<std::string, double>>;
157
164 auto roomIdsByTagId() const -> lager::reader<immer::map<std::string, immer::map<std::string, double>>>;
165
166 KAZV_WRAP_ATTR(ClientModel, clientCursor(), serverUrl)
167 KAZV_WRAP_ATTR(ClientModel, clientCursor(), loggedIn)
168 KAZV_WRAP_ATTR(ClientModel, clientCursor(), userId)
169 KAZV_WRAP_ATTR(ClientModel, clientCursor(), token)
170 KAZV_WRAP_ATTR(ClientModel, clientCursor(), deviceId)
171 KAZV_WRAP_ATTR(ClientModel, clientCursor(), toDevice)
172
181 Room room(std::string id) const;
182
208 Room roomByCursor(lager::reader<std::string> id) const;
209
227 PromiseT passwordLogin(std::string homeserver, std::string username,
228 std::string password, std::string deviceName) const;
229
245 PromiseT tokenLogin(std::string homeserver, std::string username,
246 std::string token, std::string deviceId) const;
247
264 std::string homeserver,
265 std::string loginToken,
266 std::optional<std::string> deviceName
267 ) const;
268
274 auto shouldSync() const -> lager::reader<bool>;
275
283 PromiseT logout() const;
284
298 PromiseT autoDiscover(std::string userId) const;
299
319 std::optional<std::string> name = {},
320 std::optional<std::string> alias = {},
321 immer::array<std::string> invite = {},
322 std::optional<bool> isDirect = {},
323 bool allowFederate = true,
324 std::optional<std::string> topic = {},
325 JsonWrap powerLevelContentOverride = json::object(),
326 std::optional<CreateRoomPreset> preset = std::nullopt,
327 immer::array<Event> initialState = immer::array<Event>()
328 ) const;
329
337 PromiseT joinRoomById(std::string roomId) const;
338
348 PromiseT joinRoom(std::string roomId, immer::array<std::string> serverName) const;
349
362 PromiseT uploadContent(immer::box<Bytes> content,
363 std::string uploadId,
364 std::optional<std::string> filename = std::nullopt,
365 std::optional<std::string> contentType = std::nullopt) const;
366
376 PromiseT uploadContent(FileDesc file) const;
377
378
389 inline std::string mxcUriToHttp(std::string mxcUri) const {
390 using namespace CursorOp;
391 auto [serverName, mediaId] = mxcUriToMediaDesc(mxcUri);
392 return (+clientCursor())
393 .template job<GetContentJob>()
394 .make(serverName, mediaId).url();
395 }
396
407 std::string mxcUriToHttpV1(std::string mxcUri) const;
408
423 PromiseT downloadContent(std::string mxcUri,
424 std::optional<FileDesc> downloadTo = std::nullopt) const;
425
443 PromiseT downloadThumbnail(std::string mxcUri,
444 int width,
445 int height,
446 std::optional<ThumbnailResizingMethod> method = std::nullopt,
447 std::optional<FileDesc> downloadTo = std::nullopt) const;
448
458 PromiseT getProfile(std::string userId) const;
459
467 PromiseT setAvatarUrl(std::optional<std::string> avatarUrl) const;
468
476 PromiseT setDisplayName(std::optional<std::string> displayName) const;
477
478 // lager::reader<bool>
479 inline auto syncing() const {
480 return clientCursor()[&ClientModel::syncing];
481 }
482
494 PromiseT startSyncing() const;
495
503 PromiseT stopSyncing() const;
504
512 auto devicesOfUser(std::string userId) const -> lager::reader<immer::flex_vector<DeviceKeyInfo>>;
513
522 PromiseT setDeviceTrustLevel(std::string userId, std::string deviceId, DeviceTrustLevel trustLevel) const;
523
529 auto trustLevelNeededToSendKeys() const -> lager::reader<DeviceTrustLevel>;
530
539
545 auto directRoomMap() const -> lager::reader<immer::map<std::string, std::string>>;
546
552 auto accountData() const -> lager::reader<immer::map<std::string, Event>>;
553
560 PromiseT setAccountData(Event accountDataEvent) const;
561
568
585 template<class Archive>
586 void serializeTo(Archive &ar) const {
587 ar << sdkCursor().get();
588 }
589
597 PromiseT getVersions(std::string homeserver) const;
598
606 auto supportVersions() const -> lager::reader<immer::array<std::string>>;
607
616 PromiseT addDirectRoom(std::string userId, std::string roomId) const;
617
625 BaseJob getRoomIdByAliasJob(std::string roomAlias) const;
626
637 PromiseT purgeRoomEvents(immer::map<std::string, std::size_t> roomIdToMaxToKeepMap) const;
638
649 PromiseT loadEventsFromStorage(immer::map<std::string, EventList> timelineEvents, immer::map<std::string, EventList> relatedEvents) const;
650
658 PromiseT importFromKeyBackupFile(std::string fileContent, std::string password) const;
659
660 private:
661 void syncForever(std::optional<int> retryTime = std::nullopt) const;
662
663 const lager::reader<SdkModel> &sdkCursor() const;
664 lager::reader<ClientModel> clientCursor() const;
665
666 std::optional<lager::reader<SdkModel>> m_sdk;
667 std::optional<lager::reader<ClientModel>> m_client;
668 ContextT m_ctx;
669 std::optional<DepsT> m_deps;
670 KAZV_DECLARE_THREAD_ID();
671 KAZV_DECLARE_EVENT_LOOP_THREAD_ID_KEEPER(m_deps.has_value() ? &lager::get<EventLoopThreadIdKeeper &>(m_deps.value()) : 0);
672 };
673
674}
Definition basejob.hpp:68
Represent a Matrix client.
Definition client.hpp:60
PromiseT setAccountData(Event accountDataEvent) const
Set the account data that is not associated with any room.
Definition client.cpp:474
PromiseT setDisplayName(std::optional< std::string > displayName) const
Change the display name of the current user.
Definition client.cpp:418
auto directRoomMap() const -> lager::reader< immer::map< std::string, std::string > >
Get the map from direct messaging room ids to user ids.
Definition client.cpp:452
Room roomByCursor(lager::reader< std::string > id) const
Get the room with id.
Definition client.cpp:77
auto roomIdsByTagId() const -> lager::reader< immer::map< std::string, immer::map< std::string, double > > >
Get the room ids under all tags.
Definition client.cpp:464
PromiseT setTrustLevelNeededToSendKeys(DeviceTrustLevel trustLevel) const
Set the trust level needed to send keys to a device.
Definition client.cpp:447
lager::deps< JobInterface &, EventInterface &, SdkModelCursorKey, RandomInterface & > DepsT
Definition client.hpp:69
PromiseT getVersions(std::string homeserver) const
Get all supported versions.
Definition client.cpp:484
PromiseT joinRoomById(std::string roomId) const
Join a room by its id.
Definition client.cpp:205
PromiseT joinRoom(std::string roomId, immer::array< std::string > serverName) const
Join a room by its id or alias.
Definition client.cpp:210
std::string mxcUriToHttp(std::string mxcUri) const
Convert a MXC URI to an HTTP(s) URI.
Definition client.hpp:389
auto trustLevelNeededToSendKeys() const -> lager::reader< DeviceTrustLevel >
Get the trust level needed to send keys to a device.
Definition client.cpp:442
NotificationHandler notificationHandler() const
Get a notification handler that works on this Client.
Definition client.cpp:479
Client toEventLoop() const
Create a Client that is not constructed from a cursor.
Definition client.cpp:62
auto syncing() const
Definition client.hpp:479
PromiseT setAvatarUrl(std::optional< std::string > avatarUrl) const
Change the avatar url of the current user.
Definition client.cpp:413
PromiseT mLoginTokenLogin(std::string homeserver, std::string loginToken, std::optional< std::string > deviceName) const
Login using a login token.
Definition client.cpp:105
SingleTypePromise< DefaultRetType > PromiseT
Definition client.hpp:73
PromiseT logout() const
Stop syncing and then logout current session.
Definition client.cpp:146
std::string mxcUriToHttpV1(std::string mxcUri) const
Convert a MXC URI to an HTTP(s) URI that needs Authorization.
Definition client.cpp:242
auto roomIds() const
Definition client.hpp:146
PromiseT purgeRoomEvents(immer::map< std::string, std::size_t > roomIdToMaxToKeepMap) const
Purge events in room, keeping the latest numToKeep events.
Definition client.cpp:524
Context< ActionT > ContextT
Definition client.hpp:70
PromiseT downloadContent(std::string mxcUri, std::optional< FileDesc > downloadTo=std::nullopt) const
Download content from the content repository.
Definition client.cpp:250
PromiseT stopSyncing() const
Stop the indefinite syncing.
Definition client.cpp:379
BaseJob getRoomIdByAliasJob(std::string roomAlias) const
Get a GetRoomIdByAliasJob.
Definition client.cpp:519
auto accountData() const -> lager::reader< immer::map< std::string, Event > >
Get the account data that is not associated with any room.
Definition client.cpp:469
void serializeTo(Archive &ar) const
Serialize the model to a Boost.Serialization archive.
Definition client.hpp:586
auto shouldSync() const -> lager::reader< bool >
Get the shouldSync field of current ClientModel.
Definition client.cpp:142
PromiseT loadEventsFromStorage(immer::map< std::string, EventList > timelineEvents, immer::map< std::string, EventList > relatedEvents) const
Load events from storage into the model.
Definition client.cpp:529
PromiseT setDeviceTrustLevel(std::string userId, std::string deviceId, DeviceTrustLevel trustLevel) const
Set the trust level of a device.
Definition client.cpp:437
Room room(std::string id) const
Get the room with id .
Definition client.cpp:68
ClientAction ActionT
Definition client.hpp:62
PromiseT autoDiscover(std::string userId) const
Automatically discover the homeserver for userId.
Definition client.cpp:154
PromiseT addDirectRoom(std::string userId, std::string roomId) const
Mark a room as a direct chat by send the m.direct account data.
Definition client.cpp:494
auto devicesOfUser(std::string userId) const -> lager::reader< immer::flex_vector< DeviceKeyInfo > >
Get the info of all devices of user userId that supports encryption.
Definition client.cpp:423
PromiseT tokenLogin(std::string homeserver, std::string username, std::string token, std::string deviceId) const
Login using token and deviceId.
Definition client.cpp:125
auto supportVersions() const -> lager::reader< immer::array< std::string > >
Get all supported versions.
Definition client.cpp:489
PromiseT importFromKeyBackupFile(std::string fileContent, std::string password) const
Import keys from a key backup file.
Definition client.cpp:537
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.
Definition client.cpp:216
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.
Definition client.cpp:256
PromiseT getProfile(std::string userId) const
Fetch the profile of a user.
Definition client.cpp:408
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.
Definition client.cpp:173
auto roomIdsUnderTag(std::string tagId) const -> lager::reader< immer::map< std::string, double > >
Definition client.cpp:457
PromiseT passwordLogin(std::string homeserver, std::string username, std::string password, std::string deviceName) const
Login using the password.
Definition client.cpp:86
PromiseT startSyncing() const
Start syncing if the Client is not syncing.
Definition client.cpp:267
auto rooms() const
Definition client.hpp:139
Definition context.hpp:130
Definition eventinterface.hpp:15
Definition event.hpp:21
A class to handle a notification.
Definition notification-handler.hpp:22
Definition random-generator.hpp:16
Represent a Matrix room.
Definition room.hpp:38
Definition promise-interface.hpp:122
#define KAZV_WRAP_ATTR(_type, _d, _attr)
Definition clientutil.hpp:152
Definition location.hpp:10
RoomVisibility
Definition client-model.hpp:40
DeviceTrustLevel
Definition device-list-tracker.hpp:27
lager::dep::key< SdkModelCursorTag, lager::dep::fn< std::shared_ptr< lager::reader< SdkModel > > > > SdkModelCursorKey
Definition sdk-model-cursor-tag.hpp:23
std::variant< RoomListAction, LoginAction, TokenLoginAction, MLoginTokenLoginAction, LogoutAction, HardLogoutAction, GetWellknownAction, GetVersionsAction, SyncAction, SetShouldSyncAction, PostInitialFiltersAction, SetAccountDataAction, PaginateTimelineAction, SendMessageAction, SendStateEventAction, SaveLocalEchoAction, UpdateLocalEchoStatusAction, RedactEventAction, CreateRoomAction, GetRoomStatesAction, GetStateEventAction, InviteToRoomAction, JoinRoomByIdAction, JoinRoomAction, LeaveRoomAction, ForgetRoomAction, KickAction, BanAction, UnbanAction, SetAccountDataPerRoomAction, ProcessResponseAction, SetTypingAction, PostReceiptAction, SetReadMarkerAction, UploadContentAction, DownloadContentAction, DownloadThumbnailAction, SendToDeviceMessageAction, SendMultipleToDeviceMessagesAction, UploadIdentityKeysAction, GenerateAndUploadOneTimeKeysAction, QueryKeysAction, ClaimKeysAction, EncryptMegOlmEventAction, SetDeviceTrustLevelAction, SetTrustLevelNeededToSendKeysAction, PrepareForSharingRoomKeyAction, ImportFromKeyBackupFileAction, GetUserProfileAction, SetAvatarUrlAction, SetDisplayNameAction, ResubmitJobAction, LoadEventsFromStorageAction, PurgeRoomTimelineAction > ClientAction
Definition clientfwd.hpp:154
detail::IntoImmerT intoImmer
Definition cursorutil.hpp:88
std::pair< std::string, std::string > mxcUriToMediaDesc(std::string mxcUri)
Definition content.cpp:17
immer::flex_vector< Event > EventList
Definition types.hpp:107
Definition clientutil.hpp:140
Definition client-model.hpp:59
RoomListModel roomList
Definition client-model.hpp:76
bool syncing
Definition client-model.hpp:66
Definition client.hpp:75
Definition jobinterface.hpp:21
immer::map< std::string, RoomModel > rooms
Definition room-model.hpp:410
Definition sdk-model.hpp:26
#define KAZV_DECLARE_EVENT_LOOP_THREAD_ID_KEEPER(_initializer)
Definition thread-safety-helper.hpp:75