4 extern crate strum_macros;
6 extern crate lazy_static;
7 // this is used in tests
8 #[allow(unused_imports)]
10 extern crate diesel_migrations;
12 use diesel::{result::Error, *};
14 use serde::{Deserialize, Serialize};
15 use std::{env, env::VarError};
21 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
24 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
27 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
30 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
33 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
41 pub trait Followable<T> {
42 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
45 fn follow_accepted(conn: &PgConnection, community_id: i32, user_id: i32) -> Result<Self, Error>
48 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
51 fn has_local_followers(conn: &PgConnection, community_id: i32) -> Result<bool, Error>;
54 pub trait Joinable<T> {
55 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
58 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
63 pub trait Likeable<T> {
64 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
67 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
72 pub trait Bannable<T> {
73 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
76 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
81 pub trait Saveable<T> {
82 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
85 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
90 pub trait Readable<T> {
91 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
94 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
99 pub trait Reportable<T> {
100 fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
103 fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
106 fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
111 pub trait ApubObject<T> {
112 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
115 fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
120 pub trait MaybeOptional<T> {
121 fn get_optional(self) -> Option<T>;
124 impl<T> MaybeOptional<T> for T {
125 fn get_optional(self) -> Option<T> {
130 impl<T> MaybeOptional<T> for Option<T> {
131 fn get_optional(self) -> Option<T> {
136 pub(crate) trait ToSafe {
138 fn safe_columns_tuple() -> Self::SafeColumns;
141 pub fn get_database_url_from_env() -> Result<String, VarError> {
142 env::var("LEMMY_DATABASE_URL")
145 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
157 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
158 pub enum ListingType {
165 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
166 pub enum SearchType {
175 pub fn fuzzy_search(q: &str) -> String {
176 let replaced = q.replace(" ", "%");
177 format!("%{}%", replaced)
180 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
181 let page = page.unwrap_or(1);
182 let limit = limit.unwrap_or(10);
183 let offset = limit * (page - 1);
187 pub fn is_email_regex(test: &str) -> bool {
188 EMAIL_REGEX.is_match(test)
191 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
193 // An empty string is an erase
195 if !unwrapped.eq("") {
196 Some(Some(unwrapped.to_owned()))
206 static ref EMAIL_REGEX: Regex =
207 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
210 pub(crate) mod functions {
211 use diesel::sql_types::*;
214 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
220 use super::fuzzy_search;
221 use crate::{get_database_url_from_env, is_email_regex};
222 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: {}",
235 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
236 embedded_migrations::run(&conn).unwrap();
241 fn test_fuzzy_search() {
242 let test = "This is a fuzzy search";
243 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
248 assert!(is_email_regex("gush@gmail.com"));
249 assert!(!is_email_regex("nada_neutho"));