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 ApubObject<T> {
127 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
130 fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
135 pub trait MaybeOptional<T> {
136 fn get_optional(self) -> Option<T>;
139 impl<T> MaybeOptional<T> for T {
140 fn get_optional(self) -> Option<T> {
145 impl<T> MaybeOptional<T> for Option<T> {
146 fn get_optional(self) -> Option<T> {
151 pub(crate) trait ToSafe {
153 fn safe_columns_tuple() -> Self::SafeColumns;
156 pub fn get_database_url_from_env() -> Result<String, VarError> {
157 env::var("LEMMY_DATABASE_URL")
160 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
172 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
173 pub enum ListingType {
180 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
181 pub enum SearchType {
190 pub fn fuzzy_search(q: &str) -> String {
191 let replaced = q.replace(" ", "%");
192 format!("%{}%", replaced)
195 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
196 let page = page.unwrap_or(1);
197 let limit = limit.unwrap_or(10);
198 let offset = limit * (page - 1);
202 pub fn naive_now() -> NaiveDateTime {
203 chrono::prelude::Utc::now().naive_utc()
206 pub fn is_email_regex(test: &str) -> bool {
207 EMAIL_REGEX.is_match(test)
210 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
212 // An empty string is an erase
214 if !unwrapped.eq("") {
215 Some(Some(unwrapped.to_owned()))
225 static ref EMAIL_REGEX: Regex =
226 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
229 pub(crate) mod functions {
230 use diesel::sql_types::*;
233 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
239 use super::fuzzy_search;
240 use crate::{get_database_url_from_env, is_email_regex};
241 use diesel::{Connection, PgConnection};
243 pub fn establish_unpooled_connection() -> PgConnection {
244 let db_url = match get_database_url_from_env() {
247 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
251 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
255 fn test_fuzzy_search() {
256 let test = "This is a fuzzy search";
257 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
262 assert!(is_email_regex("gush@gmail.com"));
263 assert!(!is_email_regex("nada_neutho"));