]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/mod.rs
Be more explicit about returning deleted actors or not (#2335)
[lemmy.git] / crates / apub / src / fetcher / mod.rs
1 use crate::{fetcher::webfinger::webfinger_resolve_actor, ActorType};
2 use activitypub_federation::traits::ApubObject;
3 use itertools::Itertools;
4 use lemmy_api_common::utils::blocking;
5 use lemmy_db_schema::traits::ApubActor;
6 use lemmy_utils::error::LemmyError;
7 use lemmy_websocket::LemmyContext;
8
9 pub mod post_or_comment;
10 pub mod search;
11 pub mod user_or_community;
12 pub mod webfinger;
13
14 /// Resolve actor identifier (eg `!news@example.com`) from local database to avoid network requests.
15 /// This only works for local actors, and remote actors which were previously fetched (so it doesnt
16 /// trigger any new fetch).
17 #[tracing::instrument(skip_all)]
18 pub async fn resolve_actor_identifier<Actor, DbActor>(
19   identifier: &str,
20   context: &LemmyContext,
21 ) -> Result<DbActor, LemmyError>
22 where
23   Actor: ApubObject<DataType = LemmyContext, Error = LemmyError>
24     + ApubObject<DbType = DbActor>
25     + ActorType
26     + Send
27     + 'static,
28   for<'de2> <Actor as ApubObject>::ApubType: serde::Deserialize<'de2>,
29   DbActor: ApubActor + Send + 'static,
30 {
31   // remote actor
32   if identifier.contains('@') {
33     let (name, domain) = identifier
34       .splitn(2, '@')
35       .collect_tuple()
36       .expect("invalid query");
37     let name = name.to_string();
38     let domain = format!("{}://{}", context.settings().get_protocol_string(), domain);
39     let actor = blocking(context.pool(), move |conn| {
40       DbActor::read_from_name_and_domain(conn, &name, &domain)
41     })
42     .await?;
43     if actor.is_ok() {
44       Ok(actor?)
45     } else {
46       // Fetch the actor from its home instance using webfinger
47       let id = webfinger_resolve_actor::<Actor>(identifier, context, &mut 0).await?;
48       let actor: DbActor = blocking(context.pool(), move |conn| {
49         DbActor::read_from_apub_id(conn, &id)
50       })
51       .await??
52       .expect("actor exists as we fetched just before");
53       Ok(actor)
54     }
55   }
56   // local actor
57   else {
58     let identifier = identifier.to_string();
59     Ok(
60       blocking(context.pool(), move |conn| {
61         DbActor::read_from_name(conn, &identifier, false)
62       })
63       .await??,
64     )
65   }
66 }