Skip to content

Commit

Permalink
Refactor implementation to not use MessageDTO.isSystem and only use…
Browse files Browse the repository at this point in the history
… `MessageDTO.type`
  • Loading branch information
nuno-vieira committed Oct 30, 2024
1 parent c4058e4 commit e5c1ed7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
21 changes: 11 additions & 10 deletions Sources/StreamChat/Database/DTOs/MessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class MessageDTO: NSManagedObject {
@NSManaged var extraData: Data?
@NSManaged var isSilent: Bool

// Used for creating a message as a system message from the client SDK.
@NSManaged var isSystem: Bool

@NSManaged var skipPush: Bool
@NSManaged var skipEnrichUrl: Bool
@NSManaged var isShadowed: Bool
Expand Down Expand Up @@ -659,23 +656,27 @@ extension NSManagedObjectContext: MessageDatabaseSession {
}

message.cid = cid.rawValue
message.type = parentMessageId == nil ? MessageType.regular.rawValue : MessageType.reply.rawValue
message.text = text
message.command = command
message.args = arguments
message.parentMessageId = parentMessageId
message.extraData = try JSONEncoder.default.encode(extraData)
message.isSilent = isSilent
message.isSystem = isSystem
if isSystem {
message.type = MessageType.system.rawValue
}
message.skipPush = skipPush
message.skipEnrichUrl = skipEnrichUrl
message.reactionScores = [:]
message.reactionCounts = [:]
message.reactionGroups = []


// Message type
if parentMessageId != nil {
message.type = MessageType.reply.rawValue
} else if isSystem {
message.type = MessageType.system.rawValue
} else {
message.type = MessageType.regular.rawValue
}

if let poll {
message.poll = try? savePoll(payload: poll, cache: nil)
}
Expand Down Expand Up @@ -1258,7 +1259,7 @@ extension MessageDTO {
.compactMap { $0.asRequestPayload() }

// At the moment, we only provide the type for system messages when creating a message.
let systemType = isSystem ? MessageType.system.rawValue : nil
let systemType = type == MessageType.system.rawValue ? type : nil

return .init(
id: id,
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="22758" systemVersion="23G93" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23507" 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 @@ -208,7 +208,6 @@
<attribute name="isHardDeleted" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="isShadowed" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="isSilent" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="isSystem" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="latestReactions" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromDataTransformer"/>
<attribute name="locallyCreatedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="localMessageStateRaw" optional="YES" attributeType="String"/>
Expand Down
7 changes: 3 additions & 4 deletions Tests/StreamChatTests/Database/DTOs/MessageDTO_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,6 @@ final class MessageDTO_Tests: XCTestCase {
func test_newMessage_asRequestBody_whenSystemMessage() throws {
let currentUserId: UserId = .unique
let cid: ChannelId = .unique
let parentMessageId: MessageId = .unique

// Create current user in the database.
try database.createCurrentUser(id: currentUserId)
Expand All @@ -1268,7 +1267,7 @@ final class MessageDTO_Tests: XCTestCase {

// Create message with attachments in the database.
try database.writeSynchronously { session in
let message = try session.createNewMessage(
try session.createNewMessage(
in: cid,
messageId: messageId,
text: messageText,
Expand Down Expand Up @@ -1567,7 +1566,7 @@ final class MessageDTO_Tests: XCTestCase {
let messageDTO: MessageDTO = try XCTUnwrap(database.viewContext.message(id: newMessageId))
XCTAssertEqual(messageDTO.skipPush, true)
XCTAssertEqual(messageDTO.skipEnrichUrl, true)
XCTAssertEqual(messageDTO.isSystem, false)
XCTAssertEqual(messageDTO.type, "reply")

let loadedMessage: ChatMessage = try messageDTO.asModel()
XCTAssertEqual(loadedMessage.text, newMessageText)
Expand Down Expand Up @@ -1636,7 +1635,7 @@ final class MessageDTO_Tests: XCTestCase {
let loadedChannel: ChatChannel = try XCTUnwrap(database.viewContext.channel(cid: cid)).asModel()

let messageDTO: MessageDTO = try XCTUnwrap(database.viewContext.message(id: newMessageId))
XCTAssertEqual(messageDTO.isSystem, true)
XCTAssertEqual(messageDTO.type, "system")

let loadedMessage: ChatMessage = try messageDTO.asModel()
XCTAssertEqual(loadedMessage.text, newMessageText)
Expand Down
2 changes: 1 addition & 1 deletion Tests/StreamChatTests/Workers/ChannelUpdater_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ final class ChannelUpdater_Tests: XCTestCase {
XCTAssertEqual(messageDTO.skipPush, true)
XCTAssertEqual(messageDTO.skipEnrichUrl, true)
XCTAssertEqual(messageDTO.mentionedUserIds, [currentUserId])
XCTAssertEqual(messageDTO.isSystem, false)
XCTAssertEqual(messageDTO.type, "regular")

let message = try messageDTO.asModel()
XCTAssertEqual(message.text, text)
Expand Down

0 comments on commit e5c1ed7

Please sign in to comment.