Skip to content

Commit

Permalink
feat(sync): Prepare twitter file attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed Feb 17, 2019
1 parent 753375e commit 17b5414
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ fn main() {
}

for tweet in posts.tweets {
println!("Posting to Twitter: {}", tweet);
if let Err(e) = core.run(DraftTweet::new(tweet).send(&token)) {
println!("Posting to Twitter: {}", tweet.text);
if let Err(e) = core.run(DraftTweet::new(tweet.text).send(&token)) {
println!("Error posting tweet to Twitter: {:#?}", e);
process::exit(6);
}
Expand Down
74 changes: 74 additions & 0 deletions src/mastodon_attach.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"id": "1234567890",
"created_at": "2019-02-17T19:16:00.298Z",
"in_reply_to_id": null,
"in_reply_to_account_id": null,
"sensitive": false,
"spoiler_text": "",
"visibility": "public",
"language": "en",
"uri": "https://mastodon.social/users/example/statuses/1234567890",
"content": "\u003cp\u003etest image\u003c/p\u003e",
"url": "https://mastodon.social/@example/1234567890",
"replies_count": 0,
"reblogs_count": 0,
"favourites_count": 0,
"favourited": false,
"reblogged": false,
"muted": false,
"pinned": false,
"reblog": null,
"application": {
"name": "Web",
"website": null
},
"account": {
"id": "1234567890",
"username": "example",
"acct": "example",
"display_name": "example",
"locked": false,
"bot": false,
"created_at": "2017-04-01T21:04:21.054Z",
"note": "\u003cp\u003eexample\u003c/p\u003e",
"url": "https://mastodon.social/@example",
"avatar": "https://files.mastodon.social/accounts/avatars/000/028/407/original/6ff55ef63a4c42eb.png",
"avatar_static": "https://files.mastodon.social/accounts/avatars/000/028/407/original/6ff55ef63a4c42eb.png",
"header": "https://mastodon.social/headers/original/missing.png",
"header_static": "https://mastodon.social/headers/original/missing.png",
"followers_count": 119,
"following_count": 217,
"statuses_count": 117,
"emojis": [],
"fields": []
},
"media_attachments": [
{
"id": "11514042",
"type": "image",
"url": "https://files.mastodon.social/media_attachments/files/011/514/042/original/e046a3fb6a71a07b.jpg",
"preview_url": "https://files.mastodon.social/media_attachments/files/011/514/042/small/e046a3fb6a71a07b.jpg",
"remote_url": null,
"text_url": "https://mastodon.social/media/gtTbJRHexeXg6Xn3xME",
"meta": {
"original": {
"width": 1300,
"height": 993,
"size": "1300x993",
"aspect": 1.309164149043303
},
"small": {
"width": 458,
"height": 350,
"size": "458x350",
"aspect": 1.3085714285714285
}
},
"description": "Test image from a TV screen"
}
],
"mentions": [],
"tags": [],
"emojis": [],
"card": null
}
64 changes: 55 additions & 9 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::io::prelude::*;
// Mastodon (toots).
#[derive(Debug, Clone)]
pub struct StatusUpdates {
pub tweets: Vec<String>,
pub tweets: Vec<NewStatus>,
pub toots: Vec<NewStatus>,
}

Expand Down Expand Up @@ -64,7 +64,10 @@ pub fn determine_posts(mastodon_statuses: &[Status], twitter_statuses: &[Tweet])
}
}
// The toot is not on Twitter yet, let's post it.
updates.tweets.push(post);
updates.tweets.push(NewStatus {
text: post,
attachments: toot_get_attachments(toot),
});
}
updates
}
Expand Down Expand Up @@ -211,11 +214,11 @@ pub fn filter_posted_before(posts: StatusUpdates) -> StatusUpdates {
toots: Vec::new(),
};
for tweet in posts.tweets {
if cache.contains(&tweet) {
println!("Error: preventing double posting to Twitter: {}", tweet);
if cache.contains(&tweet.text) {
println!("Error: preventing double posting to Twitter: {}", tweet.text);
} else {
filtered_posts.tweets.push(tweet.clone());
cache.insert(tweet);
cache.insert(tweet.text);
}
}
for toot in posts.toots {
Expand Down Expand Up @@ -278,6 +281,21 @@ fn tweet_get_attachments(tweet: &Tweet) -> Vec<NewMedia> {
links
}

// Returns a list of direct links to attachments for download.
fn toot_get_attachments(toot: &Status) -> Vec<NewMedia> {
let mut links = Vec::new();
for attachment in &toot.media_attachments {
// Only images are supported for now, no videos.
if let mammut::entities::attachment::MediaType::Image = attachment.media_type {
links.push(NewMedia {
attachment_url: attachment.url.clone(),
alt_text: attachment.description.clone(),
});
}
}
links
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -386,7 +404,7 @@ UNLISTED 🔓 ✅ Tagged people
let mut status = get_mastodon_status();
status.content = "<p>You &amp; me!</p>".to_string();
let posts = determine_posts(&vec![status], &Vec::new());
assert_eq!(posts.tweets[0], "You & me!");
assert_eq!(posts.tweets[0].text, "You & me!");
}

// Test that Twitter status text is posted HTML entity decoded to Mastodon.
Expand All @@ -410,7 +428,7 @@ UNLISTED 🔓 ✅ Tagged people
status.reblogged = Some(true);

let posts = determine_posts(&vec![status], &Vec::new());
assert_eq!(posts.tweets[0], "RT example: Some example toooot!");
assert_eq!(posts.tweets[0].text, "RT example: Some example toooot!");
}

// Test that the old "RT @username" prefix is considered equal to "RT
Expand Down Expand Up @@ -473,7 +491,7 @@ UNLISTED 🔓 ✅ Tagged people
let statuses = vec![status];
let posts = determine_posts(&statuses, &tweets);
assert!(posts.toots.is_empty());
assert_eq!(posts.tweets[0], "Österreich");
assert_eq!(posts.tweets[0].text, "Österreich");
}

// Test that posting something looking like a URL/domain is considered
Expand Down Expand Up @@ -544,9 +562,37 @@ UNLISTED 🔓 ✅ Tagged people
assert!(status.attachments.is_empty());
}

// Test that if there are pictures in a toot that they are attached as
// media files to the tweet.
#[test]
fn pictures_in_toot() {
let statuses = vec![get_mastodon_status_media()];
let tweets = Vec::new();
let posts = determine_posts(&statuses, &tweets);

let tweet = &posts.tweets[0];
assert_eq!(tweet.text, "test image");
assert_eq!(
tweet.attachments[0].attachment_url,
"https://files.mastodon.social/media_attachments/files/011/514/042/original/e046a3fb6a71a07b.jpg"
);
assert_eq!(
tweet.attachments[0].alt_text,
Some("Test image from a TV screen".to_string())
);
}

fn get_mastodon_status() -> Status {
read_mastodon_status("src/mastodon_status.json")
}

fn get_mastodon_status_media() -> Status {
read_mastodon_status("src/mastodon_attach.json")
}

fn read_mastodon_status(file_name: &str) -> Status {
let json = {
let mut file = File::open("src/mastodon_status.json").unwrap();
let mut file = File::open(file_name).unwrap();
let mut ret = String::new();
file.read_to_string(&mut ret).unwrap();
ret
Expand Down

0 comments on commit 17b5414

Please sign in to comment.