New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dont require leading ! or @ for webfinger resolve #4513
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use crate::{ | ||
fetcher::user_or_community::{PersonOrGroup, UserOrCommunity}, | ||
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost}, | ||
protocol::objects::{group::Group, note::Note, page::Page, person::Person}, | ||
protocol::objects::{note::Note, page::Page}, | ||
}; | ||
use activitypub_federation::{ | ||
config::Data, | ||
|
@@ -9,7 +10,7 @@ use activitypub_federation::{ | |
}; | ||
use chrono::{DateTime, Utc}; | ||
use lemmy_api_common::context::LemmyContext; | ||
use lemmy_utils::error::{LemmyError, LemmyErrorType}; | ||
use lemmy_utils::error::LemmyError; | ||
use serde::Deserialize; | ||
use url::Url; | ||
|
||
|
@@ -18,28 +19,22 @@ use url::Url; | |
/// which gets resolved to an URL. | ||
#[tracing::instrument(skip_all)] | ||
pub(crate) async fn search_query_to_object_id( | ||
query: &str, | ||
mut query: String, | ||
context: &Data<LemmyContext>, | ||
) -> Result<SearchableObjects, LemmyError> { | ||
Ok(match Url::parse(query) { | ||
Ok(match Url::parse(&query) { | ||
Ok(url) => { | ||
// its already an url, just go with it | ||
ObjectId::from(url).dereference(context).await? | ||
} | ||
Err(_) => { | ||
// not an url, try to resolve via webfinger | ||
let mut chars = query.chars(); | ||
let kind = chars.next(); | ||
let identifier = chars.as_str(); | ||
match kind { | ||
Some('@') => SearchableObjects::Person( | ||
webfinger_resolve_actor::<LemmyContext, ApubPerson>(identifier, context).await?, | ||
), | ||
Some('!') => SearchableObjects::Community( | ||
webfinger_resolve_actor::<LemmyContext, ApubCommunity>(identifier, context).await?, | ||
), | ||
_ => return Err(LemmyErrorType::InvalidQuery)?, | ||
if query.starts_with('!') || query.starts_with('@') { | ||
query.remove(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just build the new var here. let query_2 = if .... else |
||
} | ||
SearchableObjects::PersonOrCommunity(Box::new( | ||
webfinger_resolve_actor::<LemmyContext, UserOrCommunity>(&query, context).await?, | ||
)) | ||
} | ||
}) | ||
} | ||
|
@@ -59,19 +54,17 @@ pub(crate) async fn search_query_to_object_id_local( | |
/// The types of ActivityPub objects that can be fetched directly by searching for their ID. | ||
#[derive(Debug)] | ||
pub(crate) enum SearchableObjects { | ||
Person(ApubPerson), | ||
Community(ApubCommunity), | ||
Post(ApubPost), | ||
Comment(ApubComment), | ||
PersonOrCommunity(Box<UserOrCommunity>), | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(untagged)] | ||
pub(crate) enum SearchableKinds { | ||
Group(Group), | ||
Person(Person), | ||
Page(Page), | ||
Page(Box<Page>), | ||
Note(Note), | ||
PersonOrGroup(Box<PersonOrGroup>), | ||
} | ||
|
||
#[async_trait::async_trait] | ||
|
@@ -82,10 +75,9 @@ impl Object for SearchableObjects { | |
|
||
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> { | ||
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(), | ||
SearchableObjects::PersonOrCommunity(p) => p.last_refreshed_at(), | ||
} | ||
} | ||
|
||
|
@@ -99,13 +91,9 @@ impl Object for SearchableObjects { | |
object_id: Url, | ||
context: &Data<Self::DataType>, | ||
) -> Result<Option<Self>, LemmyError> { | ||
let c = ApubCommunity::read_from_id(object_id.clone(), context).await?; | ||
if let Some(c) = c { | ||
return Ok(Some(SearchableObjects::Community(c))); | ||
} | ||
let p = ApubPerson::read_from_id(object_id.clone(), context).await?; | ||
if let Some(p) = p { | ||
return Ok(Some(SearchableObjects::Person(p))); | ||
let uc = UserOrCommunity::read_from_id(object_id.clone(), context).await?; | ||
if let Some(uc) = uc { | ||
return Ok(Some(SearchableObjects::PersonOrCommunity(Box::new(uc)))); | ||
} | ||
let p = ApubPost::read_from_id(object_id.clone(), context).await?; | ||
if let Some(p) = p { | ||
|
@@ -121,10 +109,12 @@ impl Object for SearchableObjects { | |
#[tracing::instrument(skip_all)] | ||
async fn delete(self, data: &Data<Self::DataType>) -> Result<(), LemmyError> { | ||
match self { | ||
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, | ||
SearchableObjects::PersonOrCommunity(pc) => match *pc { | ||
UserOrCommunity::User(p) => p.delete(data).await, | ||
UserOrCommunity::Community(c) => c.delete(data).await, | ||
}, | ||
} | ||
} | ||
|
||
|
@@ -139,10 +129,12 @@ impl Object for SearchableObjects { | |
data: &Data<Self::DataType>, | ||
) -> Result<(), LemmyError> { | ||
match apub { | ||
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, | ||
SearchableKinds::PersonOrGroup(pg) => match pg.as_ref() { | ||
PersonOrGroup::Person(a) => ApubPerson::verify(a, expected_domain, data).await, | ||
PersonOrGroup::Group(a) => ApubCommunity::verify(a, expected_domain, data).await, | ||
}, | ||
} | ||
} | ||
|
||
|
@@ -151,10 +143,11 @@ impl Object for SearchableObjects { | |
use SearchableKinds as SAT; | ||
use SearchableObjects as SO; | ||
Ok(match apub { | ||
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::Page(p) => SO::Post(ApubPost::from_json(*p, context).await?), | ||
SAT::Note(n) => SO::Comment(ApubComment::from_json(n, context).await?), | ||
SAT::PersonOrGroup(pg) => { | ||
SO::PersonOrCommunity(Box::new(UserOrCommunity::from_json(*pg, context).await?)) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of having this a a mutable param, I think it'd be better to just create a new var inside this function. immutable functions and all that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately String::remove only works on a mutable string, and I dont see a better alternative in this case. Sure I could declare a separate mut inside the function, but that wouldnt really change anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still an immutable function.
mut
before a parameter name does the same thing as puttinglet mut foo = foo;
in the function body.