Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing conversations that contain a no valid topic #194

Merged
merged 8 commits into from
Nov 30, 2023
2 changes: 1 addition & 1 deletion Sources/XMTP/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public actor Conversations {
}

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

// TODO(perf): use DB to persist + sort
Expand Down
4 changes: 4 additions & 0 deletions Sources/XMTP/Messages/Topic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public enum Topic {
private func wrap(_ value: String) -> String {
"/xmtp/0/\(value)/proto"
}

static func isValidTopic(topic: String) -> Bool {
return topic.allSatisfy(\.isASCII)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also commented on the Android PR on this. do we want to allow characters 0-32 in topics?

}
}
44 changes: 44 additions & 0 deletions Tests/XMTPTests/ConversationsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,48 @@ class ConversationsTests: XCTestCase {

await waitForExpectations(timeout: 3)
}

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

// 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

// check if validation of topics accepts all types
XCTAssertTrue(Topic.isValidTopic(topic: privateStore))
XCTAssertTrue(Topic.isValidTopic(topic: contact))
XCTAssertTrue(Topic.isValidTopic(topic: userIntro))
XCTAssertTrue(Topic.isValidTopic(topic: userInvite))
XCTAssertTrue(Topic.isValidTopic(topic: directMessageV1))
XCTAssertTrue(Topic.isValidTopic(topic: directMessageV2))
XCTAssertTrue(Topic.isValidTopic(topic: preferenceList))
}

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

// 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

// check if validation of topics declines all types
XCTAssertFalse(Topic.isValidTopic(topic: privateStore))
XCTAssertFalse(Topic.isValidTopic(topic: contact))
XCTAssertFalse(Topic.isValidTopic(topic: userIntro))
XCTAssertFalse(Topic.isValidTopic(topic: userInvite))
XCTAssertFalse(Topic.isValidTopic(topic: directMessageV1))
XCTAssertFalse(Topic.isValidTopic(topic: directMessageV2))
XCTAssertFalse(Topic.isValidTopic(topic: preferenceList))
}
}
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.6.12-alpha0"
spec.version = "0.6.13-alpha0"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down