-pub fn from_opt_str_to_opt_enum<T: std::str::FromStr>(opt: &Option<String>) -> Option<T> {
- opt.as_ref().map(|t| T::from_str(t).ok()).flatten()
-}
-
-pub fn fuzzy_search(q: &str) -> String {
- let replaced = q.replace("%", "\\%").replace("_", "\\_").replace(" ", "%");
- format!("%{}%", replaced)
-}
-
-pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
- let page = page.unwrap_or(1);
- let limit = limit.unwrap_or(10);
- let offset = limit * (page - 1);
- (limit, offset)
-}
-
-pub fn is_email_regex(test: &str) -> bool {
- EMAIL_REGEX.is_match(test)
-}
-
-pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
- match opt {
- // An empty string is an erase
- Some(unwrapped) => {
- if !unwrapped.eq("") {
- Some(Some(unwrapped.to_owned()))
- } else {
- Some(None)
- }
- }
- None => None,
- }
-}
-
-pub fn diesel_option_overwrite_to_url(
- opt: &Option<String>,
-) -> Result<Option<Option<DbUrl>>, ApiError> {
- match opt.as_ref().map(|s| s.as_str()) {
- // An empty string is an erase
- Some("") => Ok(Some(None)),
- Some(str_url) => match Url::parse(str_url) {
- Ok(url) => Ok(Some(Some(url.into()))),
- Err(e) => Err(ApiError::err("invalid_url", e)),
- },
- None => Ok(None),
- }
-}
-
-embed_migrations!();
-
-pub fn establish_unpooled_connection() -> PgConnection {
- let db_url = match get_database_url_from_env() {
- Ok(url) => url,
- Err(e) => panic!(
- "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
- e
- ),
- };
- let conn =
- PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
- embedded_migrations::run(&conn).expect("load migrations");
- conn
-}
-
-pub fn naive_now() -> NaiveDateTime {
- chrono::prelude::Utc::now().naive_utc()
-}
-
-lazy_static! {
- static ref EMAIL_REGEX: Regex =
- Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
- .expect("compile email regex");
+#[derive(EnumString, Display, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
+#[cfg_attr(feature = "full", derive(TS))]
+#[cfg_attr(feature = "full", ts(export))]
+/// A type / status for a community subscribe.
+pub enum SubscribedType {
+ Subscribed,
+ NotSubscribed,
+ Pending,