group-actor/src/main.rs

108 lines
3.8 KiB
Rust
Raw Normal View History

2021-08-20 20:59:19 +00:00
#[macro_use]
extern crate log;
#[macro_use]
extern crate smart_default;
#[macro_use]
extern crate serde;
extern crate elefren;
use elefren::prelude::*;
use elefren::helpers::{cli, toml};
use elefren::entities::event::Event;
use log::LevelFilter;
use elefren::entities::notification::NotificationType;
use elefren::entities::account::Account;
use elefren::status_builder::Visibility;
fn main() -> anyhow::Result<()> {
simple_logging::log_to_stderr(LevelFilter::Debug);
let client = if let Ok(data) = toml::from_file("group-actor-data.toml") {
Mastodon::from(data)
} else {
register()?
};
let you = client.verify_credentials()?;
println!("{:#?}", you);
for event in client.streaming_user()? {
match event {
Event::Update(status) => {
debug!("Status: {:#?}", status);
},
Event::Notification(notification) => {
debug!("Notification: {:#?}", notification);
match notification.notification_type {
NotificationType::Mention => {
if let Some(status) = notification.status {
if status.content.contains("/gi") {
debug!("GRPIGNORE!");
} else if status.content.contains("/gb") {
debug!("GROUP BOOST PREV!");
if let Some(id) = status.in_reply_to_id {
let _ = client.reblog(&id);
}
}
// else if status.content.contains("/gping") {
// let post = StatusBuilder::new()
// .status(format!("@{} hello", &notification.account.acct))
// .content_type("text/markdown")
// //.in_reply_to(status.id)
// .visibility(Visibility::Unlisted)
// .build().expect("error build status");
//
// let _ = client.new_status(post);
// }
else {
info!("BOOSTING");
let _ = client.reblog(&status.id);
}
}
}
NotificationType::Follow => {
info!("New follower!");
let post = StatusBuilder::new()
.status(format!("@{} welcome to the group!", &notification.account.acct))
.content_type("text/markdown")
.visibility(Visibility::Unlisted)
.build().expect("error build status");
let _ = client.new_status(post);
}
_ => {}
}
},
Event::Delete(id) => {
debug!("Delete: {}", id);
},
Event::FiltersChanged => {
debug!("FiltersChanged");
// ???
},
}
}
Ok(())
}
fn register() -> anyhow::Result<Mastodon> {
let registration = Registration::new("https://piggo.space")
.client_name("group-actor")
.scopes(Scopes::all())
.build()?;
let client = cli::authenticate(registration)?;
toml::to_file(&*client, "group-actor-data.toml")?;
Ok(client)
}
fn make_mention(user : &Account) -> String {
format!(r#"<span class="h-card"><a class="u-url mention" data-user="{id}" href="{url}" rel="ugc">@<span>{handle}</span></a></span>"#,
id=user.id,
url=user.url, handle=user.acct
)
}