4 extern crate strum_macros;
6 extern crate lazy_static;
12 extern crate serde_json;
16 use chrono::NaiveDateTime;
17 use diesel::{result::Error, *};
19 use serde::{Deserialize, Serialize};
20 use std::{env, env::VarError};
27 pub mod community_view;
29 pub mod moderator_views;
30 pub mod password_reset_request;
33 pub mod private_message;
34 pub mod private_message_view;
40 pub mod user_mention_view;
43 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
46 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
49 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
52 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
55 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
63 pub trait Followable<T> {
64 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
67 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
72 pub trait Joinable<T> {
73 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
76 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
81 pub trait Likeable<T> {
82 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
85 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
90 pub trait Bannable<T> {
91 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
94 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
99 pub trait Saveable<T> {
100 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
103 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
108 pub trait Readable<T> {
109 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
112 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
117 pub trait MaybeOptional<T> {
118 fn get_optional(self) -> Option<T>;
121 impl<T> MaybeOptional<T> for T {
122 fn get_optional(self) -> Option<T> {
127 impl<T> MaybeOptional<T> for Option<T> {
128 fn get_optional(self) -> Option<T> {
133 pub fn get_database_url_from_env() -> Result<String, VarError> {
134 env::var("LEMMY_DATABASE_URL")
137 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
149 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
150 pub enum ListingType {
157 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
158 pub enum SearchType {
167 pub fn fuzzy_search(q: &str) -> String {
168 let replaced = q.replace(" ", "%");
169 format!("%{}%", replaced)
172 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
173 let page = page.unwrap_or(1);
174 let limit = limit.unwrap_or(10);
175 let offset = limit * (page - 1);
179 pub fn naive_now() -> NaiveDateTime {
180 chrono::prelude::Utc::now().naive_utc()
183 pub fn is_email_regex(test: &str) -> bool {
184 EMAIL_REGEX.is_match(test)
187 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
189 // An empty string is an erase
191 if !unwrapped.eq("") {
192 Some(Some(unwrapped.to_owned()))
202 static ref EMAIL_REGEX: Regex =
203 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
208 use super::fuzzy_search;
209 use crate::{get_database_url_from_env, is_email_regex};
210 use diesel::{Connection, PgConnection};
212 pub fn establish_unpooled_connection() -> PgConnection {
213 let db_url = match get_database_url_from_env() {
216 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
220 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
224 fn test_fuzzy_search() {
225 let test = "This is a fuzzy search";
226 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
231 assert!(is_email_regex("gush@gmail.com"));
232 assert!(!is_email_regex("nada_neutho"));