2021-11-05 00:24:10 +00:00
|
|
|
use crate::{
|
|
|
|
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
|
|
|
protocol::objects::{group::Group, note::Note, page::Page, person::Person},
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
|
|
|
|
traits::Object,
|
|
|
|
};
|
2021-10-28 15:25:26 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorType};
|
2021-11-05 00:24:10 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use url::Url;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
2022-10-13 16:30:31 +00:00
|
|
|
/// Converts search query to object id. The query can either be an URL, which will be treated as
|
|
|
|
/// ObjectId directly, or a webfinger identifier (@user@example.com or !community@example.com)
|
|
|
|
/// which gets resolved to an URL.
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-11-28 14:29:33 +00:00
|
|
|
pub(crate) async fn search_query_to_object_id(
|
2021-01-12 16:12:41 +00:00
|
|
|
query: &str,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-09-25 15:44:52 +00:00
|
|
|
) -> Result<SearchableObjects, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
Ok(match Url::parse(query) {
|
|
|
|
Ok(url) => {
|
|
|
|
// its already an url, just go with it
|
|
|
|
ObjectId::from(url).dereference(context).await?
|
|
|
|
}
|
2021-07-20 07:00:20 +00:00
|
|
|
Err(_) => {
|
2022-10-13 16:30:31 +00:00
|
|
|
// not an url, try to resolve via webfinger
|
2022-07-27 21:03:44 +00:00
|
|
|
let mut chars = query.chars();
|
|
|
|
let kind = chars.next();
|
|
|
|
let identifier = chars.as_str();
|
2023-03-21 15:03:05 +00:00
|
|
|
match kind {
|
|
|
|
Some('@') => SearchableObjects::Person(
|
|
|
|
webfinger_resolve_actor::<LemmyContext, ApubPerson>(identifier, context).await?,
|
|
|
|
),
|
|
|
|
Some('!') => SearchableObjects::Community(
|
|
|
|
webfinger_resolve_actor::<LemmyContext, ApubCommunity>(identifier, context).await?,
|
|
|
|
),
|
2023-07-10 14:50:07 +00:00
|
|
|
_ => return Err(LemmyErrorType::InvalidQuery)?,
|
2023-03-21 15:03:05 +00:00
|
|
|
}
|
2021-07-20 07:00:20 +00:00
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
})
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 16:17:42 +00:00
|
|
|
/// Converts a search query to an object id. The query MUST bbe a URL which will bbe treated
|
|
|
|
/// as the ObjectId directly. If the query is a webfinger identifier (@user@example.com or
|
|
|
|
/// !community@example.com) this method will return an error.
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
pub(crate) async fn search_query_to_object_id_local(
|
|
|
|
query: &str,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<SearchableObjects, LemmyError> {
|
|
|
|
let url = Url::parse(query)?;
|
|
|
|
ObjectId::from(url).dereference_local(context).await
|
|
|
|
}
|
|
|
|
|
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)]
|
2022-11-28 14:29:33 +00:00
|
|
|
pub(crate) 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)]
|
2023-03-21 15:03:05 +00:00
|
|
|
pub(crate) enum SearchableKinds {
|
2021-09-25 15:44:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for SearchableObjects {
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = SearchableKinds;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
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-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2021-10-18 21:36:44 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let c = ApubCommunity::read_from_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)));
|
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
let p = ApubPerson::read_from_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
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
let p = ApubPost::read_from_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
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
let c = ApubComment::read_from_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-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, data: &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
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, LemmyError> {
|
2021-10-27 16:03:07 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
2023-03-21 15:03:05 +00:00
|
|
|
apub: &Self::Kind,
|
2021-11-06 17:35:14 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
data: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
match apub {
|
2023-03-21 15:03:05 +00:00
|
|
|
SearchableKinds::Group(a) => ApubCommunity::verify(a, expected_domain, data).await,
|
|
|
|
SearchableKinds::Person(a) => ApubPerson::verify(a, expected_domain, data).await,
|
|
|
|
SearchableKinds::Page(a) => ApubPost::verify(a, expected_domain, data).await,
|
|
|
|
SearchableKinds::Note(a) => ApubComment::verify(a, expected_domain, data).await,
|
2021-11-06 17:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(apub: Self::Kind, context: &Data<LemmyContext>) -> Result<Self, LemmyError> {
|
|
|
|
use SearchableKinds as SAT;
|
2021-09-25 15:44:52 +00:00
|
|
|
use SearchableObjects as SO;
|
|
|
|
Ok(match apub {
|
2023-03-21 15:03:05 +00:00
|
|
|
SAT::Group(g) => SO::Community(ApubCommunity::from_json(g, context).await?),
|
|
|
|
SAT::Person(p) => SO::Person(ApubPerson::from_json(p, context).await?),
|
|
|
|
SAT::Page(p) => SO::Post(ApubPost::from_json(p, context).await?),
|
|
|
|
SAT::Note(n) => SO::Comment(ApubComment::from_json(n, context).await?),
|
2021-09-25 15:44:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|