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

Add support for system messages not updating channel.lastMessageAt #3476

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
# Upcoming

## StreamChat
### ✅ Added
- Add support for system messages not updating `channel.lastMessageAt` [#3476](https://github.com/GetStream/stream-chat-swift/pull/3476)
### 🐞 Fixed
- Fix watching channels when performing channel search [#3472](https://github.com/GetStream/stream-chat-swift/pull/3472)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,8 @@ final class DemoChatChannelListRouter: ChatChannelListRouter {
}
}),
.init(title: "Show Channel Info", handler: { [unowned self] _ in
self.rootViewController.presentAlert(
title: "Channel Info",
message: channelController.channel.debugDescription
)
let debugViewController = DebugObjectViewController(object: channelController.channel)
self.rootViewController.present(debugViewController, animated: true)
}),
.init(title: "Show Channel Members", handler: { [unowned self] _ in
guard let cid = channelController.channel?.cid else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public class ChannelConfig: Codable {
case commands
case createdAt = "created_at"
case updatedAt = "updated_at"
case skipLastMsgAtUpdateForSystemMsg = "skip_last_msg_update_for_system_msgs"
}

/// If users are allowed to add reactions to messages. Enabled by default.
Expand Down Expand Up @@ -258,6 +259,8 @@ public class ChannelConfig: Codable {
public let updatedAt: Date
/// Determines if polls are enabled.
public let pollsEnabled: Bool
/// Determines if system messages should not update the last message at date.
public let skipLastMsgAtUpdateForSystemMsg: Bool

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand All @@ -282,6 +285,7 @@ public class ChannelConfig: Codable {
createdAt = try container.decode(Date.self, forKey: .createdAt)
updatedAt = try container.decode(Date.self, forKey: .updatedAt)
pollsEnabled = try container.decodeIfPresent(Bool.self, forKey: .pollsEnabled) ?? false
skipLastMsgAtUpdateForSystemMsg = try container.decodeIfPresent(Bool.self, forKey: .skipLastMsgAtUpdateForSystemMsg) ?? false
}

internal required init(
Expand All @@ -296,6 +300,7 @@ public class ChannelConfig: Codable {
mutesEnabled: Bool = false,
pollsEnabled: Bool = false,
urlEnrichmentEnabled: Bool = false,
skipLastMsgAtUpdateForSystemMsg: Bool = false,
messageRetention: String = "",
maxMessageLength: Int = 0,
commands: [Command] = [],
Expand All @@ -318,5 +323,6 @@ public class ChannelConfig: Codable {
self.createdAt = createdAt
self.updatedAt = updatedAt
self.pollsEnabled = pollsEnabled
self.skipLastMsgAtUpdateForSystemMsg = skipLastMsgAtUpdateForSystemMsg
}
}
3 changes: 3 additions & 0 deletions Sources/StreamChat/Database/DTOs/ChannelConfigDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class ChannelConfigDTO: NSManagedObject {
@NSManaged var typingEventsEnabled: Bool
@NSManaged var readEventsEnabled: Bool
@NSManaged var connectEventsEnabled: Bool
@NSManaged var skipLastMsgAtUpdateForSystemMsg: Bool
@NSManaged var uploadsEnabled: Bool
@NSManaged var repliesEnabled: Bool
@NSManaged var quotesEnabled: Bool
Expand All @@ -37,6 +38,7 @@ final class ChannelConfigDTO: NSManagedObject {
mutesEnabled: mutesEnabled,
pollsEnabled: pollsEnabled,
urlEnrichmentEnabled: urlEnrichmentEnabled,
skipLastMsgAtUpdateForSystemMsg: skipLastMsgAtUpdateForSystemMsg,
messageRetention: messageRetention,
maxMessageLength: Int(maxMessageLength),
commands: Array(Set(
Expand Down Expand Up @@ -76,6 +78,7 @@ extension ChannelConfig {
dto.updatedAt = updatedAt.bridgeDate
dto.commands = NSOrderedSet(array: commands.map { $0.asDTO(context: context) })
dto.pollsEnabled = pollsEnabled
dto.skipLastMsgAtUpdateForSystemMsg = skipLastMsgAtUpdateForSystemMsg
return dto
}
}
6 changes: 5 additions & 1 deletion Sources/StreamChat/Database/DTOs/MessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,11 @@ extension NSManagedObjectContext: MessageDatabaseSession {
array: payload.threadParticipants.map { try saveUser(payload: $0) }
)

channelDTO.lastMessageAt = max(channelDTO.lastMessageAt?.bridgeDate ?? payload.createdAt, payload.createdAt).bridgeDate
let isSystemMessage = dto.type == MessageType.system.rawValue
let shouldNotUpdateLastMessageAt = isSystemMessage && channelDTO.config.skipLastMsgAtUpdateForSystemMsg
if !shouldNotUpdateLastMessageAt {
channelDTO.lastMessageAt = max(channelDTO.lastMessageAt?.bridgeDate ?? payload.createdAt, payload.createdAt).bridgeDate
}

dto.channel = channelDTO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23231" systemVersion="24A348" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22758" systemVersion="23G93" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="AttachmentDTO" representedClassName="AttachmentDTO" syncable="YES">
<attribute name="data" attributeType="Binary"/>
<attribute name="id" attributeType="String"/>
Expand Down Expand Up @@ -28,6 +28,7 @@
<attribute name="readEventsEnabled" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="repliesEnabled" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="searchEnabled" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="skipLastMsgAtUpdateForSystemMsg" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="typingEventsEnabled" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="updatedAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="uploadsEnabled" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,7 @@
"created_at" : "2019-03-21T15:49:15.40182Z",
"automod" : "AI",
"read_events" : true,
"skip_last_msg_update_for_system_msgs": true,
"commands" : [
{
"set" : "fun_set",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public extension ChannelConfig {
searchEnabled: Bool = true,
mutesEnabled: Bool = true,
urlEnrichmentEnabled: Bool = true,
skipLastMsgAtUpdateForSystemMsg: Bool = false,
messageRetention: String = "",
maxMessageLength: Int = 0,
commands: [Command] = [Command(name: "Giphy", description: "", set: "", args: "")],
Expand All @@ -35,6 +36,7 @@ public extension ChannelConfig {
searchEnabled: searchEnabled,
mutesEnabled: mutesEnabled,
urlEnrichmentEnabled: urlEnrichmentEnabled,
skipLastMsgAtUpdateForSystemMsg: skipLastMsgAtUpdateForSystemMsg,
messageRetention: messageRetention,
maxMessageLength: maxMessageLength,
commands: commands,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ final class ChannelPayload_Tests: XCTestCase {
XCTAssertEqual(config.urlEnrichmentEnabled, true)
XCTAssertEqual(config.messageRetention, "infinite")
XCTAssertEqual(config.maxMessageLength, 5000)
XCTAssertEqual(config.skipLastMsgAtUpdateForSystemMsg, true)
XCTAssertEqual(
config.commands,
[.init(name: "giphy", description: "Post a random gif to the channel", set: "fun_set", args: "[text]")]
Expand Down
46 changes: 46 additions & 0 deletions Tests/StreamChatTests/Database/DTOs/MessageDTO_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,52 @@ final class MessageDTO_Tests: XCTestCase {
XCTAssertEqual(channel.lastMessageAt?.bridgeDate, newerMessagePayload.createdAt)
}

func test_messageDoesNotUpdateChannelsLastMessageAt_whenSystemMessageAndSkipIsEnabled() throws {
let userId: UserId = .unique
let messageId: MessageId = .unique
let channelId: ChannelId = .unique
// Save channel with some messages
let channelPayload: ChannelPayload = dummyPayload(
with: channelId,
numberOfMessages: 5,
channelConfig: .mock(skipLastMsgAtUpdateForSystemMsg: true)
)
let originalLastMessageAt: Date = channelPayload.channel.lastMessageAt ?? channelPayload.channel.createdAt
try database.writeSynchronously {
try $0.saveChannel(payload: channelPayload)
}

// Create a new message payload that's older than `channel.lastMessageAt`
let olderMessagePayload: MessagePayload = .dummy(
messageId: messageId,
authorUserId: userId,
createdAt: .unique(before: channelPayload.channel.lastMessageAt!)
)
assert(olderMessagePayload.createdAt < channelPayload.channel.lastMessageAt!)
// Save the message payload and check `channel.lastMessageAt` is not updated by older message
try database.writeSynchronously {
try $0.saveMessage(payload: olderMessagePayload, for: channelId, syncOwnReactions: true, cache: nil)
}
var channel = try XCTUnwrap(database.viewContext.channel(cid: channelId))
XCTAssertNearlySameDate(channel.lastMessageAt?.bridgeDate, originalLastMessageAt)

// Create a new message payload that's newer than `channel.lastMessageAt`
let newerMessagePayload: MessagePayload = .dummy(
type: .system,
messageId: messageId,
authorUserId: userId,
createdAt: .unique(after: channelPayload.channel.lastMessageAt!)
)
assert(newerMessagePayload.createdAt > channelPayload.channel.lastMessageAt!)

// Save the message payload and check `channel.lastMessageAt` is not updated.
try database.writeSynchronously {
try $0.saveMessage(payload: newerMessagePayload, for: channelId, syncOwnReactions: true, cache: nil)
}
channel = try XCTUnwrap(database.viewContext.channel(cid: channelId))
XCTAssertNotEqual(channel.lastMessageAt?.bridgeDate, newerMessagePayload.createdAt)
}

func test_saveMultipleMessagesWithSameQuotedMessage() throws {
// We check whether a message can be quoted by multiple other messages in the same channel
// Here, secondMessage and thirdMessage quote the firstMessage
Expand Down
Loading