tele 0.1.22

Ergonomic Telegram Bot API SDK for Rust, built on reqx
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
use super::*;

/// State transition result for a finite-state machine.
#[derive(Clone, Debug)]
pub enum StateTransition<S> {
    Keep,
    Set(S),
    Clear,
}

/// Abstract async session-state store.
pub trait SessionStore<S>: Send + Sync + 'static
where
    S: Clone + Send + Sync + 'static,
{
    fn load<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, Option<S>>;
    fn save<'a>(&'a self, chat_id: i64, state: S) -> SessionFuture<'a, ()>;
    fn clear<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, ()>;
}

/// In-memory session store for prototyping and small bots.
pub struct InMemorySessionStore<S>
where
    S: Clone + Send + Sync + 'static,
{
    inner: Arc<RwLock<HashMap<i64, S>>>,
}

impl<S> Clone for InMemorySessionStore<S>
where
    S: Clone + Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<S> Default for InMemorySessionStore<S>
where
    S: Clone + Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<S> InMemorySessionStore<S>
where
    S: Clone + Send + Sync + 'static,
{
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl<S> SessionStore<S> for InMemorySessionStore<S>
where
    S: Clone + Send + Sync + 'static,
{
    fn load<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, Option<S>> {
        Box::pin(async move {
            let guard = self.inner.read().await;
            Ok(guard.get(&chat_id).cloned())
        })
    }

    fn save<'a>(&'a self, chat_id: i64, state: S) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let mut guard = self.inner.write().await;
            guard.insert(chat_id, state);
            Ok(())
        })
    }

    fn clear<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let mut guard = self.inner.write().await;
            guard.remove(&chat_id);
            Ok(())
        })
    }
}

/// JSON-file backed session store for bots that need process restart recovery.
pub struct JsonFileSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    path: PathBuf,
    inner: Arc<RwLock<HashMap<i64, S>>>,
    persist_lock: Arc<Mutex<()>>,
}

impl<S> Clone for JsonFileSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            inner: Arc::clone(&self.inner),
            persist_lock: Arc::clone(&self.persist_lock),
        }
    }
}

impl<S> JsonFileSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref().to_path_buf();
        let initial = load_session_snapshot::<S>(&path)?;
        Ok(Self {
            path,
            inner: Arc::new(RwLock::new(initial)),
            persist_lock: Arc::new(Mutex::new(())),
        })
    }

    pub fn path(&self) -> &Path {
        self.path.as_path()
    }
}

impl<S> SessionStore<S> for JsonFileSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    fn load<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, Option<S>> {
        Box::pin(async move {
            let guard = self.inner.read().await;
            Ok(guard.get(&chat_id).cloned())
        })
    }

    fn save<'a>(&'a self, chat_id: i64, state: S) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let _persist_guard = self.persist_lock.lock().await;
            let mut snapshot = {
                let guard = self.inner.read().await;
                guard.clone()
            };
            snapshot.insert(chat_id, state);
            persist_session_snapshot_async(self.path.clone(), snapshot.clone()).await?;
            let mut guard = self.inner.write().await;
            *guard = snapshot;
            Ok(())
        })
    }

    fn clear<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let _persist_guard = self.persist_lock.lock().await;
            let mut snapshot = {
                let guard = self.inner.read().await;
                guard.clone()
            };
            snapshot.remove(&chat_id);
            persist_session_snapshot_async(self.path.clone(), snapshot.clone()).await?;
            let mut guard = self.inner.write().await;
            *guard = snapshot;
            Ok(())
        })
    }
}

fn load_session_snapshot<S>(path: &Path) -> Result<HashMap<i64, S>>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    if !path.exists() {
        return Ok(HashMap::new());
    }

    let raw = fs::read(path).map_err(|source| {
        storage_error(
            "json session read",
            format!(
                "failed to read session store `{}`: {source}",
                path.display()
            ),
            true,
        )
    })?;

    if raw.is_empty() {
        return Ok(HashMap::new());
    }

    serde_json::from_slice(&raw).map_err(|source| {
        storage_error(
            "json session load",
            format!(
                "failed to deserialize session store `{}`: {source}",
                path.display()
            ),
            false,
        )
    })
}

