-
-
Notifications
You must be signed in to change notification settings - Fork 948
Add hashing for activity ids #6366
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
7cf9223
36f48fe
b4bab81
a9f4195
91f043f
b6a25e9
0a619de
99d0782
5ae79aa
ef854ef
73db584
f2d4584
5a72720
de6b70c
2205372
b7ac661
f1b062f
3195399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,15 +82,19 @@ impl AnnounceActivity { | |
| pub fn new( | ||
| object: RawAnnouncableActivities, | ||
| community: &ApubCommunity, | ||
| object_id: Option<&Url>, | ||
| context: &Data<LemmyContext>, | ||
| ) -> LemmyResult<AnnounceActivity> { | ||
| let inner_kind = object | ||
| .other | ||
| .get("type") | ||
| .and_then(serde_json::Value::as_str) | ||
| .unwrap_or("other"); | ||
| let id = | ||
| generate_announce_activity_id(inner_kind, &context.settings().get_protocol_and_hostname())?; | ||
| let id = generate_announce_activity_id( | ||
| inner_kind, | ||
| &context.settings().get_protocol_and_hostname(), | ||
| object_id, | ||
| )?; | ||
| Ok(AnnounceActivity { | ||
| actor: community.id().clone().into(), | ||
| to: generate_to(community)?, | ||
|
|
@@ -111,7 +115,7 @@ impl AnnounceActivity { | |
| community: &ApubCommunity, | ||
| context: &Data<LemmyContext>, | ||
| ) -> LemmyResult<()> { | ||
| let announce = AnnounceActivity::new(object.clone(), community, context)?; | ||
| let announce = AnnounceActivity::new(object.clone(), community, None, context)?; | ||
| let inboxes = ActivitySendTargets::to_local_community_followers(community.id); | ||
|
Author
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. Are we sure we want the activity id for
Member
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. The same activity must always have the same ID. |
||
| send_lemmy_activity(context, announce, community, inboxes.clone(), false).await?; | ||
|
|
||
|
|
@@ -122,14 +126,14 @@ impl AnnounceActivity { | |
| // Hack: need to convert Page into a format which can be sent as activity, which requires | ||
| // adding actor field. | ||
| let announcable_page = RawAnnouncableActivities { | ||
| id: generate_activity_id(AnnounceType::Announce, context)?, | ||
| id: generate_activity_id(AnnounceType::Announce, None, context)?, | ||
| actor: c.actor.clone().into_inner(), | ||
| other: serde_json::to_value(c.object)? | ||
| .as_object() | ||
| .ok_or(UntranslatedError::Unreachable)? | ||
| .clone(), | ||
| }; | ||
| let announce_compat = AnnounceActivity::new(announcable_page, community, context)?; | ||
| let announce_compat = AnnounceActivity::new(announcable_page, community, None, context)?; | ||
| send_lemmy_activity(context, announce_compat, community, inboxes, false).await?; | ||
| } | ||
| Ok(()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -55,7 +55,7 @@ impl CreateOrUpdateNote { | |||||
| .await? | ||||||
| .into(); | ||||||
|
|
||||||
| let id = generate_activity_id(kind.clone(), &context)?; | ||||||
| let id = generate_activity_id(kind.clone(), None, &context)?; | ||||||
|
Member
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.
Suggested change
Author
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. Do we need to use the timestamps here for Update as well? |
||||||
| let note = ApubComment(comment).into_json(&context).await?; | ||||||
|
|
||||||
| let create_or_update = CreateOrUpdateNote { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,9 +44,10 @@ impl CreateOrUpdatePage { | |
| actor: &ApubPerson, | ||
| community: &ApubCommunity, | ||
| kind: CreateOrUpdateType, | ||
| object_id: Option<&Url>, | ||
| context: &Data<LemmyContext>, | ||
| ) -> LemmyResult<CreateOrUpdatePage> { | ||
| let id = generate_activity_id(kind.clone(), context)?; | ||
| let id = generate_activity_id(kind.clone(), object_id, context)?; | ||
| Ok(CreateOrUpdatePage { | ||
| actor: actor.id().clone().into(), | ||
| to: generate_to(community)?, | ||
|
|
@@ -70,8 +71,22 @@ impl CreateOrUpdatePage { | |
| .await? | ||
| .into(); | ||
|
|
||
| // get object_id for activity id generation | ||
| let post_ap_id = (*post.ap_id.0).clone(); | ||
| let object_id = match kind { | ||
| // for Create, use the post's ap id | ||
| CreateOrUpdateType::Create => Some(&post_ap_id), | ||
| // for Update, use a timestamp to ensure each Update activity is unique | ||
| CreateOrUpdateType::Update => { | ||
| let timestamp = post.updated_at.unwrap_or(post.published_at); // use the latest timestamp | ||
| let mut seed_url = post_ap_id; | ||
| seed_url.set_fragment(Some(×tamp.to_rfc3339())); | ||
| Some(&seed_url.clone()) | ||
|
Member
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. See above |
||
| } | ||
| }; | ||
|
Member
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. Private message also needs the same logic. Best And you dont need to change anything in this file, it only needs to be in |
||
|
|
||
| let create_or_update = | ||
| CreateOrUpdatePage::new(post.into(), &person, &community, kind, &context).await?; | ||
| CreateOrUpdatePage::new(post.into(), &person, &community, kind, object_id, &context).await?; | ||
| let inboxes = tagged_user_inboxes(&create_or_update.object.tag, &context).await?; | ||
| let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update); | ||
| send_activity_in_community(activity, &person, &community, inboxes, false, &context).await?; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,30 +82,50 @@ pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Lemmy | |
|
|
||
| /// Generate a unique ID for an activity, in the format: | ||
| /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36` | ||
| fn generate_activity_id<T>(kind: T, context: &LemmyContext) -> Result<Url, ParseError> | ||
| fn generate_activity_id<T>( | ||
|
Member
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. To avoid passing None in so many places, you could do: fn generate_activity_id_with_object_id(kind, context) {
generate_activity_id(kind, None, context)
}
fn generate_activity_id(kind, object_id, context) {
generate_activity_id(kind, context)
} |
||
| kind: T, | ||
| object_id: Option<&Url>, | ||
| context: &LemmyContext, | ||
| ) -> Result<Url, ParseError> | ||
| where | ||
| T: ToString, | ||
| { | ||
| let id = format!( | ||
| "{}/activities/{}/{}", | ||
| &context.settings().get_protocol_and_hostname(), | ||
| kind.to_string().to_lowercase(), | ||
| Uuid::new_v4() | ||
| ); | ||
| let hostname = context.settings().get_protocol_and_hostname(); | ||
| let kind_str = kind.to_string().to_lowercase(); | ||
|
|
||
| let uuid_str = if let Some(o) = object_id { | ||
| let input = format!("{}:{}", kind_str, o.as_str()); | ||
| Uuid::from_bytes(md5::compute(input).0).to_string() | ||
| } else { | ||
| Uuid::new_v4().to_string() | ||
| }; | ||
|
|
||
| let id = format!("{}/activities/{}/{}", hostname, kind_str, uuid_str); | ||
| Url::parse(&id) | ||
| } | ||
|
|
||
| /// like generate_activity_id but also add the inner kind for easier debugging | ||
| fn generate_announce_activity_id( | ||
| inner_kind: &str, | ||
| protocol_and_hostname: &str, | ||
| object_id: Option<&Url>, | ||
| ) -> Result<Url, ParseError> { | ||
| let inner_kind_str = inner_kind.to_lowercase(); | ||
|
|
||
| let uuid_str = if let Some(o) = object_id { | ||
| // add "announce:" in front to avoid collision with generate_activity_id | ||
| let input = format!("announce:{}:{}", inner_kind_str, o.as_str()); | ||
| Uuid::from_bytes(md5::compute(input).0).to_string() | ||
| } else { | ||
| Uuid::new_v4().to_string() | ||
| }; | ||
|
|
||
| let id = format!( | ||
| "{}/activities/{}/{}/{}", | ||
| protocol_and_hostname, | ||
| AnnounceType::Announce.to_string().to_lowercase(), | ||
| inner_kind.to_lowercase(), | ||
| Uuid::new_v4() | ||
| inner_kind_str, | ||
| uuid_str | ||
| ); | ||
| Url::parse(&id) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.