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, *};
13 use lemmy_db_schema::Url;
15 use serde::{Deserialize, Serialize};
16 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: &Url) -> 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> {
138 fn safe_columns_tuple() -> Self::SafeColumns;
141 pub trait ToSafeSettings {
142 type SafeSettingsColumns;
143 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
146 pub trait ViewToVec {
148 fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
153 pub fn get_database_url_from_env() -> Result<String, VarError> {
154 env::var("LEMMY_DATABASE_URL")
157 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
170 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
171 pub enum ListingType {
178 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
179 pub enum SearchType {
188 pub fn fuzzy_search(q: &str) -> String {
189 let replaced = q.replace(" ", "%");
190 format!("%{}%", replaced)
193 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
194 let page = page.unwrap_or(1);
195 let limit = limit.unwrap_or(10);
196 let offset = limit * (page - 1);
200 pub fn is_email_regex(test: &str) -> bool {
201 EMAIL_REGEX.is_match(test)
204 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
206 // An empty string is an erase
208 if !unwrapped.eq("") {
209 Some(Some(unwrapped.to_owned()))
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: {}",
229 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
230 embedded_migrations::run(&conn).unwrap();
235 static ref EMAIL_REGEX: Regex =
236 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
240 use diesel::sql_types::*;
243 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
249 use super::fuzzy_search;
250 use crate::is_email_regex;
253 fn test_fuzzy_search() {
254 let test = "This is a fuzzy search";
255 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
260 assert!(is_email_regex("gush@gmail.com"));
261 assert!(!is_email_regex("nada_neutho"));