Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ class ChannelController_Tests: XCTestCase {
)
// Save the message payload and check `channel.lastMessageAt` is updated
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: newerMessagePayload, for: channelId)
try $0.saveMessage(payload: newerMessagePayload, for: channelId, syncOwnReactions: true)
}
channel = try XCTUnwrap(client.databaseContainer.viewContext.channel(cid: channelId))
XCTAssertEqual(channel.lastMessageAt, newerMessagePayload.createdAt)
Expand Down Expand Up @@ -891,7 +891,7 @@ class ChannelController_Tests: XCTestCase {
)
_ = try waitFor {
client.databaseContainer.write({ session in
try session.saveMessage(payload: newMessagePayload, for: self.channelId)
try session.saveMessage(payload: newMessagePayload, for: self.channelId, syncOwnReactions: true)
}, completion: $0)
}

Expand All @@ -916,8 +916,8 @@ class ChannelController_Tests: XCTestCase {
let message2: MessagePayload = .dummy(messageId: .unique, authorUserId: .unique)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: message1, for: self.channelId)
try $0.saveMessage(payload: message2, for: self.channelId)
try $0.saveMessage(payload: message1, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: message2, for: self.channelId, syncOwnReactions: true)
}

// Check the order of messages is correct
Expand All @@ -942,8 +942,8 @@ class ChannelController_Tests: XCTestCase {
let message2: MessagePayload = .dummy(messageId: .unique, authorUserId: .unique)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: message1, for: self.channelId)
try $0.saveMessage(payload: message2, for: self.channelId)
try $0.saveMessage(payload: message1, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: message2, for: self.channelId, syncOwnReactions: true)
}

// Check the order of messages is correct
Expand Down Expand Up @@ -980,10 +980,10 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: message1, for: self.channelId)
try $0.saveMessage(payload: message2, for: self.channelId)
try $0.saveMessage(payload: reply1, for: self.channelId)
try $0.saveMessage(payload: reply2, for: self.channelId)
try $0.saveMessage(payload: message1, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: message2, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: reply1, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: reply2, for: self.channelId, syncOwnReactions: true)
}

// Check the relevant reply is shown in channel
Expand Down Expand Up @@ -1011,8 +1011,8 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: message1, for: self.channelId)
try $0.saveMessage(payload: ephemeralMessage, for: self.channelId)
try $0.saveMessage(payload: message1, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: ephemeralMessage, for: self.channelId, syncOwnReactions: true)
}

// Check the relevant ephemeral message is not shown in channel
Expand Down Expand Up @@ -1046,8 +1046,8 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: incomingDeletedMessage, for: self.channelId)
try $0.saveMessage(payload: outgoingDeletedMessage, for: self.channelId)
try $0.saveMessage(payload: incomingDeletedMessage, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: outgoingDeletedMessage, for: self.channelId, syncOwnReactions: true)
}

// Only outgoing deleted messages are returned by controller
Expand Down Expand Up @@ -1081,8 +1081,8 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: incomingDeletedMessage, for: self.channelId)
try $0.saveMessage(payload: outgoingDeletedMessage, for: self.channelId)
try $0.saveMessage(payload: incomingDeletedMessage, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: outgoingDeletedMessage, for: self.channelId, syncOwnReactions: true)
}

// Both outgoing and incoming messages should NOT be visible
Expand Down Expand Up @@ -1116,8 +1116,8 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: incomingDeletedMessage, for: self.channelId)
try $0.saveMessage(payload: outgoingDeletedMessage, for: self.channelId)
try $0.saveMessage(payload: incomingDeletedMessage, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: outgoingDeletedMessage, for: self.channelId, syncOwnReactions: true)
}

// Both outgoing and incoming messages should be visible
Expand Down Expand Up @@ -1172,8 +1172,8 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: shadowedMessage, for: self.channelId)
try $0.saveMessage(payload: nonShadowedMessage, for: self.channelId)
try $0.saveMessage(payload: shadowedMessage, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: nonShadowedMessage, for: self.channelId, syncOwnReactions: true)
}

