8000 Fix `FilterKey.id` not returning any channels in `ChannelListQuery` (… · GetStream/stream-chat-swift@1d92e93 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 1d92e93

Browse files
authored
Fix FilterKey.id not returning any channels in ChannelListQuery (#3643)
* Fix `FilterKey.id` not returning any channels in `ChannelListQuery` * Update CHANGELOG.md * Fix returning channels with wrong ID * Make it more efficient * Fix tests that were using `id` instead of `cid` * Fix filter scope test
1 parent c52e1ff commit 1d92e93

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6+
## StreamChat
7+
### 🐞 Fixed
8+
- Fix `FilterKey.id` not returning any channels in `ChannelListQuery` [#3643](https://github.com/GetStream/stream-chat-swift/pull/3643)
9+
610
## StreamChatUI
711
### 🐞 Fixed
812
- Fix message search with empty avatars [#3644](https://github.com/GetStream/stream-chat-swift/pull/3644)

Sources/StreamChat/Database/DTOs/ChannelDTO.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import Foundation
77

88
@objc(ChannelDTO)
99
class ChannelDTO: NSManagedObject {
10+
// The cid without the channel type.
11+
@NSManaged var id: String?
12+
// The channel id which includes channelType:channelId.
1013
@NSManaged var cid: String
1114
@NSManaged var name: String?
1215
@NSManaged var imageURL: URL?
@@ -105,7 +108,7 @@ class ChannelDTO: NSManagedObject {
105108
newestMessageAt = nil
106109
}
107110
}
108-
111+
109112
// Update the date for sorting every time new message in this channel arrive.
110113
// This will ensure that the channel list is updated/sorted when new message arrives.
111114
// Note: If a channel is truncated, the server will update the lastMessageAt to a minimum value, and not remove it.
@@ -240,6 +243,7 @@ extension NSManagedObjectContext {
240243
dto.extraData = Data()
241244
}
242245
dto.typeRawValue = payload.typeRawValue
246+
dto.id = payload.cid.id
243247
dto.config = payload.config.asDTO(context: self, cid: dto.cid)
244248
if let ownCapabilities = payload.ownCapabilities {
245249
dto.ownCapabilities = ownCapabilities

Sources/StreamChat/Database/StreamChatModel.xcdatamodeld/StreamChatModel.xcdatamodel/contents

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23605" systemVersion="24A348" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
2+
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23507" systemVersion="24B83" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
33
<entity name="AttachmentDTO" representedClassName="AttachmentDTO" syncable="YES">
44
<attribute name="data" attributeType="Binary"/>
55
<attribute name="id" attributeType="String"/>
@@ -44,6 +44,7 @@
4444
<attribute name="defaultSortingAt" attributeType="Date" usesScalarValueType="NO" spotlightIndexingEnabled="YES"/>
4545
<attribute name="deletedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
4646
<attribute name="extraData" attributeType="Binary"/>
47+
<attribute name="id" optional="YES" attributeType="String"/>
4748
<attribute name="imageURL" optional="YES" attributeType="URI"/>
4849
<attribute name="isBlocked" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
4950
<attribute name="isDisabled" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>

Sources/StreamChat/Query/ChannelListQuery.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public extension FilterKey where Scope: AnyChannelListFilterScope {
6363
/// Supported operators: `in`, `equal`
6464
/// - Warning: Querying by the channel Identifier should be done using the `cid` field as much as possible to optimize API performance.
6565
/// As the full channel ID, `cid`s are indexed everywhere in Stream database where `id` is not.
66-
static var id: FilterKey<Scope, String> { .init(rawValue: "id", keyPathString: #keyPath(ChannelDTO.cid)) }
66+
static var id: FilterKey<Scope, String> { .init(
67+
rawValue: "id",
68+
keyPathString: #keyPath(ChannelDTO.id)
69+
) }
6770

6871
/// A filter key for matching the `name` value.
6972
static var name: FilterKey<Scope, String> { .init(rawValue: "name", keyPathString: #keyPath(ChannelDTO.name)) }

Tests/StreamChatTests/Controllers/ChannelListController/ChannelListController_Tests.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,14 +1088,14 @@ final class ChannelListController_Tests: XCTestCase {
10881088

10891089
// When all the channel ids are in DB.
10901090
try assertFilterPredicate(
1091-
.in(.id, values: expectedCids.map(\.rawValue)),
1091+
.in(.cid, values: expectedCids),
10921092
channelsInDB: expectedChannels + unexpectedChannels,
10931093
expectedResult: expectedCids
10941094
)
10951095

10961096
// When not all the channel ids are in DB.
10971097
try assertFilterPredicate(
1098-
.in(.id, values: expectedCids.map(\.rawValue)),
1098+
.in(.cid, values: expectedCids),
10991099
channelsInDB: expectedChannels.dropLast() + unexpectedChannels,
11001100
expectedResult: [cid1, cid2]
11011101
)
@@ -1489,7 +1489,7 @@ final class ChannelListController_Tests: XCTestCase {
14891489
)
14901490
}
14911491

1492-
func test_filterPredicate_inWithArrayOfIds_returnsExpectedResults() throws {
1492+
func test_filterPredicate_inWithArrayOfCids_returnsExpectedResults() throws {
14931493
let chatIds: [String] = [
14941494
"suggestions-63986de56549624f314b75cb",
14951495
"suggestions-6ukh3986de56549624f314b75cjkhagfdkjhab",
@@ -1500,7 +1500,7 @@ final class ChannelListController_Tests: XCTestCase {
15001500
let channelIds = chatIds.map { ChannelId(type: .custom("daisy-dashboard"), id: $0) }
15011501

15021502
try assertFilterPredicate(
1503-
.in(.id, values: channelIds.map(\.rawValue)),
1503+
.in(.cid, values: channelIds),
15041504
channelsInDB: [
15051505
.dummy(channel: .dummy(cid: channelIds[0])),
15061506
.dummy(channel: .dummy(cid: channelIds[1])),
@@ -1703,6 +1703,23 @@ final class ChannelListController_Tests: XCTestCase {
17031703
)
17041704
}
17051705

1706+
func test_filterPredicate_id_returnsExpectedResults() throws {
1707+
let cid1 = ChannelId(type: .commerce, id: "123")
1708+
let cid2 = ChannelId(type: .livestream, id: "123")
1709+
1710+
try assertFilterPredicate(
1711+
.equal(.id, to: "123"),
1712+
channelsInDB: [
1713+
.dummy(channel: .dummy(cid: cid1)),
1714+
.dummy(channel: .dummy(cid: .init(type: .commerce, id: "123123"))),
1715+
.dummy(channel: .dummy()),
1716+
.dummy(channel: .dummy()),
1717+
.dummy(channel: .dummy(cid: cid2))
1718+
],
1719+
expectedResult: [cid1, cid2]
1720+
)
1721+
}
1722+
17061723
// MARK: - Private Helpers
17071724

17081725
private func assertFilterPredicate(

Tests/StreamChatTests/Query/ChannelListFilterScope_Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class ChannelListFilterScope_Tests: XCTestCase {
3838

3939
func test_filterKeys_haveExpectedKeyPathValueMapper() {
4040
XCTAssertEqual(Key<ChannelId>.cid.keyPathString, "cid")
41-
XCTAssertEqual(Key<String>.id.keyPathString, "cid")
41+
XCTAssertEqual(Key<String>.id.keyPathString, "id")
4242
XCTAssertEqual(Key<String>.name.keyPathString, "name")
4343
XCTAssertEqual(Key<URL>.imageURL.keyPathString, "imageURL")
4444
XCTAssertEqual(Key<ChannelType>.type.keyPathString, "typeRawValue")

0 commit comments

Comments
 (0)
0