]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/mod.rs
Check user accepted before sending jwt in password reset (fixes #2591) (#2597)
[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_db_schema::traits::ApubActor;
5 use lemmy_utils::error::LemmyError;
6 use lemmy_websocket::LemmyContext;
7
8 pub mod post_or_comment;
9 pub mod search;
10 pub mod user_or_community;
11 pub mod webfinger;
12
13 /// Resolve actor identifier (eg `!news@example.com`) from local database to avoid network requests.
14 /// This only works for local actors, and remote actors which were previously fetched (so it doesnt
15 /// trigger any new fetch).
16 #[tracing::instrument(skip_all)]
17 pub async fn resolve_actor_identifier<Actor, DbActor>(
18   identifier: &str,
19   context: &LemmyContext,
20   include_deleted: bool,
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 = DbActor::read_from_name_and_domain(context.pool(), &name, &domain).await;
40     if actor.is_ok() {
41       Ok(actor?)
42     } else {
43       // Fetch the actor from its home instance using webfinger
44       let id = webfinger_resolve_actor::<Actor>(identifier, true, context, &mut 0).await?;
45       let actor: DbActor = DbActor::read_from_apub_id(context.pool(), &id)
46         .await?
47         .expect("actor exists as we fetched just before");
48       Ok(actor)
49     }
50   }
51   // local actor
52   else {
53     let identifier = identifier.to_string();
54     Ok(DbActor::read_from_name(context.pool(), &identifier, include_deleted).await?)
55   }
56 }