simploxide-client 0.13.1

SimpleX-Chat API client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! FFI backend that embeds the SimpleX-Chat library in-process via native Rust bindings.
//!
//! Use [`BotBuilder`] to initialise the FFI runtime and get a ready-to-use [`Bot`].
//! For lower-level access, [`init`] and [`init_with_config`] return a [`Client`] and an
//! [`EventStream`](crate::EventStream) directly.
//!
//! Requires AGPL-3.0 and additional build configuration. See `simploxide-sxcrt-sys`.

pub use simploxide_ffi_core::{
    CallError, DbOpts, DefaultUser, Event as CoreEvent, InitError as CoreInitError, RawClient,
    Result as CoreResult, SimplexVersion, VersionError, WorkerConfig,
};

use simploxide_api_types::{
    Preferences, Profile,
    client_api::{ExtractResponse as _, FfiResponseShape},
    events::{Event, EventKind},
};
use simploxide_core::{MAX_SUPPORTED_VERSION, MIN_SUPPORTED_VERSION};

use std::sync::Arc;

use crate::{
    BadResponseError, ClientApi, ClientApiError, EventParser,
    bot::{BotProfileSettings, BotSettings},
    id::UserId,
    preview::ImagePreview,
    util,
};

pub type EventResult = CoreResult<CoreEvent>;
pub type EventStream = crate::EventStream<EventResult>;
pub type ClientResult<T = ()> = ::std::result::Result<T, ClientError>;

#[cfg(not(feature = "xftp"))]
pub type Bot = crate::bot::Bot<Client>;

#[cfg(feature = "xftp")]
pub type Bot = crate::bot::Bot<crate::xftp::XftpClient<Client>>;

#[cfg(feature = "farm")]
pub type FarmBot = crate::bot::farm::FarmBot<Client>;

#[cfg(feature = "farm")]
pub type InitFarm = crate::bot::farm::InitFarm<Client, EventResult>;

#[cfg(feature = "farm")]
pub type RunningFarm = crate::bot::farm::RunningFarm<Client, EventResult>;

pub async fn init(
    default_user: DefaultUser,
    db_opts: DbOpts,
) -> Result<(Client, EventStream), InitError> {
    init_with_config(default_user, db_opts, WorkerConfig::default()).await
}

pub async fn init_with_config(
    default_user: DefaultUser,
    db_opts: DbOpts,
    config: WorkerConfig,
) -> Result<(Client, EventStream), InitError> {
    let (raw_client, raw_event_queue) =
        simploxide_ffi_core::init_with_config(default_user, db_opts, config).await?;

    let version = raw_client
        .version()
        .await
        .map_err(InitError::VersionError)?;

    if !version.is_supported() {
        return Err(InitError::VersionMismatch(version));
    }

    Ok((
        Client::from(raw_client),
        EventStream::from(raw_event_queue.into_receiver()),
    ))
}

/// A cheaply clonable high-level FFI client implementing [`ClientApi`]
#[derive(Clone)]
pub struct Client {
    inner: RawClient,
}

impl From<RawClient> for Client {
    fn from(inner: RawClient) -> Self {
        Self { inner }
    }
}

/// A high level SimpleX-Chat client which provides typed API methods with automatic command
/// serialization and response deserialization.
impl Client {
    pub fn version(&self) -> impl Future<Output = Result<SimplexVersion, VersionError>> {
        self.inner.version()
    }

    /// Initiates a graceful shutdown for the underlying web socket connection. See
    /// [`simploxide_ffi_core::RawClient::disconnect`] for details.
    pub fn disconnect(self) -> impl Future<Output = ()> {
        self.inner.disconnect()
    }
}

impl ClientApi for Client {
    type ResponseShape<'de, T>
        = FfiResponseShape<T>
    where
        T: 'de + serde::Deserialize<'de>;

    type Error = ClientError;

    async fn send_raw(&self, command: String) -> Result<String, Self::Error> {
        self.inner
            .send(command)
            .await
            .map_err(ClientError::FfiFailure)
    }
}

impl EventParser for EventResult {
    type Error = ClientError;

    fn parse_kind(&self) -> Result<EventKind, Self::Error> {
        match parse_data::<util::TypeField<'_>>(self) {
            Ok(f) => Ok(EventKind::from_type_str(f.typ)),
            // FFI chat error shapes are the same for events and responses which confuses the parser therefore
            // chat errors must be handled manually
            Err(ClientError::BadResponse(BadResponseError::ChatError(_))) => {
                Ok(EventKind::ChatError)
            }
            Err(ClientError::BadResponse(BadResponseError::Undocumented(_))) => {
                Ok(EventKind::Undocumented)
            }
            Err(e) => Err(e),
        }
    }

    fn parse_user_id(&self) -> Result<Option<UserId>, Self::Error> {
        match parse_data::<util::UserField>(self) {
            Ok(f) => Ok(UserId::try_from(f.user.user_id).ok()),
            Err(ClientError::BadResponse(_)) => Ok(None),
            Err(e) => Err(e),
        }
    }

    fn parse_event(&self) -> Result<Event, Self::Error> {
        match parse_data(self) {
            Ok(ev) => Ok(ev),
            // FFI chat error shapes are the same for events and responses which confuses the parser therefore
            // chat errors must be handled manually
            Err(ClientError::BadResponse(BadResponseError::ChatError(err))) => Ok(
                Event::ChatError(Arc::new(simploxide_api_types::events::ChatError {
                    chat_error: err.as_ref().clone(),
                    undocumented: Default::default(),
                })),
            ),
            Err(ClientError::BadResponse(BadResponseError::Undocumented(json))) => {
                Ok(Event::Undocumented(json))
            }
            Err(e) => Err(e),
        }
    }
}

