implement post to mastodon branch
This commit is contained in:
parent
287beed7d6
commit
0dddbe75d6
4 changed files with 76 additions and 9 deletions
|
@ -1,6 +1,6 @@
|
||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand};
|
||||||
|
|
||||||
/// Bluesky Command Arguments
|
/// Bluesky command arguments
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
pub struct BlueskyCommand {
|
pub struct BlueskyCommand {
|
||||||
/// The Bluesky bot user's handle.
|
/// The Bluesky bot user's handle.
|
||||||
|
@ -12,6 +12,14 @@ pub struct BlueskyCommand {
|
||||||
pub bluesky_password: String,
|
pub bluesky_password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mastodon command arguments
|
||||||
|
#[derive(Args, Debug)]
|
||||||
|
pub struct MastodonCommand {
|
||||||
|
/// The Bluesky bot user's handle.
|
||||||
|
#[arg(short = 'a', long)]
|
||||||
|
pub access_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about = "Social media posting bot.", long_about = None)]
|
#[command(version, about = "Social media posting bot.", long_about = None)]
|
||||||
pub struct CliArgs {
|
pub struct CliArgs {
|
||||||
|
@ -42,5 +50,5 @@ pub enum Command {
|
||||||
/// Post on bluesky platform.
|
/// Post on bluesky platform.
|
||||||
Bluesky(BlueskyCommand),
|
Bluesky(BlueskyCommand),
|
||||||
/// Post on Mastodon, the FediVerse
|
/// Post on Mastodon, the FediVerse
|
||||||
Mastodon,
|
Mastodon(MastodonCommand),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
use crate::bluesky::BlueSkyClient;
|
use crate::bluesky::BlueSkyClient;
|
||||||
use crate::cli::{CliArgs, Command};
|
use crate::cli::{CliArgs, Command};
|
||||||
|
use crate::mastodon::api::{PartialMediaResponse, PartialPostStatusResponse, PostStatusRequest};
|
||||||
|
use crate::mastodon::MastodonClient;
|
||||||
|
use anyhow::{anyhow, Error};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use infrastructure::RedisService;
|
use infrastructure::RedisService;
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
|
@ -115,8 +118,54 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::Mastodon => {
|
Command::Mastodon(mastodon) => {
|
||||||
unimplemented!("This command is not currently implemented.")
|
let mut mastodon_client = MastodonClient::new(mastodon.access_token);
|
||||||
|
// Read from stream
|
||||||
|
while running.load(Ordering::SeqCst) {
|
||||||
|
match redis_service
|
||||||
|
.read_stream::<NewsPost>(
|
||||||
|
&args.redis_stream_name,
|
||||||
|
&args.redis_consumer_group,
|
||||||
|
&args.redis_consumer_name,
|
||||||
|
5000,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(post) => {
|
||||||
|
// Step1: Upload image to Mastodon
|
||||||
|
let media_response = if post.image.is_some() {
|
||||||
|
Ok(mastodon_client
|
||||||
|
.upload_media_by_url(post.image.clone().unwrap().as_str())
|
||||||
|
.await?)
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("No image exists on post."))
|
||||||
|
};
|
||||||
|
|
||||||
|
// Step2: Post to Mastodon.
|
||||||
|
let mut status: PostStatusRequest = post.into();
|
||||||
|
match media_response {
|
||||||
|
Ok(response) => {
|
||||||
|
status.media_ids.push(response.id);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("Error uploading image: {err}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let response = mastodon_client.post_status(status).await;
|
||||||
|
match response {
|
||||||
|
Ok(response) => {
|
||||||
|
info!("Posted tooth on Mastodon! {response:?}")
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("Failed to post toot on Mastodon: {err}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("error reading stream: {err}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::mastodon::api::{PartialMediaResponse, PartialPostStatusResponse, PostStatusRequest};
|
use crate::mastodon::api::{PartialMediaResponse, PartialPostStatusResponse, PostStatusRequest};
|
||||||
|
|
||||||
mod api;
|
pub mod api;
|
||||||
|
|
||||||
/// The Mastodon client for interacting with the platform.
|
/// The Mastodon client for interacting with the platform.
|
||||||
pub struct MastodonClient {
|
pub struct MastodonClient {
|
||||||
|
@ -10,7 +10,7 @@ pub struct MastodonClient {
|
||||||
|
|
||||||
impl MastodonClient {
|
impl MastodonClient {
|
||||||
/// Creates a new mastodon client from the given access token.
|
/// Creates a new mastodon client from the given access token.
|
||||||
fn new(access_token: String) -> Self {
|
pub fn new(access_token: String) -> Self {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
MastodonClient {
|
MastodonClient {
|
||||||
access_token,
|
access_token,
|
||||||
|
@ -18,14 +18,17 @@ impl MastodonClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_status<T>(&mut self, data: T) -> Result<PartialPostStatusResponse, anyhow::Error>
|
pub async fn post_status<T>(
|
||||||
|
&mut self,
|
||||||
|
data: T,
|
||||||
|
) -> Result<PartialPostStatusResponse, anyhow::Error>
|
||||||
where
|
where
|
||||||
T: Into<PostStatusRequest>,
|
T: Into<PostStatusRequest>,
|
||||||
{
|
{
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn upload_media_by_url(
|
pub async fn upload_media_by_url(
|
||||||
&mut self,
|
&mut self,
|
||||||
image_url: &str,
|
image_url: &str,
|
||||||
) -> Result<PartialMediaResponse, anyhow::Error> {
|
) -> Result<PartialMediaResponse, anyhow::Error> {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use post::NewsPost;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Is a truncated response from Mastodon's /api/v2/media endpoint.
|
/// Is a truncated response from Mastodon's /api/v2/media endpoint.
|
||||||
|
@ -17,7 +18,13 @@ pub struct PostStatusRequest {
|
||||||
pub status: String,
|
pub status: String,
|
||||||
pub language: String,
|
pub language: String,
|
||||||
pub visibility: String,
|
pub visibility: String,
|
||||||
pub media_ids: Vec<u64>,
|
pub media_ids: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<NewsPost> for PostStatusRequest {
|
||||||
|
fn from(value: NewsPost) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Is a partial response from /api/v1/statuses route.
|
/// Is a partial response from /api/v1/statuses route.
|
||||||
|
|
Loading…
Reference in a new issue