Skip to content

Commit

Permalink
fix links on bluesky
Browse files Browse the repository at this point in the history
  • Loading branch information
ka-de committed Nov 25, 2024
1 parent 02c7c8c commit b2e771c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
39 changes: 39 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ async-trait = "0.1.83"
toml = "0.8.19"
dirs = "5.0.1"
chrono = { version = "0.4.38", features = ["serde"] }
regex = "1.11.1"
43 changes: 42 additions & 1 deletion src/social/bluesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use reqwest::Client;
use serde_json::json;
use crate::config::Config;
use super::SocialClient;
use regex::Regex;

pub struct BlueskyClient {
identifier: String,
Expand All @@ -10,6 +11,30 @@ pub struct BlueskyClient {
client: Client,
}

#[derive(Debug)]
struct UrlFacet {
start: usize,
end: usize,
url: String,
}

impl BlueskyClient {
fn detect_urls(text: &str) -> Vec<UrlFacet> {
let url_regex = Regex::new(r"https?://[^\s]+").unwrap();
let mut facets = Vec::new();

for mat in url_regex.find_iter(text) {
facets.push(UrlFacet {
start: mat.start(),
end: mat.end(),
url: mat.as_str().to_string(),
});
}

facets
}
}

#[async_trait::async_trait]
impl SocialClient for BlueskyClient {
fn new() -> Result<Self, Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -39,6 +64,21 @@ impl SocialClient for BlueskyClient {
let access_jwt = auth_response["accessJwt"].as_str()
.ok_or("Failed to get access token")?;

// Detect URLs and create facets
let url_facets = Self::detect_urls(message);
let facets = url_facets.iter().map(|f| {
json!({
"index": {
"byteStart": f.start,
"byteEnd": f.end
},
"features": [{
"$type": "app.bsky.richtext.facet#link",
"uri": f.url
}]
})
}).collect::<Vec<serde_json::Value>>();

// Create the post
let response = self.client
.post(format!("{}/xrpc/com.atproto.repo.createRecord", self.instance_url))
Expand All @@ -49,7 +89,8 @@ impl SocialClient for BlueskyClient {
"record": {
"$type": "app.bsky.feed.post",
"text": message,
"createdAt": chrono::Utc::now().to_rfc3339()
"createdAt": chrono::Utc::now().to_rfc3339(),
"facets": facets
}
}))
.send()
Expand Down

0 comments on commit b2e771c

Please sign in to comment.