3 community::get_or_fetch_and_upsert_community,
4 fetch::fetch_remote_object,
6 person::get_or_fetch_and_upsert_person,
9 objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
13 use itertools::Itertools;
14 use lemmy_api_common::{blocking, site::ResolveObjectResponse};
15 use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
16 use lemmy_db_queries::source::{
18 community::Community_,
21 private_message::PrivateMessage_,
23 use lemmy_db_schema::source::{
28 private_message::PrivateMessage,
31 comment_view::CommentView,
32 local_user_view::LocalUserView,
35 use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
36 use lemmy_utils::LemmyError;
37 use lemmy_websocket::LemmyContext;
40 /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
41 #[derive(serde::Deserialize, Debug)]
43 enum SearchAcceptedObjects {
44 Person(Box<ApubPerson>),
50 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
52 /// Some working examples for use with the `docker/federation/` setup:
53 /// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
54 /// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
55 /// http://lemmy_gamma:8561/post/3
56 /// http://lemmy_delta:8571/comment/2
57 pub async fn search_by_apub_id(
59 local_user_view: Option<LocalUserView>,
60 context: &LemmyContext,
61 ) -> Result<ResolveObjectResponse, LemmyError> {
62 let query_url = match Url::parse(query) {
65 let (kind, name) = query.split_at(1);
66 let kind = match kind {
67 "@" => WebfingerType::Person,
68 "!" => WebfingerType::Group,
69 _ => return Err(anyhow!("invalid query").into()),
71 // remote actor, use webfinger to resolve url
72 if name.contains('@') {
73 let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
74 webfinger_resolve_actor(name, domain, kind, context.client()).await?
76 // local actor, read from database and return
78 let name: String = name.into();
80 WebfingerType::Group => {
81 let res = blocking(context.pool(), move |conn| {
82 let community = Community::read_from_name(conn, &name)?;
83 CommunityView::read(conn, community.id, local_user_view.map(|l| l.person.id))
86 Ok(ResolveObjectResponse {
88 ..ResolveObjectResponse::default()
91 WebfingerType::Person => {
92 let res = blocking(context.pool(), move |conn| {
93 let person = Person::find_by_name(conn, &name)?;
94 PersonViewSafe::read(conn, person.id)
97 Ok(ResolveObjectResponse {
99 ..ResolveObjectResponse::default()
107 let request_counter = &mut 0;
108 // this does a fetch (even for local objects), just to determine its type and fetch it again
109 // below. we need to fix this when rewriting the fetcher.
111 fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, request_counter)
113 if is_deleted(&fetch_response) {
114 delete_object_locally(&query_url, context).await?;
115 return Err(anyhow!("Object was deleted").into());
118 // Necessary because we get a stack overflow using FetchError
119 let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?;
120 build_response(fet_res, query_url, request_counter, context).await
123 async fn build_response(
124 fetch_response: SearchAcceptedObjects,
126 recursion_counter: &mut i32,
127 context: &LemmyContext,
128 ) -> Result<ResolveObjectResponse, LemmyError> {
129 use ResolveObjectResponse as ROR;
130 Ok(match fetch_response {
131 SearchAcceptedObjects::Person(p) => {
132 let person_uri = p.id(&query_url)?;
134 let person = get_or_fetch_and_upsert_person(person_uri, context, recursion_counter).await?;
136 person: blocking(context.pool(), move |conn| {
137 PersonViewSafe::read(conn, person.id)
144 SearchAcceptedObjects::Group(g) => {
145 let community_uri = g.id(&query_url)?;
147 get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?;
149 community: blocking(context.pool(), move |conn| {
150 CommunityView::read(conn, community.id, None)
157 SearchAcceptedObjects::Page(p) => {
158 let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?;
160 post: blocking(context.pool(), move |conn| PostView::read(conn, p.id, None))
166 SearchAcceptedObjects::Comment(c) => {
167 let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?;
169 comment: blocking(context.pool(), move |conn| {
170 CommentView::read(conn, c.id, None)
180 async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> {
181 let res = find_object_by_id(context, query_url.to_owned()).await?;
183 Object::Comment(c) => {
184 blocking(context.pool(), move |conn| {
185 Comment::update_deleted(conn, c.id, true)
190 blocking(context.pool(), move |conn| {
191 Post::update_deleted(conn, p.id, true)
195 Object::Person(u) => {
196 // TODO: implement update_deleted() for user, move it to ApubObject trait
197 blocking(context.pool(), move |conn| {
198 Person::delete_account(conn, u.id)
202 Object::Community(c) => {
203 blocking(context.pool(), move |conn| {
204 Community::update_deleted(conn, c.id, true)
208 Object::PrivateMessage(pm) => {
209 blocking(context.pool(), move |conn| {
210 PrivateMessage::update_deleted(conn, pm.id, true)