Skip to content

Commit

Permalink
fix(sync): Do not escape user name mentions, Bluesky does not auto-li…
Browse files Browse the repository at this point in the history
…nk them
  • Loading branch information
klausi committed Dec 22, 2024
1 parent 528846d commit bacb428
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ Todo list for the future, not implemented yet:
- Build portable binaries on Github without OpenSSL dependencies
- Add open graph link preview when posting to Bluesky
- Parallel execution of fetching and syncing requests at the same time
- Remove username escaping with backslash from Bluesky, not needed

## Acknowledgements

Expand Down
19 changes: 13 additions & 6 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ fn unify_post_content(content: String) -> String {
result = result.replace("http://", "");
result = result.replace("https://", "");

// Escape direct user mentions with \@.
result = result.replace(" \\@", " @");
result.replace(" @\\", " @")
result
}

// Extend URLs and HTML entity decode &.
Expand Down Expand Up @@ -400,9 +398,6 @@ pub fn mastodon_toot_get_text(toot: &Status) -> String {

replaced = voca_rs::strip::strip_tags(&replaced);

// Escape direct user mentions with @\.
replaced = replaced.replace(" @", " @\\").replace(" @\\\\", " @\\");

html_escape::decode_html_entities(&replaced).to_string()
}

Expand Down Expand Up @@ -718,6 +713,18 @@ https://github.com/klausi/mastodon-bluesky-sync/releases/tag/v0.2.0"
https://www.derstandard.at/story/3000000250190/der-fall-pelicot-unfassbar-monstroes?ref=article");
}

// Test that a user mention on mastodon is posted as is to Bluesky. We don't
// need to escape it.
#[test]
fn mastodon_user_mention() {
let post = read_mastodon_post_from_json("tests/mastodon_mention.json");
let posts = determine_posts(&vec![post], &Vec::new(), &SyncOptions::default());
assert_eq!(
posts.bsky_posts[0].text,
"Finally watched #RebelRidge recommended by @mekkaokereke a while ago... Good stuff! 🎬"
);
}

// Read static bluesky post from test file.
fn read_bsky_post_from_json(file_name: &str) -> Object<FeedViewPostData> {
let json = fs::read_to_string(file_name).unwrap();
Expand Down
85 changes: 85 additions & 0 deletions tests/mastodon_mention.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"id": "113693143551648776",
"uri": "https://mastodon.social/users/klausi/statuses/113693143551648776",
"url": "https://mastodon.social/@klausi/113693143551648776",
"account": {
"id": "28407",
"username": "klausi",
"acct": "klausi",
"display_name": "klausi",
"locked": false,
"discoverable": true,
"group": false,
"noindex": true,
"moved": null,
"suspended": null,
"limited": null,
"created_at": "2017-04-01T00:00:00Z",
"followers_count": 530,
"following_count": 1026,
"statuses_count": 126,
"note": "<p>💻 Software Engineer</p><p>🌍 Location: Vienna, Austria <a href=\"https://mastodon.social/tags/WienLiebe\" class=\"mention hashtag\" rel=\"tag\">#<span>WienLiebe</span></a><br />🏷️ Identity: upper middle class, 80% male, white, 80% straight, cis, dya<br />💼 Work: Jobiqo, <a href=\"https://mastodon.social/tags/Drupal\" class=\"mention hashtag\" rel=\"tag\">#<span>Drupal</span></a>, <a href=\"https://mastodon.social/tags/PHP\" class=\"mention hashtag\" rel=\"tag\">#<span>PHP</span></a>, <a href=\"https://mastodon.social/tags/D7Security\" class=\"mention hashtag\" rel=\"tag\">#<span>D7Security</span></a><br />🎓 Education: Computer Science, soft skill focus on Gender Studies<br />💡 Interests: tech, politics, infosec, <a href=\"https://mastodon.social/tags/rustlang\" class=\"mention hashtag\" rel=\"tag\">#<span>rustlang</span></a>, ethics, social justice<br />🚩🏴 Politics: somewhere on the left, drawing from anarchism, communism, liberalism<br />🗣️ Languages: German, English<br />🏳️‍🌈 Pronouns: He/him or they/them<br />🏳️‍⚧️ Support: trans and nonbinary people</p>",
"url": "https://mastodon.social/@klausi",
"avatar": "https://files.mastodon.social/accounts/avatars/000/028/407/original/638b002381039f45.png",
"avatar_static": "https://files.mastodon.social/accounts/avatars/000/028/407/original/638b002381039f45.png",
"header": "https://files.mastodon.social/accounts/headers/000/028/407/original/AUK6UYSC52D9.png",
"header_static": "https://files.mastodon.social/accounts/headers/000/028/407/original/AUK6UYSC52D9.png",
"emojis": [],
"fields": [
{
"name": "Blog",
"value": "<a href=\"https://klau.si\" target=\"_blank\" rel=\"nofollow noopener me\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">klau.si</span><span class=\"invisible\"></span></a>",
"verified_at": "2019-11-23T23:49:42.885Z",
"verified": null
}
],
"bot": false,
"source": null,
"role": null,
"mute_expires_at": null
},
"in_reply_to_id": null,
"in_reply_to_account_id": null,
"reblog": null,
"content": "<p>Finally watched <a href=\"https://mastodon.social/tags/RebelRidge\" class=\"mention hashtag\" rel=\"tag\">#<span>RebelRidge</span></a> recommended by <span class=\"h-card\" translate=\"no\"><a href=\"https://hachyderm.io/@mekkaokereke\" class=\"u-url mention\">@<span>mekkaokereke</span></a></span> a while ago... Good stuff! 🎬</p>",
"plain_content": null,
"created_at": "2024-12-21T22:21:35.323Z",
"edited_at": "2024-12-22T06:53:41.848Z",
"emojis": [],
"replies_count": 0,
"reblogs_count": 0,
"favourites_count": 1,
"reblogged": false,
"favourited": false,
"muted": false,
"sensitive": false,
"spoiler_text": "",
"visibility": "public",
"media_attachments": [],
"mentions": [
{
"id": "109387287658543773",
"username": "mekkaokereke",
"url": "https://hachyderm.io/@mekkaokereke",
"acct": "[email protected]"
}
],
"tags": [
{
"name": "rebelridge",
"url": "https://mastodon.social/tags/rebelridge"
}
],
"card": null,
"poll": null,
"application": {
"name": "Mastodon for Android",
"website": "https://app.joinmastodon.org/android",
"vapid_key": null
},
"language": "en",
"pinned": false,
"emoji_reactions": null,
"quote": false,
"bookmarked": false
}

0 comments on commit bacb428

Please sign in to comment.