Retrieve signing user from database instead of expecting it to materialize from thin air

This commit is contained in:
Violet white 2020-01-14 13:56:51 -05:00
parent 2b4e802914
commit 26867ca6be
2 changed files with 11 additions and 14 deletions

View File

@ -42,7 +42,6 @@ pub struct NewInstance {
lazy_static! { lazy_static! {
static ref LOCAL_INSTANCE: RwLock<Option<Instance>> = RwLock::new(None); static ref LOCAL_INSTANCE: RwLock<Option<Instance>> = RwLock::new(None);
static ref INSTANCE_USER: RwLock<Option<User>> = RwLock::new(None);
} }
impl Instance { impl Instance {
@ -57,15 +56,6 @@ impl Instance {
.clone() .clone()
.ok_or(Error::NotFound) .ok_or(Error::NotFound)
} }
pub fn set_local_user(u: User) {
INSTANCE_USER.write().unwrap().replace(u);
}
pub fn get_local_user() -> Result<User> {
INSTANCE_USER.read().unwrap().clone().ok_or(Error::NotFound)
}
pub fn get_local_uncached(conn: &Connection) -> Result<Instance> { pub fn get_local_uncached(conn: &Connection) -> Result<Instance> {
instances::table instances::table
.filter(instances::local.eq(true)) .filter(instances::local.eq(true))

View File

@ -211,6 +211,13 @@ impl User {
} }
} }
pub fn find_first_local(conn: &Connection) -> Result<User> {
users::table
.filter(users::instance_id.eq(Instance::get_local()?.id))
.first(&*conn)
.map_err(|_| Error::NotFound)
}
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<User> { fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<User> {
let link = resolve(acct.to_owned(), true)? let link = resolve(acct.to_owned(), true)?
.links .links
@ -229,7 +236,7 @@ impl User {
.ok_or(Error::Webfinger) .ok_or(Error::Webfinger)
} }
fn fetch(url: &str) -> Result<CustomPerson> { fn fetch(url: &str, lu: User) -> Result<CustomPerson> {
let mut headers = plume_common::activity_pub::request::headers(); let mut headers = plume_common::activity_pub::request::headers();
headers.insert( headers.insert(
ACCEPT, ACCEPT,
@ -240,7 +247,6 @@ impl User {
.join(", "), .join(", "),
)?, )?,
); );
let lu = Instance::get_local_user()?;
let mut res = ClientBuilder::new() let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5))) .connect_timeout(Some(std::time::Duration::from_secs(5)))
.build()? .build()?
@ -261,11 +267,12 @@ impl User {
} }
pub fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> { pub fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> {
User::fetch(url).and_then(|json| User::from_activity(c, json)) User::fetch(url, User::find_first_local(&*c.conn)?)
.and_then(|json| User::from_activity(c, json))
} }
pub fn refetch(&self, conn: &Connection) -> Result<()> { pub fn refetch(&self, conn: &Connection) -> Result<()> {
User::fetch(&self.ap_url.clone()).and_then(|json| { User::fetch(&self.ap_url.clone(), User::find_first_local(conn)?).and_then(|json| {
let avatar = Media::save_remote( let avatar = Media::save_remote(
conn, conn,
json.object json.object