fn parse_data<'de, 'r: 'de, D: 'de + serde::Deserialize<'de>>(
    result: &'r EventResult,
) -> Result<D, ClientError> {
    result
        .as_ref()
        .map_err(|e| ClientError::FfiFailure(e.clone()))
        .and_then(|ev| {
            serde_json::from_str::<FfiResponseShape<D>>(ev)
                .map_err(BadResponseError::InvalidJson)
                .and_then(|shape| shape.extract_response())
                .map_err(ClientError::BadResponse)
        })
}

/// Builder for an FFI-backed [`Bot`].
#[derive(Clone)]
pub struct BotBuilder {
    display_name: String,
    db_opts: DbOpts,
    default_user: Option<DefaultUser>,
    auto_accept: Option<String>,
    profile: Option<Profile>,
    preferences: Option<Preferences>,
    avatar: Option<ImagePreview>,
    worker_config: WorkerConfig,
}

impl BotBuilder {
    /// Build a bot account (default).
    pub fn new(name: impl Into<String>, db_opts: DbOpts) -> Self {
        Self {
            display_name: name.into(),
            db_opts,
            default_user: None,
            auto_accept: None,
            profile: None,
            preferences: None,
            avatar: None,
            worker_config: WorkerConfig::default(),
        }
    }

    /// Override the default user created for empty databases.
    ///
    /// By default the default user name matches the bot name. This setting allows to create a user
    /// different from an active bot
    pub fn with_default_user(mut self, user: DefaultUser) -> Self {
        self.default_user = Some(user);
        self
    }

    /// Create public address and auto accept users
    pub fn auto_accept(mut self) -> Self {
        self.auto_accept = Some(String::default());
        self
    }

    /// [Self::auto_accept] with a welcome message
    pub fn auto_accept_with(mut self, welcome_message: impl Into<String>) -> Self {
        self.auto_accept = Some(welcome_message.into());
        self
    }

    /// Set the bot avatar during initialisation
    pub fn with_avatar(mut self, avatar: ImagePreview) -> Self {
        self.avatar = Some(avatar);
        self
    }

    /// Update/create the whole bot profile on launch
    pub fn with_profile(mut self, profile: Profile) -> Self {
        self.profile = Some(profile);
        self
    }

    /// Apply these preferences to the bot's profile during initialisation.
    pub fn with_preferences(mut self, prefs: Preferences) -> Self {
        self.preferences = Some(prefs);
        self
    }

    /// Set max permissible event latency. See [`WorkerConfig::max_event_latency`] for details
    pub fn max_event_latency(mut self, latency: std::time::Duration) -> Self {
        self.worker_config.max_event_latency = Some(latency);
        self
    }

    /// Set max concurrent SimpleX-Chat instances. See [`WorkerConfig::max_instances`] for details
    pub fn max_instances(mut self, instances: usize) -> Self {
        self.worker_config.max_instances = Some(instances);
        self
    }

    /// Initialise the SimpleX FFI runtime and return a ready-to-use bot.
    pub async fn launch(self) -> Result<(Bot, EventStream), BotInitError> {
        let default_user = self
            .default_user
            .unwrap_or_else(|| DefaultUser::bot(&self.display_name));

        let (client, events) = init_with_config(default_user, self.db_opts, self.worker_config)
            .await
            .map_err(BotInitError::Init)?;

        #[cfg(feature = "xftp")]
        let (client, events) = events.hook_xftp(client);

        let settings = BotSettings {
            display_name: self.display_name,
            auto_accept: self.auto_accept,
            profile_settings: match (self.profile, self.preferences) {
                (Some(mut profile), Some(preferences)) => {
                    profile.preferences = Some(preferences);
                    Some(BotProfileSettings::FullProfile(profile))
                }
                (Some(profile), None) => Some(BotProfileSettings::FullProfile(profile)),
                (None, Some(preferences)) => Some(BotProfileSettings::Preferences(preferences)),
                (None, None) => None,
            },
            avatar: self.avatar,
        };

        let bot = Bot::init(client, settings).await?;

        let mut events = events;
        events.set_owner(bot.user_id());

        Ok((bot, events))
    }
}

