Skip to content

Commit

Permalink
Incorrect notifications muted state from channel membership data over…
Browse files Browse the repository at this point in the history
…rode the current user's channel member state (#3239)
  • Loading branch information
laevandus authored Jun 13, 2024
1 parent 4bdfd8e commit 635ea84
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 🔄 Changed
## StreamChat
### 🐞 Fixed
- 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)
_June 06, 2024_
Expand Down
13 changes: 7 additions & 6 deletions Sources/StreamChat/Database/DTOs/ChannelDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,19 @@ extension NSManagedObjectContext {
_ = try saveMessage(payload: $0, channelDTO: dto, syncOwnReactions: true, cache: cache)
}

// Sometimes, `members` are not part of `ChannelDetailPayload` so they need to be saved here too.
try payload.members.forEach {
let member = try saveMember(payload: $0, channelId: payload.channel.cid, query: nil, cache: cache)
dto.members.insert(member)
}

// Note: membership payload should be saved before all the members
if let membership = payload.membership {
let membership = try saveMember(payload: membership, channelId: payload.channel.cid, query: nil, cache: cache)
dto.membership = membership
} else {
dto.membership = nil
}

// Sometimes, `members` are not part of `ChannelDetailPayload` so they need to be saved here too.
try payload.members.forEach {
let member = try saveMember(payload: $0, channelId: payload.channel.cid, query: nil, cache: cache)
dto.members.insert(member)
}

dto.watcherCount = Int64(clamping: payload.watcherCount ?? 0)

Expand Down
45 changes: 45 additions & 0 deletions Tests/StreamChatTests/Database/DTOs/ChannelDTO_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,51 @@ final class ChannelDTO_Tests: XCTestCase {
XCTAssertEqual(unreadCount.messages, unreadMessages)
XCTAssertEqual(unreadCount.mentions, 1)
}

func test_channelMembersIsUsedOverMembership_whenNotificationsMutedIsIncorrect() throws {
// Issue where membership.notificationsMuted is incorrect
let cid = ChannelId.unique
let userId: UserId = .unique
let user = UserPayload.dummy(userId: userId)
let memberPayload = MemberPayload(
user: user,
userId: user.id,
role: nil,
createdAt: .unique,
updatedAt: .unique,
notificationsMuted: true
)
let membershipPayload = MemberPayload(
user: user,
userId: user.id,
role: nil,
createdAt: .unique,
updatedAt: .unique,
notificationsMuted: false // incorrectly false
)
let channelPayload = ChannelPayload(
channel: .dummy(cid: cid),
watcherCount: nil,
watchers: nil,
members: [memberPayload],
membership: membershipPayload,
messages: [],
pinnedMessages: [],
channelReads: [],
isHidden: nil
)
try database.writeSynchronously { session in
try session.saveChannel(payload: channelPayload)
}

var channelMember: ChatChannelMember?
let session = database.backgroundReadOnlyContext
session.performAndWait {
channelMember = try? session.member(userId: userId, cid: cid)?.asModel()
}
let member = try XCTUnwrap(channelMember)
XCTAssertEqual(true, member.notificationsMuted)
}

func test_typingUsers_areCleared_onResetEphemeralValues() throws {
let cid: ChannelId = .unique
Expand Down

0 comments on commit 635ea84

Please sign in to comment.