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

[CIS-2024, CIS-2000] Fix Channel List pagination gaps + Truncated channels moved to bottom #2420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 🔄 Changed
## StreamChat
### 🐞 Fixed
- Fix Channel List pagination gaps [#2420](https://github.com/GetStream/stream-chat-swift/pull/2420)
- Fix truncated channels being moved to the bottom of the channel list [#2420](https://github.com/GetStream/stream-chat-swift/pull/2420)

# [4.25.0](https://github.com/GetStream/stream-chat-swift/releases/tag/4.25.0)
_December 15, 2022_
Expand Down
9 changes: 8 additions & 1 deletion Sources/StreamChat/Database/DTOs/ChannelDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ class ChannelDTO: NSManagedObject {

// Update the date for sorting every time new message in this channel arrive.
// This will ensure that the channel list is updated/sorted when new message arrives.
let lastDate = lastMessageAt ?? createdAt
// Note: If a channel is truncated, the server will update the lastMessageAt to a minimum value, and not remove it.
// So, if lastMessageAt is nil or is equal to distantPast, we need to fallback to createdAt.
var lastDate = lastMessageAt ?? createdAt
if lastDate.bridgeDate <= .distantPast {
lastDate = createdAt
}
if lastDate != defaultSortingAt {
defaultSortingAt = lastDate
}
Expand Down Expand Up @@ -352,6 +357,8 @@ extension ChannelDTO {
]

request.predicate = NSCompoundPredicate(type: .and, subpredicates: subpredicates)
request.fetchLimit = query.pagination.pageSize
request.fetchBatchSize = query.pagination.pageSize
return request
}

Expand Down
46 changes: 41 additions & 5 deletions Tests/StreamChatTests/Database/DTOs/ChannelDTO_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,42 @@ final class ChannelDTO_Tests: XCTestCase {
}
}

func test_defaultSortingAt_updates_whenLastMessageAtChanges() throws {
func test_defaultSortingAt_shouldBeEqualToLastMessageAt() throws {
let channelId: ChannelId = .unique

try database.createChannel(cid: channelId)

try database.writeSynchronously {
let channel = try XCTUnwrap($0.channel(cid: channelId))
channel.lastMessageAt = .unique(after: channel.lastMessageAt ?? channel.createdAt)
channel.lastMessageAt = .unique
}

let channel = try XCTUnwrap(database.viewContext.channel(cid: channelId))
XCTAssertEqual(channel.lastMessageAt, channel.defaultSortingAt)
XCTAssertEqual(channel.defaultSortingAt, channel.lastMessageAt)
}

func test_defaultSortingAt_whenMissingLastMessageAt_shouldBeEqualToCreatedAt() throws {
let channelId: ChannelId = .unique
try database.createChannel(cid: channelId)
try database.writeSynchronously {
let channel = try XCTUnwrap($0.channel(cid: channelId))
channel.createdAt = .unique
channel.lastMessageAt = nil
}

let channel = try XCTUnwrap(database.viewContext.channel(cid: channelId))
XCTAssertEqual(channel.defaultSortingAt, channel.createdAt)
}

func test_defaultSortingAt_whenLastMessageAtEqualDistantPast_shouldBeEqualToCreatedAt() throws {
let channelId: ChannelId = .unique
try database.createChannel(cid: channelId)
try database.writeSynchronously {
let channel = try XCTUnwrap($0.channel(cid: channelId))
channel.createdAt = .unique
channel.lastMessageAt = .distantPast.bridgeDate
}

let channel = try XCTUnwrap(database.viewContext.channel(cid: channelId))
XCTAssertEqual(channel.defaultSortingAt, channel.createdAt)
}

func test_channelPayload_nilMembershipRemovesExistingMembership() throws {
Expand Down Expand Up @@ -889,6 +913,18 @@ final class ChannelDTO_Tests: XCTestCase {
XCTAssertEqual(loadedChannels.count, 1)
XCTAssertEqual(loadedChannels.first?.cid, channel1Id.rawValue)
}

func test_channelWithChannelListQuery_shouldUseLimitAndBatchSize() {
let query = ChannelListQuery(
filter: .and([.less(.createdAt, than: .unique), .exists(.deletedAt, exists: false)]),
pageSize: 25
)

let fetchRequest = ChannelDTO.channelListFetchRequest(query: query)

XCTAssertEqual(fetchRequest.fetchBatchSize, 25)
XCTAssertEqual(fetchRequest.fetchLimit, 25)
}

func test_channelListQuery_withSorting() {
// Create two channels queries with different sortings.
Expand Down