mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-20 20:41:17 +02:00
✨: add embed card when available
This commit is contained in:
43
src/bsky.rs
43
src/bsky.rs
@@ -11,7 +11,10 @@ use bsky_sdk::{
|
||||
use futures::{stream, StreamExt};
|
||||
use image::ImageReader;
|
||||
use log::{debug, error, warn};
|
||||
use megalodon::entities::attachment::{Attachment, AttachmentType};
|
||||
use megalodon::entities::{
|
||||
attachment::{Attachment, AttachmentType},
|
||||
card::Card,
|
||||
};
|
||||
use regex::Regex;
|
||||
use std::{error::Error, fs::exists, io::Cursor};
|
||||
use webp::*;
|
||||
@@ -145,7 +148,43 @@ async fn get_record(
|
||||
Ok(record)
|
||||
}
|
||||
|
||||
// it’s ugly af but it gets the job done for now
|
||||
/// Generate an embed card record into Bsky
|
||||
/// If the preview image does not exist or fails to upload, it is simply ignored
|
||||
pub async fn generate_embed_records(
|
||||
bsky: &BskyAgent,
|
||||
card: &Card,
|
||||
) -> Option<atrium_api::types::Union<atrium_api::app::bsky::feed::post::RecordEmbedRefs>> {
|
||||
// uploads the image card, if it fails, simply ignore everything
|
||||
let blob = if let Some(url) = &card.image {
|
||||
if let Ok(image_blob) = upload_media(true, bsky, url).await {
|
||||
Some(image_blob.blob.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let record_card = atrium_api::app::bsky::embed::external::ExternalData {
|
||||
description: card.description.clone(),
|
||||
thumb: blob,
|
||||
title: card.title.clone(),
|
||||
uri: card.url.clone(),
|
||||
};
|
||||
|
||||
Some(atrium_api::types::Union::Refs(
|
||||
atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedExternalMain(Box::new(
|
||||
atrium_api::app::bsky::embed::external::MainData {
|
||||
external: record_card.into(),
|
||||
}
|
||||
.into(),
|
||||
)),
|
||||
))
|
||||
}
|
||||
|
||||
/// Generate an array of Bsky media records
|
||||
/// As Bsky does not support multiple video in a record or mix of video and images, video has the
|
||||
/// highest priority
|
||||
pub async fn generate_media_records(
|
||||
bsky: &BskyAgent,
|
||||
media_attach: &[Attachment],
|
||||
|
15
src/lib.rs
15
src/lib.rs
@@ -18,7 +18,9 @@ mod utils;
|
||||
use utils::{generate_multi_tweets, strip_everything};
|
||||
|
||||
mod bsky;
|
||||
use bsky::{build_post_record, generate_media_records, get_session, BskyReply};
|
||||
use bsky::{
|
||||
build_post_record, generate_embed_records, generate_media_records, get_session, BskyReply,
|
||||
};
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
@@ -152,14 +154,21 @@ pub async fn run(config: &Config) {
|
||||
};
|
||||
|
||||
// treats medias
|
||||
let record_medias = generate_media_records(&bluesky, &toot.media_attachments).await;
|
||||
let mut record_embed = generate_media_records(&bluesky, &toot.media_attachments).await;
|
||||
|
||||
// treats embed cards if any
|
||||
if let Some(card) = &toot.card {
|
||||
if record_embed.is_none() {
|
||||
record_embed = generate_embed_records(&bluesky, card).await;
|
||||
}
|
||||
}
|
||||
|
||||
// posts corresponding tweet
|
||||
let record = build_post_record(
|
||||
&config.bluesky,
|
||||
&tweet_content,
|
||||
&toot.language,
|
||||
record_medias,
|
||||
record_embed,
|
||||
&record_reply_to,
|
||||
)
|
||||
.await
|
||||
|
Reference in New Issue
Block a user