cache local instance (#572)
* cache local instance fix #564 * don't use local instance cache for plm * use instance cache for plm, but initialize it * cargo fmt
This commit is contained in:
committed by
Baptiste Gelez
parent
5b50f90d2b
commit
773fbfe7c8
+2
-2
@@ -114,7 +114,7 @@ pub fn create(
|
||||
NaiveDateTime::parse_from_str(format!("{} 00:00:00", d).as_ref(), "%Y-%m-%d %H:%M:%S").ok()
|
||||
});
|
||||
|
||||
let domain = &Instance::get_local(conn)?.public_domain;
|
||||
let domain = &Instance::get_local()?.public_domain;
|
||||
let (content, mentions, hashtags) = md_to_html(
|
||||
&payload.source,
|
||||
Some(domain),
|
||||
@@ -144,7 +144,7 @@ pub fn create(
|
||||
content: SafeString::new(content.as_ref()),
|
||||
published: payload.published.unwrap_or(true),
|
||||
license: payload.license.clone().unwrap_or_else(|| {
|
||||
Instance::get_local(conn)
|
||||
Instance::get_local()
|
||||
.map(|i| i.default_license)
|
||||
.unwrap_or_else(|_| String::from("CC-BY-SA"))
|
||||
}),
|
||||
|
||||
+5
-2
@@ -41,6 +41,7 @@ extern crate webfinger;
|
||||
use diesel::r2d2::ConnectionManager;
|
||||
use plume_models::{
|
||||
db_conn::{DbPool, PragmaForeignKey},
|
||||
instance::Instance,
|
||||
migrations::IMPORTED_MIGRATIONS,
|
||||
search::{Searcher as UnmanagedSearcher, SearcherError},
|
||||
Connection, Error, CONFIG,
|
||||
@@ -73,10 +74,12 @@ fn init_pool() -> Option<DbPool> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let manager = ConnectionManager::<Connection>::new(CONFIG.database_url.as_str());
|
||||
DbPool::builder()
|
||||
let pool = DbPool::builder()
|
||||
.connection_customizer(Box::new(PragmaForeignKey))
|
||||
.build(manager)
|
||||
.ok()
|
||||
.ok()?;
|
||||
Instance::cache_local(&pool.get().unwrap());
|
||||
Some(pool)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
+2
-2
@@ -118,7 +118,7 @@ pub fn create(
|
||||
slug.clone(),
|
||||
form.title.to_string(),
|
||||
String::from(""),
|
||||
Instance::get_local(&*conn)
|
||||
Instance::get_local()
|
||||
.expect("blog::create: instance error")
|
||||
.id,
|
||||
)
|
||||
@@ -348,7 +348,7 @@ pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>>
|
||||
let conn = &*rockets.conn;
|
||||
let feed = FeedBuilder::default()
|
||||
.title(blog.title.clone())
|
||||
.id(Instance::get_local(&*conn)
|
||||
.id(Instance::get_local()
|
||||
.ok()?
|
||||
.compute_box("~", &name, "atom.xml"))
|
||||
.entries(
|
||||
|
||||
@@ -43,7 +43,7 @@ pub fn create(
|
||||
let (html, mentions, _hashtags) = utils::md_to_html(
|
||||
form.content.as_ref(),
|
||||
Some(
|
||||
&Instance::get_local(&conn)
|
||||
&Instance::get_local()
|
||||
.expect("comments::create: local instance error")
|
||||
.public_domain,
|
||||
),
|
||||
|
||||
+12
-12
@@ -19,7 +19,7 @@ use template_utils::{IntoContext, Ructe};
|
||||
#[get("/")]
|
||||
pub fn index(rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let conn = &*rockets.conn;
|
||||
let inst = Instance::get_local(conn)?;
|
||||
let inst = Instance::get_local()?;
|
||||
let federated = Post::get_recents_page(conn, Page::default().limits())?;
|
||||
let local = Post::get_instance_page(conn, inst.id, Page::default().limits())?;
|
||||
let user_feed = rockets.user.clone().and_then(|user| {
|
||||
@@ -43,7 +43,7 @@ pub fn index(rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
#[get("/local?<page>")]
|
||||
pub fn local(page: Option<Page>, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let page = page.unwrap_or_default();
|
||||
let instance = Instance::get_local(&*rockets.conn)?;
|
||||
let instance = Instance::get_local()?;
|
||||
let articles = Post::get_instance_page(&*rockets.conn, instance.id, page.limits())?;
|
||||
Ok(render!(instance::local(
|
||||
&rockets.to_context(),
|
||||
@@ -83,7 +83,7 @@ pub fn federated(page: Option<Page>, rockets: PlumeRocket) -> Result<Ructe, Erro
|
||||
|
||||
#[get("/admin")]
|
||||
pub fn admin(_admin: Admin, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let local_inst = Instance::get_local(&*rockets.conn)?;
|
||||
let local_inst = Instance::get_local()?;
|
||||
Ok(render!(instance::admin(
|
||||
&rockets.to_context(),
|
||||
local_inst.clone(),
|
||||
@@ -119,7 +119,7 @@ pub fn update_settings(
|
||||
form.validate()
|
||||
.and_then(|_| {
|
||||
let instance =
|
||||
Instance::get_local(conn).expect("instance::update_settings: local instance error");
|
||||
Instance::get_local().expect("instance::update_settings: local instance error");
|
||||
instance
|
||||
.update(
|
||||
conn,
|
||||
@@ -136,7 +136,7 @@ pub fn update_settings(
|
||||
})
|
||||
.or_else(|e| {
|
||||
let local_inst =
|
||||
Instance::get_local(conn).expect("instance::update_settings: local instance error");
|
||||
Instance::get_local().expect("instance::update_settings: local instance error");
|
||||
Err(render!(instance::admin(
|
||||
&rockets.to_context(),
|
||||
local_inst,
|
||||
@@ -156,7 +156,7 @@ pub fn admin_instances(
|
||||
let instances = Instance::page(&*rockets.conn, page.limits())?;
|
||||
Ok(render!(instance::list(
|
||||
&rockets.to_context(),
|
||||
Instance::get_local(&*rockets.conn)?,
|
||||
Instance::get_local()?,
|
||||
instances,
|
||||
page.0,
|
||||
Page::total(Instance::count(&*rockets.conn)? as i32)
|
||||
@@ -204,7 +204,7 @@ pub fn ban(_admin: Admin, id: i32, rockets: PlumeRocket) -> Result<Flash<Redirec
|
||||
let u = User::get(&*rockets.conn, id)?;
|
||||
u.delete(&*rockets.conn, &rockets.searcher)?;
|
||||
|
||||
if Instance::get_local(&*rockets.conn)
|
||||
if Instance::get_local()
|
||||
.map(|i| u.instance_id == i.id)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
@@ -267,7 +267,7 @@ pub fn nodeinfo(conn: DbConn, version: String) -> Result<Json<serde_json::Value>
|
||||
return Err(ErrorPage::from(Error::NotFound));
|
||||
}
|
||||
|
||||
let local_inst = Instance::get_local(&*conn)?;
|
||||
let local_inst = Instance::get_local()?;
|
||||
let mut doc = json!({
|
||||
"version": version,
|
||||
"software": {
|
||||
@@ -305,8 +305,8 @@ pub fn about(rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let conn = &*rockets.conn;
|
||||
Ok(render!(instance::about(
|
||||
&rockets.to_context(),
|
||||
Instance::get_local(conn)?,
|
||||
Instance::get_local(conn)?.main_admin(conn)?,
|
||||
Instance::get_local()?,
|
||||
Instance::get_local()?.main_admin(conn)?,
|
||||
User::count_local(conn)?,
|
||||
Post::count_local(conn)?,
|
||||
Instance::count(conn)? - 1
|
||||
@@ -314,8 +314,8 @@ pub fn about(rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
}
|
||||
|
||||
#[get("/manifest.json")]
|
||||
pub fn web_manifest(conn: DbConn) -> Result<Json<serde_json::Value>, ErrorPage> {
|
||||
let instance = Instance::get_local(&*conn)?;
|
||||
pub fn web_manifest() -> Result<Json<serde_json::Value>, ErrorPage> {
|
||||
let instance = Instance::get_local()?;
|
||||
Ok(Json(json!({
|
||||
"name": &instance.name,
|
||||
"description": &instance.short_description,
|
||||
|
||||
+5
-5
@@ -147,7 +147,7 @@ pub fn new(blog: String, cl: ContentLen, rockets: PlumeRocket) -> Result<Ructe,
|
||||
b,
|
||||
false,
|
||||
&NewPostForm {
|
||||
license: Instance::get_local(&*conn)?.default_license,
|
||||
license: Instance::get_local()?.default_license,
|
||||
..NewPostForm::default()
|
||||
},
|
||||
true,
|
||||
@@ -263,7 +263,7 @@ pub fn update(
|
||||
let (content, mentions, hashtags) = utils::md_to_html(
|
||||
form.content.to_string().as_ref(),
|
||||
Some(
|
||||
&Instance::get_local(&conn)
|
||||
&Instance::get_local()
|
||||
.expect("posts::update: Error getting local instance")
|
||||
.public_domain,
|
||||
),
|
||||
@@ -314,7 +314,7 @@ pub fn update(
|
||||
.filter(|t| !t.is_empty())
|
||||
.collect::<HashSet<_>>()
|
||||
.into_iter()
|
||||
.filter_map(|t| Tag::build_activity(&conn, t).ok())
|
||||
.filter_map(|t| Tag::build_activity(t).ok())
|
||||
.collect::<Vec<_>>();
|
||||
post.update_tags(&conn, tags)
|
||||
.expect("post::update: tags error");
|
||||
@@ -324,7 +324,7 @@ pub fn update(
|
||||
.map(|h| h.to_camel_case())
|
||||
.collect::<HashSet<_>>()
|
||||
.into_iter()
|
||||
.filter_map(|t| Tag::build_activity(&conn, t).ok())
|
||||
.filter_map(|t| Tag::build_activity(t).ok())
|
||||
.collect::<Vec<_>>();
|
||||
post.update_hashtags(&conn, hashtags)
|
||||
.expect("post::update: hashtags error");
|
||||
@@ -435,7 +435,7 @@ pub fn create(
|
||||
let (content, mentions, hashtags) = utils::md_to_html(
|
||||
form.content.to_string().as_ref(),
|
||||
Some(
|
||||
&Instance::get_local(&conn)
|
||||
&Instance::get_local()
|
||||
.expect("post::create: local instance error")
|
||||
.public_domain,
|
||||
),
|
||||
|
||||
+7
-7
@@ -109,7 +109,7 @@ pub fn details(
|
||||
.clone()
|
||||
.and_then(|x| x.is_following(&*conn, user.id).ok())
|
||||
.unwrap_or(false),
|
||||
user.instance_id != Instance::get_local(&*conn)?.id,
|
||||
user.instance_id != Instance::get_local()?.id,
|
||||
user.get_instance(&*conn)?.public_domain,
|
||||
recents,
|
||||
reshares
|
||||
@@ -278,7 +278,7 @@ pub fn followers(
|
||||
.clone()
|
||||
.and_then(|x| x.is_following(&*conn, user.id).ok())
|
||||
.unwrap_or(false),
|
||||
user.instance_id != Instance::get_local(&*conn)?.id,
|
||||
user.instance_id != Instance::get_local()?.id,
|
||||
user.get_instance(&*conn)?.public_domain,
|
||||
user.get_followers_page(&*conn, page.limits())?,
|
||||
page.0,
|
||||
@@ -305,7 +305,7 @@ pub fn followed(
|
||||
.clone()
|
||||
.and_then(|x| x.is_following(conn, user.id).ok())
|
||||
.unwrap_or(false),
|
||||
user.instance_id != Instance::get_local(conn)?.id,
|
||||
user.instance_id != Instance::get_local()?.id,
|
||||
user.get_instance(conn)?.public_domain,
|
||||
user.get_followed_page(conn, page.limits())?,
|
||||
page.0,
|
||||
@@ -327,7 +327,7 @@ pub fn activity_details(
|
||||
pub fn new(rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
Ok(render!(users::new(
|
||||
&rockets.to_context(),
|
||||
Instance::get_local(&*rockets.conn)?.open_registrations,
|
||||
Instance::get_local()?.open_registrations,
|
||||
&NewUserForm::default(),
|
||||
ValidationErrors::default()
|
||||
)))
|
||||
@@ -494,7 +494,7 @@ pub fn create(
|
||||
rockets: PlumeRocket,
|
||||
) -> Result<Flash<Redirect>, Ructe> {
|
||||
let conn = &*rockets.conn;
|
||||
if !Instance::get_local(conn)
|
||||
if !Instance::get_local()
|
||||
.map(|i| i.open_registrations)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
@@ -533,7 +533,7 @@ pub fn create(
|
||||
.map_err(|err| {
|
||||
render!(users::new(
|
||||
&rockets.to_context(),
|
||||
Instance::get_local(conn)
|
||||
Instance::get_local()
|
||||
.map(|i| i.open_registrations)
|
||||
.unwrap_or(true),
|
||||
&form,
|
||||
@@ -590,7 +590,7 @@ pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>>
|
||||
let author = User::find_by_fqn(&rockets, &name).ok()?;
|
||||
let feed = FeedBuilder::default()
|
||||
.title(author.display_name.clone())
|
||||
.id(Instance::get_local(conn)
|
||||
.id(Instance::get_local()
|
||||
.unwrap()
|
||||
.compute_box("@", &name, "atom.xml"))
|
||||
.entries(
|
||||
|
||||
Reference in New Issue
Block a user