mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-12-06 06:43:15 +01:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
47d7fdbd42 | ||
|
|
7334fb3d09 | ||
|
|
79ac915347 | ||
|
|
e89e6e51ec |
522
Cargo.lock
generated
522
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "oolatoocs"
|
name = "oolatoocs"
|
||||||
version = "4.4.0"
|
version = "4.4.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
@@ -23,7 +23,7 @@ bsky-sdk = "^0.1"
|
|||||||
atrium-api = { version = "^0.25", features = ["namespace-appbsky"] }
|
atrium-api = { version = "^0.25", features = ["namespace-appbsky"] }
|
||||||
image = "^0.25"
|
image = "^0.25"
|
||||||
webp = "^0.3"
|
webp = "^0.3"
|
||||||
megalodon = "1.0.*"
|
megalodon = "^1.1"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
strip = true
|
strip = true
|
||||||
|
|||||||
@@ -16,10 +16,13 @@ Since 2025-01-20, Twitter is now longer supported.
|
|||||||
What it can do:
|
What it can do:
|
||||||
* Reproduces the Toot content into the Record;
|
* Reproduces the Toot content into the Record;
|
||||||
* Cuts (poorly) the Toot in half in it’s too long for Bluesky and thread it (this is cut using a word count, not the best method, but it gets the job done);
|
* Cuts (poorly) the Toot in half in it’s too long for Bluesky and thread it (this is cut using a word count, not the best method, but it gets the job done);
|
||||||
* Reuploads images/gifs/videos from Mastodon to Bluesky
|
* Reuploads images/gifs/videos/webcards from Mastodon to Bluesky
|
||||||
* ⚠️ Bluesky does not support mixing images and videos. You can have up to 4 images on a Bsky record **or** 1 video but not mix around. If you do so, only the video will be posted on Bluesky.
|
* ⚠️ Bluesky does not support mixing images and videos. You can have up to 4 images on a Bsky record **or** 1 video but not mix around. If you do so, only the video will be posted on Bluesky.
|
||||||
* ⚠️ Bluesky does not support images greater than 1Mb (that is 1,000,000 bytes or 976.6 KiB), so Oolatoocs converts the image to WebP and progressively reduces the quality to fit that limitation.
|
* ⚠️ Bluesky does not support images greater than 1Mb (that is 1,000,000 bytes or 976.6 KiB), so Oolatoocs converts the image to WebP and progressively reduces the quality to fit that limitation.
|
||||||
|
* ⚠️ Bluesky does not support webcards with any other media/quote, so webcards have the last priority
|
||||||
* Can reproduce threads from Mastodon to Bluesky
|
* Can reproduce threads from Mastodon to Bluesky
|
||||||
|
* Can reproduce (self-)quotes from Mastodon to Bluesky
|
||||||
|
* ⚠️ Bluesky can’t do quotes with webcards, you can only embed images **or** a video with quotes
|
||||||
* ⚠️ Bluesky does support polls for now. So the poll itself is just presented as text from Mastodon instead which is not the most elegant.
|
* ⚠️ Bluesky does support polls for now. So the poll itself is just presented as text from Mastodon instead which is not the most elegant.
|
||||||
* Can prevent a Toot from being recorded to Bluesky by using the #NoTweet (case-insensitive) hashtag in Mastodon
|
* Can prevent a Toot from being recorded to Bluesky by using the #NoTweet (case-insensitive) hashtag in Mastodon
|
||||||
|
|
||||||
|
|||||||
60
src/bsky.rs
60
src/bsky.rs
@@ -1,4 +1,4 @@
|
|||||||
use crate::config::BlueskyConfig;
|
use crate::{config::BlueskyConfig, OolatoocsError};
|
||||||
use atrium_api::{
|
use atrium_api::{
|
||||||
app::bsky::feed::post::RecordData, com::atproto::repo::upload_blob::Output,
|
app::bsky::feed::post::RecordData, com::atproto::repo::upload_blob::Output,
|
||||||
types::string::Datetime, types::string::Language, types::string::RecordKey,
|
types::string::Datetime, types::string::Language, types::string::RecordKey,
|
||||||
@@ -148,18 +148,16 @@ async fn get_record(
|
|||||||
Ok(record)
|
Ok(record)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate an quote embed record into Bsky
|
/// Generate an quote embed record
|
||||||
|
/// it is encapsulated in Option to prevent this function from failing
|
||||||
pub async fn generate_quote_records(
|
pub async fn generate_quote_records(
|
||||||
config: &BlueskyConfig,
|
config: &BlueskyConfig,
|
||||||
quote_id: &str,
|
quote_id: &str,
|
||||||
) -> Option<atrium_api::types::Union<atrium_api::app::bsky::feed::post::RecordEmbedRefs>> {
|
) -> Result<atrium_api::app::bsky::feed::post::RecordEmbedRefs, Box<dyn Error>> {
|
||||||
// if we can’t match the quote_id, simply return None
|
// if we can’t match the quote_id, simply return None
|
||||||
let quote_record = match get_record(&config.handle, &rkey(quote_id)).await {
|
let quote_record = get_record(&config.handle, &rkey(quote_id)).await?;
|
||||||
Ok(a) => a,
|
|
||||||
Err(_) => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(atrium_api::types::Union::Refs(
|
Ok(
|
||||||
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedRecordMain(Box::new(
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedRecordMain(Box::new(
|
||||||
atrium_api::app::bsky::embed::record::MainData {
|
atrium_api::app::bsky::embed::record::MainData {
|
||||||
record: atrium_api::com::atproto::repo::strong_ref::MainData {
|
record: atrium_api::com::atproto::repo::strong_ref::MainData {
|
||||||
@@ -170,24 +168,18 @@ pub async fn generate_quote_records(
|
|||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
)),
|
)),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate an embed card record into Bsky
|
/// Generate an embed webcard record into Bsky
|
||||||
/// If the preview image does not exist or fails to upload, it is simply ignored
|
/// If the preview image does not exist or fails to upload, it is simply ignored
|
||||||
pub async fn generate_embed_records(
|
pub async fn generate_webcard_records(
|
||||||
bsky: &BskyAgent,
|
bsky: &BskyAgent,
|
||||||
card: &Card,
|
card: &Card,
|
||||||
) -> Option<atrium_api::types::Union<atrium_api::app::bsky::feed::post::RecordEmbedRefs>> {
|
) -> Result<atrium_api::app::bsky::feed::post::RecordEmbedRefs, Box<dyn Error + Send + Sync>> {
|
||||||
// uploads the image card, if it fails, simply ignore everything
|
let blob = match &card.image {
|
||||||
let blob = if let Some(url) = &card.image {
|
Some(url) => upload_media(true, bsky, url).await?.blob.clone().into(),
|
||||||
if let Ok(image_blob) = upload_media(true, bsky, url).await {
|
None => None,
|
||||||
Some(image_blob.blob.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let record_card = atrium_api::app::bsky::embed::external::ExternalData {
|
let record_card = atrium_api::app::bsky::embed::external::ExternalData {
|
||||||
@@ -197,14 +189,14 @@ pub async fn generate_embed_records(
|
|||||||
uri: card.url.clone(),
|
uri: card.url.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(atrium_api::types::Union::Refs(
|
Ok(
|
||||||
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedExternalMain(Box::new(
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedExternalMain(Box::new(
|
||||||
atrium_api::app::bsky::embed::external::MainData {
|
atrium_api::app::bsky::embed::external::MainData {
|
||||||
external: record_card.into(),
|
external: record_card.into(),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
)),
|
)),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate an array of Bsky media records
|
/// Generate an array of Bsky media records
|
||||||
@@ -213,11 +205,7 @@ pub async fn generate_embed_records(
|
|||||||
pub async fn generate_media_records(
|
pub async fn generate_media_records(
|
||||||
bsky: &BskyAgent,
|
bsky: &BskyAgent,
|
||||||
media_attach: &[Attachment],
|
media_attach: &[Attachment],
|
||||||
) -> Option<atrium_api::types::Union<atrium_api::app::bsky::feed::post::RecordEmbedRefs>> {
|
) -> Result<atrium_api::app::bsky::feed::post::RecordEmbedRefs, Box<dyn Error + Send + Sync>> {
|
||||||
let mut embed: Option<
|
|
||||||
atrium_api::types::Union<atrium_api::app::bsky::feed::post::RecordEmbedRefs>,
|
|
||||||
> = None;
|
|
||||||
|
|
||||||
let image_media_attach: Vec<_> = media_attach
|
let image_media_attach: Vec<_> = media_attach
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| x.r#type == AttachmentType::Image)
|
.filter(|x| x.r#type == AttachmentType::Image)
|
||||||
@@ -233,9 +221,9 @@ pub async fn generate_media_records(
|
|||||||
if !video_media_attach.is_empty() {
|
if !video_media_attach.is_empty() {
|
||||||
// treat only the very first video, ignore the rest
|
// treat only the very first video, ignore the rest
|
||||||
let media = &video_media_attach[0];
|
let media = &video_media_attach[0];
|
||||||
let blob = upload_media(false, bsky, &media.url).await.unwrap();
|
let blob = upload_media(false, bsky, &media.url).await?;
|
||||||
|
|
||||||
embed = Some(atrium_api::types::Union::Refs(
|
return Ok(
|
||||||
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedVideoMain(Box::new(
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedVideoMain(Box::new(
|
||||||
atrium_api::app::bsky::embed::video::MainData {
|
atrium_api::app::bsky::embed::video::MainData {
|
||||||
alt: media.description.clone(),
|
alt: media.description.clone(),
|
||||||
@@ -245,12 +233,10 @@ pub async fn generate_media_records(
|
|||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
)),
|
)),
|
||||||
));
|
);
|
||||||
|
|
||||||
// returns immediately, we don’t want to treat the other medias
|
|
||||||
return embed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It wasn’t a video, then it’s an image or a gallery of 4 images
|
||||||
let mut stream = stream::iter(image_media_attach)
|
let mut stream = stream::iter(image_media_attach)
|
||||||
.map(|media| {
|
.map(|media| {
|
||||||
let bsky = bsky.clone();
|
let bsky = bsky.clone();
|
||||||
@@ -281,14 +267,14 @@ pub async fn generate_media_records(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !images.is_empty() {
|
if !images.is_empty() {
|
||||||
embed = Some(atrium_api::types::Union::Refs(
|
return Ok(
|
||||||
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedImagesMain(Box::new(
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedImagesMain(Box::new(
|
||||||
atrium_api::app::bsky::embed::images::MainData { images }.into(),
|
atrium_api::app::bsky::embed::images::MainData { images }.into(),
|
||||||
)),
|
)),
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
embed
|
Err(OolatoocsError::new("Cannot embed media").into())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn upload_media(
|
async fn upload_media(
|
||||||
|
|||||||
76
src/lib.rs
76
src/lib.rs
@@ -19,7 +19,7 @@ use utils::{generate_multi_tweets, strip_everything};
|
|||||||
|
|
||||||
mod bsky;
|
mod bsky;
|
||||||
use bsky::{
|
use bsky::{
|
||||||
build_post_record, generate_embed_records, generate_media_records, generate_quote_records,
|
build_post_record, generate_media_records, generate_quote_records, generate_webcard_records,
|
||||||
get_session, BskyReply,
|
get_session, BskyReply,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -161,22 +161,66 @@ pub async fn run(config: &Config) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Don’t know how to union things so…
|
// handle quote if any
|
||||||
// cards have the least priority (you cannot embed card and images anyway)
|
let quote_embed = match toot.reblog {
|
||||||
// images have a higher priority
|
Some(r) => {
|
||||||
// quotes have the highest priority
|
let quote_record = read_state(&conn, Some(r.id.parse::<u64>().unwrap()));
|
||||||
|
match quote_record {
|
||||||
let record_embed = if toot.reblog.is_some() {
|
Ok(Some(q)) => generate_quote_records(&config.bluesky, &q.record_uri)
|
||||||
let quote_record =
|
.await
|
||||||
read_state(&conn, Some(toot.reblog.unwrap().id.parse::<u64>().unwrap()));
|
.ok(),
|
||||||
match quote_record {
|
_ => None,
|
||||||
Ok(Some(q)) => generate_quote_records(&config.bluesky, &q.record_uri).await,
|
}
|
||||||
_ => None,
|
|
||||||
}
|
}
|
||||||
} else if toot.media_attachments.len() > usize::from(0u8) {
|
None => None,
|
||||||
generate_media_records(&bluesky, &toot.media_attachments).await
|
};
|
||||||
} else if toot.card.is_some() {
|
|
||||||
generate_embed_records(&bluesky, &toot.card.unwrap()).await
|
// handle medias if any
|
||||||
|
let media_embed = if toot.media_attachments.len() > usize::from(0u8) {
|
||||||
|
generate_media_records(&bluesky, &toot.media_attachments)
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// handle webcard if any
|
||||||
|
let webcard_embed = match toot.card {
|
||||||
|
Some(t) => generate_webcard_records(&bluesky, &t).await.ok(),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let record_embed = if quote_embed.is_some() {
|
||||||
|
if media_embed.is_some() {
|
||||||
|
let medias_mapped = match media_embed.unwrap() {
|
||||||
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedImagesMain(a) => atrium_api::app::bsky::embed::record_with_media::MainMediaRefs::AppBskyEmbedImagesMain(a),
|
||||||
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedVideoMain(a) => atrium_api::app::bsky::embed::record_with_media::MainMediaRefs::AppBskyEmbedVideoMain(a),
|
||||||
|
_ => continue, // this should NEVER happen as Media are either Video or
|
||||||
|
// Images at this point
|
||||||
|
};
|
||||||
|
let quote_mapped = match quote_embed.unwrap() {
|
||||||
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedRecordMain(
|
||||||
|
a,
|
||||||
|
) => a,
|
||||||
|
_ => continue, // again, this should NEVER happen
|
||||||
|
};
|
||||||
|
Some(atrium_api::types::Union::Refs(
|
||||||
|
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedRecordWithMediaMain(
|
||||||
|
Box::new(
|
||||||
|
atrium_api::app::bsky::embed::record_with_media::MainData {
|
||||||
|
media: atrium_api::types::Union::Refs(medias_mapped),
|
||||||
|
record: (*quote_mapped),
|
||||||
|
}.into()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
quote_embed.map(atrium_api::types::Union::Refs)
|
||||||
|
}
|
||||||
|
} else if media_embed.is_some() {
|
||||||
|
media_embed.map(atrium_api::types::Union::Refs)
|
||||||
|
} else if webcard_embed.is_some() {
|
||||||
|
webcard_embed.map(atrium_api::types::Union::Refs)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::config::MastodonConfig;
|
use crate::config::MastodonConfig;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use megalodon::{
|
use megalodon::{
|
||||||
entities::{Status, StatusVisibility},
|
entities::{QuotedStatus, Status, StatusVisibility},
|
||||||
generator,
|
generator,
|
||||||
mastodon::mastodon::Mastodon,
|
mastodon::mastodon::Mastodon,
|
||||||
megalodon::AppInputOptions,
|
megalodon::AppInputOptions,
|
||||||
@@ -56,12 +56,18 @@ pub async fn get_mastodon_timeline_since(
|
|||||||
.is_some_and(|r| r == t.account.id)
|
.is_some_and(|r| r == t.account.id)
|
||||||
})
|
})
|
||||||
.filter(|t| t.visibility == StatusVisibility::Public) // excludes everything that isn’t public
|
.filter(|t| t.visibility == StatusVisibility::Public) // excludes everything that isn’t public
|
||||||
|
.filter(|t| t.reblog.is_none()) // exclude reblogs
|
||||||
.filter(|t| {
|
.filter(|t| {
|
||||||
t.reblog.is_none()
|
// exclude quotes that aren’t ours
|
||||||
|| t.reblog
|
t.quote.is_none()
|
||||||
.clone()
|
|| t.quote.clone().is_some_and(|r| match r {
|
||||||
.is_some_and(|r| r.account.id == t.account.id)
|
QuotedStatus::Quote(q) => q
|
||||||
}) // excludes reblogs except by self
|
.quoted_status
|
||||||
|
.clone()
|
||||||
|
.is_some_and(|iq| iq.account.id == t.account.id),
|
||||||
|
_ => false,
|
||||||
|
})
|
||||||
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user