// Both messages should be visible
Expand Down Expand Up @@ -1204,8 +1204,8 @@ class ChannelController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: shadowedMessage, for: self.channelId)
try $0.saveMessage(payload: nonShadowedMessage, for: self.channelId)
try $0.saveMessage(payload: shadowedMessage, for: self.channelId, syncOwnReactions: true)
try $0.saveMessage(payload: nonShadowedMessage, for: self.channelId, syncOwnReactions: true)
}

// Only non-shadowed message should be visible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ final class MessageController_Tests: XCTestCase {
text: .unique
)
try client.databaseContainer.writeSynchronously { session in
try session.saveMessage(payload: messagePayload, for: self.cid)
try session.saveMessage(payload: messagePayload, for: self.cid, syncOwnReactions: true)
}

// Assert the controller's `message` is up-to-date
Expand Down Expand Up @@ -210,8 +210,8 @@ final class MessageController_Tests: XCTestCase {
)

try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: reply1, for: self.cid)
try $0.saveMessage(payload: reply2, for: self.cid)
try $0.saveMessage(payload: reply1, for: self.cid, syncOwnReactions: true)
try $0.saveMessage(payload: reply2, for: self.cid, syncOwnReactions: true)
}

// Set top-to-bottom ordering
Expand Down Expand Up @@ -280,9 +280,9 @@ final class MessageController_Tests: XCTestCase {

// Save messages
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: reply1, for: cid)
try $0.saveMessage(payload: reply2, for: cid)
try $0.saveMessage(payload: reply3, for: cid)
try $0.saveMessage(payload: reply1, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: reply2, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: reply3, for: cid, syncOwnReactions: true)
}

// Check if the replies are correct
Expand Down Expand Up @@ -331,8 +331,8 @@ final class MessageController_Tests: XCTestCase {

// Save messages
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: ownReply, for: cid)
try $0.saveMessage(payload: otherReply, for: cid)
try $0.saveMessage(payload: ownReply, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: otherReply, for: cid, syncOwnReactions: true)
}

// Only own reply should be visible
Expand Down Expand Up @@ -380,8 +380,8 @@ final class MessageController_Tests: XCTestCase {

// Save messages
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: ownReply, for: cid)
try $0.saveMessage(payload: otherReply, for: cid)
try $0.saveMessage(payload: ownReply, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: otherReply, for: cid, syncOwnReactions: true)
}

// both deleted replies should be hidden
Expand Down Expand Up @@ -429,8 +429,8 @@ final class MessageController_Tests: XCTestCase {

// Save messages
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: ownReply, for: cid)
try $0.saveMessage(payload: otherReply, for: cid)
try $0.saveMessage(payload: ownReply, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: otherReply, for: cid, syncOwnReactions: true)
}

// both deleted replies should be visible
Expand Down Expand Up @@ -478,8 +478,8 @@ final class MessageController_Tests: XCTestCase {

// Save messages
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: nonShadowedReply, for: cid)
try $0.saveMessage(payload: shadowedReply, for: cid)
try $0.saveMessage(payload: nonShadowedReply, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: shadowedReply, for: cid, syncOwnReactions: true)
}

