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)]
146 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
147 pub enum ListingType {
153 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
154 pub enum SearchType {
163 pub fn fuzzy_search(q: &str) -> String {
164 let replaced = q.replace(" ", "%");
165 format!("%{}%", replaced)
168 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
169 let page = page.unwrap_or(1);
170 let limit = limit.unwrap_or(10);
171 let offset = limit * (page - 1);
175 pub fn naive_now() -> NaiveDateTime {
176 chrono::prelude::Utc::now().naive_utc()
179 pub fn is_email_regex(test: &str) -> bool {
180 EMAIL_REGEX.is_match(test)
184 static ref EMAIL_REGEX: Regex =
185 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
190 use super::fuzzy_search;
191 use crate::{get_database_url_from_env, is_email_regex};
192 use diesel::{Connection, PgConnection};
194 pub fn establish_unpooled_connection() -> PgConnection {
195 let db_url = match get_database_url_from_env() {
198 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
202 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
206 fn test_fuzzy_search() {
207 let test = "This is a fuzzy search";
208 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
213 assert!(is_email_regex("gush@gmail.com"));
214 assert!(!is_email_regex("nada_neutho"));