2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::webfinger::webfinger_resolve_actor,
|
|
|
|
traits::{Actor, Object},
|
|
|
|
};
|
|
|
|
use diesel::NotFound;
|
2022-03-23 21:27:51 +00:00
|
|
|
use itertools::Itertools;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2022-03-23 21:27:51 +00:00
|
|
|
use lemmy_db_schema::traits::ApubActor;
|
2023-03-21 15:03:05 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
2022-06-22 20:24:54 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-03-23 21:27:51 +00:00
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
pub mod post_or_comment;
|
2021-03-11 04:43:11 +00:00
|
|
|
pub mod search;
|
2021-11-03 17:33:51 +00:00
|
|
|
pub mod user_or_community;
|
2022-03-23 21:27:51 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
/// Resolve actor identifier like `!news@example.com` to user or community object.
|
|
|
|
///
|
|
|
|
/// In case the requesting user is logged in and the object was not found locally, it is attempted
|
|
|
|
/// to fetch via webfinger from the original instance.
|
2022-03-23 21:27:51 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
pub async fn resolve_actor_identifier<ActorType, DbActor>(
|
2022-03-23 21:27:51 +00:00
|
|
|
identifier: &str,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
local_user_view: &Option<LocalUserView>,
|
2022-09-28 20:54:32 +00:00
|
|
|
include_deleted: bool,
|
2023-03-21 15:03:05 +00:00
|
|
|
) -> Result<ActorType, LemmyError>
|
2022-03-23 21:27:51 +00:00
|
|
|
where
|
2023-03-21 15:03:05 +00:00
|
|
|
ActorType: Object<DataType = LemmyContext, Error = LemmyError>
|
|
|
|
+ Object
|
|
|
|
+ Actor
|
|
|
|
+ From<DbActor>
|
2022-06-02 14:33:41 +00:00
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2023-03-21 15:03:05 +00:00
|
|
|
for<'de2> <ActorType as Object>::Kind: serde::Deserialize<'de2>,
|
2022-03-23 21:27:51 +00:00
|
|
|
DbActor: ApubActor + Send + 'static,
|
|
|
|
{
|
|
|
|
// remote actor
|
|
|
|
if identifier.contains('@') {
|
|
|
|
let (name, domain) = identifier
|
|
|
|
.splitn(2, '@')
|
|
|
|
.collect_tuple()
|
|
|
|
.expect("invalid query");
|
|
|
|
let name = name.to_string();
|
2022-06-22 20:24:54 +00:00
|
|
|
let domain = format!("{}://{}", context.settings().get_protocol_string(), domain);
|
2022-11-09 10:05:00 +00:00
|
|
|
let actor = DbActor::read_from_name_and_domain(context.pool(), &name, &domain).await;
|
2022-03-23 21:27:51 +00:00
|
|
|
if actor.is_ok() {
|
2023-03-21 15:03:05 +00:00
|
|
|
Ok(actor?.into())
|
|
|
|
} else if local_user_view.is_some() {
|
2022-03-23 21:27:51 +00:00
|
|
|
// Fetch the actor from its home instance using webfinger
|
2023-03-21 15:03:05 +00:00
|
|
|
let actor: ActorType = webfinger_resolve_actor(identifier, context).await?;
|
2022-03-23 21:27:51 +00:00
|
|
|
Ok(actor)
|
2023-03-21 15:03:05 +00:00
|
|
|
} else {
|
|
|
|
Err(NotFound.into())
|
2022-03-23 21:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// local actor
|
|
|
|
else {
|
|
|
|
let identifier = identifier.to_string();
|
2023-03-21 15:03:05 +00:00
|
|
|
Ok(
|
|
|
|
DbActor::read_from_name(context.pool(), &identifier, include_deleted)
|
|
|
|
.await?
|
|
|
|
.into(),
|
|
|
|
)
|
2022-03-23 21:27:51 +00:00
|
|
|
}
|
|
|
|
}
|