Initial commit

This commit is contained in:
VC
2023-11-07 14:19:40 +01:00
commit e8c2d35e21
9 changed files with 2644 additions and 0 deletions

100
src/mastodon.rs Normal file
View File

@@ -0,0 +1,100 @@
use crate::config::MastodonConfig;
use megalodon::{
entities::Status, generator, mastodon::mastodon::Mastodon, megalodon::AppInputOptions,
megalodon::GetHomeTimelineInputOptions, Megalodon,
};
use std::error::Error;
use std::io::stdin;
pub async fn get_mastodon_timeline_since(
config: &MastodonConfig,
id: Option<u64>,
) -> Result<Vec<Status>, Box<dyn Error>> {
let mastodon = Mastodon::new(
config.base.to_string(),
Some(config.token.to_string()),
None,
);
let input_options = GetHomeTimelineInputOptions {
only_media: Some(false),
limit: None,
max_id: None,
since_id: id.map(|i| i.to_string()),
min_id: None,
local: Some(true),
};
let mut timeline: Vec<Status> = mastodon
.get_home_timeline(Some(&input_options))
.await?
.json()
.iter()
.cloned()
.filter(|t| {
// this excludes the reply to other users
t.in_reply_to_account_id.is_none()
|| t.in_reply_to_account_id
.clone()
.is_some_and(|r| r == t.account.id)
})
.collect();
timeline.reverse();
Ok(timeline)
}
/// Generic register function
/// As this function is supposed to be run only once, it will panic for every error it encounters
/// Most of this function is a direct copy/paste of the official `elefren` crate
#[tokio::main]
pub async fn register(host: &str) {
let mastodon = generator(megalodon::SNS::Mastodon, host.to_string(), None, None);
let options = AppInputOptions {
redirect_uris: None,
scopes: Some(["read:statuses".to_string()].to_vec()),
website: Some("https://framagit.org/veretcle/oolatoocs".to_string()),
};
let app_data = mastodon
.register_app(env!("CARGO_PKG_NAME").to_string(), &options)
.await
.expect("Cannot build registration object!");
let url = app_data.url.expect("Cannot generate registration URI!");
println!("Click this link to authorize on Mastodon: {url}");
println!("Paste the returned authorization code: ");
let mut input = String::new();
stdin()
.read_line(&mut input)
.expect("Unable to read back registration code!");
let token_data = mastodon
.fetch_access_token(
app_data.client_id.to_owned(),
app_data.client_secret.to_owned(),
input.trim().to_string(),
megalodon::default::NO_REDIRECT.to_string(),
)
.await
.expect("Unable to create access token!");
println!(
r#"Please insert the following block at the end of your configuration file:
[mastodon]
base = "{}"
client_id = "{}"
client_secret = "{}"
redirect = "{}"
token = "{}""#,
host,
app_data.client_id,
app_data.client_secret,
app_data.redirect_uri.as_ref().unwrap(),
token_data.access_token,
);
}