From 8611cc0ee9afb4691f6f55fbe2fd583d4337cca2 Mon Sep 17 00:00:00 2001
From: strawberry <strawberry@puppygock.gay>
Date: Sun, 24 Nov 2024 22:30:14 -0500
Subject: [PATCH] fix ignored_filter check, exclude dummy events over sync

Signed-off-by: strawberry <strawberry@puppygock.gay>
---
 src/api/client/message.rs | 18 +++++++++---------
 src/api/client/sync/v3.rs | 25 +++----------------------
 src/api/client/sync/v4.rs | 25 +++----------------------
 3 files changed, 15 insertions(+), 53 deletions(-)

diff --git a/src/api/client/message.rs b/src/api/client/message.rs
index f1a10aa2..d8043855 100644
--- a/src/api/client/message.rs
+++ b/src/api/client/message.rs
@@ -25,8 +25,8 @@ use crate::Ruma;
 
 pub(crate) type LazySet = HashSet<OwnedUserId>;
 
-/// list of safe and common non-state events to ignore
-const IGNORED_MESSAGE_TYPES: &[TimelineEventType] = &[
+/// list of safe and common non-state events to ignore if the user is ignored
+const IGNORED_MESSAGE_TYPES: &[TimelineEventType; 16] = &[
 	RoomMessage,
 	Sticker,
 	CallInvite,
@@ -206,19 +206,19 @@ pub(crate) async fn update_lazy(
 pub(crate) async fn ignored_filter(services: &Services, item: PdusIterItem, user_id: &UserId) -> Option<PdusIterItem> {
 	let (_, pdu) = &item;
 
+	// exclude Synapse's dummy events from bloating up response bodies. clients
+	// don't need to see this.
 	if pdu.kind.to_cow_str() == "org.matrix.dummy_event" {
 		return None;
 	}
 
-	if !IGNORED_MESSAGE_TYPES.iter().any(is_equal_to!(&pdu.kind)) {
-		return Some(item);
+	if IGNORED_MESSAGE_TYPES.iter().any(is_equal_to!(&pdu.kind))
+		&& services.users.user_is_ignored(&pdu.sender, user_id).await
+	{
+		return None;
 	}
 
-	if !services.users.user_is_ignored(&pdu.sender, user_id).await {
-		return Some(item);
-	}
-
-	None
+	Some(item)
 }
 
 pub(crate) async fn visibility_filter(
diff --git a/src/api/client/sync/v3.rs b/src/api/client/sync/v3.rs
index b69cbc87..0cb22317 100644
--- a/src/api/client/sync/v3.rs
+++ b/src/api/client/sync/v3.rs
@@ -38,6 +38,7 @@ use tracing::{Instrument as _, Span};
 
 use super::{load_timeline, share_encrypted_room};
 use crate::{
+	client::ignored_filter,
 	service::{pdu::EventHash, Services},
 	utils, Error, PduEvent, Result, Ruma, RumaResponse,
 };
@@ -949,28 +950,8 @@ async fn load_joined_room(
 	let room_events: Vec<_> = timeline_pdus
 		.iter()
 		.stream()
-		.filter_map(|(_, pdu)| async move {
-			// list of safe and common non-state events to ignore
-			if matches!(
-				&pdu.kind,
-				RoomMessage
-					| Sticker | CallInvite
-					| CallNotify | RoomEncrypted
-					| Image | File | Audio
-					| Voice | Video | UnstablePollStart
-					| PollStart | KeyVerificationStart
-					| Reaction | Emote
-					| Location
-			) && services
-				.users
-				.user_is_ignored(&pdu.sender, sender_user)
-				.await
-			{
-				return None;
-			}
-
-			Some(pdu.to_sync_room_event())
-		})
+		.filter_map(|item| ignored_filter(services, item.clone(), sender_user))
+		.map(|(_, pdu)| pdu.to_sync_room_event())
 		.collect()
 		.await;
 
diff --git a/src/api/client/sync/v4.rs b/src/api/client/sync/v4.rs
index 0913336d..62c313e2 100644
--- a/src/api/client/sync/v4.rs
+++ b/src/api/client/sync/v4.rs
@@ -35,7 +35,7 @@ use ruma::{
 use service::{rooms::read_receipt::pack_receipts, Services};
 
 use super::{load_timeline, share_encrypted_room};
-use crate::Ruma;
+use crate::{client::ignored_filter, Ruma};
 
 const SINGLE_CONNECTION_SYNC: &str = "single_connection_sync";
 const DEFAULT_BUMP_TYPES: &[TimelineEventType; 6] =
@@ -539,27 +539,8 @@ pub(crate) async fn sync_events_v4_route(
 		let room_events: Vec<_> = timeline_pdus
 			.iter()
 			.stream()
-			.filter_map(|(_, pdu)| async move {
-				// list of safe and common non-state events to ignore
-				if matches!(
-					&pdu.kind,
-					RoomMessage
-						| Sticker | CallInvite
-						| CallNotify | RoomEncrypted
-						| Image | File | Audio
-						| Voice | Video | UnstablePollStart
-						| PollStart | KeyVerificationStart
-						| Reaction | Emote | Location
-				) && services
-					.users
-					.user_is_ignored(&pdu.sender, sender_user)
-					.await
-				{
-					return None;
-				}
-
-				Some(pdu.to_sync_room_event())
-			})
+			.filter_map(|item| ignored_filter(&services, item.clone(), sender_user))
+			.map(|(_, pdu)| pdu.to_sync_room_event())
 			.collect()
 			.await;