| @@ -15,7 +15,7 @@ use matrix_sdk::{ |
| 15 | 15 | }, |
| 16 | 16 | SyncStateEvent, |
| 17 | 17 | }, |
| 18 | | uint, Int, OwnedUserId, UserId, |
| 18 | uint, Int, MilliSecondsSinceUnixEpoch, OwnedUserId, UserId, |
| 19 | 19 | }, |
| 20 | 20 | }; |
| 21 | 21 | |
| @@ -32,6 +32,7 @@ pub struct Members { |
| 32 | 32 | pub(super) runtime: Handle, |
| 33 | 33 | ambiguity_map: Rc<DashMap<OwnedUserId, bool>>, |
| 34 | 34 | nicks: Rc<DashMap<OwnedUserId, String>>, |
| 35 | last_active: Rc<DashMap<OwnedUserId, MilliSecondsSinceUnixEpoch>>, |
| 35 | 36 | buffer: RoomBuffer, |
| 36 | 37 | } |
| 37 | 38 | |
| @@ -55,6 +56,7 @@ impl Members { |
| 55 | 56 | runtime, |
| 56 | 57 | nicks: DashMap::new().into(), |
| 57 | 58 | ambiguity_map: DashMap::new().into(), |
| 59 | last_active: DashMap::new().into(), |
| 58 | 60 | buffer, |
| 59 | 61 | } |
| 60 | 62 | } |
| @@ -244,6 +246,35 @@ impl Members { |
| 244 | 246 | &self.room |
| 245 | 247 | } |
| 246 | 248 | |
| 249 | pub fn mark_active( |
| 250 | &self, |
| 251 | user_id: &UserId, |
| 252 | timestamp: MilliSecondsSinceUnixEpoch, |
| 253 | ) { |
| 254 | if self |
| 255 | .last_active |
| 256 | .get(user_id) |
| 257 | .map(|last_active| last_active.0 >= timestamp.0) |
| 258 | .unwrap_or(false) |
| 259 | { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | self.last_active.insert(user_id.to_owned(), timestamp); |
| 264 | } |
| 265 | |
| 266 | fn should_smart_filter( |
| 267 | &self, |
| 268 | user_id: &UserId, |
| 269 | timestamp: MilliSecondsSinceUnixEpoch, |
| 270 | ) -> bool { |
| 271 | let Some(last_active) = self.last_active.get(user_id) else { |
| 272 | return true; |
| 273 | }; |
| 274 | |
| 275 | timestamp.0.saturating_sub(last_active.0) > uint!(300000) |
| 276 | } |
| 277 | |
| 247 | 278 | pub async fn handle_membership_event( |
| 248 | 279 | &self, |
| 249 | 280 | event: &SyncStateEvent<RoomMemberEventContent>, |
| @@ -311,7 +342,7 @@ impl Members { |
| 311 | 342 | let target = self.get(&target_id).await; |
| 312 | 343 | |
| 313 | 344 | // Display the event message |
| 314 | | let message = match (&sender, &target) { |
| 345 | let line = match (&sender, &target) { |
| 315 | 346 | (Some(sender), Some(target)) => { |
| 316 | 347 | render_membership(event, sender, target) |
| 317 | 348 | } |
| @@ -329,13 +360,23 @@ impl Members { |
| 329 | 360 | target_id); |
| 330 | 361 | } |
| 331 | 362 | |
| 332 | | "ERROR: cannot render event since sender or target are not a room member".into() |
| 363 | crate::render::RenderedLine { |
| 364 | tags: vec!["matrix_membership".to_owned()], |
| 365 | message: "ERROR: cannot render event since sender or target are not a room member".into(), |
| 366 | } |
| 333 | 367 | } |
| 334 | 368 | }; |
| 335 | 369 | |
| 336 | 370 | let timestamp: i64 = |
| 337 | 371 | (event.origin_server_ts.0 / uint!(1000)).into(); |
| 338 | | buffer.print_date_tags(timestamp as isize, &[], &message); |
| 372 | let mut tags = line.tags; |
| 373 | |
| 374 | if self.should_smart_filter(&target_id, event.origin_server_ts) { |
| 375 | tags.push("matrix_smart_filter".to_owned()); |
| 376 | } |
| 377 | |
| 378 | let tags: Vec<&str> = tags.iter().map(|t| t.as_str()).collect(); |
| 379 | buffer.print_date_tags(timestamp as isize, &tags, &line.message); |
| 339 | 380 | } |
| 340 | 381 | } |
| 341 | 382 | } |