pull/14/head
Ondřej Hruška 3 years ago
parent 3b7700a4b1
commit 8afc77dd60
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 1
      .gitignore
  2. 2
      Cargo.lock
  3. 2
      Cargo.toml
  4. 37
      src/command.rs
  5. 975
      src/group_handle.rs
  6. 2
      src/store/data.rs
  7. 21
      src/store/mod.rs

1
.gitignore vendored

@ -4,3 +4,4 @@ group-actor-data.toml
.idea/ .idea/
groups.json groups.json
fedigroups fedigroups
*.bak

2
Cargo.lock generated

@ -327,7 +327,7 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]] [[package]]
name = "fedigroups" name = "fedigroups"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

@ -1,6 +1,6 @@
[package] [package]
name = "fedigroups" name = "fedigroups"
version = "0.1.0" version = "0.2.0"
authors = ["Ondřej Hruška <ondra@ondrovo.com>"] authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
edition = "2018" edition = "2018"
publish = false publish = false

@ -122,11 +122,12 @@ static RE_ANNOUNCE: once_cell::sync::Lazy<Regex> =
Lazy::new(|| Regex::new(concat!(r"(?:^|\s|>|\n)[\\/]announce\s+(.*)$")).unwrap()); Lazy::new(|| Regex::new(concat!(r"(?:^|\s|>|\n)[\\/]announce\s+(.*)$")).unwrap());
static RE_A_HASHTAG: once_cell::sync::Lazy<Regex> = static RE_A_HASHTAG: once_cell::sync::Lazy<Regex> =
Lazy::new(|| Regex::new(concat!(r"\b#(\w+)")).unwrap()); Lazy::new(|| Regex::new(concat!(r"(?:^|\b|\s|>|\n)#(\w+)")).unwrap());
pub fn parse_status_tags(content: &str) -> Vec<String> { pub fn parse_status_tags(content: &str) -> Vec<String> {
debug!("Raw content: {}", content); debug!("Raw content: {}", content);
let content = content.replace("<br/>", " "); let content = content.replace("<br/>", "<br/> ");
let content = content.replace("</p>", "</p> ");
let content = voca_rs::strip::strip_tags(&content); let content = voca_rs::strip::strip_tags(&content);
debug!("Stripped tags: {}", content); debug!("Stripped tags: {}", content);
@ -143,10 +144,8 @@ pub fn parse_status_tags(content: &str) -> Vec<String> {
pub fn parse_slash_commands(content: &str) -> Vec<StatusCommand> { pub fn parse_slash_commands(content: &str) -> Vec<StatusCommand> {
debug!("Raw content: {}", content); debug!("Raw content: {}", content);
let content = content.replace("<br/>", " "); let content = content.replace("<br/>", "<br/> ");
// let content = content.replace("<br />", " "); let content = content.replace("</p>", "</p> ");
// let content = content.replace("<BR/>", " ");
// let content = content.replace("<BR />", " ");
let content = voca_rs::strip::strip_tags(&content); let content = voca_rs::strip::strip_tags(&content);
debug!("Stripped tags: {}", content); debug!("Stripped tags: {}", content);
@ -423,13 +422,14 @@ mod test {
} }
#[test] #[test]
fn test_add_member() { fn test_add_tag() {
assert!(RE_ADD_TAG.is_match("/add #breadposting")); assert!(RE_ADD_TAG.is_match("/add #breadposting"));
assert!(RE_ADD_TAG.is_match("/add #čučkaři")); assert!(RE_ADD_TAG.is_match("/add #čučkaři"));
assert!(RE_ADD_TAG.is_match("/add #χαλβάς")); assert!(RE_ADD_TAG.is_match("/add #χαλβάς"));
assert!(RE_ADD_TAG.is_match("\\add #ласточка")); assert!(RE_ADD_TAG.is_match("\\add #ласточка"));
assert!(RE_ADD_TAG.is_match("/add #nya.")); assert!(RE_ADD_TAG.is_match("/add #nya."));
assert!(RE_ADD_TAG.is_match("/add #nya)")); assert!(RE_ADD_TAG.is_match("/add #nya)"));
assert!(RE_ADD_TAG.is_match("/add #nya and more)"));
let c = RE_ADD_TAG.captures("/add #breadposting"); let c = RE_ADD_TAG.captures("/add #breadposting");
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "breadposting"); assert_eq!(c.unwrap().get(1).unwrap().as_str(), "breadposting");
@ -440,10 +440,10 @@ mod test {
let c = RE_ADD_TAG.captures("/add #ласточка"); let c = RE_ADD_TAG.captures("/add #ласточка");
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "ласточка"); assert_eq!(c.unwrap().get(1).unwrap().as_str(), "ласточка");
let c = RE_ADD_TAG.captures("#nya."); let c = RE_ADD_TAG.captures("/add #nya.");
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "nya"); assert_eq!(c.unwrap().get(1).unwrap().as_str(), "nya");
let c = RE_ADD_TAG.captures("#nya)"); let c = RE_ADD_TAG.captures("/add #nya)");
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "nya"); assert_eq!(c.unwrap().get(1).unwrap().as_str(), "nya");
} }
@ -510,7 +510,7 @@ mod test {
} }
#[test] #[test]
fn test_members() { fn test_tags() {
assert!(!RE_TAGS.is_match("/members")); assert!(!RE_TAGS.is_match("/members"));
assert!(RE_TAGS.is_match("/hashtags")); assert!(RE_TAGS.is_match("/hashtags"));
assert!(RE_TAGS.is_match("dsfsd /tags dfgd d")); assert!(RE_TAGS.is_match("dsfsd /tags dfgd d"));
@ -524,10 +524,17 @@ mod test {
assert!(RE_A_HASHTAG.is_match("#χαλβάς")); assert!(RE_A_HASHTAG.is_match("#χαλβάς"));
assert!(RE_A_HASHTAG.is_match("foo #banana gfdfgd")); assert!(RE_A_HASHTAG.is_match("foo #banana gfdfgd"));
let c = RE_GRANT_ADMIN.captures("foo #banana #χαλβάς #ласточка."); for (i, c) in RE_A_HASHTAG.captures_iter("foo #banana #χαλβάς #ласточка").enumerate() {
assert_eq!(c.unwrap().get(1).unwrap().as_str(), "banana"); if i == 0 {
assert_eq!(c.unwrap().get(2).unwrap().as_str(), "χαλβάς"); assert_eq!(c.get(1).unwrap().as_str(), "banana");
assert_eq!(c.unwrap().get(3).unwrap().as_str(), "ласточка"); }
else if i == 1 {
assert_eq!(c.get(1).unwrap().as_str(), "χαλβάς");
}
else if i == 2 {
assert_eq!(c.get(1).unwrap().as_str(), "ласточка");
}
}
} }
#[test] #[test]
@ -540,7 +547,7 @@ mod test {
} }
#[test] #[test]
fn test_leave() { fn test_join() {
assert!(!RE_JOIN.is_match("/list")); assert!(!RE_JOIN.is_match("/list"));
assert!(RE_JOIN.is_match("/join")); assert!(RE_JOIN.is_match("/join"));
assert!(RE_JOIN.is_match("/join")); assert!(RE_JOIN.is_match("/join"));

File diff suppressed because it is too large Load Diff

@ -7,7 +7,7 @@ use crate::error::GroupError;
/// 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)]
pub(crate) struct Config { pub(crate) struct Config {
groups: HashMap<String, GroupConfig>, pub(crate) groups: HashMap<String, GroupConfig>,
} }
impl Config { impl Config {

@ -9,6 +9,7 @@ use data::{Config, GroupConfig};
use crate::error::GroupError; use crate::error::GroupError;
use crate::group_handle::GroupHandle; use crate::group_handle::GroupHandle;
use std::time::Duration;
pub(crate) mod data; pub(crate) mod data;
@ -82,6 +83,7 @@ impl ConfigStore {
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();
drop(groups);
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())
@ -92,6 +94,8 @@ impl ConfigStore {
.await?; .await?;
let client = elefren::helpers::cli::authenticate(registration).await?; let client = elefren::helpers::cli::authenticate(registration).await?;
println!("Auth complete");
let appdata = client.data.clone(); let appdata = client.data.clone();
config.set_appdata(appdata); config.set_appdata(appdata);
@ -106,8 +110,8 @@ impl ConfigStore {
/// Spawn existing group using saved creds /// Spawn existing group using saved creds
pub async fn spawn_groups(self: Arc<Self>) -> Vec<GroupHandle> { pub async fn spawn_groups(self: Arc<Self>) -> Vec<GroupHandle> {
let groups = self.data.read().await; let groups = self.data.read().await.clone();
let groups_iter = groups.iter_groups().cloned(); let groups_iter = groups.groups.into_values();
// Connect in parallel // Connect in parallel
futures::stream::iter(groups_iter) futures::stream::iter(groups_iter)
@ -158,9 +162,15 @@ impl ConfigStore {
//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(&self, config: GroupConfig) -> Result<(), GroupError> { pub(crate) async fn set_group_config(&self, config: GroupConfig) -> Result<(), GroupError> {
let mut data = self.data.write().await; debug!("Locking mutex");
data.set_group_config(config); if let Ok(mut data) = tokio::time::timeout(Duration::from_secs(1), self.data.write()).await {
self.persist(&data).await?; debug!("Locked");
data.set_group_config(config);
debug!("Writing file");
self.persist(&data).await?;
} else {
error!("DEADLOCK? Timeout waiting for data RW Lock in settings store");
}
Ok(()) Ok(())
} }
@ -187,6 +197,7 @@ fn make_scopes() -> Scopes {
| Scopes::read(scopes::Read::Follows) | Scopes::read(scopes::Read::Follows)
| Scopes::write(scopes::Write::Statuses) | Scopes::write(scopes::Write::Statuses)
| Scopes::write(scopes::Write::Media) | Scopes::write(scopes::Write::Media)
| Scopes::write(scopes::Write::Follows)
} }
// trait TapOk<T> { // trait TapOk<T> {

Loading…
Cancel
Save