mirror of
https://git.ondrovo.com/MightyPork/group-actor.git
synced 2024-12-18 13:16:45 +00:00
formatting & lints, one fix for member-only groups
This commit is contained in:
parent
4c8fbb413d
commit
8822d8d11d
10 changed files with 528 additions and 323 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -341,7 +341,6 @@ dependencies = [
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"smart-default",
|
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
|
@ -1747,17 +1746,6 @@ version = "1.6.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
|
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "smart-default"
|
|
||||||
version = "0.6.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "socket2"
|
name = "socket2"
|
||||||
version = "0.3.19"
|
version = "0.3.19"
|
||||||
|
|
|
@ -19,7 +19,7 @@ env_logger = "0.9.0"
|
||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
smart-default = "0.6.0"
|
#smart-default = "0.6.0"
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
clap = "2.33.0"
|
clap = "2.33.0"
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
|
9
build.rs
9
build.rs
|
@ -2,11 +2,12 @@ use std::process::Command;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let desc_c = Command::new("git").args(&["describe", "--all", "--long"]).output().unwrap();
|
let desc_c = Command::new("git")
|
||||||
|
.args(&["describe", "--all", "--long"])
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let desc = unsafe {
|
let desc = unsafe { str::from_utf8_unchecked(&desc_c.stdout) };
|
||||||
str::from_utf8_unchecked( &desc_c.stdout )
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("cargo:rustc-env=GIT_REV={}", desc);
|
println!("cargo:rustc-env=GIT_REV={}", desc);
|
||||||
}
|
}
|
||||||
|
|
155
src/command.rs
155
src/command.rs
|
@ -1,5 +1,5 @@
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::{Regex, RegexSetBuilder};
|
use regex::Regex;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum StatusCommand {
|
pub enum StatusCommand {
|
||||||
|
@ -24,12 +24,12 @@ pub enum StatusCommand {
|
||||||
macro_rules! p_user {
|
macro_rules! p_user {
|
||||||
() => {
|
() => {
|
||||||
r"(@?[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-z0-9_-]+|@[a-zA-Z0-9_.-]+)"
|
r"(@?[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-z0-9_-]+|@[a-zA-Z0-9_.-]+)"
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
macro_rules! p_server {
|
macro_rules! p_server {
|
||||||
() => {
|
() => {
|
||||||
r"([a-zA-Z0-9_.-]+\.[a-zA-Z0-9_-]+)"
|
r"([a-zA-Z0-9_.-]+\.[a-zA-Z0-9_-]+)"
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! command {
|
macro_rules! command {
|
||||||
|
@ -38,69 +38,43 @@ macro_rules! command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static RE_BOOST: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_BOOST: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"b(?:oost)?"));
|
||||||
command!(r"b(?:oost)?")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_IGNORE: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_IGNORE: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"i(?:g(?:n(?:ore)?)?)?"));
|
||||||
command!(r"i(?:g(?:n(?:ore)?)?)?")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_BAN_USER: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_BAN_USER: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"ban\s+", p_user!()));
|
||||||
command!(r"ban\s+", p_user!())
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_UNBAN_USER: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_UNBAN_USER: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"unban\s+", p_user!()));
|
||||||
command!(r"unban\s+", p_user!())
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_BAN_SERVER: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_BAN_SERVER: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"ban\s+", p_server!()));
|
||||||
command!(r"ban\s+", p_server!())
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_UNBAN_SERVER: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_UNBAN_SERVER: once_cell::sync::Lazy<Regex> =
|
||||||
command!(r"unban\s+", p_server!())
|
Lazy::new(|| command!(r"unban\s+", p_server!()));
|
||||||
});
|
|
||||||
|
|
||||||
static RE_ADD_MEMBER: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_ADD_MEMBER: once_cell::sync::Lazy<Regex> =
|
||||||
command!(r"(?:add)\s+", p_user!())
|
Lazy::new(|| command!(r"(?:add)\s+", p_user!()));
|
||||||
});
|
|
||||||
|
|
||||||
static RE_REMOVE_MEMBER: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_REMOVE_MEMBER: once_cell::sync::Lazy<Regex> =
|
||||||
command!(r"(?:kick|remove)\s+", p_user!())
|
Lazy::new(|| command!(r"(?:kick|remove)\s+", p_user!()));
|
||||||
});
|
|
||||||
|
|
||||||
static RE_GRANT_ADMIN: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_GRANT_ADMIN: once_cell::sync::Lazy<Regex> =
|
||||||
command!(r"(?:op|admin)\s+", p_user!())
|
Lazy::new(|| command!(r"(?:op|admin)\s+", p_user!()));
|
||||||
});
|
|
||||||
|
|
||||||
static RE_REVOKE_ADMIN: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_REVOKE_ADMIN: once_cell::sync::Lazy<Regex> =
|
||||||
command!(r"(?:deop|deadmin)\s+", p_user!())
|
Lazy::new(|| command!(r"(?:deop|deadmin)\s+", p_user!()));
|
||||||
});
|
|
||||||
|
|
||||||
static RE_OPEN_GROUP: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_OPEN_GROUP: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"opengroup"));
|
||||||
command!(r"opengroup")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_CLOSE_GROUP: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_CLOSE_GROUP: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"closegroup"));
|
||||||
command!(r"closegroup")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_HELP: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_HELP: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"help"));
|
||||||
command!(r"help")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_MEMBERS: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_MEMBERS: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"(?:members|who)"));
|
||||||
command!(r"(?:members|who)")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_LEAVE: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_LEAVE: once_cell::sync::Lazy<Regex> = Lazy::new(|| command!(r"(?:leave)"));
|
||||||
command!(r"(?:leave)")
|
|
||||||
});
|
|
||||||
|
|
||||||
static RE_ANNOUNCE: once_cell::sync::Lazy<Regex> = Lazy::new(|| {
|
static RE_ANNOUNCE: once_cell::sync::Lazy<Regex> =
|
||||||
Regex::new(concat!(r"(?:^|\s|>|\n)[\\/]announce\s+(.*)$")).unwrap()
|
Lazy::new(|| Regex::new(concat!(r"(?:^|\s|>|\n)[\\/]announce\s+(.*)$")).unwrap());
|
||||||
});
|
|
||||||
|
|
||||||
pub fn parse_status(content: &str) -> Vec<StatusCommand> {
|
pub fn parse_status(content: &str) -> Vec<StatusCommand> {
|
||||||
debug!("Raw content: {}", content);
|
debug!("Raw content: {}", content);
|
||||||
|
@ -241,8 +215,11 @@ pub fn parse_status(content: &str) -> Vec<StatusCommand> {
|
||||||
mod test {
|
mod test {
|
||||||
use crate::command::{parse_status, StatusCommand};
|
use crate::command::{parse_status, StatusCommand};
|
||||||
|
|
||||||
use super::{RE_ADD_MEMBER, RE_ANNOUNCE, RE_BAN_SERVER, RE_BAN_USER, RE_BOOST, RE_CLOSE_GROUP, RE_GRANT_ADMIN,
|
use super::{
|
||||||
RE_HELP, RE_IGNORE, RE_LEAVE, RE_MEMBERS, RE_OPEN_GROUP, RE_REMOVE_MEMBER, RE_REVOKE_ADMIN};
|
RE_ADD_MEMBER, RE_ANNOUNCE, RE_BAN_SERVER, RE_BAN_USER, RE_BOOST, RE_CLOSE_GROUP,
|
||||||
|
RE_GRANT_ADMIN, RE_HELP, RE_IGNORE, RE_LEAVE, RE_MEMBERS, RE_OPEN_GROUP, RE_REMOVE_MEMBER,
|
||||||
|
RE_REVOKE_ADMIN,
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_boost() {
|
fn test_boost() {
|
||||||
|
@ -298,15 +275,24 @@ mod test {
|
||||||
|
|
||||||
let c = RE_BAN_USER.captures("/ban lain@pleroma.soykaf.com");
|
let c = RE_BAN_USER.captures("/ban lain@pleroma.soykaf.com");
|
||||||
assert!(c.is_some());
|
assert!(c.is_some());
|
||||||
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "lain@pleroma.soykaf.com");
|
assert_eq!(
|
||||||
|
c.unwrap().get(1).unwrap().as_str(),
|
||||||
|
"lain@pleroma.soykaf.com"
|
||||||
|
);
|
||||||
|
|
||||||
let c = RE_BAN_USER.captures("/ban lain@pleroma.soykaf.com xx");
|
let c = RE_BAN_USER.captures("/ban lain@pleroma.soykaf.com xx");
|
||||||
assert!(c.is_some());
|
assert!(c.is_some());
|
||||||
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "lain@pleroma.soykaf.com");
|
assert_eq!(
|
||||||
|
c.unwrap().get(1).unwrap().as_str(),
|
||||||
|
"lain@pleroma.soykaf.com"
|
||||||
|
);
|
||||||
|
|
||||||
let c = RE_BAN_USER.captures("/ban @lain@pleroma.soykaf.com");
|
let c = RE_BAN_USER.captures("/ban @lain@pleroma.soykaf.com");
|
||||||
assert!(c.is_some());
|
assert!(c.is_some());
|
||||||
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "@lain@pleroma.soykaf.com");
|
assert_eq!(
|
||||||
|
c.unwrap().get(1).unwrap().as_str(),
|
||||||
|
"@lain@pleroma.soykaf.com"
|
||||||
|
);
|
||||||
|
|
||||||
let c = RE_BAN_USER.captures("/ban @lain");
|
let c = RE_BAN_USER.captures("/ban @lain");
|
||||||
assert!(c.is_some());
|
assert!(c.is_some());
|
||||||
|
@ -359,7 +345,10 @@ mod test {
|
||||||
|
|
||||||
let c = RE_REMOVE_MEMBER.captures("/kick lain@pleroma.soykaf.com");
|
let c = RE_REMOVE_MEMBER.captures("/kick lain@pleroma.soykaf.com");
|
||||||
assert!(c.is_some());
|
assert!(c.is_some());
|
||||||
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "lain@pleroma.soykaf.com");
|
assert_eq!(
|
||||||
|
c.unwrap().get(1).unwrap().as_str(),
|
||||||
|
"lain@pleroma.soykaf.com"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -372,7 +361,10 @@ mod test {
|
||||||
|
|
||||||
let c = RE_GRANT_ADMIN.captures("/op @lain@pleroma.soykaf.com");
|
let c = RE_GRANT_ADMIN.captures("/op @lain@pleroma.soykaf.com");
|
||||||
assert!(c.is_some());
|
assert!(c.is_some());
|
||||||
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "@lain@pleroma.soykaf.com");
|
assert_eq!(
|
||||||
|
c.unwrap().get(1).unwrap().as_str(),
|
||||||
|
"@lain@pleroma.soykaf.com"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -441,27 +433,54 @@ mod test {
|
||||||
assert!(RE_ANNOUNCE.is_match("sdfsdffsd /announce b"));
|
assert!(RE_ANNOUNCE.is_match("sdfsdffsd /announce b"));
|
||||||
assert!(RE_ANNOUNCE.is_match("/announce bla bla bla"));
|
assert!(RE_ANNOUNCE.is_match("/announce bla bla bla"));
|
||||||
assert!(RE_ANNOUNCE.is_match("sdfsdffsd /announce bla bla bla"));
|
assert!(RE_ANNOUNCE.is_match("sdfsdffsd /announce bla bla bla"));
|
||||||
assert_eq!("bla bla bla", RE_ANNOUNCE.captures("sdfsdffsd /announce bla bla bla").unwrap().get(1).unwrap().as_str());
|
assert_eq!(
|
||||||
|
"bla bla bla",
|
||||||
|
RE_ANNOUNCE
|
||||||
|
.captures("sdfsdffsd /announce bla bla bla")
|
||||||
|
.unwrap()
|
||||||
|
.get(1)
|
||||||
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_real_post() {
|
fn test_real_post() {
|
||||||
assert_eq!(Vec::<StatusCommand>::new(), parse_status("Hello there is nothing here /fake command"));
|
assert_eq!(
|
||||||
assert_eq!(vec![StatusCommand::Help], parse_status("lets see some \\help and /ban @lain"));
|
Vec::<StatusCommand>::new(),
|
||||||
assert_eq!(vec![StatusCommand::Ignore], parse_status("lets see some /ignore and /ban @lain"));
|
parse_status("Hello there is nothing here /fake command")
|
||||||
assert_eq!(vec![
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vec![StatusCommand::Help],
|
||||||
|
parse_status("lets see some \\help and /ban @lain")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vec![StatusCommand::Ignore],
|
||||||
|
parse_status("lets see some /ignore and /ban @lain")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vec![
|
||||||
StatusCommand::BanUser("lain".to_string()),
|
StatusCommand::BanUser("lain".to_string()),
|
||||||
StatusCommand::BanUser("piggo@piggo.space".to_string()),
|
StatusCommand::BanUser("piggo@piggo.space".to_string()),
|
||||||
StatusCommand::BanServer("soykaf.com".to_string())
|
StatusCommand::BanServer("soykaf.com".to_string())
|
||||||
],
|
],
|
||||||
parse_status("let's /ban @lain! /ban @piggo@piggo.space and also /ban soykaf.com"));
|
parse_status("let's /ban @lain! /ban @piggo@piggo.space and also /ban soykaf.com")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_strip() {
|
fn test_strip() {
|
||||||
assert_eq!(vec![StatusCommand::BanUser("betty".to_string())],
|
assert_eq!(
|
||||||
parse_status(r#"Let's bad the naughty bot: /ban <span class="h-card"><a class="u-url mention" data-user="9nXpaGZL88fPAiP8xU" href="https://piggo.space/users/betty" rel="ugc">@<span>betty</span></a></span>"#));
|
vec![StatusCommand::BanUser("betty".to_string())],
|
||||||
assert_eq!(vec![StatusCommand::BanUser("betty@abstarbauze.com".to_string())],
|
parse_status(
|
||||||
parse_status(r#"Let's bad the naughty bot: /ban <span class="h-card"><a class="u-url mention" data-user="9nXpaGZL88fPAiP8xU" href="https://piggo.space/users/betty" rel="ugc">@<span>betty@abstarbauze.com</span></a></span>"#));
|
r#"Let's bad the naughty bot: /ban <span class="h-card"><a class="u-url mention" data-user="9nXpaGZL88fPAiP8xU" href="https://piggo.space/users/betty" rel="ugc">@<span>betty</span></a></span>"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
vec![StatusCommand::BanUser("betty@abstarbauze.com".to_string())],
|
||||||
|
parse_status(
|
||||||
|
r#"Let's bad the naughty bot: /ban <span class="h-card"><a class="u-url mention" data-user="9nXpaGZL88fPAiP8xU" href="https://piggo.space/users/betty" rel="ugc">@<span>betty@abstarbauze.com</span></a></span>"#
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
src/error.rs
16
src/error.rs
|
@ -23,13 +23,13 @@ pub enum GroupError {
|
||||||
// this is for tests
|
// this is for tests
|
||||||
impl PartialEq for GroupError {
|
impl PartialEq for GroupError {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
match (self, other) {
|
matches!(
|
||||||
(Self::UserIsAdmin, Self::UserIsAdmin) => true,
|
(self, other),
|
||||||
(Self::UserIsBanned, Self::UserIsBanned) => true,
|
(Self::UserIsAdmin, Self::UserIsAdmin)
|
||||||
(Self::AdminsOnServer, Self::AdminsOnServer) => true,
|
| (Self::UserIsBanned, Self::UserIsBanned)
|
||||||
(Self::GroupNotExist, Self::GroupNotExist) => true,
|
| (Self::AdminsOnServer, Self::AdminsOnServer)
|
||||||
(Self::BadConfig(_), Self::BadConfig(_)) => true,
|
| (Self::GroupNotExist, Self::GroupNotExist)
|
||||||
_ => false,
|
| (Self::BadConfig(_), Self::BadConfig(_))
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,19 @@ use std::collections::HashSet;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use elefren::{FediClient, StatusBuilder};
|
|
||||||
use elefren::debug::EventDisplay;
|
use elefren::debug::EventDisplay;
|
||||||
use elefren::debug::NotificationDisplay;
|
use elefren::debug::NotificationDisplay;
|
||||||
use elefren::entities::event::Event;
|
use elefren::entities::event::Event;
|
||||||
use elefren::entities::notification::{Notification, NotificationType};
|
use elefren::entities::notification::{Notification, NotificationType};
|
||||||
use elefren::status_builder::Visibility;
|
use elefren::status_builder::Visibility;
|
||||||
|
use elefren::{FediClient, StatusBuilder};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
|
||||||
use crate::command::StatusCommand;
|
use crate::command::StatusCommand;
|
||||||
use crate::error::GroupError;
|
use crate::error::GroupError;
|
||||||
use crate::store::ConfigStore;
|
|
||||||
use crate::store::data::GroupConfig;
|
use crate::store::data::GroupConfig;
|
||||||
use crate::utils::{LogError, normalize_acct};
|
use crate::store::ConfigStore;
|
||||||
|
use crate::utils::{normalize_acct, LogError};
|
||||||
|
|
||||||
/// This is one group's config store capable of persistence
|
/// This is one group's config store capable of persistence
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -91,22 +91,29 @@ impl GroupHandle {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if next_save < Instant::now() {
|
if next_save < Instant::now() {
|
||||||
self.save_if_needed().await
|
self.save_if_needed()
|
||||||
|
.await
|
||||||
.log_error("Failed to save group");
|
.log_error("Failed to save group");
|
||||||
next_save = Instant::now() + PERIODIC_SAVE;
|
next_save = Instant::now() + PERIODIC_SAVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
let timeout = next_save.saturating_duration_since(Instant::now())
|
let timeout = next_save
|
||||||
|
.saturating_duration_since(Instant::now())
|
||||||
.min(PING_INTERVAL)
|
.min(PING_INTERVAL)
|
||||||
.max(Duration::from_secs(1));
|
.max(Duration::from_secs(1));
|
||||||
|
|
||||||
match tokio::time::timeout(timeout, events.next()).await {
|
match tokio::time::timeout(timeout, events.next()).await {
|
||||||
Ok(Some(event)) => {
|
Ok(Some(event)) => {
|
||||||
debug!("(@{}) Event: {}", self.config.get_acct(), EventDisplay(&event));
|
debug!(
|
||||||
|
"(@{}) Event: {}",
|
||||||
|
self.config.get_acct(),
|
||||||
|
EventDisplay(&event)
|
||||||
|
);
|
||||||
match event {
|
match event {
|
||||||
Event::Update(_status) => {}
|
Event::Update(_status) => {}
|
||||||
Event::Notification(n) => {
|
Event::Notification(n) => {
|
||||||
self.handle_notification(n).await
|
self.handle_notification(n)
|
||||||
|
.await
|
||||||
.log_error("Error handling a notification");
|
.log_error("Error handling a notification");
|
||||||
}
|
}
|
||||||
Event::Delete(_id) => {}
|
Event::Delete(_id) => {}
|
||||||
|
@ -114,7 +121,10 @@ impl GroupHandle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
warn!("Group @{} socket closed, restarting...", self.config.get_acct());
|
warn!(
|
||||||
|
"Group @{} socket closed, restarting...",
|
||||||
|
self.config.get_acct()
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
@ -176,10 +186,13 @@ impl GroupHandle {
|
||||||
// Someone tagged the group in OP, boost it.
|
// Someone tagged the group in OP, boost it.
|
||||||
info!("Boosting OP mention");
|
info!("Boosting OP mention");
|
||||||
tokio::time::sleep(DELAY_BEFORE_ACTION).await;
|
tokio::time::sleep(DELAY_BEFORE_ACTION).await;
|
||||||
self.client.reblog(&status.id).await
|
self.client
|
||||||
|
.reblog(&status.id)
|
||||||
|
.await
|
||||||
.log_error("Failed to boost");
|
.log_error("Failed to boost");
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("You are not allowed to post to this group"));
|
replies
|
||||||
|
.push("You are not allowed to post to this group".to_string());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!("Not OP, ignore mention");
|
debug!("Not OP, ignore mention");
|
||||||
|
@ -200,7 +213,10 @@ impl GroupHandle {
|
||||||
if can_write {
|
if can_write {
|
||||||
do_boost_prev_post = status.in_reply_to_id.is_some();
|
do_boost_prev_post = status.in_reply_to_id.is_some();
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("You are not allowed to share to this group"));
|
replies.push(
|
||||||
|
"You are not allowed to share to this group"
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::BanUser(u) => {
|
StatusCommand::BanUser(u) => {
|
||||||
|
@ -210,17 +226,24 @@ impl GroupHandle {
|
||||||
match self.config.ban_user(&u, true) {
|
match self.config.ban_user(&u, true) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
replies.push(format!("User {} banned from group!", u));
|
replies.push(format!(
|
||||||
|
"User {} banned from group!",
|
||||||
|
u
|
||||||
|
));
|
||||||
|
|
||||||
// no announcement here
|
// no announcement here
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
replies.push(format!("Failed to ban user {}: {}", u, e));
|
replies.push(format!(
|
||||||
|
"Failed to ban user {}: {}",
|
||||||
|
u, e
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage user bans"));
|
replies
|
||||||
|
.push("Only admins can manage user bans".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::UnbanUser(u) => {
|
StatusCommand::UnbanUser(u) => {
|
||||||
|
@ -234,13 +257,14 @@ impl GroupHandle {
|
||||||
|
|
||||||
// no announcement here
|
// no announcement here
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage user bans"));
|
replies
|
||||||
|
.push("Only admins can manage user bans".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::BanServer(s) => {
|
StatusCommand::BanServer(s) => {
|
||||||
|
@ -249,16 +273,26 @@ impl GroupHandle {
|
||||||
match self.config.ban_server(&s, true) {
|
match self.config.ban_server(&s, true) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
announcements.push(format!("Server \"{}\" has been banned.", s));
|
announcements.push(format!(
|
||||||
replies.push(format!("Server {} banned from group!", s));
|
"Server \"{}\" has been banned.",
|
||||||
|
s
|
||||||
|
));
|
||||||
|
replies.push(format!(
|
||||||
|
"Server {} banned from group!",
|
||||||
|
s
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
replies.push(format!("Failed to ban server {}: {}", s, e));
|
replies.push(format!(
|
||||||
|
"Failed to ban server {}: {}",
|
||||||
|
s, e
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage server bans"));
|
replies
|
||||||
|
.push("Only admins can manage server bans".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::UnbanServer(s) => {
|
StatusCommand::UnbanServer(s) => {
|
||||||
|
@ -267,16 +301,21 @@ impl GroupHandle {
|
||||||
match self.config.ban_server(&s, false) {
|
match self.config.ban_server(&s, false) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
announcements.push(format!("Server \"{}\" has been un-banned.", s));
|
announcements.push(format!(
|
||||||
replies.push(format!("Server {} un-banned!", s));
|
"Server \"{}\" has been un-banned.",
|
||||||
|
s
|
||||||
|
));
|
||||||
|
replies
|
||||||
|
.push(format!("Server {} un-banned!", s));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage server bans"));
|
replies
|
||||||
|
.push("Only admins can manage server bans".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::AddMember(u) => {
|
StatusCommand::AddMember(u) => {
|
||||||
|
@ -286,19 +325,28 @@ impl GroupHandle {
|
||||||
match self.config.set_member(&u, true) {
|
match self.config.set_member(&u, true) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
replies.push(format!("User {} added to the group!", u));
|
replies.push(format!(
|
||||||
|
"User {} added to the group!",
|
||||||
|
u
|
||||||
|
));
|
||||||
|
|
||||||
if self.config.is_member_only() {
|
if self.config.is_member_only() {
|
||||||
announcements.push(format!("Welcome new member @{} to the group!", u));
|
announcements.push(format!(
|
||||||
|
"Welcome new member @{} to the group!",
|
||||||
|
u
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
replies.push(format!("Failed to add user {} to group: {}", u, e));
|
replies.push(format!(
|
||||||
|
"Failed to add user {} to group: {}",
|
||||||
|
u, e
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage members"));
|
replies.push("Only admins can manage members".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::RemoveMember(u) => {
|
StatusCommand::RemoveMember(u) => {
|
||||||
|
@ -308,15 +356,18 @@ impl GroupHandle {
|
||||||
match self.config.set_member(&u, false) {
|
match self.config.set_member(&u, false) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
replies.push(format!("User {} removed from the group.", u));
|
replies.push(format!(
|
||||||
|
"User {} removed from the group.",
|
||||||
|
u
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage members"));
|
replies.push("Only admins can manage members".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::GrantAdmin(u) => {
|
StatusCommand::GrantAdmin(u) => {
|
||||||
|
@ -326,16 +377,25 @@ impl GroupHandle {
|
||||||
match self.config.set_admin(&u, true) {
|
match self.config.set_admin(&u, true) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
replies.push(format!("User {} is now a group admin!", u));
|
replies.push(format!(
|
||||||
announcements.push(format!("User @{} can now manage this group!", u));
|
"User {} is now a group admin!",
|
||||||
|
u
|
||||||
|
));
|
||||||
|
announcements.push(format!(
|
||||||
|
"User @{} can now manage this group!",
|
||||||
|
u
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
replies.push(format!("Failed to make user {} a group admin: {}", u, e));
|
replies.push(format!(
|
||||||
|
"Failed to make user {} a group admin: {}",
|
||||||
|
u, e
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage admins"));
|
replies.push("Only admins can manage admins".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::RemoveAdmin(u) => {
|
StatusCommand::RemoveAdmin(u) => {
|
||||||
|
@ -345,16 +405,25 @@ impl GroupHandle {
|
||||||
match self.config.set_admin(&u, false) {
|
match self.config.set_admin(&u, false) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
replies.push(format!("User {} is no longer a group admin!", u));
|
replies.push(format!(
|
||||||
announcements.push(format!("User @{} no longer manages this group.", u));
|
"User {} is no longer a group admin!",
|
||||||
|
u
|
||||||
|
));
|
||||||
|
announcements.push(format!(
|
||||||
|
"User @{} no longer manages this group.",
|
||||||
|
u
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
replies.push(format!("Failed to revoke {}'s group admin: {}", u, e));
|
replies.push(format!(
|
||||||
|
"Failed to revoke {}'s group admin: {}",
|
||||||
|
u, e
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can manage admins"));
|
replies.push("Only admins can manage admins".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::OpenGroup => {
|
StatusCommand::OpenGroup => {
|
||||||
|
@ -362,11 +431,14 @@ impl GroupHandle {
|
||||||
if self.config.is_member_only() {
|
if self.config.is_member_only() {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
self.config.set_member_only(false);
|
self.config.set_member_only(false);
|
||||||
replies.push(format!("Group changed to open-access"));
|
replies
|
||||||
announcements.push(format!("This group is now open-access!"));
|
.push("Group changed to open-access".to_string());
|
||||||
|
announcements
|
||||||
|
.push("This group is now open-access!".to_string());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can set group options"));
|
replies
|
||||||
|
.push("Only admins can set group options".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::CloseGroup => {
|
StatusCommand::CloseGroup => {
|
||||||
|
@ -374,24 +446,27 @@ impl GroupHandle {
|
||||||
if !self.config.is_member_only() {
|
if !self.config.is_member_only() {
|
||||||
any_admin_cmd = true;
|
any_admin_cmd = true;
|
||||||
self.config.set_member_only(true);
|
self.config.set_member_only(true);
|
||||||
replies.push(format!("Group changed to member-only"));
|
replies
|
||||||
announcements.push(format!("This group is now member-only!"));
|
.push("Group changed to member-only".to_string());
|
||||||
|
announcements
|
||||||
|
.push("This group is now member-only!".to_string());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("Only admins can set group options"));
|
replies
|
||||||
|
.push("Only admins can set group options".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCommand::Help => {
|
StatusCommand::Help => {
|
||||||
if self.config.is_member_only() {
|
if self.config.is_member_only() {
|
||||||
let mut s = "This is a member-only group. ".to_string();
|
let mut s = "This is a member-only group. ".to_string();
|
||||||
if self.config.can_write(¬if_acct) {
|
if self.config.can_write(¬if_acct) {
|
||||||
s.push_str("*You are not a member, ask one of the admins to add you.*");
|
|
||||||
} else {
|
|
||||||
if is_admin {
|
if is_admin {
|
||||||
s.push_str("*You are an admin.*");
|
s.push_str("*You are an admin.*");
|
||||||
} else {
|
} else {
|
||||||
s.push_str("*You are a member.*");
|
s.push_str("*You are a member.*");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
s.push_str("*You are not a member, ask one of the admins to add you.*");
|
||||||
}
|
}
|
||||||
replies.push(s);
|
replies.push(s);
|
||||||
} else {
|
} else {
|
||||||
|
@ -409,7 +484,9 @@ impl GroupHandle {
|
||||||
**Supported commands:**\n\
|
**Supported commands:**\n\
|
||||||
`/ignore, /i` - make the group completely ignore the post\n\
|
`/ignore, /i` - make the group completely ignore the post\n\
|
||||||
`/members, /who` - show group members / admins\n\
|
`/members, /who` - show group members / admins\n\
|
||||||
`/boost, /b` - boost the replied-to post into the group".to_string());
|
`/boost, /b` - boost the replied-to post into the group"
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
if self.config.is_member_only() {
|
if self.config.is_member_only() {
|
||||||
replies.push("`/leave` - leave the group".to_string());
|
replies.push("`/leave` - leave the group".to_string());
|
||||||
|
@ -433,8 +510,10 @@ impl GroupHandle {
|
||||||
if is_admin {
|
if is_admin {
|
||||||
if self.config.is_member_only() {
|
if self.config.is_member_only() {
|
||||||
replies.push("Group members:".to_string());
|
replies.push("Group members:".to_string());
|
||||||
let admins = self.config.get_admins().collect::<HashSet<_>>();
|
let admins =
|
||||||
let mut members = self.config.get_members().collect::<Vec<_>>();
|
self.config.get_admins().collect::<HashSet<_>>();
|
||||||
|
let mut members =
|
||||||
|
self.config.get_members().collect::<Vec<_>>();
|
||||||
members.extend(admins.iter());
|
members.extend(admins.iter());
|
||||||
members.sort();
|
members.sort();
|
||||||
members.dedup();
|
members.dedup();
|
||||||
|
@ -442,7 +521,7 @@ impl GroupHandle {
|
||||||
if admins.contains(&m) {
|
if admins.contains(&m) {
|
||||||
replies.push(format!("{} [admin]", m));
|
replies.push(format!("{} [admin]", m));
|
||||||
} else {
|
} else {
|
||||||
replies.push(format!("{}", m));
|
replies.push(m.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -454,10 +533,11 @@ impl GroupHandle {
|
||||||
|
|
||||||
if show_admins {
|
if show_admins {
|
||||||
replies.push("Group admins:".to_string());
|
replies.push("Group admins:".to_string());
|
||||||
let mut admins = self.config.get_admins().collect::<Vec<_>>();
|
let mut admins =
|
||||||
|
self.config.get_admins().collect::<Vec<_>>();
|
||||||
admins.sort();
|
admins.sort();
|
||||||
for a in admins {
|
for a in admins {
|
||||||
replies.push(format!("{}", a));
|
replies.push(a.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -475,7 +555,9 @@ impl GroupHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
if do_boost_prev_post {
|
if do_boost_prev_post {
|
||||||
self.client.reblog(&status.in_reply_to_id.as_ref().unwrap()).await
|
self.client
|
||||||
|
.reblog(&status.in_reply_to_id.as_ref().unwrap())
|
||||||
|
.await
|
||||||
.log_error("Failed to boost");
|
.log_error("Failed to boost");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -486,9 +568,14 @@ impl GroupHandle {
|
||||||
.status(format!("@{user}\n{msg}", user = notif_acct, msg = r))
|
.status(format!("@{user}\n{msg}", user = notif_acct, msg = r))
|
||||||
.content_type("text/markdown")
|
.content_type("text/markdown")
|
||||||
.visibility(Visibility::Direct)
|
.visibility(Visibility::Direct)
|
||||||
.build().expect("error build status");
|
.build()
|
||||||
|
.expect("error build status");
|
||||||
|
|
||||||
let _ = self.client.new_status(post).await.log_error("Failed to post");
|
let _ = self
|
||||||
|
.client
|
||||||
|
.new_status(post)
|
||||||
|
.await
|
||||||
|
.log_error("Failed to post");
|
||||||
}
|
}
|
||||||
|
|
||||||
if !announcements.is_empty() {
|
if !announcements.is_empty() {
|
||||||
|
@ -497,9 +584,14 @@ impl GroupHandle {
|
||||||
.status(format!("**📢 Group announcement**\n{msg}", msg = msg))
|
.status(format!("**📢 Group announcement**\n{msg}", msg = msg))
|
||||||
.content_type("text/markdown")
|
.content_type("text/markdown")
|
||||||
.visibility(Visibility::Unlisted)
|
.visibility(Visibility::Unlisted)
|
||||||
.build().expect("error build status");
|
.build()
|
||||||
|
.expect("error build status");
|
||||||
|
|
||||||
let _ = self.client.new_status(post).await.log_error("Failed to post");
|
let _ = self
|
||||||
|
.client
|
||||||
|
.new_status(post)
|
||||||
|
.await
|
||||||
|
.log_error("Failed to post");
|
||||||
}
|
}
|
||||||
|
|
||||||
if any_admin_cmd {
|
if any_admin_cmd {
|
||||||
|
@ -511,7 +603,8 @@ impl GroupHandle {
|
||||||
info!("New follower!");
|
info!("New follower!");
|
||||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||||
|
|
||||||
let text = if self.config.is_member_only() {
|
let text =
|
||||||
|
if self.config.is_member_only() {
|
||||||
// Admins are listed without @, so they won't become handles here.
|
// Admins are listed without @, so they won't become handles here.
|
||||||
// Tagging all admins would be annoying.
|
// Tagging all admins would be annoying.
|
||||||
let mut admins = self.config.get_admins().cloned().collect::<Vec<_>>();
|
let mut admins = self.config.get_admins().cloned().collect::<Vec<_>>();
|
||||||
|
@ -523,16 +616,23 @@ impl GroupHandle {
|
||||||
} else {
|
} else {
|
||||||
format!(
|
format!(
|
||||||
"@{user} welcome to the group! \
|
"@{user} welcome to the group! \
|
||||||
To share a post, tag the group user. Use /help for more info.", user = notif_acct)
|
To share a post, tag the group user. Use /help for more info.",
|
||||||
|
user = notif_acct
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let post = StatusBuilder::new()
|
let post = StatusBuilder::new()
|
||||||
.status(text)
|
.status(text)
|
||||||
.content_type("text/markdown")
|
.content_type("text/markdown")
|
||||||
.visibility(Visibility::Direct)
|
.visibility(Visibility::Direct)
|
||||||
.build().expect("error build status");
|
.build()
|
||||||
|
.expect("error build status");
|
||||||
|
|
||||||
let _ = self.client.new_status(post).await.log_error("Failed to post");
|
let _ = self
|
||||||
|
.client
|
||||||
|
.new_status(post)
|
||||||
|
.await
|
||||||
|
.log_error("Failed to post");
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -575,10 +675,11 @@ impl GroupHandle {
|
||||||
|
|
||||||
for n in notifs_to_handle {
|
for n in notifs_to_handle {
|
||||||
debug!("Handling missed notification: {}", NotificationDisplay(&n));
|
debug!("Handling missed notification: {}", NotificationDisplay(&n));
|
||||||
self.handle_notification(n).await
|
self.handle_notification(n)
|
||||||
|
.await
|
||||||
.log_error("Error handling a notification");
|
.log_error("Error handling a notification");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(true);
|
Ok(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -5,67 +5,62 @@ extern crate elefren;
|
||||||
extern crate log;
|
extern crate log;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
#[macro_use]
|
// #[macro_use]
|
||||||
extern crate smart_default;
|
// extern crate smart_default;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate thiserror;
|
extern crate thiserror;
|
||||||
|
|
||||||
use clap::Arg;
|
use clap::Arg;
|
||||||
use elefren::{FediClient, Registration, Scopes, StatusBuilder};
|
|
||||||
use elefren::debug::NotificationDisplay;
|
|
||||||
use elefren::entities::account::Account;
|
|
||||||
use elefren::entities::event::Event;
|
|
||||||
use elefren::entities::notification::NotificationType;
|
|
||||||
use elefren::scopes;
|
|
||||||
use elefren::status_builder::Visibility;
|
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use tokio_stream::{Stream, StreamExt};
|
|
||||||
|
|
||||||
use crate::store::{NewGroupOptions, StoreOptions};
|
use crate::store::{NewGroupOptions, StoreOptions};
|
||||||
use crate::utils::acct_to_server;
|
use crate::utils::acct_to_server;
|
||||||
|
|
||||||
mod store;
|
|
||||||
mod group_handle;
|
|
||||||
mod utils;
|
|
||||||
mod command;
|
mod command;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod group_handle;
|
||||||
|
mod store;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
let args = clap::App::new("groups")
|
let args = clap::App::new("groups")
|
||||||
.arg(Arg::with_name("verbose")
|
.arg(
|
||||||
|
Arg::with_name("verbose")
|
||||||
.short("v")
|
.short("v")
|
||||||
.multiple(true)
|
.multiple(true)
|
||||||
.help("increase logging, can be repeated"))
|
.help("increase logging, can be repeated"),
|
||||||
.arg(Arg::with_name("config")
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("config")
|
||||||
.short("c")
|
.short("c")
|
||||||
.long("config")
|
.long("config")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.help("set custom storage file, defaults to groups.json"))
|
.help("set custom storage file, defaults to groups.json"),
|
||||||
.arg(Arg::with_name("auth")
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("auth")
|
||||||
.short("a")
|
.short("a")
|
||||||
.long("auth")
|
.long("auth")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.value_name("HANDLE")
|
.value_name("HANDLE")
|
||||||
.help("authenticate to a new server (always using https)"))
|
.help("authenticate to a new server (always using https)"),
|
||||||
.arg(Arg::with_name("reauth")
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("reauth")
|
||||||
.short("A")
|
.short("A")
|
||||||
.long("reauth")
|
.long("reauth")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.value_name("HANDLE")
|
.value_name("HANDLE")
|
||||||
.help("authenticate to a new server (always using https)"))
|
.help("authenticate to a new server (always using https)"),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
const LEVELS: [LevelFilter; 5] = [
|
const LEVELS: [LevelFilter; 5] = [
|
||||||
/// Corresponds to the `Error` log level.
|
|
||||||
LevelFilter::Error,
|
LevelFilter::Error,
|
||||||
/// Corresponds to the `Warn` log level.
|
|
||||||
LevelFilter::Warn,
|
LevelFilter::Warn,
|
||||||
/// Corresponds to the `Info` log level.
|
|
||||||
LevelFilter::Info,
|
LevelFilter::Info,
|
||||||
/// Corresponds to the `Debug` log level.
|
|
||||||
LevelFilter::Debug,
|
LevelFilter::Debug,
|
||||||
/// Corresponds to the `Trace` log level.
|
|
||||||
LevelFilter::Trace,
|
LevelFilter::Trace,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -83,15 +78,18 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let store = store::ConfigStore::new(StoreOptions {
|
let store = store::ConfigStore::new(StoreOptions {
|
||||||
store_path: args.value_of("config").unwrap_or("groups.json").to_string(),
|
store_path: args.value_of("config").unwrap_or("groups.json").to_string(),
|
||||||
save_pretty: true,
|
save_pretty: true,
|
||||||
}).await?;
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
if let Some(handle) = args.value_of("auth") {
|
if let Some(handle) = args.value_of("auth") {
|
||||||
let acct = handle.trim_start_matches('@');
|
let acct = handle.trim_start_matches('@');
|
||||||
if let Some(server) = acct_to_server(acct) {
|
if let Some(server) = acct_to_server(acct) {
|
||||||
let g = store.auth_new_group(NewGroupOptions {
|
let g = store
|
||||||
|
.auth_new_group(NewGroupOptions {
|
||||||
server: format!("https://{}", server),
|
server: format!("https://{}", server),
|
||||||
acct: acct.to_string()
|
acct: acct.to_string(),
|
||||||
}).await?;
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
eprintln!("New group @{} added to config!", g.config.get_acct());
|
eprintln!("New group @{} added to config!", g.config.get_acct());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -108,13 +106,11 @@ async fn main() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start
|
// Start
|
||||||
let mut groups = store.spawn_groups().await;
|
let groups = store.spawn_groups().await;
|
||||||
|
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for mut g in groups {
|
for mut g in groups {
|
||||||
handles.push(tokio::spawn(async move {
|
handles.push(tokio::spawn(async move { g.run().await }));
|
||||||
g.run().await
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
futures::future::join_all(handles).await;
|
futures::future::join_all(handles).await;
|
||||||
|
|
|
@ -3,7 +3,6 @@ use std::collections::{HashMap, HashSet};
|
||||||
use elefren::AppData;
|
use elefren::AppData;
|
||||||
|
|
||||||
use crate::error::GroupError;
|
use crate::error::GroupError;
|
||||||
use crate::store;
|
|
||||||
|
|
||||||
/// This is the inner data struct holding the config
|
/// This is the inner data struct holding the config
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
|
@ -23,7 +22,6 @@ impl Config {
|
||||||
pub(crate) fn set_group_config(&mut self, grp: GroupConfig) {
|
pub(crate) fn set_group_config(&mut self, grp: GroupConfig) {
|
||||||
self.groups.insert(grp.acct.clone(), grp);
|
self.groups.insert(grp.acct.clone(), grp);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is the inner data struct holding a group's config
|
/// This is the inner data struct holding a group's config
|
||||||
|
@ -62,7 +60,7 @@ impl Default for GroupConfig {
|
||||||
client_id: Default::default(),
|
client_id: Default::default(),
|
||||||
client_secret: Default::default(),
|
client_secret: Default::default(),
|
||||||
redirect: Default::default(),
|
redirect: Default::default(),
|
||||||
token: Default::default()
|
token: Default::default(),
|
||||||
},
|
},
|
||||||
admin_users: Default::default(),
|
admin_users: Default::default(),
|
||||||
member_users: Default::default(),
|
member_users: Default::default(),
|
||||||
|
@ -132,8 +130,7 @@ impl GroupConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_banned(&self, acct: &str) -> bool {
|
pub(crate) fn is_banned(&self, acct: &str) -> bool {
|
||||||
self.banned_users.contains(acct)
|
self.banned_users.contains(acct) || self.is_users_server_banned(acct)
|
||||||
|| self.is_users_server_banned(acct)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_server_banned(&self, server: &str) -> bool {
|
pub(crate) fn is_server_banned(&self, server: &str) -> bool {
|
||||||
|
@ -150,10 +147,7 @@ impl GroupConfig {
|
||||||
if self.is_admin(acct) {
|
if self.is_admin(acct) {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
!self.is_banned(acct) && (
|
!self.is_banned(acct) && (!self.is_member_only() || self.is_member(acct))
|
||||||
!self.is_member_only()
|
|
||||||
|| self.is_member(acct)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,8 +228,7 @@ impl GroupConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn acct_to_server(acct: &str) -> &str {
|
fn acct_to_server(acct: &str) -> &str {
|
||||||
acct.split('@').nth(1)
|
acct.split('@').nth(1).unwrap_or_default()
|
||||||
.unwrap_or_default()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -252,32 +245,53 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_rules() {
|
fn test_default_rules() {
|
||||||
let mut group = GroupConfig::default();
|
let group = GroupConfig::default();
|
||||||
assert!(!group.is_member_only());
|
assert!(!group.is_member_only());
|
||||||
assert!(!group.is_member("piggo@piggo.space"));
|
assert!(!group.is_member("piggo@piggo.space"));
|
||||||
assert!(!group.is_admin("piggo@piggo.space"));
|
assert!(!group.is_admin("piggo@piggo.space"));
|
||||||
assert!(group.can_write("piggo@piggo.space"), "anyone can post by default");
|
assert!(
|
||||||
|
group.can_write("piggo@piggo.space"),
|
||||||
|
"anyone can post by default"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_member_only() {
|
fn test_member_only() {
|
||||||
let mut group = GroupConfig::default();
|
let mut group = GroupConfig::default();
|
||||||
assert!(group.can_write("piggo@piggo.space"), "rando can write in public group");
|
assert!(
|
||||||
|
group.can_write("piggo@piggo.space"),
|
||||||
|
"rando can write in public group"
|
||||||
|
);
|
||||||
|
|
||||||
group.set_member_only(true);
|
group.set_member_only(true);
|
||||||
assert!(!group.can_write("piggo@piggo.space"), "rando can't write in member-only group");
|
assert!(
|
||||||
|
!group.can_write("piggo@piggo.space"),
|
||||||
|
"rando can't write in member-only group"
|
||||||
|
);
|
||||||
|
|
||||||
// Admin in member only
|
// Admin in member only
|
||||||
group.set_admin("piggo@piggo.space", true).unwrap();
|
group.set_admin("piggo@piggo.space", true).unwrap();
|
||||||
assert!(group.can_write("piggo@piggo.space"), "admin non-member can write in member-only group");
|
assert!(
|
||||||
|
group.can_write("piggo@piggo.space"),
|
||||||
|
"admin non-member can write in member-only group"
|
||||||
|
);
|
||||||
group.set_admin("piggo@piggo.space", false).unwrap();
|
group.set_admin("piggo@piggo.space", false).unwrap();
|
||||||
assert!(!group.can_write("piggo@piggo.space"), "removed admin removes privileged write access");
|
assert!(
|
||||||
|
!group.can_write("piggo@piggo.space"),
|
||||||
|
"removed admin removes privileged write access"
|
||||||
|
);
|
||||||
|
|
||||||
// Member in member only
|
// Member in member only
|
||||||
group.set_member("piggo@piggo.space", true).unwrap();
|
group.set_member("piggo@piggo.space", true).unwrap();
|
||||||
assert!(group.can_write("piggo@piggo.space"), "member can post in member-only group");
|
assert!(
|
||||||
|
group.can_write("piggo@piggo.space"),
|
||||||
|
"member can post in member-only group"
|
||||||
|
);
|
||||||
group.set_admin("piggo@piggo.space", true).unwrap();
|
group.set_admin("piggo@piggo.space", true).unwrap();
|
||||||
assert!(group.can_write("piggo@piggo.space"), "member+admin can post in member-only group");
|
assert!(
|
||||||
|
group.can_write("piggo@piggo.space"),
|
||||||
|
"member+admin can post in member-only group"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -285,7 +299,10 @@ mod tests {
|
||||||
// Banning single user
|
// Banning single user
|
||||||
let mut group = GroupConfig::default();
|
let mut group = GroupConfig::default();
|
||||||
group.ban_user("piggo@piggo.space", true).unwrap();
|
group.ban_user("piggo@piggo.space", true).unwrap();
|
||||||
assert!(!group.can_write("piggo@piggo.space"), "banned user can't post");
|
assert!(
|
||||||
|
!group.can_write("piggo@piggo.space"),
|
||||||
|
"banned user can't post"
|
||||||
|
);
|
||||||
group.ban_user("piggo@piggo.space", false).unwrap();
|
group.ban_user("piggo@piggo.space", false).unwrap();
|
||||||
assert!(group.can_write("piggo@piggo.space"), "un-ban works");
|
assert!(group.can_write("piggo@piggo.space"), "un-ban works");
|
||||||
}
|
}
|
||||||
|
@ -299,13 +316,25 @@ mod tests {
|
||||||
group.set_member("piggo@piggo.space", true).unwrap();
|
group.set_member("piggo@piggo.space", true).unwrap();
|
||||||
assert!(group.can_write("piggo@piggo.space"), "member can write");
|
assert!(group.can_write("piggo@piggo.space"), "member can write");
|
||||||
assert!(group.is_member("piggo@piggo.space"), "member is member");
|
assert!(group.is_member("piggo@piggo.space"), "member is member");
|
||||||
assert!(!group.is_banned("piggo@piggo.space"), "user not banned by default");
|
assert!(
|
||||||
|
!group.is_banned("piggo@piggo.space"),
|
||||||
|
"user not banned by default"
|
||||||
|
);
|
||||||
|
|
||||||
group.ban_user("piggo@piggo.space", true).unwrap();
|
group.ban_user("piggo@piggo.space", true).unwrap();
|
||||||
assert!(group.is_member("piggo@piggo.space"), "still member even if banned");
|
assert!(
|
||||||
assert!(group.is_banned("piggo@piggo.space"), "banned user is banned");
|
group.is_member("piggo@piggo.space"),
|
||||||
|
"still member even if banned"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
group.is_banned("piggo@piggo.space"),
|
||||||
|
"banned user is banned"
|
||||||
|
);
|
||||||
|
|
||||||
assert!(!group.can_write("piggo@piggo.space"), "banned member can't post");
|
assert!(
|
||||||
|
!group.can_write("piggo@piggo.space"),
|
||||||
|
"banned member can't post"
|
||||||
|
);
|
||||||
|
|
||||||
// unban
|
// unban
|
||||||
group.ban_user("piggo@piggo.space", false).unwrap();
|
group.ban_user("piggo@piggo.space", false).unwrap();
|
||||||
|
@ -318,9 +347,18 @@ mod tests {
|
||||||
assert!(group.can_write("hitler@nazi.camp"), "randos can write");
|
assert!(group.can_write("hitler@nazi.camp"), "randos can write");
|
||||||
|
|
||||||
group.ban_server("nazi.camp", true).unwrap();
|
group.ban_server("nazi.camp", true).unwrap();
|
||||||
assert!(!group.can_write("hitler@nazi.camp"), "users from banned server can't write");
|
assert!(
|
||||||
assert!(!group.can_write("1488@nazi.camp"), "users from banned server can't write");
|
!group.can_write("hitler@nazi.camp"),
|
||||||
assert!(group.can_write("troll@freezepeach.xyz"), "other users can still write");
|
"users from banned server can't write"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!group.can_write("1488@nazi.camp"),
|
||||||
|
"users from banned server can't write"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
group.can_write("troll@freezepeach.xyz"),
|
||||||
|
"other users can still write"
|
||||||
|
);
|
||||||
|
|
||||||
group.ban_server("nazi.camp", false).unwrap();
|
group.ban_server("nazi.camp", false).unwrap();
|
||||||
assert!(group.can_write("hitler@nazi.camp"), "server unban works");
|
assert!(group.can_write("hitler@nazi.camp"), "server unban works");
|
||||||
|
@ -331,16 +369,34 @@ mod tests {
|
||||||
let mut group = GroupConfig::default();
|
let mut group = GroupConfig::default();
|
||||||
|
|
||||||
group.set_admin("piggo@piggo.space", true).unwrap();
|
group.set_admin("piggo@piggo.space", true).unwrap();
|
||||||
assert_eq!(Err(GroupError::UserIsAdmin), group.ban_user("piggo@piggo.space", true), "can't bad admin users");
|
assert_eq!(
|
||||||
group.ban_user("piggo@piggo.space", false).expect("can unbad admin");
|
Err(GroupError::UserIsAdmin),
|
||||||
|
group.ban_user("piggo@piggo.space", true),
|
||||||
|
"can't bad admin users"
|
||||||
|
);
|
||||||
|
group
|
||||||
|
.ban_user("piggo@piggo.space", false)
|
||||||
|
.expect("can unbad admin");
|
||||||
|
|
||||||
group.ban_user("hitler@nazi.camp", true).unwrap();
|
group.ban_user("hitler@nazi.camp", true).unwrap();
|
||||||
assert_eq!(Err(GroupError::UserIsBanned), group.set_admin("hitler@nazi.camp", true), "can't make banned users admins");
|
assert_eq!(
|
||||||
|
Err(GroupError::UserIsBanned),
|
||||||
|
group.set_admin("hitler@nazi.camp", true),
|
||||||
|
"can't make banned users admins"
|
||||||
|
);
|
||||||
|
|
||||||
group.ban_server("freespeechextremist.com", true).unwrap();
|
group.ban_server("freespeechextremist.com", true).unwrap();
|
||||||
assert_eq!(Err(GroupError::UserIsBanned), group.set_admin("nibber@freespeechextremist.com", true), "can't make server-banned users admins");
|
assert_eq!(
|
||||||
|
Err(GroupError::UserIsBanned),
|
||||||
|
group.set_admin("nibber@freespeechextremist.com", true),
|
||||||
|
"can't make server-banned users admins"
|
||||||
|
);
|
||||||
|
|
||||||
assert!(group.is_admin("piggo@piggo.space"));
|
assert!(group.is_admin("piggo@piggo.space"));
|
||||||
assert_eq!(Err(GroupError::AdminsOnServer), group.ban_server("piggo.space", true), "can't bad server with admins");
|
assert_eq!(
|
||||||
|
Err(GroupError::AdminsOnServer),
|
||||||
|
group.ban_server("piggo.space", true),
|
||||||
|
"can't bad server with admins"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use elefren::{FediClient, Registration, Scopes, scopes};
|
use elefren::{scopes, FediClient, Registration, Scopes};
|
||||||
use elefren::entities::event::Event;
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
@ -57,12 +54,16 @@ impl ConfigStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawn a new group
|
/// Spawn a new group
|
||||||
pub async fn auth_new_group(self: &Arc<Self>, opts: NewGroupOptions) -> Result<GroupHandle, GroupError> {
|
pub async fn auth_new_group(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
opts: NewGroupOptions,
|
||||||
|
) -> Result<GroupHandle, GroupError> {
|
||||||
let registration = Registration::new(&opts.server)
|
let registration = Registration::new(&opts.server)
|
||||||
.client_name("group-actor")
|
.client_name("group-actor")
|
||||||
.force_login(true)
|
.force_login(true)
|
||||||
.scopes(make_scopes())
|
.scopes(make_scopes())
|
||||||
.build().await?;
|
.build()
|
||||||
|
.await?;
|
||||||
|
|
||||||
println!("--- Authenticating NEW bot user @{} ---", opts.acct);
|
println!("--- Authenticating NEW bot user @{} ---", opts.acct);
|
||||||
let client = elefren::helpers::cli::authenticate(registration).await?;
|
let client = elefren::helpers::cli::authenticate(registration).await?;
|
||||||
|
@ -83,14 +84,18 @@ impl ConfigStore {
|
||||||
/// Re-auth an existing group
|
/// Re-auth an existing group
|
||||||
pub async fn reauth_group(self: &Arc<Self>, acct: &str) -> Result<GroupHandle, GroupError> {
|
pub async fn reauth_group(self: &Arc<Self>, acct: &str) -> Result<GroupHandle, GroupError> {
|
||||||
let groups = self.data.read().await;
|
let groups = self.data.read().await;
|
||||||
let mut config = groups.get_group_config(acct).ok_or(GroupError::GroupNotExist)?.clone();
|
let mut config = groups
|
||||||
|
.get_group_config(acct)
|
||||||
|
.ok_or(GroupError::GroupNotExist)?
|
||||||
|
.clone();
|
||||||
|
|
||||||
println!("--- Re-authenticating bot user @{} ---", acct);
|
println!("--- Re-authenticating bot user @{} ---", acct);
|
||||||
let registration = Registration::new(config.get_appdata().base.to_string())
|
let registration = Registration::new(config.get_appdata().base.to_string())
|
||||||
.client_name("group-actor")
|
.client_name("group-actor")
|
||||||
.force_login(true)
|
.force_login(true)
|
||||||
.scopes(make_scopes())
|
.scopes(make_scopes())
|
||||||
.build().await?;
|
.build()
|
||||||
|
.await?;
|
||||||
|
|
||||||
let client = elefren::helpers::cli::authenticate(registration).await?;
|
let client = elefren::helpers::cli::authenticate(registration).await?;
|
||||||
let appdata = client.data.clone();
|
let appdata = client.data.clone();
|
||||||
|
@ -111,7 +116,8 @@ impl ConfigStore {
|
||||||
let groups_iter = groups.iter_groups().cloned();
|
let groups_iter = groups.iter_groups().cloned();
|
||||||
|
|
||||||
// Connect in parallel
|
// Connect in parallel
|
||||||
futures::stream::iter(groups_iter).map(|gc| async {
|
futures::stream::iter(groups_iter)
|
||||||
|
.map(|gc| async {
|
||||||
if !gc.is_enabled() {
|
if !gc.is_enabled() {
|
||||||
debug!("Group @{} is DISABLED", gc.get_acct());
|
debug!("Group @{} is DISABLED", gc.get_acct());
|
||||||
return None;
|
return None;
|
||||||
|
@ -123,7 +129,10 @@ impl ConfigStore {
|
||||||
|
|
||||||
match client.verify_credentials().await {
|
match client.verify_credentials().await {
|
||||||
Ok(account) => {
|
Ok(account) => {
|
||||||
info!("Group account verified: @{}, {}", account.acct, account.display_name);
|
info!(
|
||||||
|
"Group account verified: @{}, {}",
|
||||||
|
account.acct, account.display_name
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Group @{} auth error: {}", gc.get_acct(), e);
|
error!("Group @{} auth error: {}", gc.get_acct(), e);
|
||||||
|
@ -136,20 +145,23 @@ impl ConfigStore {
|
||||||
config: gc,
|
config: gc,
|
||||||
store: self.clone(),
|
store: self.clone(),
|
||||||
})
|
})
|
||||||
}).buffer_unordered(8).collect::<Vec<_>>().await
|
})
|
||||||
.into_iter().flatten().collect()
|
.buffer_unordered(8)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.await
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn get_group_config(&self, group: &str) -> Option<GroupConfig> {
|
pub(crate) async fn get_group_config(&self, group: &str) -> Option<GroupConfig> {
|
||||||
let c = self.data.read().await;
|
let c = self.data.read().await;
|
||||||
c.get_group_config(group).map(|inner| {
|
c.get_group_config(group).cloned()
|
||||||
inner.clone()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection RsSelfConvention
|
//noinspection RsSelfConvention
|
||||||
/// Set group config to the store. The store then saved.
|
/// Set group config to the store. The store then saved.
|
||||||
pub(crate) async fn set_group_config<'a>(&'a self, config: GroupConfig) -> Result<(), GroupError> {
|
pub(crate) async fn set_group_config(&self, config: GroupConfig) -> Result<(), GroupError> {
|
||||||
let mut data = self.data.write().await;
|
let mut data = self.data.write().await;
|
||||||
data.set_group_config(config);
|
data.set_group_config(config);
|
||||||
self.persist(&data).await?;
|
self.persist(&data).await?;
|
||||||
|
@ -158,12 +170,15 @@ impl ConfigStore {
|
||||||
|
|
||||||
/// Persist the store
|
/// Persist the store
|
||||||
async fn persist(&self, data: &Config) -> Result<(), GroupError> {
|
async fn persist(&self, data: &Config) -> Result<(), GroupError> {
|
||||||
tokio::fs::write(&self.store_path,
|
tokio::fs::write(
|
||||||
|
&self.store_path,
|
||||||
if self.save_pretty {
|
if self.save_pretty {
|
||||||
serde_json::to_string_pretty(&data)
|
serde_json::to_string_pretty(&data)
|
||||||
} else {
|
} else {
|
||||||
serde_json::to_string(&data)
|
serde_json::to_string(&data)
|
||||||
}?.as_bytes())
|
}?
|
||||||
|
.as_bytes(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
61
src/utils.rs
61
src/utils.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::error::Error;
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
use crate::error::GroupError;
|
use crate::error::GroupError;
|
||||||
|
|
||||||
pub trait LogError {
|
pub trait LogError {
|
||||||
|
@ -21,24 +22,28 @@ pub(crate) fn acct_to_server(acct : &str) -> Option<&str> {
|
||||||
acct.trim_start_matches('@').split('@').nth(1)
|
acct.trim_start_matches('@').split('@').nth(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn normalize_acct<'a, 'g>(acct : &'a str, group: &'g str) -> Result<Cow<'a, str>, GroupError> {
|
pub(crate) fn normalize_acct<'a, 'g>(
|
||||||
|
acct: &'a str,
|
||||||
|
group: &'g str,
|
||||||
|
) -> Result<Cow<'a, str>, GroupError> {
|
||||||
let acct = acct.trim_start_matches('@');
|
let acct = acct.trim_start_matches('@');
|
||||||
if acct_to_server(acct).is_some() {
|
if acct_to_server(acct).is_some() {
|
||||||
|
// already has server
|
||||||
Ok(Cow::Borrowed(acct))
|
Ok(Cow::Borrowed(acct))
|
||||||
} else {
|
} else if let Some(gs) = acct_to_server(group) {
|
||||||
if let Some(gs) = acct_to_server(group) {
|
// attach server from the group actor
|
||||||
Ok(Cow::Owned(format!("{}@{}", acct, gs)))
|
Ok(Cow::Owned(format!("{}@{}", acct, gs)))
|
||||||
} else {
|
} else {
|
||||||
Err(GroupError::BadConfig(format!("Group acct {} is missing server!", group).into()))
|
Err(GroupError::BadConfig(
|
||||||
}
|
format!("Group acct {} is missing server!", group).into(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::utils::{acct_to_server, normalize_acct};
|
|
||||||
use crate::error::GroupError;
|
use crate::error::GroupError;
|
||||||
use std::borrow::Cow;
|
use crate::utils::{acct_to_server, normalize_acct};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_acct_to_server() {
|
fn test_acct_to_server() {
|
||||||
|
@ -49,13 +54,37 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normalize_acct() {
|
fn test_normalize_acct() {
|
||||||
assert_eq!(Ok("piggo@piggo.space".into()), normalize_acct("piggo", "betty@piggo.space"));
|
assert_eq!(
|
||||||
assert_eq!(Ok("piggo@piggo.space".into()), normalize_acct("@piggo", "betty@piggo.space"));
|
Ok("piggo@piggo.space".into()),
|
||||||
assert_eq!(Ok("piggo@piggo.space".into()), normalize_acct("@piggo@piggo.space", "betty@piggo.space"));
|
normalize_acct("piggo", "betty@piggo.space")
|
||||||
assert_eq!(Ok("piggo@piggo.space".into()), normalize_acct("@piggo@piggo.space", "oggip@mastodon.social"));
|
);
|
||||||
assert_eq!(Ok("piggo@piggo.space".into()), normalize_acct("piggo@piggo.space", "oggip@mastodon.social"));
|
assert_eq!(
|
||||||
assert_eq!(Ok("piggo@mastodon.social".into()), normalize_acct("@piggo", "oggip@mastodon.social"));
|
Ok("piggo@piggo.space".into()),
|
||||||
assert_eq!(Ok("piggo@piggo.space".into()), normalize_acct("piggo@piggo.space", "uhh"));
|
normalize_acct("@piggo", "betty@piggo.space")
|
||||||
assert_eq!(Err(GroupError::BadConfig("_".into())), normalize_acct("piggo", "uhh"));
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Ok("piggo@piggo.space".into()),
|
||||||
|
normalize_acct("@piggo@piggo.space", "betty@piggo.space")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Ok("piggo@piggo.space".into()),
|
||||||
|
normalize_acct("@piggo@piggo.space", "oggip@mastodon.social")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Ok("piggo@piggo.space".into()),
|
||||||
|
normalize_acct("piggo@piggo.space", "oggip@mastodon.social")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Ok("piggo@mastodon.social".into()),
|
||||||
|
normalize_acct("@piggo", "oggip@mastodon.social")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Ok("piggo@piggo.space".into()),
|
||||||
|
normalize_acct("piggo@piggo.space", "uhh")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Err(GroupError::BadConfig("_".into())),
|
||||||
|
normalize_acct("piggo", "uhh")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue