forked from MightyPork/group-actor
improvements, more config, add -q, readme
This commit is contained in:
+86
-99
@@ -1,24 +1,24 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use elefren::{FediClient, StatusBuilder};
|
||||
use elefren::debug::EventDisplay;
|
||||
use elefren::debug::NotificationDisplay;
|
||||
use elefren::debug::StatusDisplay;
|
||||
use elefren::entities::account::Account;
|
||||
use elefren::entities::event::Event;
|
||||
use elefren::entities::notification::{Notification, NotificationType};
|
||||
use elefren::entities::status::Status;
|
||||
use elefren::status_builder::Visibility;
|
||||
use elefren::{FediClient, StatusBuilder};
|
||||
use futures::StreamExt;
|
||||
|
||||
use handle_mention::ProcessMention;
|
||||
|
||||
use crate::error::GroupError;
|
||||
use crate::store::ConfigStore;
|
||||
use crate::store::data::{CommonConfig, GroupConfig};
|
||||
use crate::utils::{LogError, normalize_acct, VisExt};
|
||||
use crate::command::StatusCommand;
|
||||
use elefren::entities::account::Account;
|
||||
use crate::error::GroupError;
|
||||
use crate::store::CommonConfig;
|
||||
use crate::store::GroupConfig;
|
||||
use crate::utils::{normalize_acct, LogError, VisExt};
|
||||
|
||||
mod handle_mention;
|
||||
|
||||
@@ -28,18 +28,16 @@ pub struct GroupHandle {
|
||||
pub group_account: Account,
|
||||
pub client: FediClient,
|
||||
pub config: GroupConfig,
|
||||
pub common_config: Arc<CommonConfig>,
|
||||
pub cc: Arc<CommonConfig>,
|
||||
}
|
||||
|
||||
// TODO move other options to common_config!
|
||||
|
||||
// const DELAY_BEFORE_ACTION: Duration = Duration::from_millis(250);
|
||||
const DELAY_REOPEN_STREAM: Duration = Duration::from_millis(500);
|
||||
// higher because we can expect a lot of non-hashtag statuses here
|
||||
const PERIODIC_SAVE: Duration = Duration::from_secs(60);
|
||||
const SOCKET_ALIVE_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
const SOCKET_RETIRE_TIME: Duration = Duration::from_secs(120);
|
||||
const PING_INTERVAL: Duration = Duration::from_secs(15); // must be < periodic save!
|
||||
// // const DELAY_BEFORE_ACTION: Duration = Duration::from_millis(250);
|
||||
// const DELAY_REOPEN_STREAM: Duration = Duration::from_millis(500);
|
||||
// // higher because we can expect a lot of non-hashtag statuses here
|
||||
// const SOCKET_ALIVE_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
// const SOCKET_RETIRE_TIME: Duration = Duration::from_secs(120);
|
||||
|
||||
macro_rules! grp_debug {
|
||||
($self:ident, $f:expr) => {
|
||||
@@ -59,6 +57,7 @@ macro_rules! grp_info {
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! grp_trace {
|
||||
($self:ident, $f:expr) => {
|
||||
::log::trace!(concat!("(@{}) ", $f), $self.config.get_acct());
|
||||
@@ -86,30 +85,21 @@ macro_rules! grp_error {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl GroupHandle {
|
||||
#[allow(unused)]
|
||||
pub async fn save(&mut self) -> Result<(), GroupError> {
|
||||
grp_debug!(self, "Saving group config & status");
|
||||
grp_debug!(self, "Saving group state unconditionally");
|
||||
self.config.save(false).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn save_if_needed(&mut self) -> Result<(), GroupError> {
|
||||
self.config.save_if_needed(false).await?;
|
||||
if self.config.is_dirty() {
|
||||
grp_debug!(self, "Saving group state due to changes");
|
||||
self.config.save_if_needed(false).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*
|
||||
pub async fn reload(&mut self) -> Result<(), GroupError> {
|
||||
if let Some(g) = self.store.get_group_config(self.config.get_acct()).await {
|
||||
self.config = g;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(GroupError::GroupNotExist)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
trait NotifTimestamp {
|
||||
@@ -130,15 +120,6 @@ impl NotifTimestamp for Status {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! grp_trace {
|
||||
($self:ident, $f:expr) => {
|
||||
::log::trace!(concat!("(@{}) ", $f), $self.config.get_acct());
|
||||
};
|
||||
($self:ident, $f:expr, $($arg:tt)+) => {
|
||||
::log::trace!(concat!("(@{}) ", $f), $self.config.get_acct(), $($arg)+);
|
||||
};
|
||||
}
|
||||
|
||||
impl GroupHandle {
|
||||
pub async fn run(&mut self) -> Result<(), GroupError> {
|
||||
loop {
|
||||
@@ -150,18 +131,15 @@ impl GroupHandle {
|
||||
}
|
||||
Err(other) => {
|
||||
grp_error!(self, "ERROR in group handler, will restart! {}", other);
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
tokio::time::sleep(Duration::from_secs_f64(self.cc.delay_reopen_error_s)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_internal(&mut self) -> Result<(), GroupError> {
|
||||
assert!(PERIODIC_SAVE >= PING_INTERVAL);
|
||||
|
||||
loop {
|
||||
grp_debug!(self, "Opening streaming API socket");
|
||||
let mut next_save = Instant::now() + PERIODIC_SAVE; // so we save at start
|
||||
|
||||
// wrapped in a timeout, this seems like the only place the group could hang
|
||||
// (https://git.ondrovo.com/MightyPork/group-actor/issues/8)
|
||||
@@ -175,7 +153,6 @@ impl GroupHandle {
|
||||
|
||||
let socket_open_time = Instant::now();
|
||||
let mut last_rx = Instant::now();
|
||||
// let mut last_ping = Instant::now();
|
||||
|
||||
match self.catch_up_with_missed_notifications().await {
|
||||
Ok(true) => {
|
||||
@@ -201,20 +178,14 @@ impl GroupHandle {
|
||||
}
|
||||
}
|
||||
|
||||
if self.config.is_dirty() {
|
||||
// save asap
|
||||
next_save = Instant::now() - PERIODIC_SAVE
|
||||
}
|
||||
self.save_if_needed().await.log_error("Failed to save");
|
||||
|
||||
'rx: loop {
|
||||
if next_save < Instant::now() {
|
||||
grp_trace!(self, "Save time elapsed, saving if needed");
|
||||
self.save_if_needed().await.log_error("Failed to save group");
|
||||
next_save = Instant::now() + PERIODIC_SAVE;
|
||||
}
|
||||
let remains_to_idle_close =
|
||||
Duration::from_secs_f64(self.cc.socket_alive_timeout_s).saturating_sub(last_rx.elapsed());
|
||||
|
||||
let remains_to_idle_close = SOCKET_ALIVE_TIMEOUT.saturating_sub(last_rx.elapsed());
|
||||
let remains_to_retire = SOCKET_RETIRE_TIME.saturating_sub(socket_open_time.elapsed());
|
||||
let remains_to_retire =
|
||||
Duration::from_secs_f64(self.cc.socket_retire_time_s).saturating_sub(socket_open_time.elapsed());
|
||||
|
||||
if remains_to_idle_close.is_zero() {
|
||||
grp_warn!(self, "Socket idle too long, close");
|
||||
@@ -225,11 +196,7 @@ impl GroupHandle {
|
||||
break 'rx;
|
||||
}
|
||||
|
||||
let timeout = next_save
|
||||
.saturating_duration_since(Instant::now())
|
||||
.min(remains_to_idle_close)
|
||||
.min(remains_to_retire)
|
||||
.max(Duration::from_secs(1)); // at least 1s
|
||||
let timeout = remains_to_idle_close.min(remains_to_retire).max(Duration::from_secs(1)); // at least 1s
|
||||
|
||||
grp_debug!(self, "Wait for message {:?}", timeout);
|
||||
match tokio::time::timeout(timeout, events.next()).await {
|
||||
@@ -247,6 +214,8 @@ impl GroupHandle {
|
||||
Event::FiltersChanged => {}
|
||||
Event::Heartbeat => {}
|
||||
}
|
||||
|
||||
self.save_if_needed().await.log_error("Failed to save");
|
||||
}
|
||||
Ok(None) => {
|
||||
grp_warn!(self, "Group @{} socket closed, restarting...", self.config.get_acct());
|
||||
@@ -259,7 +228,7 @@ impl GroupHandle {
|
||||
}
|
||||
|
||||
grp_warn!(self, "Notif stream closed, will reopen");
|
||||
tokio::time::sleep(DELAY_REOPEN_STREAM).await;
|
||||
tokio::time::sleep(Duration::from_secs_f64(self.cc.delay_reopen_closed_s)).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,8 +293,7 @@ impl GroupHandle {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if self.config.is_optout(&status_user) && !member_or_admin
|
||||
{
|
||||
if self.config.is_optout(&status_user) && !member_or_admin {
|
||||
grp_debug!(self, "Status author @{} opted out, discard", status_user);
|
||||
return Ok(());
|
||||
}
|
||||
@@ -340,24 +308,30 @@ impl GroupHandle {
|
||||
let mentioned_user = normalize_acct(&m.acct, group_user)?;
|
||||
if mentioned_user == group_user {
|
||||
let notif_time = self.config.get_last_notif();
|
||||
if notif_time <= ts {
|
||||
grp_debug!(self, "mentioned but status is older than last notif, can't be a valid notif, discard");
|
||||
return Ok(());
|
||||
} else {
|
||||
if !commands.is_empty() {
|
||||
grp_debug!(self, "Detected commands for this group, handle as notif");
|
||||
|
||||
return self.handle_notification(Notification {
|
||||
if notif_time <= ts {
|
||||
grp_debug!(
|
||||
self,
|
||||
"mentioned but status is older than last notif, can't be a valid notif, discard"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !commands.is_empty() {
|
||||
grp_debug!(self, "Detected commands for this group, handle as notif");
|
||||
|
||||
return self
|
||||
.handle_notification(Notification {
|
||||
id: s.id.clone(), // ???
|
||||
notification_type: NotificationType::Mention,
|
||||
created_at: s.created_at.clone(),
|
||||
created_at: s.created_at,
|
||||
account: s.account.clone(),
|
||||
status: Some(s)
|
||||
}).await;
|
||||
} else if private {
|
||||
grp_debug!(self, "mention in private without commands, discard, this is nothing");
|
||||
return Ok(());
|
||||
}
|
||||
status: Some(s),
|
||||
})
|
||||
.await;
|
||||
} else if private {
|
||||
grp_debug!(self, "mention in private without commands, discard, this is nothing");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -385,8 +359,8 @@ impl GroupHandle {
|
||||
'tags: for t in tags {
|
||||
if self.config.is_tag_followed(&t) {
|
||||
grp_info!(self, "REBLOG #{} STATUS", t);
|
||||
self.client.reblog(&s.id).await
|
||||
.log_error("Failed to reblog");
|
||||
self.client.reblog(&s.id).await.log_error("Failed to reblog");
|
||||
self.delay_after_post().await;
|
||||
break 'tags; // do not reblog multiple times!
|
||||
} else {
|
||||
grp_debug!(self, "#{} is not a group tag", t);
|
||||
@@ -398,6 +372,7 @@ impl GroupHandle {
|
||||
|
||||
async fn follow_user(&mut self, id: &str) -> Result<(), GroupError> {
|
||||
self.client.follow(id).await?;
|
||||
self.delay_after_post().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -413,6 +388,7 @@ impl GroupHandle {
|
||||
// They are retrieved newest first, but we want oldest first for chronological handling
|
||||
|
||||
let mut num = 0;
|
||||
let mut old_pn = 0;
|
||||
while let Some(n) = iter.next_item().await {
|
||||
let ts = n.timestamp_millis();
|
||||
if ts <= last_notif {
|
||||
@@ -422,13 +398,17 @@ impl GroupHandle {
|
||||
grp_debug!(self, "Inspecting notif {}", NotificationDisplay(&n));
|
||||
notifs_to_handle.push(n);
|
||||
num += 1;
|
||||
if num > self.common_config.max_catchup_notifs {
|
||||
if num > self.cc.max_catchup_notifs {
|
||||
grp_warn!(self, "Too many notifs missed to catch up!");
|
||||
break;
|
||||
}
|
||||
|
||||
// sleep so we dont make the api angry
|
||||
tokio::time::sleep(Duration::from_millis(250)).await;
|
||||
let pn = iter.page_num();
|
||||
if pn != old_pn {
|
||||
old_pn = pn;
|
||||
// sleep so we dont make the api angry
|
||||
tokio::time::sleep(Duration::from_secs_f64(self.cc.delay_fetch_page_s)).await;
|
||||
}
|
||||
}
|
||||
|
||||
if notifs_to_handle.is_empty() {
|
||||
@@ -461,6 +441,7 @@ impl GroupHandle {
|
||||
let mut newest_status = None;
|
||||
|
||||
let mut num = 0;
|
||||
let mut old_pn = 0;
|
||||
while let Some(s) = iter.next_item().await {
|
||||
let ts = s.timestamp_millis();
|
||||
if ts <= last_status {
|
||||
@@ -475,13 +456,17 @@ impl GroupHandle {
|
||||
|
||||
statuses_to_handle.push(s);
|
||||
num += 1;
|
||||
if num > self.common_config.max_catchup_statuses {
|
||||
if num > self.cc.max_catchup_statuses {
|
||||
grp_warn!(self, "Too many statuses missed to catch up!");
|
||||
break;
|
||||
}
|
||||
|
||||
// sleep so we dont make the api angry
|
||||
tokio::time::sleep(Duration::from_millis(250)).await;
|
||||
let pn = iter.page_num();
|
||||
if pn != old_pn {
|
||||
old_pn = pn;
|
||||
// sleep so we dont make the api angry
|
||||
tokio::time::sleep(Duration::from_secs_f64(self.cc.delay_fetch_page_s)).await;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ts) = newest_status {
|
||||
@@ -499,8 +484,7 @@ impl GroupHandle {
|
||||
|
||||
for s in statuses_to_handle {
|
||||
grp_debug!(self, "Handling missed status: {}", StatusDisplay(&s));
|
||||
self.handle_status(s).await
|
||||
.log_error("Error handling a status");
|
||||
self.handle_status(s).await.log_error("Error handling a status");
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
@@ -509,8 +493,7 @@ impl GroupHandle {
|
||||
async fn handle_mention_status(&mut self, status: Status) -> Result<(), GroupError> {
|
||||
let res = ProcessMention::run(self, status).await;
|
||||
|
||||
self.save_if_needed().await
|
||||
.log_error("Failed to save");
|
||||
self.save_if_needed().await.log_error("Failed to save");
|
||||
|
||||
res
|
||||
}
|
||||
@@ -523,24 +506,25 @@ impl GroupHandle {
|
||||
let mut admins = self.config.get_admins().cloned().collect::<Vec<_>>();
|
||||
admins.sort();
|
||||
|
||||
format!("\
|
||||
format!(
|
||||
"\
|
||||
@{user} Welcome to the group! This group has posting restricted to members. \
|
||||
If you'd like to join, please ask one of the group admins:\n\
|
||||
{admins}",
|
||||
user = notif_acct,
|
||||
admins = admins.join(", ")
|
||||
user = notif_acct,
|
||||
admins = admins.join(", ")
|
||||
)
|
||||
} else {
|
||||
follow_back = true;
|
||||
|
||||
self.config.set_member(notif_acct, true)
|
||||
.log_error("Fail add a member");
|
||||
self.config.set_member(notif_acct, true).log_error("Fail add a member");
|
||||
|
||||
format!("\
|
||||
format!(
|
||||
"\
|
||||
@{user} Welcome to the group! The group user will now follow you back to complete the sign-up. \
|
||||
To share a post, @ the group user or use a group hashtag.\n\n\
|
||||
Use /help for more info.",
|
||||
user = notif_acct
|
||||
user = notif_acct
|
||||
)
|
||||
};
|
||||
|
||||
@@ -551,12 +535,15 @@ impl GroupHandle {
|
||||
.build()
|
||||
.expect("error build status");
|
||||
|
||||
self.client.new_status(post).await
|
||||
.log_error("Failed to post");
|
||||
self.client.new_status(post).await.log_error("Failed to post");
|
||||
self.delay_after_post().await;
|
||||
|
||||
if follow_back {
|
||||
self.follow_user(notif_user_id).await
|
||||
.log_error("Failed to follow back");
|
||||
self.follow_user(notif_user_id).await.log_error("Failed to follow back");
|
||||
}
|
||||
}
|
||||
|
||||
async fn delay_after_post(&self) {
|
||||
tokio::time::sleep(Duration::from_secs_f64(self.cc.delay_after_post_s)).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user