lemmy_api_common/
lib.rs

1#[cfg(feature = "full")]
2pub mod build_response;
3#[cfg(feature = "full")]
4pub mod claims;
5pub mod comment;
6pub mod community;
7#[cfg(feature = "full")]
8pub mod context;
9pub mod custom_emoji;
10pub mod person;
11pub mod post;
12pub mod private_message;
13#[cfg(feature = "full")]
14pub mod request;
15#[cfg(feature = "full")]
16pub mod send_activity;
17pub mod site;
18#[cfg(feature = "full")]
19pub mod utils;
20
21pub extern crate lemmy_db_schema;
22pub extern crate lemmy_db_views;
23pub extern crate lemmy_db_views_actor;
24pub extern crate lemmy_db_views_moderator;
25pub extern crate lemmy_utils;
26
27pub use lemmy_utils::LemmyErrorType;
28use serde::{Deserialize, Serialize};
29use std::{cmp::min, time::Duration};
30
31#[derive(Debug, Serialize, Deserialize, Clone)]
32#[cfg_attr(feature = "full", derive(ts_rs::TS))]
33#[cfg_attr(feature = "full", ts(export))]
34/// Saves settings for your user.
35pub struct SuccessResponse {
36  pub success: bool,
37}
38
39impl Default for SuccessResponse {
40  fn default() -> Self {
41    SuccessResponse { success: true }
42  }
43}
44
45// TODO: use from_days once stabilized
46// https://github.com/rust-lang/rust/issues/120301
47const DAY: Duration = Duration::from_secs(24 * 60 * 60);
48
49/// Calculate how long to sleep until next federation send based on how many
50/// retries have already happened. Uses exponential backoff with maximum of one day. The first
51/// error is ignored.
52pub fn federate_retry_sleep_duration(retry_count: i32) -> Duration {
53  debug_assert!(retry_count != 0);
54  if retry_count == 1 {
55    return Duration::from_secs(0);
56  }
57  let retry_count = retry_count - 1;
58  let pow = 1.25_f64.powf(retry_count.into());
59  let pow = Duration::try_from_secs_f64(pow).unwrap_or(DAY);
60  min(DAY, pow)
61}
62
63#[cfg(test)]
64pub(crate) mod tests {
65  use super::*;
66
67  #[test]
68  fn test_federate_retry_sleep_duration() {
69    assert_eq!(Duration::from_secs(0), federate_retry_sleep_duration(1));
70    assert_eq!(
71      Duration::new(1, 250000000),
72      federate_retry_sleep_duration(2)
73    );
74    assert_eq!(
75      Duration::new(2, 441406250),
76      federate_retry_sleep_duration(5)
77    );
78    assert_eq!(DAY, federate_retry_sleep_duration(100));
79  }
80}