Skip to content
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: 1 addition & 1 deletion Sources/QuickBloxData/DTO/Dialog/RemoteDialogDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct RemoteDialogDTO: Equatable {
var participantsIds: [String] = []
var toDeleteIds: [String] = []
var toAddIds: [String] = []
var photo = "null"
var photo = ""
var ownerId = ""
var isOwnedByCurrentUser = false

Expand Down
12 changes: 11 additions & 1 deletion Sources/QuickBloxData/DTO/Message/LocalMessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import QuickBloxDomain
import Foundation

/// This is a DTO model for interactions with the message model in local storage.
public struct LocalMessageDTO: Equatable, Identifiable, Hashable {
public struct LocalMessageDTO: Identifiable, Hashable {
public var id = UUID().uuidString
var dialogId = ""
var text = ""
Expand All @@ -24,6 +24,16 @@ public struct LocalMessageDTO: Equatable, Identifiable, Hashable {
var isDelivered = false
var eventType: MessageEventType = .message
var type: MessageType = .chat
var actionType: MessageAction = .none
var originSenderName: String?
var originalMessages: [LocalMessageDTO] = []
var relatedId: String = ""
}

extension LocalMessageDTO: Equatable {
public static func == (lhs: LocalMessageDTO, rhs: LocalMessageDTO) -> Bool {
return lhs.id == rhs.id && lhs.dialogId == rhs.dialogId
}
}

extension LocalMessageDTO: Dated {
Expand Down
6 changes: 5 additions & 1 deletion Sources/QuickBloxData/DTO/Message/RemoteMessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ public struct RemoteMessageDTO: Equatable {
var eventType: MessageEventType = .message
var type: MessageType = .chat
var saveToHistory: Bool = true
var actionType: MessageAction = .none
var originSenderName: String = ""
var originalMessages: [RemoteMessageDTO] = []
var relatedId = ""
}

public struct RemoteFileInfoDTO: Equatable {
public struct RemoteFileInfoDTO: Equatable, Codable {
var id = ""
var name = ""
var type = ""
Expand Down
108 changes: 108 additions & 0 deletions Sources/QuickBloxData/DTO/Message/RemoteOriginalMessageDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// RemoteOriginalMessageDTO.swift
// QuickBloxUIKit
//
// Created by Injoit on 22.11.2023.
// Copyright © 2023 QuickBlox. All rights reserved.
//

import Foundation

/// This is a DTO model for interactions with the original message model in remote storage.
public struct RemoteOriginalMessageDTO: Codable {
var id: String
var dialogId: String
var text: String
var recipientId: UInt
var senderId: UInt
var dateSent: Int64
var attachments: [RemoteOriginalFileInfoDTO]
var createdAt: Date
var updatedAt: Date
var deliveredIds: [UInt]
var readIds: [UInt]

public init(id: String,
dialogId: String = "",
text: String = "",
recipientId: UInt = 0,
senderId: UInt = 0,
dateSent: Int64,
attachments: [RemoteOriginalFileInfoDTO] = [],
createdAt: Date,
updatedAt: Date,
deliveredIds: [UInt],
readIds: [UInt]) {
self.id = id
self.dialogId = dialogId
self.text = text
self.recipientId = recipientId
self.senderId = senderId
self.dateSent = dateSent
self.attachments = attachments
self.createdAt = createdAt
self.updatedAt = updatedAt
self.deliveredIds = deliveredIds
self.readIds = readIds
}

public enum CodingKeys: String, CodingKey {
case id = "_id"
case dialogId = "chat_dialog_id"
case text = "message"
case recipientId = "recipient_id"
case senderId = "sender_id"
case dateSent = "date_sent"
case attachments = "attachments"
case createdAt = "created_at"
case updatedAt = "updated_at"
case deliveredIds = "delivered_ids"
case readIds = "read_ids"
}
}

public struct RemoteOriginalFileInfoDTO: Codable {
var id: String
var name: String
var type: String
var url: String
var uid: String
}

extension RemoteOriginalFileInfoDTO {
init(_ value: RemoteFileInfoDTO) {
id = value.id
name = value.name
type = value.type
uid = value.uid
url = value.path
}
}

extension RemoteOriginalMessageDTO {
init(_ value: RemoteMessageDTO) {
id = value.id
dialogId = value.dialogId
text = value.text
senderId = UInt(value.senderId) ?? 0
recipientId = UInt(value.recipientId) ?? 0
dateSent = value.dateSent.timeStampInt
createdAt = value.createdAt
updatedAt = value.updatedAt

attachments = value.filesInfo.compactMap{ RemoteOriginalFileInfoDTO($0) }

readIds = value.readIds.compactMap { UInt($0) ?? 0 }
deliveredIds = value.deliveredIds.compactMap { UInt($0) ?? 0 }
}
}

private extension Date {
var timeStampString: String {
return String(Int64(self.timeIntervalSince1970 * 1000))
}

var timeStampInt: Int64 {
return Int64(self.timeIntervalSince1970 * 1000)
}
}
22 changes: 22 additions & 0 deletions Sources/QuickBloxData/Repository/MessagesRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ extension LocalMessageDTO {
fileInfo?.path = new.path
fileInfo?.uid = new.uid
}
actionType = value.actionType
originSenderName = value.originSenderName
originalMessages = value.originalMessages.compactMap { LocalMessageDTO($0) }
relatedId = value.relatedId
}
}

Expand Down Expand Up @@ -73,6 +77,12 @@ private extension RemoteMessageDTO {
uid: file.uid
))
}
actionType = value.actionType
if let originSenderName = value.originSenderName {
self.originSenderName = originSenderName
}
originalMessages = value.originalMessages.compactMap { RemoteMessageDTO($0) }
relatedId = value.relatedId
}
}

