2021-11-05 00:24:10 +00:00
|
|
|
use crate::{
|
2021-11-16 17:03:09 +00:00
|
|
|
fetcher::webfinger::webfinger_resolve,
|
2021-11-05 00:24:10 +00:00
|
|
|
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
|
|
|
protocol::objects::{group::Group, note::Note, page::Page, person::Person},
|
2021-11-16 17:03:09 +00:00
|
|
|
EndpointType,
|
2021-11-05 00:24:10 +00:00
|
|
|
};
|
2021-08-12 12:48:09 +00:00
|
|
|
use anyhow::anyhow;
|
2021-10-28 15:25:26 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-11-16 17:03:09 +00:00
|
|
|
use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
|
2021-07-20 07:00:20 +00:00
|
|
|
use lemmy_utils::LemmyError;
|
2021-01-12 16:12:41 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-11-05 00:24:10 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use url::Url;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
|
|
|
/// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
|
|
|
|
///
|
|
|
|
/// Some working examples for use with the `docker/federation/` setup:
|
|
|
|
/// http://lemmy_alpha:8541/c/main, or !main@lemmy_alpha:8541
|
|
|
|
/// http://lemmy_beta:8551/u/lemmy_alpha, or @lemmy_beta@lemmy_beta:8551
|
|
|
|
/// http://lemmy_gamma:8561/post/3
|
|
|
|
/// http://lemmy_delta:8571/comment/2
|
|
|
|
pub async fn search_by_apub_id(
|
|
|
|
query: &str,
|
|
|
|
context: &LemmyContext,
|
2021-09-25 15:44:52 +00:00
|
|
|
) -> Result<SearchableObjects, LemmyError> {
|
2021-11-16 17:03:09 +00:00
|
|
|
let request_counter = &mut 0;
|
|
|
|
match Url::parse(query) {
|
|
|
|
Ok(url) => {
|
|
|
|
ObjectId::new(url)
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await
|
|
|
|
}
|
2021-07-20 07:00:20 +00:00
|
|
|
Err(_) => {
|
2021-11-16 17:03:09 +00:00
|
|
|
let (kind, identifier) = query.split_at(1);
|
|
|
|
match kind {
|
|
|
|
"@" => {
|
|
|
|
let id = webfinger_resolve::<ApubPerson>(
|
|
|
|
identifier,
|
|
|
|
EndpointType::Person,
|
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(SearchableObjects::Person(
|
|
|
|
ObjectId::new(id)
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
"!" => {
|
|
|
|
let id = webfinger_resolve::<ApubCommunity>(
|
|
|
|
identifier,
|
|
|
|
EndpointType::Community,
|
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(SearchableObjects::Community(
|
|
|
|
ObjectId::new(id)
|
|
|
|
.dereference(context, request_counter)
|
|
|
|
.await?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => Err(anyhow!("invalid query").into()),
|
2021-07-20 07:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-16 17:03:09 +00:00
|
|
|
}
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
/// The types of ActivityPub objects that can be fetched directly by searching for their ID.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SearchableObjects {
|
2021-10-18 21:36:44 +00:00
|
|
|
Person(ApubPerson),
|
|
|
|
Community(ApubCommunity),
|
|
|
|
Post(ApubPost),
|
|
|
|
Comment(ApubComment),
|
2021-09-25 15:44:52 +00:00
|
|
|
}
|
2021-01-12 16:12:41 +00:00
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum SearchableApubTypes {
|
|
|
|
Group(Group),
|
2021-10-18 21:36:44 +00:00
|
|
|
Person(Person),
|
2021-09-25 15:44:52 +00:00
|
|
|
Page(Page),
|
|
|
|
Note(Note),
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-09-25 15:44:52 +00:00
|
|
|
impl ApubObject for SearchableObjects {
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2021-10-27 16:03:07 +00:00
|
|
|
type ApubType = SearchableApubTypes;
|
|
|
|
type TombstoneType = ();
|
2021-10-06 20:20:05 +00:00
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
match self {
|
|
|
|
SearchableObjects::Person(p) => p.last_refreshed_at(),
|
|
|
|
SearchableObjects::Community(c) => c.last_refreshed_at(),
|
|
|
|
SearchableObjects::Post(p) => p.last_refreshed_at(),
|
|
|
|
SearchableObjects::Comment(c) => c.last_refreshed_at(),
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is inefficient, because if the object is not in local db, it will run 4 db queries
|
|
|
|
// before finally returning an error. it would be nice if we could check all 4 tables in
|
|
|
|
// a single query.
|
2021-10-06 20:20:05 +00:00
|
|
|
// we could skip this and always return an error, but then it would always fetch objects
|
|
|
|
// over http, and not be able to mark objects as deleted that were deleted by remote server.
|
2021-10-18 21:36:44 +00:00
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
let c = ApubCommunity::read_from_apub_id(object_id.clone(), context).await?;
|
2021-10-06 20:20:05 +00:00
|
|
|
if let Some(c) = c {
|
|
|
|
return Ok(Some(SearchableObjects::Community(c)));
|
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
let p = ApubPerson::read_from_apub_id(object_id.clone(), context).await?;
|
2021-10-06 20:20:05 +00:00
|
|
|
if let Some(p) = p {
|
|
|
|
return Ok(Some(SearchableObjects::Person(p)));
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
let p = ApubPost::read_from_apub_id(object_id.clone(), context).await?;
|
2021-10-06 20:20:05 +00:00
|
|
|
if let Some(p) = p {
|
|
|
|
return Ok(Some(SearchableObjects::Post(p)));
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
let c = ApubComment::read_from_apub_id(object_id, context).await?;
|
2021-10-06 20:20:05 +00:00
|
|
|
if let Some(c) = c {
|
|
|
|
return Ok(Some(SearchableObjects::Comment(c)));
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
2021-10-06 20:20:05 +00:00
|
|
|
Ok(None)
|
2021-09-25 15:44:52 +00:00
|
|
|
}
|
2021-10-16 11:14:02 +00:00
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
|
2021-10-16 11:14:02 +00:00
|
|
|
match self {
|
2021-10-18 21:36:44 +00:00
|
|
|
SearchableObjects::Person(p) => p.delete(data).await,
|
|
|
|
SearchableObjects::Community(c) => c.delete(data).await,
|
|
|
|
SearchableObjects::Post(p) => p.delete(data).await,
|
|
|
|
SearchableObjects::Comment(c) => c.delete(data).await,
|
2021-10-16 11:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
2021-10-27 16:03:07 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
|
|
|
apub: &Self::ApubType,
|
|
|
|
expected_domain: &Url,
|
|
|
|
data: &Self::DataType,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
match apub {
|
|
|
|
SearchableApubTypes::Group(a) => {
|
|
|
|
ApubCommunity::verify(a, expected_domain, data, request_counter).await
|
|
|
|
}
|
|
|
|
SearchableApubTypes::Person(a) => {
|
|
|
|
ApubPerson::verify(a, expected_domain, data, request_counter).await
|
|
|
|
}
|
|
|
|
SearchableApubTypes::Page(a) => {
|
|
|
|
ApubPost::verify(a, expected_domain, data, request_counter).await
|
|
|
|
}
|
|
|
|
SearchableApubTypes::Note(a) => {
|
|
|
|
ApubComment::verify(a, expected_domain, data, request_counter).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
async fn from_apub(
|
2021-11-06 12:37:55 +00:00
|
|
|
apub: Self::ApubType,
|
2021-09-25 15:44:52 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
rc: &mut i32,
|
|
|
|
) -> Result<Self, LemmyError> {
|
|
|
|
use SearchableApubTypes as SAT;
|
|
|
|
use SearchableObjects as SO;
|
|
|
|
Ok(match apub {
|
2021-11-06 17:35:14 +00:00
|
|
|
SAT::Group(g) => SO::Community(ApubCommunity::from_apub(g, context, rc).await?),
|
|
|
|
SAT::Person(p) => SO::Person(ApubPerson::from_apub(p, context, rc).await?),
|
|
|
|
SAT::Page(p) => SO::Post(ApubPost::from_apub(p, context, rc).await?),
|
|
|
|
SAT::Note(n) => SO::Comment(ApubComment::from_apub(n, context, rc).await?),
|
2021-09-25 15:44:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|