Skip to content

Commit

Permalink
Fix channel.pinnedMessages with missing messages (#3244)
Browse files Browse the repository at this point in the history
* Fix `channel.pinnedMessages` with missing messages

* Update CHANGELOG.md

* Update CHANGELOG.md
  • Loading branch information
nuno-vieira authored Jun 13, 2024
1 parent 635ea84 commit 846f754
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## StreamChat
### 🐞 Fixed
- Fix `channel.pinnedMessages` with missing messages [#3244](https://github.com/GetStream/stream-chat-swift/pull/3244)
- Fix notifications muted state for the current user in channel members [#3236](https://github.com/GetStream/stream-chat-swift/pull/3236)

# [4.57.0](https://github.com/GetStream/stream-chat-swift/releases/tag/4.57.0)
Expand Down
2 changes: 1 addition & 1 deletion Sources/StreamChat/Database/DTOs/MessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ extension NSManagedObjectContext: MessageDatabaseSession {

if dto.pinned && !channelDTO.pinnedMessages.contains(dto) {
channelDTO.pinnedMessages.insert(dto)
} else {
} else if !dto.pinned {
channelDTO.pinnedMessages.remove(dto)
}

Expand Down
32 changes: 32 additions & 0 deletions Tests/StreamChatTests/Database/DTOs/MessageDTO_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,38 @@ final class MessageDTO_Tests: XCTestCase {
)
}

func test_messagePayload_isPinned_whenAlreadyAddedToPinnedMessages() throws {
let channelId: ChannelId = .unique
let messageId: MessageId = .unique
let channelPayload: ChannelPayload = dummyPayload(with: channelId, pinnedMessages: [.dummy(messageId: messageId)])
let payload: MessagePayload = .dummy(
messageId: messageId,
authorUserId: .unique,
createdAt: "2018-12-12T15:33:46.488935Z".toDate(),
pinned: true
)

let (channelDTO, messageDTO): (ChannelDTO, MessageDTO) = try waitFor { completion in
var channelDTO: ChannelDTO!
var messageDTO: MessageDTO!

database.write { session in
// Create the channel first
channelDTO = try! session.saveChannel(payload: channelPayload, query: nil, cache: nil)
// Save the message twice
messageDTO = try! session.saveMessage(payload: payload, for: channelId, syncOwnReactions: true, cache: nil)
messageDTO = try! session.saveMessage(payload: payload, for: channelId, syncOwnReactions: true, cache: nil)
} completion: { _ in
completion((channelDTO, messageDTO))
}
}

let channel = try XCTUnwrap(channelDTO.inContext(database.viewContext))
let message = try XCTUnwrap(messageDTO.inContext(database.viewContext))
XCTAssertTrue(channel.pinnedMessages.contains(message))
XCTAssertEqual(channel.pinnedMessages.count, 1)
}

func test_messagePayload_isNotPinned_removedFromPinnedMessages() throws {
let channelId: ChannelId = .unique
let channelPayload: ChannelPayload = dummyPayload(with: channelId)
Expand Down

0 comments on commit 846f754

Please sign in to comment.