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 chrono::NaiveDateTime;
13 use diesel::{result::Error, *};
15 use serde::{Deserialize, Serialize};
16 use std::{env, env::VarError};
23 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
26 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
29 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
32 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
35 fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
43 pub trait Followable<T> {
44 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
47 fn follow_accepted(conn: &PgConnection, community_id: i32, user_id: i32) -> Result<Self, Error>
50 fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
53 fn has_local_followers(conn: &PgConnection, community_id: i32) -> Result<bool, Error>;
56 pub trait Joinable<T> {
57 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
60 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
65 pub trait Likeable<T> {
66 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
69 fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
74 pub trait Bannable<T> {
75 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
78 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
83 pub trait Saveable<T> {
84 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
87 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
92 pub trait Readable<T> {
93 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
96 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
101 pub trait Reportable<T> {
102 fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
105 fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
108 fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
113 pub trait ApubObject<T> {
114 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error>
117 fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
122 pub trait MaybeOptional<T> {
123 fn get_optional(self) -> Option<T>;
126 impl<T> MaybeOptional<T> for T {
127 fn get_optional(self) -> Option<T> {
132 impl<T> MaybeOptional<T> for Option<T> {
133 fn get_optional(self) -> Option<T> {
138 pub(crate) trait ToSafe {
140 fn safe_columns_tuple() -> Self::SafeColumns;
143 pub fn get_database_url_from_env() -> Result<String, VarError> {
144 env::var("LEMMY_DATABASE_URL")
147 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
159 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
160 pub enum ListingType {
167 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
168 pub enum SearchType {
177 pub fn fuzzy_search(q: &str) -> String {
178 let replaced = q.replace(" ", "%");
179 format!("%{}%", replaced)
182 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
183 let page = page.unwrap_or(1);
184 let limit = limit.unwrap_or(10);
185 let offset = limit * (page - 1);
189 pub fn naive_now() -> NaiveDateTime {
190 chrono::prelude::Utc::now().naive_utc()
193 pub fn is_email_regex(test: &str) -> bool {
194 EMAIL_REGEX.is_match(test)
197 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
199 // An empty string is an erase
201 if !unwrapped.eq("") {
202 Some(Some(unwrapped.to_owned()))
212 static ref EMAIL_REGEX: Regex =
213 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
216 pub(crate) mod functions {
217 use diesel::sql_types::*;
220 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
226 use super::fuzzy_search;
227 use crate::{get_database_url_from_env, is_email_regex};
228 use diesel::{Connection, PgConnection};
232 pub fn establish_unpooled_connection() -> PgConnection {
233 let db_url = match get_database_url_from_env() {
236 "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
241 PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
242 embedded_migrations::run(&conn).unwrap();
247 fn test_fuzzy_search() {
248 let test = "This is a fuzzy search";
249 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
254 assert!(is_email_regex("gush@gmail.com"));
255 assert!(!is_email_regex("nada_neutho"));