fn persist_session_snapshot<S>(path: &Path, snapshot: &HashMap<i64, S>) -> Result<()>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    let encoded =
        serde_json::to_vec(snapshot).map_err(|source| Error::SerializeRequest { source })?;
    write_file_atomic(path, encoded.as_slice(), "session store")?;
    Ok(())
}

async fn persist_session_snapshot_async<S>(path: PathBuf, snapshot: HashMap<i64, S>) -> Result<()>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    run_blocking_io(move || persist_session_snapshot(path.as_path(), &snapshot)).await
}

#[cfg(feature = "redis-session")]
/// Redis-backed session store for distributed bot deployments.
pub struct RedisSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    client: redis::Client,
    namespace: String,
    _state: std::marker::PhantomData<S>,
}

#[cfg(feature = "redis-session")]
impl<S> Clone for RedisSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            namespace: self.namespace.clone(),
            _state: std::marker::PhantomData,
        }
    }
}

#[cfg(feature = "redis-session")]
impl<S> RedisSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    pub fn new(redis_url: &str, namespace: impl Into<String>) -> Result<Self> {
        let namespace = namespace.into();
        validate_redis_namespace(&namespace)?;

        let client = redis::Client::open(redis_url).map_err(|source| {
            invalid_request(format!(
                "failed to create redis client `{}`: {source}",
                crate::util::redact_url_credentials(redis_url)
            ))
        })?;

        Ok(Self {
            client,
            namespace,
            _state: std::marker::PhantomData,
        })
    }

    pub fn namespace(&self) -> &str {
        self.namespace.as_str()
    }

    fn session_key(&self, chat_id: i64) -> String {
        format!("{}:{chat_id}", self.namespace)
    }

    async fn connection(&self) -> Result<redis::aio::MultiplexedConnection> {
        self.client
            .get_multiplexed_async_connection()
            .await
            .map_err(|source| storage_error("redis connect", source.to_string(), true))
    }
}

#[cfg(feature = "redis-session")]
fn validate_redis_namespace(namespace: &str) -> Result<()> {
    if namespace.is_empty() || namespace.chars().any(char::is_whitespace) {
        return Err(invalid_request(
            "redis session namespace must be non-empty and contain no whitespace",
        ));
    }
    if namespace.chars().any(char::is_control) {
        return Err(invalid_request(
            "redis session namespace must not contain control characters",
        ));
    }

    Ok(())
}

#[cfg(feature = "redis-session")]
impl<S> SessionStore<S> for RedisSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    fn load<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, Option<S>> {
        Box::pin(async move {
            let key = self.session_key(chat_id);
            let mut connection = self.connection().await?;
            let payload: Option<String> = redis::cmd("GET")
                .arg(&key)
                .query_async(&mut connection)
                .await
                .map_err(|source| {
                    storage_error("redis GET", format!("key `{key}` failed: {source}"), true)
                })?;

            let Some(payload) = payload else {
                return Ok(None);
            };

            let state = serde_json::from_str::<S>(&payload).map_err(|source| {
                storage_error(
                    "redis decode",
                    format!("redis state decode failed for key `{key}`: {source}"),
                    false,
                )
            })?;
            Ok(Some(state))
        })
    }

    fn save<'a>(&'a self, chat_id: i64, state: S) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let key = self.session_key(chat_id);
            let payload = serde_json::to_string(&state)
                .map_err(|source| Error::SerializeRequest { source })?;
            let mut connection = self.connection().await?;
            let _: () = redis::cmd("SET")
                .arg(&key)
                .arg(&payload)
                .query_async(&mut connection)
                .await
                .map_err(|source| {
                    storage_error("redis SET", format!("key `{key}` failed: {source}"), true)
                })?;
            Ok(())
        })
    }

    fn clear<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let key = self.session_key(chat_id);
            let mut connection = self.connection().await?;
            let _: i64 = redis::cmd("DEL")
                .arg(&key)
                .query_async(&mut connection)
                .await
                .map_err(|source| {
                    storage_error("redis DEL", format!("key `{key}` failed: {source}"), true)
                })?;
            Ok(())
        })
    }
}

#[cfg(feature = "postgres-session")]
/// Postgres-backed session store for durable multi-instance bots.
pub struct PostgresSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    pool: sqlx::PgPool,
    table: String,
    _state: std::marker::PhantomData<S>,
}

