4 extern crate strum_macros;
6 extern crate lazy_static;
8 use diesel::{result::Error, *};
10 use serde::{Deserialize, Serialize};
11 use std::{env, env::VarError};
17 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
20 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
23 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
26 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
29 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
37 pub trait Followable<T> {
38 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
41 fn follow_accepted(conn: &PgConnection, community_id: i32, user_id: i32) -> Result<Self, Error>
44 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
47 fn has_local_followers(conn: &PgConnection, community_id: i32) -> Result<bool, Error>;
50 pub trait Joinable<T> {
51 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
54 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
59 pub trait Likeable<T> {
60 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
63 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
68 pub trait Bannable<T> {
69 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
72 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
77 pub trait Saveable<T> {
78 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
81 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
86 pub trait Readable<T> {
87 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
90 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
95 pub trait Reportable<T> {
96 fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
99 fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
102 fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
107 pub trait ApubObject<T> {
108 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
111 fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
116 pub trait MaybeOptional<T> {
117 fn get_optional(self) -> Option<T>;
120 impl<T> MaybeOptional<T> for T {
121 fn get_optional(self) -> Option<T> {
126 impl<T> MaybeOptional<T> for Option<T> {
127 fn get_optional(self) -> Option<T> {
132 pub(crate) trait ToSafe {
134 fn safe_columns_tuple() -> Self::SafeColumns;
137 pub fn get_database_url_from_env() -> Result<String, VarError> {
138 env::var("LEMMY_DATABASE_URL")
141 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
153 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
154 pub enum ListingType {
161 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
162 pub enum SearchType {
171 pub fn fuzzy_search(q: &str) -> String {
172 let replaced = q.replace(" ", "%");
173 format!("%{}%", replaced)
176 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
177 let page = page.unwrap_or(1);
178 let limit = limit.unwrap_or(10);
179 let offset = limit * (page - 1);
183 pub fn is_email_regex(test: &str) -> bool {
184 EMAIL_REGEX.is_match(test)
187 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
189 // An empty string is an erase
191 if !unwrapped.eq("") {
192 Some(Some(unwrapped.to_owned()))
202 static ref EMAIL_REGEX: Regex =
203 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
206 pub(crate) mod functions {
207 use diesel::sql_types::*;
210 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
216 use super::fuzzy_search;
217 use crate::{get_database_url_from_env, is_email_regex};
218 use diesel::{Connection, PgConnection};
220 pub fn establish_unpooled_connection() -> PgConnection {
221 let db_url = match get_database_url_from_env() {
224 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
228 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
232 fn test_fuzzy_search() {
233 let test = "This is a fuzzy search";
234 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
239 assert!(is_email_regex("gush@gmail.com"));
240 assert!(!is_email_regex("nada_neutho"));