feature: make thread in Twitter thread in Mastodon

This commit is contained in:
VC
2022-04-23 13:39:41 +02:00
parent abfb2ff50a
commit 13bb6d6f37
5 changed files with 142 additions and 86 deletions

View File

@@ -1,4 +1,3 @@
// auto-imports
mod error;
use crate::error::ScootalooError;
@@ -19,22 +18,13 @@ mod state;
use state::{read_state, write_state, TweetToToot};
pub use state::init_db;
// std
use std::borrow::Cow;
// tokio
use tokio::fs::remove_file;
// elefren
use elefren::{
prelude::*,
status_builder::StatusBuilder,
};
// log
use log::{info, warn, error, debug};
// rusqlite
use rusqlite::Connection;
/// This is where the magic happens
@@ -72,6 +62,8 @@ pub async fn run(config: Config) {
for tweet in &feed {
debug!("Treating Tweet {} inside feed", tweet.id);
// initiate the toot_reply_id var
let mut toot_reply_id: Option<String> = None;
// determine if the tweet is part of a thread (response to self) or a standard response
if let Some(r) = &tweet.in_reply_to_screen_name {
if &r.to_lowercase() != &config.twitter.username.to_lowercase() {
@@ -79,6 +71,11 @@ pub async fn run(config: Config) {
info!("Tweet is a direct response, skipping");
continue;
}
let searched_toot = read_state(&conn, tweet.in_reply_to_status_id).unwrap_or(None);
if let Some(i) = searched_toot {
toot_reply_id = Some(i.toot_id);
};
};
// build basic status by just yielding text and dereferencing contained urls
@@ -125,10 +122,17 @@ pub async fn run(config: Config) {
// finished reuploading attachments, now lets do the toot baby!
debug!("Building corresponding Mastodon status");
let status = StatusBuilder::new()
.status(&status_text)
.media_ids(status_medias)
.build()
let mut status_builder = StatusBuilder::new();
status_builder.status(&status_text)
.media_ids(status_medias);
if let Some(i) = toot_reply_id {
status_builder.in_reply_to(&i);
}
let status = status_builder.build()
.expect(&format!("Cannot build status with text {}", &status_text));
// publish status