#[cfg(feature = "postgres-session")]
impl<S> Clone for PostgresSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    fn clone(&self) -> Self {
        Self {
            pool: self.pool.clone(),
            table: self.table.clone(),
            _state: std::marker::PhantomData,
        }
    }
}

#[cfg(feature = "postgres-session")]
impl<S> PostgresSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    pub async fn connect(database_url: &str, table: impl Into<String>) -> Result<Self> {
        let table = table.into();
        validate_sql_identifier(&table)?;

        let pool = sqlx::postgres::PgPoolOptions::new()
            .connect(database_url)
            .await
            .map_err(|source| {
                storage_error(
                    "postgres connect",
                    format!(
                        "failed to connect postgres `{}`: {source}",
                        crate::util::redact_url_credentials(database_url)
                    ),
                    true,
                )
            })?;

        Self::with_pool(pool, table).await
    }

    pub async fn with_pool(pool: sqlx::PgPool, table: impl Into<String>) -> Result<Self> {
        let table = table.into();
        validate_sql_identifier(&table)?;

        let table_sql = quote_sql_identifier(&table);
        let create = format!(
            "CREATE TABLE IF NOT EXISTS {table_sql} (chat_id BIGINT PRIMARY KEY, state JSONB NOT NULL)"
        );
        sqlx::query(&create)
            .execute(&pool)
            .await
            .map_err(|source| {
                storage_error(
                    "postgres migrate",
                    format!("failed to create postgres session table `{table}`: {source}"),
                    false,
                )
            })?;

        Ok(Self {
            pool,
            table,
            _state: std::marker::PhantomData,
        })
    }

    pub fn table(&self) -> &str {
        self.table.as_str()
    }

    pub fn pool(&self) -> &sqlx::PgPool {
        &self.pool
    }
}

#[cfg(feature = "postgres-session")]
impl<S> SessionStore<S> for PostgresSessionStore<S>
where
    S: Clone + Send + Sync + Serialize + DeserializeOwned + 'static,
{
    fn load<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, Option<S>> {
        Box::pin(async move {
            use sqlx::Row as _;

            let table = quote_sql_identifier(&self.table);
            let query = format!("SELECT state FROM {table} WHERE chat_id = $1");
            let row = sqlx::query(&query)
                .bind(chat_id)
                .fetch_optional(&self.pool)
                .await
                .map_err(|source| {
                    storage_error(
                        "postgres load",
                        format!("postgres load failed for chat_id `{chat_id}`: {source}"),
                        true,
                    )
                })?;

            let Some(row) = row else {
                return Ok(None);
            };

            let sqlx::types::Json(state) = row.try_get(0).map_err(|source| {
                storage_error(
                    "postgres decode",
                    format!(
                        "postgres session payload decode failed for chat_id `{chat_id}`: {source}"
                    ),
                    false,
                )
            })?;

            Ok(Some(state))
        })
    }

    fn save<'a>(&'a self, chat_id: i64, state: S) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let table = quote_sql_identifier(&self.table);
            let query = format!(
                "INSERT INTO {table} (chat_id, state) VALUES ($1, $2) \
                 ON CONFLICT (chat_id) DO UPDATE SET state = EXCLUDED.state",
            );
            sqlx::query(&query)
                .bind(chat_id)
                .bind(sqlx::types::Json(state))
                .execute(&self.pool)
                .await
                .map_err(|source| {
                    storage_error(
                        "postgres save",
                        format!("postgres save failed for chat_id `{chat_id}`: {source}"),
                        true,
                    )
                })?;
            Ok(())
        })
    }

    fn clear<'a>(&'a self, chat_id: i64) -> SessionFuture<'a, ()> {
        Box::pin(async move {
            let table = quote_sql_identifier(&self.table);
            let query = format!("DELETE FROM {table} WHERE chat_id = $1");
            sqlx::query(&query)
                .bind(chat_id)
                .execute(&self.pool)
                .await
                .map_err(|source| {
                    storage_error(
                        "postgres clear",
                        format!("postgres clear failed for chat_id `{chat_id}`: {source}"),
                        true,
                    )
                })?;
            Ok(())
        })
    }
}

