use std::collections::HashSet; 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::event::Event; use elefren::entities::notification::{Notification, NotificationType}; use elefren::entities::status::Status; use elefren::status_builder::Visibility; use futures::StreamExt; use crate::command::StatusCommand; use crate::error::GroupError; use crate::store::ConfigStore; use crate::store::data::GroupConfig; use crate::utils::{LogError, normalize_acct}; /// This is one group's config store capable of persistence #[derive(Debug)] pub struct GroupHandle { pub(crate) client: FediClient, pub(crate) config: GroupConfig, pub(crate) store: Arc, } const DELAY_BEFORE_ACTION: Duration = Duration::from_millis(250); const DELAY_REOPEN_STREAM: Duration = Duration::from_millis(1000); const MAX_CATCHUP_NOTIFS: usize = 25; // also statuses const MAX_CATCHUP_STATUSES: usize = 100; // higher because we can expect a lot of non-hashtag statuses here const PERIODIC_SAVE: Duration = Duration::from_secs(60); const PING_INTERVAL: Duration = Duration::from_secs(15); // must be < periodic save! impl GroupHandle { pub async fn save(&mut self) -> Result<(), GroupError> { debug!("Saving group config & status"); self.store.set_group_config(self.config.clone()).await?; self.config.clear_dirty_status(); Ok(()) } pub async fn save_if_needed(&mut self) -> Result<(), GroupError> { if self.config.is_dirty() { self.save().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 { fn timestamp_millis(&self) -> u64; } impl NotifTimestamp for Notification { fn timestamp_millis(&self) -> u64 { self.created_at.timestamp_millis().max(0) as u64 } } impl NotifTimestamp for Status { fn timestamp_millis(&self) -> u64 { // this may not work well for unseen status tracking, // if ancient statuses were to appear in the timeline :( self.created_at.timestamp_millis().max(0) as u64 } } impl GroupHandle { pub async fn run(&mut self) -> Result<(), GroupError> { assert!(PERIODIC_SAVE >= PING_INTERVAL); let mut next_save = Instant::now() + PERIODIC_SAVE; // so we save at start loop { debug!("Opening streaming API socket"); let mut events = self.client.streaming_user().await?; match self.catch_up_with_missed_notifications().await { Ok(true) => { debug!("Some missed notifs handled"); // Save asap! next_save = Instant::now() - PERIODIC_SAVE } Ok(false) => { debug!("No notifs missed"); } Err(e) => { error!("Failed to handle missed notifs: {}", e); } } match self.catch_up_with_missed_statuses().await { Ok(true) => { debug!("Some missed statuses handled"); // Save asap! next_save = Instant::now() - PERIODIC_SAVE } Ok(false) => { debug!("No statuses missed"); } Err(e) => { error!("Failed to handle missed statuses: {}", e); } } loop { if next_save < Instant::now() { self.save_if_needed().await.log_error("Failed to save group"); next_save = Instant::now() + PERIODIC_SAVE; } let timeout = next_save .saturating_duration_since(Instant::now()) .min(PING_INTERVAL) .max(Duration::from_secs(1)); match tokio::time::timeout(timeout, events.next()).await { Ok(Some(event)) => { debug!("(@{}) Event: {}", self.config.get_acct(), EventDisplay(&event)); match event { Event::Update(status) => { self.handle_status(status).await.log_error("Error handling a status"); } Event::Notification(n) => { self.handle_notification(n).await.log_error("Error handling a notification"); } Event::Delete(_id) => {} Event::FiltersChanged => {} } } Ok(None) => { warn!("Group @{} socket closed, restarting...", self.config.get_acct()); break; } Err(_) => { // Timeout so we can save if needed } } trace!("Pinging"); events.send_ping().await.log_error("Fail to send ping"); } warn!("Notif stream closed, will reopen"); tokio::time::sleep(DELAY_REOPEN_STREAM).await; } } async fn handle_notification(&mut self, n: Notification) -> Result<(), GroupError> { debug!("Handling notif #{}", n.id); let ts = n.timestamp_millis(); self.config.set_last_notif(ts); let group_acct = self.config.get_acct().to_string(); let notif_acct = normalize_acct(&n.account.acct, &group_acct)?; let can_write = self.config.can_write(¬if_acct); let is_admin = self.config.is_admin(¬if_acct); if self.config.is_banned(¬if_acct) { warn!("Notification actor {} is banned!", notif_acct); return Ok(()); } match n.notification_type { NotificationType::Mention => { if let Some(status) = n.status { let status_acct = normalize_acct(&status.account.acct, &group_acct)?; if self.config.is_banned(&status_acct) { warn!("Status author {} is banned!", status_acct); return Ok(()); } let commands = crate::command::parse_slash_commands(&status.content); let mut replies = vec![]; let mut announcements = vec![]; let mut do_boost_prev_post = false; let mut any_admin_cmd = false; if commands.is_empty() { debug!("No commands in post"); if status.in_reply_to_id.is_none() { if can_write { // Someone tagged the group in OP, boost it. info!("Boosting OP mention"); tokio::time::sleep(DELAY_BEFORE_ACTION).await; self.client.reblog(&status.id).await.log_error("Failed to boost"); } else { replies.push("You are not allowed to post to this group".to_string()); } } else { debug!("Not OP, ignore mention"); } } else { for cmd in commands { match cmd { // ignore is first on purpose StatusCommand::Ignore => { debug!("Notif ignored because of ignore command"); return Ok(()); } StatusCommand::Announce(a) => { info!("Sending PSA"); announcements.push(a); } StatusCommand::Boost => { if can_write { do_boost_prev_post = status.in_reply_to_id.is_some(); } else { replies.push("You are not allowed to share to this group".to_string()); } } StatusCommand::BanUser(u) => { let u = normalize_acct(&u, &group_acct)?; if is_admin { if !self.config.is_banned(&u) { match self.config.ban_user(&u, true) { Ok(_) => { any_admin_cmd = true; replies.push(format!("User {} banned from group!", u)); self.unfollow_user(&u).await .log_error("Failed to unfollow"); // no announcement here } Err(e) => { replies.push(format!("Failed to ban user {}: {}", u, e)); } } } } else { replies.push("Only admins can manage user bans".to_string()); } } StatusCommand::UnbanUser(u) => { let u = normalize_acct(&u, &group_acct)?; if is_admin { if self.config.is_banned(&u) { match self.config.ban_user(&u, false) { Ok(_) => { any_admin_cmd = true; replies.push(format!("User {} un-banned!", u)); // no announcement here } Err(_) => { unreachable!() } } } } else { replies.push("Only admins can manage user bans".to_string()); } } StatusCommand::BanServer(s) => { if is_admin { if !self.config.is_server_banned(&s) { match self.config.ban_server(&s, true) { Ok(_) => { any_admin_cmd = true; announcements.push(format!("Server \"{}\" has been banned.", s)); replies.push(format!("Server {} banned from group!", s)); } Err(e) => { replies.push(format!("Failed to ban server {}: {}", s, e)); } } } } else { replies.push("Only admins can manage server bans".to_string()); } } StatusCommand::UnbanServer(s) => { if is_admin { if self.config.is_server_banned(&s) { match self.config.ban_server(&s, false) { Ok(_) => { any_admin_cmd = true; announcements.push(format!("Server \"{}\" has been un-banned.", s)); replies.push(format!("Server {} un-banned!", s)); } Err(_) => { unreachable!() } } } } else { replies.push("Only admins can manage server bans".to_string()); } } StatusCommand::AddMember(u) => { let u = normalize_acct(&u, &group_acct)?; if is_admin { if !self.config.is_member(&u) { match self.config.set_member(&u, true) { Ok(_) => { any_admin_cmd = true; replies.push(format!("User {} added to the group!", u)); self.follow_user(&u).await.log_error("Failed to follow"); } Err(e) => { replies.push(format!("Failed to add user {} to group: {}", u, e)); } } } } else { replies.push("Only admins can manage members".to_string()); } } StatusCommand::RemoveMember(u) => { let u = normalize_acct(&u, &group_acct)?; if is_admin { if self.config.is_member(&u) { match self.config.set_member(&u, false) { Ok(_) => { any_admin_cmd = true; replies.push(format!("User {} removed from the group.", u)); self.unfollow_user(&u).await .log_error("Failed to unfollow"); } Err(_) => { unreachable!() } } } } else { replies.push("Only admins can manage members".to_string()); } } StatusCommand::AddTag(tag) => { if is_admin { any_admin_cmd = true; self.config.add_tag(&tag); replies.push(format!("Tag #{} added to the group!", tag)); } else { replies.push("Only admins can manage group tags".to_string()); } } StatusCommand::RemoveTag(tag) => { if is_admin { any_admin_cmd = true; self.config.remove_tag(&tag); replies.push(format!("Tag #{} removed from the group!", tag)); } else { replies.push("Only admins can manage group tags".to_string()); } } StatusCommand::GrantAdmin(u) => { let u = normalize_acct(&u, &group_acct)?; if is_admin { if !self.config.is_admin(&u) { match self.config.set_admin(&u, true) { Ok(_) => { // try to make the config a little more sane, admins should be members let _ = self.config.set_member(&u, true); any_admin_cmd = true; replies.push(format!("User {} is now a group admin!", u)); announcements .push(format!("User @{} can now manage this group!", u)); } Err(e) => { replies.push(format!( "Failed to make user {} a group admin: {}", u, e )); } } } } else { replies.push("Only admins can manage admins".to_string()); } } StatusCommand::RemoveAdmin(u) => { let u = normalize_acct(&u, &group_acct)?; if is_admin { if self.config.is_admin(&u) { match self.config.set_admin(&u, false) { Ok(_) => { any_admin_cmd = true; replies.push(format!("User {} is no longer a group admin!", u)); announcements .push(format!("User @{} no longer manages this group.", u)); } Err(e) => { replies .push(format!("Failed to revoke {}'s group admin: {}", u, e)); } } } } else { replies.push("Only admins can manage admins".to_string()); } } StatusCommand::OpenGroup => { if is_admin { if self.config.is_member_only() { any_admin_cmd = true; self.config.set_member_only(false); replies.push("Group changed to open-access".to_string()); announcements.push("This group is now open-access!".to_string()); } } else { replies.push("Only admins can set group options".to_string()); } } StatusCommand::CloseGroup => { if is_admin { if !self.config.is_member_only() { any_admin_cmd = true; self.config.set_member_only(true); replies.push("Group changed to member-only".to_string()); announcements.push("This group is now member-only!".to_string()); } } else { replies.push("Only admins can set group options".to_string()); } } StatusCommand::Help => { if self.config.is_member_only() { let mut s = "This is a member-only group. ".to_string(); if self.config.can_write(¬if_acct) { if is_admin { s.push_str("*You are an admin.*"); } else { 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); } else { let mut s = "This is a public-access group. ".to_string(); if is_admin { s.push_str("*You are an admin.*"); } replies.push(s); } replies.push( "\nTo share an original post, mention the group user.\n\ Replies and mentions with commands won't be shared.\n\ \n\ **Supported commands:**\n\ `/boost, /b` - boost the replied-to post into the group\n\ `/ignore, /i` - make the group completely ignore the post\n\ `/ping` - check that the service is alive\n\ `/join` - join the group\n\ `/leave` - leave the group".to_string(), ); if self.config.is_member_only() { replies.push("`/members, /who` - show group members / admins".to_string()); } else { replies.push("`/members, /who` - show group admins".to_string()); } if is_admin { replies.push( "\n\ **Admin commands:**\n\ `/add user` - add a member (use e-mail style address)\n\ `/kick, /remove user` - kick a member\n\ `/ban x` - ban a user or a server\n\ `/unban x` - lift a ban\n\ `/op, /admin user` - grant admin rights\n\ `/deop, /deadmin user` - revoke admin rights\n\ `/opengroup` - make member-only\n\ `/closegroup` - make public-access\n\ `/announce x` - make a public announcement from the rest of the status" .to_string(), ); } } StatusCommand::ListMembers => { let mut show_admins = false; if is_admin { if self.config.is_member_only() { replies.push("Group members:".to_string()); self.list_members(&mut replies); } else { show_admins = true; } } else { show_admins = true; } if show_admins { replies.push("Group admins:".to_string()); self.list_admins(&mut replies); } } StatusCommand::ListTags => { replies.push("Group tags:".to_string()); let mut tags = self.config.get_tags().collect::>(); tags.sort(); for t in tags { replies.push(format!("#{}", t)); } } StatusCommand::Leave => { if self.config.is_member_or_admin(¬if_acct) { // admin can leave but that's a bad idea any_admin_cmd = true; let _ = self.config.set_member(¬if_acct, false); replies.push("You're no longer a group member. Unfollow the group user to stop receiving group messages.".to_string()); self.unfollow_user(¬if_acct).await .log_error("Failed to unfollow"); } } StatusCommand::Join => { if self.config.is_member_or_admin(¬if_acct) { // Already a member, so let's try to follow the user // again, maybe first time it failed self.follow_user(¬if_acct).await .log_error("Failed to follow"); } else { // Not a member yet if self.config.is_member_only() { // No you can't replies.push(format!( "Hi, this group is closed to new sign-ups.\n\ Please ask one of the group admins to add you:")); self.list_admins(&mut replies); } else { // Open access self.follow_user(¬if_acct).await .log_error("Failed to follow"); // This only fails if the user is banned, but that is filtered above let _ = self.config.set_member(¬if_acct, true); replies.push(format!("\ Thanks for joining, you are now a member and the group user will \ follow you so you can use group hashtags. Make sure you follow the \ group user to receive group messages.")); } } } StatusCommand::Ping => { replies.push("Pong".to_string()); } } } tokio::time::sleep(DELAY_BEFORE_ACTION).await; } if do_boost_prev_post { self.client .reblog(&status.in_reply_to_id.as_ref().unwrap()) .await .log_error("Failed to boost"); } if !replies.is_empty() { let r = replies.join("\n"); let post = StatusBuilder::new() .status(format!("@{user}\n{msg}", user = notif_acct, msg = r)) .content_type("text/markdown") .visibility(Visibility::Direct) .build() .expect("error build status"); let _ = self.client.new_status(post).await.log_error("Failed to post"); } if !announcements.is_empty() { let msg = announcements.join("\n"); let post = StatusBuilder::new() .status(format!("**📢 Group announcement**\n{msg}", msg = msg)) .content_type("text/markdown") .visibility(Visibility::Public) .build() .expect("error build status"); let _ = self.client.new_status(post).await.log_error("Failed to post"); } if any_admin_cmd { self.save_if_needed().await.log_error("Failed to save"); } } } NotificationType::Follow => { info!("New follower!"); if self.config.is_member_or_admin(¬if_acct) { // Already joined, just doing something silly, ignore this debug!("User already a member, ignoring"); } else { let text = if self.config.is_member_only() { // Admins are listed without @, so they won't become handles here. // Tagging all admins would be annoying. let mut admins = self.config.get_admins().cloned().collect::>(); admins.sort(); format!( "@{user} Hi, this is a member-only group, you won't be \ able to post. You can still receive group posts though. If you'd like to join, \ please ask one of the group admins to add you:\n\n\ {admins}", user = notif_acct, admins = admins.join(", ") ) } else { self.follow_user(¬if_acct).await .log_error("Failed to follow"); make_welcome_text(¬if_acct) }; let post = StatusBuilder::new() .status(text) .content_type("text/markdown") .visibility(Visibility::Direct) .build() .expect("error build status"); tokio::time::sleep(Duration::from_millis(500)).await; let _ = self.client.new_status(post).await.log_error("Failed to post"); } } NotificationType::Favourite => {} NotificationType::Reblog => {} } Ok(()) } /// Handle a non-mention status async fn handle_status(&mut self, s: Status) -> Result<(), GroupError> { debug!("Handling status #{}", s.id); let ts = s.timestamp_millis(); self.config.set_last_status(ts); if !s.content.contains('#') { debug!("No tags in status"); return Ok(()); } if s.visibility.is_private() { debug!("Status is direct/private, not boosting"); return Ok(()); } if s.content.contains("/add ") || s.content.contains("/remove ") { debug!("Discard, looks like a hashtag manipulation command"); return Ok(()); } let gu = self.config.get_acct(); let su = normalize_acct(&s.account.acct, gu)?; if self.config.is_banned(&su) { debug!("Status author @{} is banned.", su); return Ok(()); } if !self.config.is_member_or_admin(&su) { debug!("Status author @{} is not a member.", su); return Ok(()); } let tags = crate::command::parse_status_tags(&s.content); debug!("Tags in status: {:?}", tags); for t in tags { if self.config.is_tag_followed(&t) { self.client.reblog(&s.id).await .log_error("Failed to reblog"); break; } } Ok(()) } async fn follow_user(&mut self, acct: &str) -> Result<(), GroupError> { self.client.follow(acct).await?; Ok(()) } async fn unfollow_user(&mut self, acct: &str) -> Result<(), GroupError> { self.client.unfollow(acct).await?; Ok(()) } /// Catch up with missed notifications, returns true if any were handled async fn catch_up_with_missed_notifications(&mut self) -> Result { let last_notif = self.config.get_last_notif(); let notifications = self.client.notifications().await?; let mut iter = notifications.items_iter(); let mut notifs_to_handle = vec![]; // They are retrieved newest first, but we want oldest first for chronological handling let mut num = 0; while let Some(n) = iter.next_item().await { let ts = n.timestamp_millis(); if ts <= last_notif { break; // reached our last seen notif } notifs_to_handle.push(n); num += 1; if num > MAX_CATCHUP_NOTIFS { warn!("Too many notifs missed to catch up!"); break; } } if notifs_to_handle.is_empty() { return Ok(false); } notifs_to_handle.reverse(); debug!("{} notifications to catch up!", notifs_to_handle.len()); for n in notifs_to_handle { debug!("Handling missed notification: {}", NotificationDisplay(&n)); self.handle_notification(n).await.log_error("Error handling a notification"); } Ok(true) } /// Catch up with missed statuses, returns true if any were handled async fn catch_up_with_missed_statuses(&mut self) -> Result { let last_status = self.config.get_last_status(); let notifications = self.client.get_home_timeline().await?; let mut iter = notifications.items_iter(); let mut statuses_to_handle = vec![]; // They are retrieved newest first, but we want oldest first for chronological handling let mut num = 0; while let Some(s) = iter.next_item().await { let ts = s.timestamp_millis(); if ts <= last_status { break; // reached our last seen status (hopefully there arent any retro-bumped) } if s.content.contains('#') && !s.visibility.is_private() { statuses_to_handle.push(s); } num += 1; if num > MAX_CATCHUP_STATUSES { warn!("Too many statuses missed to catch up!"); break; } } if statuses_to_handle.is_empty() { return Ok(false); } statuses_to_handle.reverse(); debug!("{} statuses to catch up!", statuses_to_handle.len()); for s in statuses_to_handle { debug!("Handling missed status: {}", StatusDisplay(&s)); self.handle_status(s).await .log_error("Error handling a status"); } Ok(true) } fn list_admins(&self, replies: &mut Vec) { let mut admins = self.config.get_admins().collect::>(); admins.sort(); for a in admins { replies.push(a.to_string()); } } fn list_members(&self, replies: &mut Vec) { let admins = self.config.get_admins().collect::>(); let mut members = self.config.get_members().collect::>(); members.extend(admins.iter()); members.sort(); members.dedup(); for m in members { if admins.contains(&m) { replies.push(format!("{} [admin]", m)); } else { replies.push(m.to_string()); } } } } fn make_welcome_text(user: &str) -> String { format!( "@{user} Welcome to the group! To share a post, tag the group user \ or use one of the group hashtags. Use /help for more info.", user = user ) } trait VisExt: Copy { /// Check if is private or direct fn is_private(self) -> bool; } impl VisExt for Visibility { fn is_private(self) -> bool { self == Visibility::Direct || self == Visibility::Private } }