4 extern crate strum_macros;
6 extern crate lazy_static;
8 use chrono::NaiveDateTime;
9 use diesel::{result::Error, *};
11 use serde::{Deserialize, Serialize};
12 use std::{env, env::VarError};
19 pub mod community_view;
21 pub mod moderator_views;
22 pub mod password_reset_request;
25 pub mod private_message;
26 pub mod private_message_view;
32 pub mod user_mention_view;
35 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
38 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
41 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
44 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
47 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
55 pub trait Followable<T> {
56 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
59 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
64 pub trait Joinable<T> {
65 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
68 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
73 pub trait Likeable<T> {
74 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
77 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
82 pub trait Bannable<T> {
83 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
86 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
91 pub trait Saveable<T> {
92 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
95 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
100 pub trait Readable<T> {
101 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
104 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
109 pub trait MaybeOptional<T> {
110 fn get_optional(self) -> Option<T>;
113 impl<T> MaybeOptional<T> for T {
114 fn get_optional(self) -> Option<T> {
119 impl<T> MaybeOptional<T> for Option<T> {
120 fn get_optional(self) -> Option<T> {
125 pub fn get_database_url_from_env() -> Result<String, VarError> {
126 env::var("LEMMY_DATABASE_URL")
129 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
141 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
142 pub enum ListingType {
149 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
150 pub enum SearchType {
159 pub fn fuzzy_search(q: &str) -> String {
160 let replaced = q.replace(" ", "%");
161 format!("%{}%", replaced)
164 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
165 let page = page.unwrap_or(1);
166 let limit = limit.unwrap_or(10);
167 let offset = limit * (page - 1);
171 pub fn naive_now() -> NaiveDateTime {
172 chrono::prelude::Utc::now().naive_utc()
175 pub fn is_email_regex(test: &str) -> bool {
176 EMAIL_REGEX.is_match(test)
179 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
181 // An empty string is an erase
183 if !unwrapped.eq("") {
184 Some(Some(unwrapped.to_owned()))
194 static ref EMAIL_REGEX: Regex =
195 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
200 use super::fuzzy_search;
201 use crate::{get_database_url_from_env, is_email_regex};
202 use diesel::{Connection, PgConnection};
204 pub fn establish_unpooled_connection() -> PgConnection {
205 let db_url = match get_database_url_from_env() {
208 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
212 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
216 fn test_fuzzy_search() {
217 let test = "This is a fuzzy search";
218 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
223 assert!(is_email_regex("gush@gmail.com"));
224 assert!(!is_email_regex("nada_neutho"));