#[cfg(feature = "postgres-session")]
fn validate_sql_identifier(identifier: &str) -> Result<()> {
    let mut chars = identifier.chars();
    let Some(first) = chars.next() else {
        return Err(invalid_request("sql identifier cannot be empty"));
    };

    if !(first.is_ascii_alphabetic() || first == '_') {
        return Err(invalid_request(format!(
            "sql identifier `{identifier}` must start with [A-Za-z_]"
        )));
    }

    if !chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_') {
        return Err(invalid_request(format!(
            "sql identifier `{identifier}` contains invalid characters"
        )));
    }

    Ok(())
}

#[cfg(feature = "postgres-session")]
fn quote_sql_identifier(identifier: &str) -> String {
    format!("\"{identifier}\"")
}

/// Loads chat-scoped state from a store.
pub async fn load_chat_state<S, Store>(store: &Store, update: &Update) -> Result<Option<S>>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S> + ?Sized,
{
    store.load(chat_id_for_state(update)?).await
}

/// Saves chat-scoped state into a store.
pub async fn save_chat_state<S, Store>(store: &Store, update: &Update, state: S) -> Result<()>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S> + ?Sized,
{
    store.save(chat_id_for_state(update)?, state).await
}

/// Clears chat-scoped state from a store.
pub async fn clear_chat_state<S, Store>(store: &Store, update: &Update) -> Result<()>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S> + ?Sized,
{
    store.clear(chat_id_for_state(update)?).await
}

/// Applies an FSM transition to chat-scoped state.
pub async fn apply_chat_state_transition<S, Store>(
    store: &Store,
    update: &Update,
    transition: StateTransition<S>,
) -> Result<()>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S> + ?Sized,
{
    let chat_id = chat_id_for_state(update)?;
    match transition {
        StateTransition::Keep => Ok(()),
        StateTransition::Set(state) => store.save(chat_id, state).await,
        StateTransition::Clear => store.clear(chat_id).await,
    }
}

fn chat_id_for_state(update: &Update) -> Result<i64> {
    update_chat_id(update)
        .ok_or_else(|| invalid_request("update does not contain a chat id for state operations"))
}

async fn apply_chat_state_transition_for_chat_id<S, Store>(
    store: &Store,
    chat_id: i64,
    transition: StateTransition<S>,
) -> Result<()>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S> + ?Sized,
{
    match transition {
        StateTransition::Keep => Ok(()),
        StateTransition::Set(state) => store.save(chat_id, state).await,
        StateTransition::Clear => store.clear(chat_id).await,
    }
}

#[derive(Clone)]
struct ChatSessionLocks {
    inner: Arc<ChatSessionLockMap>,
}

type ChatSessionLockMap = Mutex<HashMap<i64, Arc<Mutex<()>>>>;
type ChatSessionLockRegistry =
    std::sync::Mutex<HashMap<usize, std::sync::Weak<ChatSessionLockMap>>>;

fn chat_session_lock_registry() -> &'static ChatSessionLockRegistry {
    static REGISTRY: std::sync::OnceLock<ChatSessionLockRegistry> = std::sync::OnceLock::new();
    REGISTRY.get_or_init(|| std::sync::Mutex::new(HashMap::new()))
}

fn chat_session_store_key<Store>(store: &Arc<Store>) -> usize {
    Arc::as_ptr(store) as *const () as usize
}

impl ChatSessionLocks {
    fn for_store<Store>(store: &Arc<Store>) -> Self {
        let key = chat_session_store_key(store);
        let mut registry = chat_session_lock_registry()
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());

        if let Some(inner) = registry.get(&key).and_then(std::sync::Weak::upgrade) {
            return Self { inner };
        }

        registry.retain(|_, locks| locks.strong_count() > 0);
        let inner = Arc::new(Mutex::new(HashMap::new()));
        registry.insert(key, Arc::downgrade(&inner));
        Self { inner }
    }

    async fn acquire(&self, chat_id: i64) -> ChatSessionLockGuard {
        let lock = {
            let mut locks = self.inner.lock().await;
            locks
                .entry(chat_id)
                .or_insert_with(|| Arc::new(Mutex::new(())))
                .clone()
        };
        let guard = lock.lock_owned().await;
        ChatSessionLockGuard { _guard: guard }
    }

    async fn prune_idle(&self, chat_id: i64) {
        let mut locks = self.inner.lock().await;
        if locks
            .get(&chat_id)
            .is_some_and(|lock| Arc::strong_count(lock) == 1)
        {
            let _ = locks.remove(&chat_id);
        }
    }
}

