2024-10-03 12:24:05 +00:00
|
|
|
use super::post_or_comment::{PageOrNote, PostOrComment};
|
|
|
|
use crate::fetcher::user_or_community::{PersonOrGroup, UserOrCommunity};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
|
|
|
|
traits::Object,
|
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyResult};
|
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(
|
2024-03-15 12:42:09 +00:00
|
|
|
mut query: String,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<SearchableObjects> {
|
2024-03-15 12:42:09 +00:00
|
|
|
Ok(match Url::parse(&query) {
|
2023-03-21 15:03:05 +00:00
|
|
|
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
|
2024-03-15 12:42:09 +00:00
|
|
|
if query.starts_with('!') || query.starts_with('@') {
|
|
|
|
query.remove(0);
|
2023-03-21 15:03:05 +00:00
|
|
|
}
|
2024-03-15 12:42:09 +00:00
|
|
|
SearchableObjects::PersonOrCommunity(Box::new(
|
|
|
|
webfinger_resolve_actor::<LemmyContext, UserOrCommunity>(&query, context).await?,
|
|
|
|
))
|
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>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<SearchableObjects> {
|
2023-07-26 16:17:42 +00:00
|
|
|
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 {
|
2024-10-03 12:24:05 +00:00
|
|
|
PostOrComment(Box<PostOrComment>),
|
2024-03-15 12:42:09 +00:00
|
|
|
PersonOrCommunity(Box<UserOrCommunity>),
|
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 {
|
2024-10-03 12:24:05 +00:00
|
|
|
PageOrNote(Box<PageOrNote>),
|
2024-03-15 12:42:09 +00:00
|
|
|
PersonOrGroup(Box<PersonOrGroup>),
|
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
|
|
|
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2021-09-25 15:44:52 +00:00
|
|
|
match self {
|
2024-10-03 12:24:05 +00:00
|
|
|
SearchableObjects::PostOrComment(p) => p.last_refreshed_at(),
|
2024-03-15 12:42:09 +00:00
|
|
|
SearchableObjects::PersonOrCommunity(p) => p.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>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Option<Self>> {
|
2024-03-15 12:42:09 +00:00
|
|
|
let uc = UserOrCommunity::read_from_id(object_id.clone(), context).await?;
|
|
|
|
if let Some(uc) = uc {
|
|
|
|
return Ok(Some(SearchableObjects::PersonOrCommunity(Box::new(uc))));
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
2024-10-03 12:24:05 +00:00
|
|
|
let pc = PostOrComment::read_from_id(object_id.clone(), context).await?;
|
|
|
|
if let Some(pc) = pc {
|
|
|
|
return Ok(Some(SearchableObjects::PostOrComment(Box::new(pc))));
|
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)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn delete(self, data: &Data<Self::DataType>) -> LemmyResult<()> {
|
2021-10-16 11:14:02 +00:00
|
|
|
match self {
|
2024-10-03 12:24:05 +00:00
|
|
|
SearchableObjects::PostOrComment(pc) => pc.delete(data).await,
|
|
|
|
SearchableObjects::PersonOrCommunity(pc) => pc.delete(data).await,
|
2021-10-16 11:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
|
2024-08-01 00:28:41 +00:00
|
|
|
async fn into_json(self, data: &Data<Self::DataType>) -> LemmyResult<Self::Kind> {
|
2024-10-03 12:24:05 +00:00
|
|
|
use SearchableObjects::*;
|
2024-08-01 00:28:41 +00:00
|
|
|
Ok(match self {
|
2024-10-03 12:24:05 +00:00
|
|
|
PostOrComment(pc) => SearchableKinds::PageOrNote(Box::new(pc.into_json(data).await?)),
|
|
|
|
PersonOrCommunity(pc) => SearchableKinds::PersonOrGroup(Box::new(pc.into_json(data).await?)),
|
2024-08-01 00:28:41 +00:00
|
|
|
})
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
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>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<()> {
|
2024-10-03 12:24:05 +00:00
|
|
|
use SearchableKinds::*;
|
2021-11-06 17:35:14 +00:00
|
|
|
match apub {
|
2024-10-03 12:24:05 +00:00
|
|
|
PageOrNote(pn) => PostOrComment::verify(pn, expected_domain, data).await,
|
|
|
|
PersonOrGroup(pg) => UserOrCommunity::verify(pg, expected_domain, data).await,
|
2021-11-06 17:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn from_json(apub: Self::Kind, context: &Data<LemmyContext>) -> LemmyResult<Self> {
|
2024-10-03 12:24:05 +00:00
|
|
|
use SearchableKinds::*;
|
2021-09-25 15:44:52 +00:00
|
|
|
use SearchableObjects as SO;
|
|
|
|
Ok(match apub {
|
2024-10-03 12:24:05 +00:00
|
|
|
PageOrNote(pg) => SO::PostOrComment(Box::new(PostOrComment::from_json(*pg, context).await?)),
|
|
|
|
PersonOrGroup(pg) => {
|
2024-03-15 12:42:09 +00:00
|
|
|
SO::PersonOrCommunity(Box::new(UserOrCommunity::from_json(*pg, context).await?))
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|