Introduce features to choose between SQlite or Postgres
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
|
||||
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods};
|
||||
use std::iter::Iterator;
|
||||
|
||||
use plume_common::utils::md_to_html;
|
||||
use {Connection, SqlDateTime};
|
||||
use safe_string::SafeString;
|
||||
use ap_url;
|
||||
use users::User;
|
||||
@@ -15,7 +15,7 @@ pub struct Instance {
|
||||
pub name: String,
|
||||
pub local: bool,
|
||||
pub blocked: bool,
|
||||
pub creation_date: NaiveDateTime,
|
||||
pub creation_date: SqlDateTime,
|
||||
pub open_registrations: bool,
|
||||
pub short_description: SafeString,
|
||||
pub long_description: SafeString,
|
||||
@@ -39,7 +39,7 @@ pub struct NewInstance {
|
||||
}
|
||||
|
||||
impl Instance {
|
||||
pub fn get_local(conn: &PgConnection) -> Option<Instance> {
|
||||
pub fn get_local(conn: &Connection) -> Option<Instance> {
|
||||
instances::table.filter(instances::local.eq(true))
|
||||
.limit(1)
|
||||
.load::<Instance>(conn)
|
||||
@@ -47,13 +47,13 @@ impl Instance {
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn get_remotes(conn: &PgConnection) -> Vec<Instance> {
|
||||
pub fn get_remotes(conn: &Connection) -> Vec<Instance> {
|
||||
instances::table.filter(instances::local.eq(false))
|
||||
.load::<Instance>(conn)
|
||||
.expect("Error loading remote instances infos")
|
||||
}
|
||||
|
||||
pub fn page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec<Instance> {
|
||||
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Vec<Instance> {
|
||||
instances::table.order(instances::public_domain.asc())
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
@@ -61,7 +61,7 @@ impl Instance {
|
||||
.expect("Error loading a page of instances")
|
||||
}
|
||||
|
||||
pub fn local_id(conn: &PgConnection) -> i32 {
|
||||
pub fn local_id(conn: &Connection) -> i32 {
|
||||
Instance::get_local(conn).unwrap().id
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ impl Instance {
|
||||
get!(instances);
|
||||
find_by!(instances, find_by_domain, public_domain as String);
|
||||
|
||||
pub fn toggle_block(&self, conn: &PgConnection) {
|
||||
pub fn toggle_block(&self, conn: &Connection) {
|
||||
diesel::update(self)
|
||||
.set(instances::blocked.eq(!self.blocked))
|
||||
.get_result::<Instance>(conn)
|
||||
@@ -77,7 +77,7 @@ impl Instance {
|
||||
}
|
||||
|
||||
/// id: AP object id
|
||||
pub fn is_blocked(conn: &PgConnection, id: String) -> bool {
|
||||
pub fn is_blocked(conn: &Connection, id: String) -> bool {
|
||||
for block in instances::table.filter(instances::blocked.eq(true))
|
||||
.get_results::<Instance>(conn)
|
||||
.expect("Error listing blocked instances") {
|
||||
@@ -89,7 +89,7 @@ impl Instance {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn has_admin(&self, conn: &PgConnection) -> bool {
|
||||
pub fn has_admin(&self, conn: &Connection) -> bool {
|
||||
users::table.filter(users::instance_id.eq(self.id))
|
||||
.filter(users::is_admin.eq(true))
|
||||
.load::<User>(conn)
|
||||
@@ -97,7 +97,7 @@ impl Instance {
|
||||
.len() > 0
|
||||
}
|
||||
|
||||
pub fn main_admin(&self, conn: &PgConnection) -> User {
|
||||
pub fn main_admin(&self, conn: &Connection) -> User {
|
||||
users::table.filter(users::instance_id.eq(self.id))
|
||||
.filter(users::is_admin.eq(true))
|
||||
.limit(1)
|
||||
@@ -115,7 +115,7 @@ impl Instance {
|
||||
))
|
||||
}
|
||||
|
||||
pub fn update(&self, conn: &PgConnection, name: String, open_registrations: bool, short_description: SafeString, long_description: SafeString) -> Instance {
|
||||
pub fn update(&self, conn: &Connection, name: String, open_registrations: bool, short_description: SafeString, long_description: SafeString) -> Instance {
|
||||
let (sd, _) = md_to_html(short_description.as_ref());
|
||||
let (ld, _) = md_to_html(long_description.as_ref());
|
||||
diesel::update(self)
|
||||
@@ -130,7 +130,7 @@ impl Instance {
|
||||
.expect("Couldn't update instance")
|
||||
}
|
||||
|
||||
pub fn count(conn: &PgConnection) -> i64 {
|
||||
pub fn count(conn: &Connection) -> i64 {
|
||||
instances::table.count().get_result(conn).expect("Couldn't count instances")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user