// all replies should be visible
Expand Down Expand Up @@ -526,8 +526,8 @@ final class MessageController_Tests: XCTestCase {

// Save messages
try client.databaseContainer.writeSynchronously {
try $0.saveMessage(payload: nonShadowedReply, for: cid)
try $0.saveMessage(payload: shadowedReply, for: cid)
try $0.saveMessage(payload: nonShadowedReply, for: cid, syncOwnReactions: true)
try $0.saveMessage(payload: shadowedReply, for: cid, syncOwnReactions: true)
}

// only non-shadowed reply should be visible
Expand Down Expand Up @@ -596,7 +596,7 @@ final class MessageController_Tests: XCTestCase {
authorUserId: currentUserId
)
try client.databaseContainer.writeSynchronously { session in
try session.saveMessage(payload: messagePayload, for: self.cid)
try session.saveMessage(payload: messagePayload, for: self.cid, syncOwnReactions: true)
}
env.messageUpdater.getMessage_completion?(nil)

Expand Down Expand Up @@ -633,7 +633,7 @@ final class MessageController_Tests: XCTestCase {
text: "new text"
)
try client.databaseContainer.writeSynchronously { session in
try session.saveMessage(payload: messagePayload, for: self.cid)
try session.saveMessage(payload: messagePayload, for: self.cid, syncOwnReactions: true)
}
env.messageUpdater.getMessage_completion?(nil)

Expand Down Expand Up @@ -671,7 +671,7 @@ final class MessageController_Tests: XCTestCase {

var replyModel: ChatMessage?
try client.databaseContainer.writeSynchronously { session in
replyModel = try session.saveMessage(payload: reply, for: self.cid)!.asModel()
replyModel = try session.saveMessage(payload: reply, for: self.cid, syncOwnReactions: true)!.asModel()
}

// Assert `insert` entity change is received by the delegate
Expand Down
4 changes: 2 additions & 2 deletions Sources/StreamChat/Database/DTOs/ChannelDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ extension NSManagedObjectContext {
) throws -> ChannelDTO {
let dto = try saveChannel(payload: payload.channel, query: query)

try payload.messages.forEach { _ = try saveMessage(payload: $0, channelDTO: dto) }
try payload.messages.forEach { _ = try saveMessage(payload: $0, channelDTO: dto, syncOwnReactions: true) }

dto.updateOldestMessageAt(payload: payload)

try payload.pinnedMessages.forEach {
_ = try saveMessage(payload: $0, channelDTO: dto)
_ = try saveMessage(payload: $0, channelDTO: dto, syncOwnReactions: true)
}

try payload.channelReads.forEach { _ = try saveChannelRead(payload: $0, for: payload.channel.cid) }
Expand Down
14 changes: 6 additions & 8 deletions Sources/StreamChat/Database/DTOs/MessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ extension NSManagedObjectContext: MessageDatabaseSession {

return message
}
func saveMessage(payload: MessagePayload, channelDTO: ChannelDTO) throws -> MessageDTO {

func saveMessage(payload: MessagePayload, channelDTO: ChannelDTO, syncOwnReactions: Bool) throws -> MessageDTO {
let cid = try ChannelId(cid: channelDTO.cid)
let dto = MessageDTO.loadOrCreate(id: payload.id, context: self)

Expand Down Expand Up @@ -435,7 +435,7 @@ extension NSManagedObjectContext: MessageDatabaseSession {
}

if let quotedMessage = payload.quotedMessage {
dto.quotedMessage = try saveMessage(payload: quotedMessage, channelDTO: channelDTO)
dto.quotedMessage = try saveMessage(payload: quotedMessage, channelDTO: channelDTO, syncOwnReactions: false)
} else if let quotedMessageId = payload.quotedMessageId {
// In case we do not have a fully formed quoted message in the payload,
// we check for quotedMessageId. This can happen in the case of nested quoted messages.
Expand Down Expand Up @@ -479,9 +479,7 @@ extension NSManagedObjectContext: MessageDatabaseSession {
return MessageReactionDTO.createId(dto: reactionDTO)
}

// the .filter part is a workaround an API bug, some WS events contain
// the message.own_reactions field populated with reactions from other users
if payload.ownReactions.filter({ currentUser?.user.id != $0.user.id }).isEmpty {
if syncOwnReactions {
dto.ownReactions = payload.ownReactions.compactMap {
guard let reactionDTO = try? saveReaction(payload: $0, message: dto) else {
return nil
Expand Down Expand Up @@ -514,7 +512,7 @@ extension NSManagedObjectContext: MessageDatabaseSession {
return dto
}

func saveMessage(payload: MessagePayload, for cid: ChannelId?) throws -> MessageDTO? {
func saveMessage(payload: MessagePayload, for cid: ChannelId?, syncOwnReactions: Bool = true) throws -> MessageDTO? {
guard payload.channel != nil || cid != nil else {
throw ClientError.MessagePayloadSavingFailure("""
Either `payload.channel` or `cid` must be provided to sucessfuly save the message payload.
Expand Down Expand Up @@ -543,7 +541,7 @@ extension NSManagedObjectContext: MessageDatabaseSession {
return nil
}

return try saveMessage(payload: payload, channelDTO: channel)
return try saveMessage(payload: payload, channelDTO: channel, syncOwnReactions: syncOwnReactions)
}

func saveMessage(payload: MessagePayload, for query: MessageSearchQuery) throws -> MessageDTO? {
Expand Down
Loading