4 extern crate diesel_derive_newtype;
5 // this is used in tests
6 #[allow(unused_imports)]
8 extern crate diesel_migrations;
10 extern crate strum_macros;
19 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
21 use crate::newtypes::DbUrl;
22 use chrono::NaiveDateTime;
23 use diesel::{Connection, PgConnection};
24 use lemmy_utils::ApiError;
25 use once_cell::sync::Lazy;
27 use serde::{Deserialize, Serialize};
28 use std::{env, env::VarError};
31 pub fn get_database_url_from_env() -> Result<String, VarError> {
32 env::var("LEMMY_DATABASE_URL")
35 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)]
49 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)]
50 pub enum ListingType {
57 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)]
67 pub fn from_opt_str_to_opt_enum<T: std::str::FromStr>(opt: &Option<String>) -> Option<T> {
68 opt.as_ref().map(|t| T::from_str(t).ok()).flatten()
71 pub fn fuzzy_search(q: &str) -> String {
72 let replaced = q.replace("%", "\\%").replace("_", "\\_").replace(" ", "%");
73 format!("%{}%", replaced)
76 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
77 let page = page.unwrap_or(1);
78 let limit = limit.unwrap_or(10);
79 let offset = limit * (page - 1);
83 pub fn is_email_regex(test: &str) -> bool {
84 EMAIL_REGEX.is_match(test)
87 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
89 // An empty string is an erase
91 if !unwrapped.eq("") {
92 Some(Some(unwrapped.to_owned()))
101 pub fn diesel_option_overwrite_to_url(
102 opt: &Option<String>,
103 ) -> Result<Option<Option<DbUrl>>, ApiError> {
104 match opt.as_ref().map(|s| s.as_str()) {
105 // An empty string is an erase
106 Some("") => Ok(Some(None)),
107 Some(str_url) => match Url::parse(str_url) {
108 Ok(url) => Ok(Some(Some(url.into()))),
109 Err(e) => Err(ApiError::err("invalid_url", e)),
117 pub fn establish_unpooled_connection() -> PgConnection {
118 let db_url = match get_database_url_from_env() {
121 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
126 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
127 embedded_migrations::run(&conn).expect("load migrations");
131 pub fn naive_now() -> NaiveDateTime {
132 chrono::prelude::Utc::now().naive_utc()
135 static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
136 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
137 .expect("compile email regex")
141 use diesel::sql_types::*;
144 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
150 use super::{fuzzy_search, *};
151 use crate::is_email_regex;
154 fn test_fuzzy_search() {
155 let test = "This %is% _a_ fuzzy search";
158 "%This%\\%is\\%%\\_a\\_%fuzzy%search%".to_string()
164 assert!(is_email_regex("gush@gmail.com"));
165 assert!(!is_email_regex("nada_neutho"));
169 fn test_diesel_option_overwrite() {
170 assert_eq!(diesel_option_overwrite(&None), None);
171 assert_eq!(diesel_option_overwrite(&Some("".to_string())), Some(None));
173 diesel_option_overwrite(&Some("test".to_string())),
174 Some(Some("test".to_string()))
179 fn test_diesel_option_overwrite_to_url() {
180 assert!(matches!(diesel_option_overwrite_to_url(&None), Ok(None)));
182 diesel_option_overwrite_to_url(&Some("".to_string())),
186 diesel_option_overwrite_to_url(&Some("invalid_url".to_string())),
189 let example_url = "https://example.com";
191 diesel_option_overwrite_to_url(&Some(example_url.to_string())),
192 Ok(Some(Some(url))) if url == Url::parse(example_url).unwrap().into()