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};
18 pub mod comment_report;
22 pub mod moderator_views;
23 pub mod password_reset_request;
27 pub mod private_message;
28 pub mod private_message_view;
33 pub mod user_mention_view;
36 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
39 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
42 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
45 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
48 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
56 pub trait Followable<T> {
57 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
60 fn follow_accepted(conn: &PgConnection, community_id: i32, user_id: i32) -> Result<Self, Error>
63 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
66 fn has_local_followers(conn: &PgConnection, community_id: i32) -> Result<bool, Error>;
69 pub trait Joinable<T> {
70 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
73 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
78 pub trait Likeable<T> {
79 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
82 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
87 pub trait Bannable<T> {
88 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
91 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
96 pub trait Saveable<T> {
97 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
100 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
105 pub trait Readable<T> {
106 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
109 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
114 pub trait Reportable<T> {
115 fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
118 fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
121 fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
126 pub trait MaybeOptional<T> {
127 fn get_optional(self) -> Option<T>;
130 impl<T> MaybeOptional<T> for T {
131 fn get_optional(self) -> Option<T> {
136 impl<T> MaybeOptional<T> for Option<T> {
137 fn get_optional(self) -> Option<T> {
142 pub(crate) trait ToSafe {
144 fn safe_columns_tuple() -> Self::SafeColumns;
147 pub fn get_database_url_from_env() -> Result<String, VarError> {
148 env::var("LEMMY_DATABASE_URL")
151 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
163 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
164 pub enum ListingType {
171 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
172 pub enum SearchType {
181 pub fn fuzzy_search(q: &str) -> String {
182 let replaced = q.replace(" ", "%");
183 format!("%{}%", replaced)
186 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
187 let page = page.unwrap_or(1);
188 let limit = limit.unwrap_or(10);
189 let offset = limit * (page - 1);
193 pub fn naive_now() -> NaiveDateTime {
194 chrono::prelude::Utc::now().naive_utc()
197 pub fn is_email_regex(test: &str) -> bool {
198 EMAIL_REGEX.is_match(test)
201 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
203 // An empty string is an erase
205 if !unwrapped.eq("") {
206 Some(Some(unwrapped.to_owned()))
216 static ref EMAIL_REGEX: Regex =
217 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
222 use super::fuzzy_search;
223 use crate::{get_database_url_from_env, is_email_regex};
224 use diesel::{Connection, PgConnection};
226 pub fn establish_unpooled_connection() -> PgConnection {
227 let db_url = match get_database_url_from_env() {
230 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
234 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
238 fn test_fuzzy_search() {
239 let test = "This is a fuzzy search";
240 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
245 assert!(is_email_regex("gush@gmail.com"));
246 assert!(!is_email_regex("nada_neutho"));