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};
20 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
23 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
26 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
29 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
32 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
40 pub trait Followable<T> {
41 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
44 fn follow_accepted(conn: &PgConnection, community_id: i32, user_id: i32) -> Result<Self, Error>
47 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
50 fn has_local_followers(conn: &PgConnection, community_id: i32) -> Result<bool, Error>;
53 pub trait Joinable<T> {
54 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
57 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
62 pub trait Likeable<T> {
63 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
66 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
71 pub trait Bannable<T> {
72 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
75 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
80 pub trait Saveable<T> {
81 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
84 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
89 pub trait Readable<T> {
90 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
93 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
98 pub trait Reportable<T> {
99 fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
102 fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
105 fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
110 pub trait ApubObject<T> {
111 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
114 fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
119 pub trait MaybeOptional<T> {
120 fn get_optional(self) -> Option<T>;
123 impl<T> MaybeOptional<T> for T {
124 fn get_optional(self) -> Option<T> {
129 impl<T> MaybeOptional<T> for Option<T> {
130 fn get_optional(self) -> Option<T> {
137 fn safe_columns_tuple() -> Self::SafeColumns;
140 pub trait ToSafeSettings {
141 type SafeSettingsColumns;
142 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
145 pub trait ViewToVec {
147 fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
152 pub fn get_database_url_from_env() -> Result<String, VarError> {
153 env::var("LEMMY_DATABASE_URL")
156 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
168 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
169 pub enum ListingType {
176 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
177 pub enum SearchType {
186 pub fn fuzzy_search(q: &str) -> String {
187 let replaced = q.replace(" ", "%");
188 format!("%{}%", replaced)
191 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
192 let page = page.unwrap_or(1);
193 let limit = limit.unwrap_or(10);
194 let offset = limit * (page - 1);
198 pub fn is_email_regex(test: &str) -> bool {
199 EMAIL_REGEX.is_match(test)
202 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
204 // An empty string is an erase
206 if !unwrapped.eq("") {
207 Some(Some(unwrapped.to_owned()))
218 pub fn establish_unpooled_connection() -> PgConnection {
219 let db_url = match get_database_url_from_env() {
222 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
227 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
228 embedded_migrations::run(&conn).unwrap();
233 static ref EMAIL_REGEX: Regex =
234 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
238 use diesel::sql_types::*;
241 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
247 use super::fuzzy_search;
248 use crate::is_email_regex;
251 fn test_fuzzy_search() {
252 let test = "This is a fuzzy search";
253 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
258 assert!(is_email_regex("gush@gmail.com"));
259 assert!(!is_email_regex("nada_neutho"));