Skip to content

Commit

Permalink
Adding a filter to not show conversations that contain no valid topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovani Gonzalez committed Nov 25, 2023
1 parent ff07763 commit 595864d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Sources/XMTP/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,27 @@ public actor Conversations {
}

newConversations
.filter { $0.peerAddress != client.address }
.filter { $0.peerAddress != client.address && isValidTopic(topic: $0.topic) }
.forEach { conversationsByTopic[$0.topic] = $0 }

// TODO(perf): use DB to persist + sort
return conversationsByTopic.values.sorted { a, b in
a.createdAt < b.createdAt
}
}

public func isValidTopic(topic: String) -> Bool {
let regex = "^[\\x00-\\x7F]+$"
let initialIndex = String.Index(utf16Offset: 8, in: topic)
let finalIndex = topic.lastIndex(of: "/")
if finalIndex != nil {
let unwrappedTopic = String(topic[initialIndex..<finalIndex!])
let indices = unwrappedTopic.startIndex..<unwrappedTopic.endIndex
let resultMatch = unwrappedTopic.range(of: regex, options: .regularExpression)
return indices == resultMatch
}
return false
}

private func listIntroductionPeers(pagination: Pagination?) async throws -> [String: Date] {
let envelopes = try await client.apiClient.query(
Expand Down
60 changes: 60 additions & 0 deletions Tests/XMTPTests/ConversationsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,64 @@ class ConversationsTests: XCTestCase {

await waitForExpectations(timeout: 3)
}

func testCanValidateTopicsInsideConversation() async throws {
let validId = "sdfsadf095b97a9284dcd82b2274856ccac8a21de57bebe34e7f9eeb855fb21126d3b8f"
let fixtures = await fixtures()

// Creation of all known types of topics
let privateStore = Topic.userPrivateStoreKeyBundle(validId).description
let contact = Topic.contact(validId).description
let userIntro = Topic.userIntro(validId).description
let userInvite = Topic.userInvite(validId).description
let directMessageV1 = Topic.directMessageV1(validId, "sd").description
let directMessageV2 = Topic.directMessageV2(validId).description
let preferenceList = Topic.preferenceList(validId).description
let conversations = fixtures.bobClient.conversations

// check if validation of topics accepts all types
Task(priority: .userInitiated) {
let resultPrivateStore = await conversations.isValidTopic(topic: privateStore)
let resultContact = await conversations.isValidTopic(topic: contact)
let resultUserIntro = await conversations.isValidTopic(topic: userIntro)
let resultUserInvite = await conversations.isValidTopic(topic: userInvite)
let resultDirectMessageV1 = await conversations.isValidTopic(topic: directMessageV1)
let resultDirectMessageV2 = await conversations.isValidTopic(topic: directMessageV2)
let resultPreferenceList = await conversations.isValidTopic(topic: preferenceList)
XCTAssertTrue(
resultPrivateStore && resultContact && resultUserIntro && resultUserInvite &&
resultDirectMessageV1 && resultDirectMessageV2 && resultPreferenceList
)
}
}

func testCannotValidateTopicsInsideConversation() async throws {
let invalidId = "��\\u0005�!\\u000b���5\\u00001\\u0007�蛨\\u001f\\u00172��.����K9K`�"
let fixtures = await fixtures()

// Creation of all known types of topics
let privateStore = Topic.userPrivateStoreKeyBundle(invalidId).description
let contact = Topic.contact(invalidId).description
let userIntro = Topic.userIntro(invalidId).description
let userInvite = Topic.userInvite(invalidId).description
let directMessageV1 = Topic.directMessageV1(invalidId, "sd").description
let directMessageV2 = Topic.directMessageV2(invalidId).description
let preferenceList = Topic.preferenceList(invalidId).description
let conversations = fixtures.bobClient.conversations

// check if validation of topics declines all types
Task(priority: .userInitiated) {
let resultPrivateStore = await conversations.isValidTopic(topic: privateStore)
let resultContact = await conversations.isValidTopic(topic: contact)
let resultUserIntro = await conversations.isValidTopic(topic: userIntro)
let resultUserInvite = await conversations.isValidTopic(topic: userInvite)
let resultDirectMessageV1 = await conversations.isValidTopic(topic: directMessageV1)
let resultDirectMessageV2 = await conversations.isValidTopic(topic: directMessageV2)
let resultPreferenceList = await conversations.isValidTopic(topic: preferenceList)
XCTAssertFalse(
resultPrivateStore && resultContact && resultUserIntro && resultUserInvite &&
resultDirectMessageV1 && resultDirectMessageV2 && resultPreferenceList
)
}
}
}

0 comments on commit 595864d

Please sign in to comment.