struct ChatSessionLockGuard {
    _guard: tokio::sync::OwnedMutexGuard<()>,
}

/// High-level chat-scoped session manager for FSM-style bots.
pub struct ChatSession<S, Store>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S>,
{
    store: Arc<Store>,
    locks: ChatSessionLocks,
    _state: std::marker::PhantomData<S>,
}

impl<S, Store> Clone for ChatSession<S, Store>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S>,
{
    fn clone(&self) -> Self {
        Self {
            store: Arc::clone(&self.store),
            locks: self.locks.clone(),
            _state: std::marker::PhantomData,
        }
    }
}

impl<S, Store> ChatSession<S, Store>
where
    S: Clone + Send + Sync + 'static,
    Store: SessionStore<S>,
{
    pub fn new(store: Store) -> Self {
        let store = Arc::new(store);
        let locks = ChatSessionLocks::for_store(&store);
        Self {
            store,
            locks,
            _state: std::marker::PhantomData,
        }
    }

    pub fn from_shared(store: Arc<Store>) -> Self {
        let locks = ChatSessionLocks::for_store(&store);
        Self {
            store,
            locks,
            _state: std::marker::PhantomData,
        }
    }

    pub fn store(&self) -> &Store {
        self.store.as_ref()
    }

    pub fn shared_store(&self) -> Arc<Store> {
        Arc::clone(&self.store)
    }

    pub async fn load(&self, update: &Update) -> Result<Option<S>> {
        load_chat_state(self.store(), update).await
    }

    pub async fn save(&self, update: &Update, state: S) -> Result<()> {
        let chat_id = chat_id_for_state(update)?;
        let guard = self.locks.acquire(chat_id).await;
        let result = self.store.save(chat_id, state).await;
        drop(guard);
        self.locks.prune_idle(chat_id).await;
        result
    }

    pub async fn clear(&self, update: &Update) -> Result<()> {
        let chat_id = chat_id_for_state(update)?;
        let guard = self.locks.acquire(chat_id).await;
        let result = self.store.clear(chat_id).await;
        drop(guard);
        self.locks.prune_idle(chat_id).await;
        result
    }

    pub async fn apply(&self, update: &Update, transition: StateTransition<S>) -> Result<()> {
        let chat_id = chat_id_for_state(update)?;
        let guard = self.locks.acquire(chat_id).await;
        let result =
            apply_chat_state_transition_for_chat_id(self.store(), chat_id, transition).await;
        drop(guard);
        self.locks.prune_idle(chat_id).await;
        result
    }

    /// Loads state, runs transition function, then applies resulting state transition.
    ///
    /// Transitions are serialized per chat id, so concurrent updates for the same chat cannot
    /// overwrite each other's load-modify-save cycle. Different chats still progress independently.
    pub async fn transition<R, F, Fut>(&self, update: &Update, f: F) -> Result<R>
    where
        F: FnOnce(Option<S>) -> Fut + Send,
        Fut: Future<Output = (R, StateTransition<S>)> + Send,
    {
        let chat_id = chat_id_for_state(update)?;
        let guard = self.locks.acquire(chat_id).await;
        let current = self.store.load(chat_id).await?;
        let (output, transition) = f(current).await;
        let result =
            apply_chat_state_transition_for_chat_id(self.store(), chat_id, transition).await;
        drop(guard);
        self.locks.prune_idle(chat_id).await;
        result.map(|()| output)
    }
}

#[cfg(all(test, any(feature = "redis-session", feature = "postgres-session")))]
mod tests {
    use super::*;

    #[cfg(feature = "redis-session")]
    #[test]
    fn validates_redis_namespace() {
        assert!(validate_redis_namespace("tele:sessions").is_ok());

        for namespace in ["", " ", "tele sessions", "tele\nsessions"] {
            assert!(matches!(
                validate_redis_namespace(namespace),
                Err(Error::InvalidRequest { .. })
            ));
        }
    }

    #[cfg(feature = "postgres-session")]
    #[test]
    fn quotes_valid_postgres_identifier() -> Result<()> {
        validate_sql_identifier("SessionState")?;
        assert_eq!(quote_sql_identifier("SessionState"), "\"SessionState\"");
        Ok(())
    }
}