Expand Down Expand Up @@ -109,6 +119,12 @@ extension Message {
fileInfo?.path.remote = filesInfo.path
fileInfo?.uid = filesInfo.uid
}
actionType = value.actionType
if value.originSenderName.isEmpty == false {
self.originSenderName = value.originSenderName
}
originalMessages = value.originalMessages.compactMap { Message($0) }
relatedId = value.relatedId
}

init(_ value: LocalMessageDTO) {
Expand All @@ -131,6 +147,12 @@ extension Message {
fileInfo?.path = info.path
fileInfo?.uid = info.uid
}
actionType = value.actionType
if let originSenderName = value.originSenderName {
self.originSenderName = originSenderName
}
originalMessages = value.originalMessages.compactMap { Message($0) }
relatedId = value.relatedId
}
}

Expand Down
12 changes: 10 additions & 2 deletions Sources/QuickBloxData/Source/Entity/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Foundation
///
/// This is an active model that conforms to the ``MessageEntity`` protocol.
public struct Message: MessageEntity {

public var id: String

/// The unique ID of the conversation that the message belongs to.
Expand All @@ -34,6 +35,10 @@ public struct Message: MessageEntity {
public var readIds: [String] = []
public var eventType: MessageEventType = .message
public var type: MessageType = .chat
public var actionType: MessageAction = .none
public var originSenderName: String?
public var originalMessages: [Message] = []
public var relatedId: String = ""

public init(id: String = UUID().uuidString,
dialogId: String,
Expand All @@ -58,10 +63,13 @@ public extension Message {
isDelivered: value.isDelivered,
isRead: value.isRead,
eventType: value.eventType,
type: value.type)
type: value.type,
actionType: value.actionType,
originSenderName: value.originSenderName,
relatedId: value.relatedId)
if let fileInfo = value.fileInfo {
self.fileInfo = FileInfo(fileInfo)
}

self.originalMessages = value.originalMessages.compactMap { Message($0) }
}
}
14 changes: 4 additions & 10 deletions Sources/QuickBloxData/Source/Local/LocalDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ extension LocalDataSource {
}
}

if dto.photo.isEmpty == false {
if dialog.photo != dto.photo {
isUpdated = true
dialog.photo = dto.photo
}
if dialog.photo != dto.photo {
isUpdated = true
dialog.photo = dto.photo
}

dialog.updatedAt = dto.updatedAt
Expand All @@ -109,11 +107,7 @@ extension LocalDataSource {
if dto.messages.isEmpty == false {
for new in dto.messages {
isUpdated = true
if let index = dialog.messages.firstIndex(where: { $0.id == new.id }) {
dialog.messages[index] = new
} else {
dialog.messages.insertElement(new, withSorting: .orderedAscending)
}
dialog.messages.insertElement(new, withSorting: .orderedAscending)
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/QuickBloxData/Source/Remote/Chat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ actor ChatStream: NSObject, QBChatDelegate {
guard let id = dialog.id else { return }
let new = Chat(dialog)
chats[id] = new
await new.subscribe()
await subscribeEvents(new)
}

Expand Down Expand Up @@ -242,6 +241,11 @@ actor ChatStream: NSObject, QBChatDelegate {
}
}

func subscribe(chat id: String) async {
guard let chat = chats[id] else { return }
await chat.subscribe()
}

func subscribeToTyping(chat id: String) async {
guard let chat = chats[id] else { return }
await chat.subscribeTyping()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ extension QBChatMessage {
return attachment
}

switch value.actionType {
case .forward:
customParameters[QBChatMessage.Key.messageAction]
= QBChatMessage.ActionValue.forward
customParameters[QBChatMessage.Key.originSenderName]
= value.originSenderName
if value.originalMessages.isEmpty == false {
customParameters[QBChatMessage.Key.originalMessages]
= originalMessages(value.originalMessages)
}
case .reply:
customParameters[QBChatMessage.Key.messageAction]
= QBChatMessage.ActionValue.reply
customParameters[QBChatMessage.Key.originSenderName]
= value.originSenderName
if value.originalMessages.isEmpty == false {
customParameters[QBChatMessage.Key.originalMessages]
= originalMessages(value.originalMessages)
}
default:
break
}

delayed = value.delayed
markable = value.markable

Expand All @@ -65,4 +88,23 @@ extension QBChatMessage {
deliveredIDs = toSend == true ? [NSNumber(value: QBSession.current.currentUserID)]
: value.deliveredIds.compactMap { NSNumber(value: UInt($0) ?? 0) }
}

private func originalMessages(_ messages: [RemoteMessageDTO]) -> String {
let payload = messages.compactMap{ RemoteOriginalMessageDTO($0) }

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
jsonEncoder.dateEncodingStrategy = .iso8601

do {
let encodeOriginalMessages = try jsonEncoder.encode(payload)
guard let endcodeString = String(data: encodeOriginalMessages, encoding: .utf8) else {
return ""
}
return endcodeString
} catch {
print(error.localizedDescription)
return ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ extension RemoteDialogDTO {
}

}
photo = value.photo ?? "null"
photo = value.avatarPath
unreadMessagesCount = Int(value.unreadMessagesCount)
}
}

extension QBChatDialog {
var avatarPath: String {
guard let photo = photo else {
return ""
}
return (photo == "null" || photo.isEmpty) ? "" : photo
}
}
Loading