From 773fbfe7c8bdc5ad6b76c4ed8cbc71e0cf9b14f5 Mon Sep 17 00:00:00 2001 From: fdb-hiroshima <35889323+fdb-hiroshima@users.noreply.github.com> Date: Fri, 10 May 2019 22:59:34 +0200 Subject: [PATCH] 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 --- plume-cli/src/main.rs | 3 ++- plume-cli/src/users.rs | 2 +- plume-models/src/blogs.rs | 34 +++++++++++++++------------- plume-models/src/comments.rs | 4 ++-- plume-models/src/instance.rs | 39 +++++++++++++++++++++++++++----- plume-models/src/medias.rs | 14 ++++++------ plume-models/src/posts.rs | 8 +++---- plume-models/src/tags.rs | 8 +++---- plume-models/src/users.rs | 20 ++++++++-------- src/api/posts.rs | 4 ++-- src/main.rs | 7 ++++-- src/routes/blogs.rs | 4 ++-- src/routes/comments.rs | 2 +- src/routes/instance.rs | 24 ++++++++++---------- src/routes/posts.rs | 10 ++++---- src/routes/user.rs | 14 ++++++------ templates/blogs/details.rs.html | 4 ++-- templates/medias/details.rs.html | 4 ++-- templates/medias/index.rs.html | 2 +- templates/users/details.rs.html | 4 ++-- 20 files changed, 123 insertions(+), 88 deletions(-) diff --git a/plume-cli/src/main.rs b/plume-cli/src/main.rs index 4a87224..cc4b85d 100644 --- a/plume-cli/src/main.rs +++ b/plume-cli/src/main.rs @@ -6,7 +6,7 @@ extern crate rpassword; use clap::App; use diesel::Connection; -use plume_models::{Connection as Conn, CONFIG}; +use plume_models::{instance::Instance, Connection as Conn, CONFIG}; use std::io::{self, prelude::*}; mod instance; @@ -27,6 +27,7 @@ fn main() { dotenv::dotenv().ok(); let conn = Conn::establish(CONFIG.database_url.as_str()); + let _ = conn.as_ref().map(|conn| Instance::cache_local(conn)); match matches.subcommand() { ("instance", Some(args)) => { diff --git a/plume-cli/src/users.rs b/plume-cli/src/users.rs index 38a4972..081b3b1 100644 --- a/plume-cli/src/users.rs +++ b/plume-cli/src/users.rs @@ -129,7 +129,7 @@ fn reset_password<'a>(args: &ArgMatches<'a>, conn: &Connection) { let user = User::find_by_name( conn, &username, - Instance::get_local(conn) + Instance::get_local() .expect("Failed to get local instance") .id, ) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 0fd0e2c..a6556e2 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -172,7 +172,7 @@ impl Blog { let mut icon = Image::default(); icon.object_props.set_url_string( self.icon_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url(conn)).ok()) + .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) .unwrap_or_default(), )?; icon.object_props.set_attributed_to_link( @@ -189,7 +189,7 @@ impl Blog { let mut banner = Image::default(); banner.object_props.set_url_string( self.banner_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url(conn)).ok()) + .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) .unwrap_or_default(), )?; banner.object_props.set_attributed_to_link( @@ -271,14 +271,14 @@ impl Blog { pub fn icon_url(&self, conn: &Connection) -> String { self.icon_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url(conn)).ok()) + .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) .unwrap_or_else(|| "/static/default-avatar.png".to_string()) } pub fn banner_url(&self, conn: &Connection) -> Option { self.banner_id .and_then(|i| Media::get(conn, i).ok()) - .and_then(|c| c.url(conn).ok()) + .and_then(|c| c.url().ok()) } pub fn delete(&self, conn: &Connection, searcher: &Searcher) -> Result<()> { @@ -407,7 +407,9 @@ impl AsActor<&PlumeRocket> for Blog { } fn is_local(&self) -> bool { - self.instance_id == 1 // TODO: this is not always true + Instance::get_local() + .map(|i| self.instance_id == i.id) + .unwrap_or(false) } } @@ -474,7 +476,7 @@ pub(crate) mod tests { "BlogName".to_owned(), "Blog name".to_owned(), "This is a small blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -485,7 +487,7 @@ pub(crate) mod tests { "MyBlog".to_owned(), "My blog".to_owned(), "Welcome to my blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -496,7 +498,7 @@ pub(crate) mod tests { "WhyILikePlume".to_owned(), "Why I like Plume".to_owned(), "In this blog I will explay you why I like Plume so much".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -556,7 +558,7 @@ pub(crate) mod tests { "SomeName".to_owned(), "Some name".to_owned(), "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -564,7 +566,7 @@ pub(crate) mod tests { assert_eq!( blog.get_instance(conn).unwrap().id, - Instance::get_local(conn).unwrap().id + Instance::get_local().unwrap().id ); // TODO add tests for remote instance @@ -584,7 +586,7 @@ pub(crate) mod tests { "SomeName".to_owned(), "Some name".to_owned(), "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -595,7 +597,7 @@ pub(crate) mod tests { "Blog".to_owned(), "Blog".to_owned(), "I've named my blog Blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -687,7 +689,7 @@ pub(crate) mod tests { "SomeName".to_owned(), "Some name".to_owned(), "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -711,7 +713,7 @@ pub(crate) mod tests { "SomeName".to_owned(), "Some name".to_owned(), "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -749,7 +751,7 @@ pub(crate) mod tests { "SomeName".to_owned(), "Some name".to_owned(), "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) @@ -760,7 +762,7 @@ pub(crate) mod tests { "Blog".to_owned(), "Blog".to_owned(), "I've named my blog Blog".to_owned(), - Instance::get_local(conn).unwrap().id, + Instance::get_local().unwrap().id, ) .unwrap(), ) diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index 93b9a44..893b2ab 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -79,7 +79,7 @@ impl Comment { pub fn count_local(conn: &Connection) -> Result { use schema::users; let local_authors = users::table - .filter(users::instance_id.eq(Instance::get_local(conn)?.id)) + .filter(users::instance_id.eq(Instance::get_local()?.id)) .select(users::id); comments::table .filter(comments::author_id.eq_any(local_authors)) @@ -107,7 +107,7 @@ impl Comment { let author = User::get(&c.conn, self.author_id)?; let (html, mentions, _hashtags) = utils::md_to_html( self.content.get().as_ref(), - Some(&Instance::get_local(&c.conn)?.public_domain), + Some(&Instance::get_local()?.public_domain), true, Some(Media::get_media_processor(&c.conn, vec![&author])), ); diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index 07b9a30..003456c 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -1,6 +1,7 @@ use chrono::NaiveDateTime; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use std::iter::Iterator; +use std::sync::RwLock; use ap_url; use medias::Media; @@ -40,8 +41,24 @@ pub struct NewInstance { pub short_description_html: String, } +lazy_static! { + static ref LOCAL_INSTANCE: RwLock> = RwLock::new(None); +} + impl Instance { - pub fn get_local(conn: &Connection) -> Result { + pub fn set_local(self) { + LOCAL_INSTANCE.write().unwrap().replace(self); + } + + pub fn get_local() -> Result { + LOCAL_INSTANCE + .read() + .unwrap() + .clone() + .ok_or(Error::NotFound) + } + + pub fn get_local_uncached(conn: &Connection) -> Result { instances::table .filter(instances::local.eq(true)) .limit(1) @@ -51,6 +68,10 @@ impl Instance { .ok_or(Error::NotFound) } + pub fn cache_local(conn: &Connection) { + *LOCAL_INSTANCE.write().unwrap() = Instance::get_local_uncached(conn).ok(); + } + pub fn get_remotes(conn: &Connection) -> Result> { instances::table .filter(instances::local.eq(false)) @@ -141,7 +162,7 @@ impl Instance { false, Some(Media::get_media_processor(conn, vec![])), ); - diesel::update(self) + let res = diesel::update(self) .set(( instances::name.eq(name), instances::open_registrations.eq(open_registrations), @@ -152,7 +173,11 @@ impl Instance { )) .execute(conn) .map(|_| ()) - .map_err(Error::from) + .map_err(Error::from); + if self.local { + Instance::cache_local(conn); + } + res } pub fn count(conn: &Connection) -> Result { @@ -171,7 +196,7 @@ pub(crate) mod tests { use Connection as Conn; pub(crate) fn fill_database(conn: &Conn) -> Vec<(NewInstance, Instance)> { - vec![ + let res = vec![ NewInstance { default_license: "WTFPL".to_string(), local: true, @@ -225,7 +250,9 @@ pub(crate) mod tests { .unwrap_or_else(|_| Instance::insert(conn, inst).unwrap()), ) }) - .collect() + .collect(); + Instance::cache_local(conn); + res } #[test] @@ -237,7 +264,7 @@ pub(crate) mod tests { .map(|(inserted, _)| inserted) .find(|inst| inst.local) .unwrap(); - let res = Instance::get_local(conn).unwrap(); + let res = Instance::get_local().unwrap(); part_eq!( res, diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index d8be73a..e3d33cc 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -104,8 +104,8 @@ impl Media { } } - pub fn html(&self, conn: &Connection) -> Result { - let url = self.url(conn)?; + pub fn html(&self) -> Result { + let url = self.url()?; Ok(match self.category() { MediaCategory::Image => SafeString::trusted(&format!( r#"{}"#, @@ -126,23 +126,23 @@ impl Media { }) } - pub fn markdown(&self, conn: &Connection) -> Result { + pub fn markdown(&self) -> Result { Ok(match self.category() { MediaCategory::Image => { SafeString::new(&format!("![{}]({})", escape(&self.alt_text), self.id)) } - MediaCategory::Audio | MediaCategory::Video => self.html(conn)?, + MediaCategory::Audio | MediaCategory::Video => self.html()?, MediaCategory::Unknown => SafeString::new(""), }) } - pub fn url(&self, conn: &Connection) -> Result { + pub fn url(&self) -> Result { if self.is_remote { Ok(self.remote_url.clone().unwrap_or_default()) } else { Ok(ap_url(&format!( "{}/{}", - Instance::get_local(conn)?.public_domain, + Instance::get_local()?.public_domain, self.file_path ))) } @@ -237,7 +237,7 @@ impl Media { let media = Media::get(conn, id).ok()?; // if owner is user or check is disabled if uid.contains(&media.owner_id) || uid.is_empty() { - Some((media.url(conn).ok()?, media.content_warning)) + Some((media.url().ok()?, media.content_warning)) } else { None } diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 99a2d53..d622a5d 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -141,7 +141,7 @@ impl Post { use schema::post_authors; use schema::users; let local_authors = users::table - .filter(users::instance_id.eq(Instance::get_local(conn)?.id)) + .filter(users::instance_id.eq(Instance::get_local()?.id)) .select(users::id); let local_posts_id = post_authors::table .filter(post_authors::author_id.eq_any(local_authors)) @@ -384,7 +384,7 @@ impl Post { .collect::>(); let mut tags_json = Tag::for_post(conn, self.id)? .into_iter() - .map(|t| json!(t.to_activity(conn).ok())) + .map(|t| json!(t.to_activity().ok())) .collect::>(); mentions_json.append(&mut tags_json); @@ -419,7 +419,7 @@ impl Post { if let Some(media_id) = self.cover_id { let media = Media::get(conn, media_id)?; let mut cover = Image::default(); - cover.object_props.set_url_string(media.url(conn)?)?; + cover.object_props.set_url_string(media.url()?)?; if media.sensitive { cover .object_props @@ -603,7 +603,7 @@ impl Post { pub fn cover_url(&self, conn: &Connection) -> Option { self.cover_id .and_then(|i| Media::get(conn, i).ok()) - .and_then(|c| c.url(conn).ok()) + .and_then(|c| c.url().ok()) } pub fn build_delete(&self, conn: &Connection) -> Result { diff --git a/plume-models/src/tags.rs b/plume-models/src/tags.rs index 5711853..eba2681 100644 --- a/plume-models/src/tags.rs +++ b/plume-models/src/tags.rs @@ -27,11 +27,11 @@ impl Tag { find_by!(tags, find_by_name, tag as &str); list_by!(tags, for_post, post_id as i32); - pub fn to_activity(&self, conn: &Connection) -> Result { + pub fn to_activity(&self) -> Result { let mut ht = Hashtag::default(); ht.set_href_string(ap_url(&format!( "{}/tag/{}", - Instance::get_local(conn)?.public_domain, + Instance::get_local()?.public_domain, self.tag )))?; ht.set_name_string(self.tag.clone())?; @@ -54,11 +54,11 @@ impl Tag { ) } - pub fn build_activity(conn: &Connection, tag: String) -> Result { + pub fn build_activity(tag: String) -> Result { let mut ht = Hashtag::default(); ht.set_href_string(ap_url(&format!( "{}/tag/{}", - Instance::get_local(conn)?.public_domain, + Instance::get_local()?.public_domain, tag )))?; ht.set_name_string(tag)?; diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 0460b7b..2c07353 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -228,7 +228,7 @@ impl User { pub fn count_local(conn: &Connection) -> Result { users::table - .filter(users::instance_id.eq(Instance::get_local(conn)?.id)) + .filter(users::instance_id.eq(Instance::get_local()?.id)) .count() .get_result(conn) .map_err(Error::from) @@ -353,7 +353,7 @@ impl User { pub fn get_local_page(conn: &Connection, (min, max): (i32, i32)) -> Result> { users::table - .filter(users::instance_id.eq(Instance::get_local(conn)?.id)) + .filter(users::instance_id.eq(Instance::get_local()?.id)) .order(users::username.asc()) .offset(min.into()) .limit((max - min).into()) @@ -634,7 +634,7 @@ impl User { let mut avatar = Image::default(); avatar.object_props.set_url_string( self.avatar_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url(conn)).ok()) + .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) .unwrap_or_default(), )?; actor.object_props.set_icon_object(avatar)?; @@ -667,7 +667,7 @@ impl User { pub fn avatar_url(&self, conn: &Connection) -> String { self.avatar_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url(conn)).ok()) + .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) .unwrap_or_else(|| "/static/default-avatar.png".to_string()) } @@ -861,7 +861,9 @@ impl AsActor<&PlumeRocket> for User { } fn is_local(&self) -> bool { - self.instance_id == 1 + Instance::get_local() + .map(|i| self.instance_id == i.id) + .unwrap_or(false) } } @@ -934,7 +936,7 @@ impl NewUser { summary_html: SafeString::new(&utils::md_to_html(&summary, None, false, None).0), email: Some(email), hashed_password: Some(password), - instance_id: Instance::get_local(conn)?.id, + instance_id: Instance::get_local()?.id, ap_url: String::new(), public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?, private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?), @@ -1007,7 +1009,7 @@ pub(crate) mod tests { assert_eq!( test_user.id, - User::find_by_name(conn, "test", Instance::get_local(conn).unwrap().id) + User::find_by_name(conn, "test", Instance::get_local().unwrap().id) .unwrap() .id ); @@ -1025,7 +1027,7 @@ pub(crate) mod tests { conn, &format!( "https://{}/@/{}/", - Instance::get_local(conn).unwrap().public_domain, + Instance::get_local().unwrap().public_domain, "test" ) ) @@ -1056,7 +1058,7 @@ pub(crate) mod tests { let conn = &db(); conn.test_transaction::<_, (), _>(|| { let inserted = fill_database(conn); - let local_inst = Instance::get_local(conn).unwrap(); + let local_inst = Instance::get_local().unwrap(); let mut i = 0; while local_inst.has_admin(conn).unwrap() { assert!(i < 100); //prevent from looping indefinitelly diff --git a/src/api/posts.rs b/src/api/posts.rs index 2151658..2399cf5 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -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")) }), diff --git a/src/main.rs b/src/main.rs index f3cd655..d609ff1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { dotenv::dotenv().ok(); let manager = ConnectionManager::::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() { diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 24d9b83..08d5f43 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -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> 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( diff --git a/src/routes/comments.rs b/src/routes/comments.rs index fcd9b99..8c38231 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -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, ), diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 26b057c..98757c6 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -19,7 +19,7 @@ use template_utils::{IntoContext, Ructe}; #[get("/")] pub fn index(rockets: PlumeRocket) -> Result { 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 { #[get("/local?")] pub fn local(page: Option, rockets: PlumeRocket) -> Result { 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, rockets: PlumeRocket) -> Result Result { - 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 Result 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 { 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 { } #[get("/manifest.json")] -pub fn web_manifest(conn: DbConn) -> Result, ErrorPage> { - let instance = Instance::get_local(&*conn)?; +pub fn web_manifest() -> Result, ErrorPage> { + let instance = Instance::get_local()?; Ok(Json(json!({ "name": &instance.name, "description": &instance.short_description, diff --git a/src/routes/posts.rs b/src/routes/posts.rs index b836b39..0ca476a 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -147,7 +147,7 @@ pub fn new(blog: String, cl: ContentLen, rockets: PlumeRocket) -> Result>() .into_iter() - .filter_map(|t| Tag::build_activity(&conn, t).ok()) + .filter_map(|t| Tag::build_activity(t).ok()) .collect::>(); post.update_tags(&conn, tags) .expect("post::update: tags error"); @@ -324,7 +324,7 @@ pub fn update( .map(|h| h.to_camel_case()) .collect::>() .into_iter() - .filter_map(|t| Tag::build_activity(&conn, t).ok()) + .filter_map(|t| Tag::build_activity(t).ok()) .collect::>(); 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, ), diff --git a/src/routes/user.rs b/src/routes/user.rs index c45f2c3..fe47939 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -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 { 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, 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> 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( diff --git a/templates/blogs/details.rs.html b/templates/blogs/details.rs.html index a5babe0..6dbf7a5 100644 --- a/templates/blogs/details.rs.html +++ b/templates/blogs/details.rs.html @@ -13,7 +13,7 @@ - + @@ -21,7 +21,7 @@ - + }, { @blog.title diff --git a/templates/medias/details.rs.html b/templates/medias/details.rs.html index a541993..fe6d3ed 100644 --- a/templates/medias/details.rs.html +++ b/templates/medias/details.rs.html @@ -14,7 +14,7 @@
- @Html(media.html(ctx.0).unwrap_or_else(|_| SafeString::new(""))) + @Html(media.html().unwrap_or_else(|_| SafeString::new("")))
@media.alt_text
@@ -22,7 +22,7 @@ @i18n!(ctx.1, "Markdown syntax") @i18n!(ctx.1, "Copy it into your articles, to insert this media:")

- @media.markdown(ctx.0).unwrap_or_else(|_| SafeString::new("")) + @media.markdown().unwrap_or_else(|_| SafeString::new(""))
@if media.category() == MediaCategory::Image { diff --git a/templates/medias/index.rs.html b/templates/medias/index.rs.html index a2927ad..9764e75 100644 --- a/templates/medias/index.rs.html +++ b/templates/medias/index.rs.html @@ -20,7 +20,7 @@
diff --git a/templates/users/details.rs.html b/templates/users/details.rs.html index d27fd9a..7663f2e 100644 --- a/templates/users/details.rs.html +++ b/templates/users/details.rs.html @@ -12,7 +12,7 @@ - + @@ -20,7 +20,7 @@ - + }, {}, { @:header(ctx, &user, follows, is_remote, remote_url)