2 pub extern crate diesel;
4 pub extern crate strum_macros;
6 pub extern crate lazy_static;
7 pub extern crate bcrypt;
8 pub extern crate chrono;
10 pub extern crate regex;
11 pub extern crate serde;
12 pub extern crate serde_json;
13 pub extern crate sha2;
14 pub extern crate strum;
16 use chrono::NaiveDateTime;
17 use diesel::{dsl::*, 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;
44 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
47 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
50 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
53 fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error>
58 pub trait Followable<T> {
59 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
62 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
67 pub trait Joinable<T> {
68 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
71 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
76 pub trait Likeable<T> {
77 fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
80 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
83 fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error>
88 pub trait Bannable<T> {
89 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
92 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
97 pub trait Saveable<T> {
98 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
101 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
106 pub trait Readable<T> {
107 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
110 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
115 pub trait MaybeOptional<T> {
116 fn get_optional(self) -> Option<T>;
119 impl<T> MaybeOptional<T> for T {
120 fn get_optional(self) -> Option<T> {
125 impl<T> MaybeOptional<T> for Option<T> {
126 fn get_optional(self) -> Option<T> {
131 pub fn get_database_url_from_env() -> Result<String, VarError> {
132 env::var("LEMMY_DATABASE_URL")
135 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
147 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
148 pub enum ListingType {
154 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
155 pub enum SearchType {
164 pub fn fuzzy_search(q: &str) -> String {
165 let replaced = q.replace(" ", "%");
166 format!("%{}%", replaced)
169 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
170 let page = page.unwrap_or(1);
171 let limit = limit.unwrap_or(10);
172 let offset = limit * (page - 1);
176 pub fn naive_now() -> NaiveDateTime {
177 chrono::prelude::Utc::now().naive_utc()
180 pub fn is_email_regex(test: &str) -> bool {
181 EMAIL_REGEX.is_match(test)
184 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
186 // An empty string is an erase
188 if !unwrapped.eq("") {
189 Some(Some(unwrapped.to_owned()))
199 static ref EMAIL_REGEX: Regex =
200 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
205 use super::fuzzy_search;
206 use crate::{get_database_url_from_env, is_email_regex};
207 use diesel::{Connection, PgConnection};
209 pub fn establish_unpooled_connection() -> PgConnection {
210 let db_url = match get_database_url_from_env() {
213 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
217 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
221 fn test_fuzzy_search() {
222 let test = "This is a fuzzy search";
223 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
228 assert!(is_email_regex("gush@gmail.com"));
229 assert!(!is_email_regex("nada_neutho"));