#[cfg(feature = "farm")]
#[derive(Clone)]
pub struct BotFarmBuilder {
    display_name: String,
    db_opts: DbOpts,
    default_user: Option<DefaultUser>,
    worker_config: WorkerConfig,
}

#[cfg(feature = "farm")]
impl BotFarmBuilder {
    pub fn new(name: impl Into<String>, db_opts: DbOpts) -> Self {
        Self {
            display_name: name.into(),
            db_opts,
            default_user: None,
            worker_config: WorkerConfig::default(),
        }
    }

    /// Override the default user created for empty databases.
    ///
    /// By default the default user name matches the bot name. This setting allows to create a user
    /// different from an active bot
    pub fn with_default_user(mut self, user: DefaultUser) -> Self {
        self.default_user = Some(user);
        self
    }

    /// Set max permissible event latency. See [`WorkerConfig::max_event_latency`] for details
    pub fn max_event_latency(mut self, latency: std::time::Duration) -> Self {
        self.worker_config.max_event_latency = Some(latency);
        self
    }

    /// Set max concurrent SimpleX-Chat instances. See [`WorkerConfig::max_instances`] for details
    pub fn max_instances(mut self, instances: usize) -> Self {
        self.worker_config.max_instances = Some(instances);
        self
    }

    /// Initialise the SimpleX FFI runtime and return a farm
    pub async fn launch(self) -> Result<InitFarm, BotInitError> {
        let default_user = self
            .default_user
            .unwrap_or_else(|| DefaultUser::bot(&self.display_name));

        let (client, events) = init_with_config(default_user, self.db_opts, self.worker_config)
            .await
            .map_err(BotInitError::Init)?;

        let bot = crate::bot::BotFarm::init(self.display_name, client, events).await?;
        Ok(bot)
    }
}

/// See [`crate::client_api::AllowUndocumentedResponses`] if you don't want to trigger an error
/// when you receive undocumeted responses(you usually receive undocumented responses when your
/// simplex-chat version is not compatible with the current simploxide-client version. Keep an eye
/// on the [Version compatability table](https://github.com/a1akris/simploxide?tab=readme-ov-file#version-compatability-table))
#[derive(Debug)]
pub enum ClientError {
    FfiFailure(Arc<CallError>),
    BadResponse(BadResponseError),
}

impl std::error::Error for ClientError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::FfiFailure(error) => Some(error),
            Self::BadResponse(error) => Some(error),
        }
    }
}

impl std::fmt::Display for ClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ClientError::FfiFailure(err) => writeln!(f, "FFI error: {err}"),
            ClientError::BadResponse(err) => err.fmt(f),
        }
    }
}

impl From<BadResponseError> for ClientError {
    fn from(err: BadResponseError) -> Self {
        Self::BadResponse(err)
    }
}

impl ClientApiError for ClientError {
    fn bad_response(&self) -> Option<&BadResponseError> {
        if let Self::BadResponse(resp) = self {
            Some(resp)
        } else {
            None
        }
    }

    fn bad_response_mut(&mut self) -> Option<&mut BadResponseError> {
        if let Self::BadResponse(resp) = self {
            Some(resp)
        } else {
            None
        }
    }
}

#[derive(Debug)]
pub enum InitError {
    /// Failure to init the FFI instance
    Ffi(CoreInitError),
    /// Failure to get the backend version
    VersionError(VersionError),
    /// Unsupported backend version
    VersionMismatch(SimplexVersion),
}

impl InitError {
    pub fn is_ffi(&self) -> bool {
        matches!(self, Self::Ffi(_))
    }

    pub fn is_version_mismatch(&self) -> bool {
        matches!(self, Self::VersionMismatch(_))
    }
}

impl From<CoreInitError> for InitError {
    fn from(value: CoreInitError) -> Self {
        Self::Ffi(value)
    }
}

impl std::fmt::Display for InitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Ffi(error) => write!(f, "Cannot initialize the FFI backend: {error}"),
            Self::VersionError(error) => write!(f, "Cannot get FFI version {error}"),
            Self::VersionMismatch(v) => write!(
                f,
                "Version {v} is unsupported by the current client. Supported versions are {MIN_SUPPORTED_VERSION}..{MAX_SUPPORTED_VERSION}"
            ),
        }
    }
}

impl std::error::Error for InitError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Ffi(error) => Some(error),
            Self::VersionError(error) => Some(error),
            Self::VersionMismatch(_) => None,
        }
    }
}

/// Error returned by [`BotBuilder::launch`].
#[derive(Debug)]
pub enum BotInitError {
    Init(InitError),
    Api(ClientError),
}

impl std::fmt::Display for BotInitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Init(e) => write!(f, "SimpleX FFI init failed: {e}"),
            Self::Api(e) => write!(f, "SimpleX API error during init: {e}"),
        }
    }
}

impl std::error::Error for BotInitError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Init(e) => Some(e),
            Self::Api(e) => Some(e),
        }
    }
}

impl From<ClientError> for BotInitError {
    fn from(e: ClientError) -> Self {
        Self::Api(e)
    }
}