Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "threads-rs"
version = "0.2.0"
version = "0.3.0"
edition = "2024"
rust-version = "1.85"
description = "Rust client library for the Meta Threads API"
Expand Down
2 changes: 2 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod location;
pub mod posts;
/// Post deletion endpoints.
pub mod posts_delete;
/// Recovery from `/threads_publish` false failures.
pub mod posts_publish_recovery;
/// Post retrieval and listing endpoints.
pub mod posts_read;
/// Reply management endpoints.
Expand Down
48 changes: 31 additions & 17 deletions src/api/posts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use crate::api::posts_publish_recovery as recovery;
use crate::client::Client;
use crate::constants;
use crate::error;
Expand Down Expand Up @@ -78,7 +79,8 @@ impl Client {
// Text containers are ready immediately — skip polling
let params = self.build_text_params(content, &user_id);
let container_id = self.create_container(params).await?;
self.publish_container(&container_id).await
self.publish_with_recovery(&container_id, recovery::text_matcher(content))
.await
}

/// Create an image post.
Expand Down Expand Up @@ -123,7 +125,8 @@ impl Client {
let container_id = self.create_container(params).await?;
let cid = ContainerId::from(container_id.as_str());
self.wait_for_container_ready(&cid).await?;
self.publish_container(&container_id).await
self.publish_with_recovery(&container_id, recovery::image_matcher(content))
.await
}

/// Create a video post.
Expand Down Expand Up @@ -168,7 +171,8 @@ impl Client {
let container_id = self.create_container(params).await?;
let cid = ContainerId::from(container_id.as_str());
self.wait_for_container_ready(&cid).await?;
self.publish_container(&container_id).await
self.publish_with_recovery(&container_id, recovery::video_matcher(content))
.await
}

/// Create a carousel post.
Expand Down Expand Up @@ -210,7 +214,8 @@ impl Client {
let container_id = self.create_container(params).await?;
let cid = ContainerId::from(container_id.as_str());
self.wait_for_container_ready(&cid).await?;
self.publish_container(&container_id).await
self.publish_with_recovery(&container_id, recovery::carousel_matcher(content))
.await
}

/// Create a quote post — a text post that quotes another post.
Expand All @@ -222,19 +227,7 @@ impl Client {
let content = TextPostContent {
text: text.to_owned(),
quoted_post_id: Some(quoted_post_id.clone()),
link_attachment: None,
poll_attachment: None,
reply_control: None,
reply_to_id: None,
topic_tag: None,
allowlisted_country_codes: None,
location_id: None,
auto_publish_text: false,
text_entities: None,
text_attachment: None,
gif_attachment: None,
is_ghost_post: false,
enable_reply_approvals: false,
..Default::default()
};
self.create_text_post(&content).await
}
Expand Down Expand Up @@ -368,6 +361,27 @@ impl Client {
Ok(container.id)
}

/// Publish a media container, attempting false-failure recovery on error.
///
/// `matches` identifies the resulting post among the user's recent posts
/// if Meta reports a publish failure that actually succeeded (see
/// `posts_publish_recovery`). On unrecovered errors the original publish
/// error is returned.
async fn publish_with_recovery(
&self,
container_id: &str,
matches: impl Fn(&Post) -> bool,
) -> crate::Result<Post> {
let publish_start = chrono::Utc::now();
match self.publish_container(container_id).await {
Ok(post) => Ok(post),
Err(err) => {
self.recover_or_original(container_id, publish_start, err, matches)
.await
}
}
}

/// Publish a media container and return the resulting post.
async fn publish_container(&self, container_id: &str) -> crate::Result<Post> {
let token = self.access_token().await;
Expand Down
Loading
Loading