Skip to content

Commit 68de442

Browse files
Merge pull request #15 from QuickBlox/Release-0.3.2
0.3.2
2 parents a59acec + a1ea048 commit 68de442

File tree

78 files changed

+5616
-1353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+5616
-1353
lines changed

Sources/QuickBloxData/DTO/Dialog/RemoteDialogDTO.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public struct RemoteDialogDTO: Equatable {
1717
var participantsIds: [String] = []
1818
var toDeleteIds: [String] = []
1919
var toAddIds: [String] = []
20-
var photo = "null"
20+
var photo = ""
2121
var ownerId = ""
2222
var isOwnedByCurrentUser = false
2323

Sources/QuickBloxData/DTO/Message/LocalMessageDTO.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import QuickBloxDomain
1010
import Foundation
1111

1212
/// This is a DTO model for interactions with the message model in local storage.
13-
public struct LocalMessageDTO: Equatable, Identifiable, Hashable {
13+
public struct LocalMessageDTO: Identifiable, Hashable {
1414
public var id = UUID().uuidString
1515
var dialogId = ""
1616
var text = ""
@@ -24,6 +24,16 @@ public struct LocalMessageDTO: Equatable, Identifiable, Hashable {
2424
var isDelivered = false
2525
var eventType: MessageEventType = .message
2626
var type: MessageType = .chat
27+
var actionType: MessageAction = .none
28+
var originSenderName: String?
29+
var originalMessages: [LocalMessageDTO] = []
30+
var relatedId: String = ""
31+
}
32+
33+
extension LocalMessageDTO: Equatable {
34+
public static func == (lhs: LocalMessageDTO, rhs: LocalMessageDTO) -> Bool {
35+
return lhs.id == rhs.id && lhs.dialogId == rhs.dialogId
36+
}
2737
}
2838

2939
extension LocalMessageDTO: Dated {

Sources/QuickBloxData/DTO/Message/RemoteMessageDTO.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ public struct RemoteMessageDTO: Equatable {
3232
var eventType: MessageEventType = .message
3333
var type: MessageType = .chat
3434
var saveToHistory: Bool = true
35+
var actionType: MessageAction = .none
36+
var originSenderName: String = ""
37+
var originalMessages: [RemoteMessageDTO] = []
38+
var relatedId = ""
3539
}
3640

37-
public struct RemoteFileInfoDTO: Equatable {
41+
public struct RemoteFileInfoDTO: Equatable, Codable {
3842
var id = ""
3943
var name = ""
4044
var type = ""
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//
2+
// RemoteOriginalMessageDTO.swift
3+
// QuickBloxUIKit
4+
//
5+
// Created by Injoit on 22.11.2023.
6+
// Copyright © 2023 QuickBlox. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// This is a DTO model for interactions with the original message model in remote storage.
12+
public struct RemoteOriginalMessageDTO: Codable {
13+
var id: String
14+
var dialogId: String
15+
var text: String
16+
var recipientId: UInt
17+
var senderId: UInt
18+
var dateSent: Int64
19+
var attachments: [RemoteOriginalFileInfoDTO]
20+
var createdAt: Date
21+
var updatedAt: Date
22+
var deliveredIds: [UInt]
23+
var readIds: [UInt]
24+
25+
public init(id: String,
26+
dialogId: String = "",
27+
text: String = "",
28+
recipientId: UInt = 0,
29+
senderId: UInt = 0,
30+
dateSent: Int64,
31+
attachments: [RemoteOriginalFileInfoDTO] = [],
32+
createdAt: Date,
33+
updatedAt: Date,
34+
deliveredIds: [UInt],
35+
readIds: [UInt]) {
36+
self.id = id
37+
self.dialogId = dialogId
38+
self.text = text
39+
self.recipientId = recipientId
40+
self.senderId = senderId
41+
self.dateSent = dateSent
42+
self.attachments = attachments
43+
self.createdAt = createdAt
44+
self.updatedAt = updatedAt
45+
self.deliveredIds = deliveredIds
46+
self.readIds = readIds
47+
}
48+
49+
public enum CodingKeys: String, CodingKey {
50+
case id = "_id"
51+
case dialogId = "chat_dialog_id"
52+
case text = "message"
53+
case recipientId = "recipient_id"
54+
case senderId = "sender_id"
55+
case dateSent = "date_sent"
56+
case attachments = "attachments"
57+
case createdAt = "created_at"
58+
case updatedAt = "updated_at"
59+
case deliveredIds = "delivered_ids"
60+
case readIds = "read_ids"
61+
}
62+
}
63+
64+
public struct RemoteOriginalFileInfoDTO: Codable {
65+
var id: String
66+
var name: String
67+
var type: String
68+
var url: String
69+
var uid: String
70+
}
71+
72+
extension RemoteOriginalFileInfoDTO {
73+
init(_ value: RemoteFileInfoDTO) {
74+
id = value.id
75+
name = value.name
76+
type = value.type
77+
uid = value.uid
78+
url = value.path
79+
}
80+
}
81+
82+
extension RemoteOriginalMessageDTO {
83+
init(_ value: RemoteMessageDTO) {
84+
id = value.id
85+
dialogId = value.dialogId
86+
text = value.text
87+
senderId = UInt(value.senderId) ?? 0
88+
recipientId = UInt(value.recipientId) ?? 0
89+
dateSent = value.dateSent.timeStampInt
90+
createdAt = value.createdAt
91+
updatedAt = value.updatedAt
92+
93+
attachments = value.filesInfo.compactMap{ RemoteOriginalFileInfoDTO($0) }
94+
95+
readIds = value.readIds.compactMap { UInt($0) ?? 0 }
96+
deliveredIds = value.deliveredIds.compactMap { UInt($0) ?? 0 }
97+
}
98+
}
99+
100+
private extension Date {
101+
var timeStampString: String {
102+
return String(Int64(self.timeIntervalSince1970 * 1000))
103+
}
104+
105+
var timeStampInt: Int64 {
106+
return Int64(self.timeIntervalSince1970 * 1000)
107+
}
108+
}

Sources/QuickBloxData/Repository/MessagesRepository.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ extension LocalMessageDTO {
4646
fileInfo?.path = new.path
4747
fileInfo?.uid = new.uid
4848
}
49+
actionType = value.actionType
50+
originSenderName = value.originSenderName
51+
originalMessages = value.originalMessages.compactMap { LocalMessageDTO($0) }
52+
relatedId = value.relatedId
4953
}
5054
}
5155

@@ -73,6 +77,12 @@ private extension RemoteMessageDTO {
7377
uid: file.uid
7478
))
7579
}
80+
actionType = value.actionType
81+
if let originSenderName = value.originSenderName {
82+
self.originSenderName = originSenderName
83+
}
84+
originalMessages = value.originalMessages.compactMap { RemoteMessageDTO($0) }
85+
relatedId = value.relatedId
7686
}
7787
}
7888

@@ -109,6 +119,12 @@ extension Message {
109119
fileInfo?.path.remote = filesInfo.path
110120
fileInfo?.uid = filesInfo.uid
111121
}
122+
actionType = value.actionType
123+
if value.originSenderName.isEmpty == false {
124+
self.originSenderName = value.originSenderName
125+
}
126+
originalMessages = value.originalMessages.compactMap { Message($0) }
127+
relatedId = value.relatedId
112128
}
113129

114130
init(_ value: LocalMessageDTO) {
@@ -131,6 +147,12 @@ extension Message {
131147
fileInfo?.path = info.path
132148
fileInfo?.uid = info.uid
133149
}
150+
actionType = value.actionType
151+
if let originSenderName = value.originSenderName {
152+
self.originSenderName = originSenderName
153+
}
154+
originalMessages = value.originalMessages.compactMap { Message($0) }
155+
relatedId = value.relatedId
134156
}
135157
}
136158

Sources/QuickBloxData/Source/Entity/Message.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Foundation
1313
///
1414
/// This is an active model that conforms to the ``MessageEntity`` protocol.
1515
public struct Message: MessageEntity {
16+
1617
public var id: String
1718

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

3843
public init(id: String = UUID().uuidString,
3944
dialogId: String,
@@ -58,10 +63,13 @@ public extension Message {
5863
isDelivered: value.isDelivered,
5964
isRead: value.isRead,
6065
eventType: value.eventType,
61-
type: value.type)
66+
type: value.type,
67+
actionType: value.actionType,
68+
originSenderName: value.originSenderName,
69+
relatedId: value.relatedId)
6270
if let fileInfo = value.fileInfo {
6371
self.fileInfo = FileInfo(fileInfo)
6472
}
65-
73+
self.originalMessages = value.originalMessages.compactMap { Message($0) }
6674
}
6775
}

Sources/QuickBloxData/Source/Local/LocalDataSource.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ extension LocalDataSource {
9191
}
9292
}
9393

94-
if dto.photo.isEmpty == false {
95-
if dialog.photo != dto.photo {
96-
isUpdated = true
97-
dialog.photo = dto.photo
98-
}
94+
if dialog.photo != dto.photo {
95+
isUpdated = true
96+
dialog.photo = dto.photo
9997
}
10098

10199
dialog.updatedAt = dto.updatedAt
@@ -109,11 +107,7 @@ extension LocalDataSource {
109107
if dto.messages.isEmpty == false {
110108
for new in dto.messages {
111109
isUpdated = true
112-
if let index = dialog.messages.firstIndex(where: { $0.id == new.id }) {
113-
dialog.messages[index] = new
114-
} else {
115-
dialog.messages.insertElement(new, withSorting: .orderedAscending)
116-
}
110+
dialog.messages.insertElement(new, withSorting: .orderedAscending)
117111
}
118112
}
119113

Sources/QuickBloxData/Source/Remote/Chat.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ actor ChatStream: NSObject, QBChatDelegate {
203203
guard let id = dialog.id else { return }
204204
let new = Chat(dialog)
205205
chats[id] = new
206-
await new.subscribe()
207206
await subscribeEvents(new)
208207
}
209208

@@ -242,6 +241,11 @@ actor ChatStream: NSObject, QBChatDelegate {
242241
}
243242
}
244243

244+
func subscribe(chat id: String) async {
245+
guard let chat = chats[id] else { return }
246+
await chat.subscribe()
247+
}
248+
245249
func subscribeToTyping(chat id: String) async {
246250
guard let chat = chats[id] else { return }
247251
await chat.subscribeTyping()

Sources/QuickBloxData/Source/Remote/Extension/QBChatMessage+RemoteMessageDTO.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ extension QBChatMessage {
5757
return attachment
5858
}
5959

60+
switch value.actionType {
61+
case .forward:
62+
customParameters[QBChatMessage.Key.messageAction]
63+
= QBChatMessage.ActionValue.forward
64+
customParameters[QBChatMessage.Key.originSenderName]
65+
= value.originSenderName
66+
if value.originalMessages.isEmpty == false {
67+
customParameters[QBChatMessage.Key.originalMessages]
68+
= originalMessages(value.originalMessages)
69+
}
70+
case .reply:
71+
customParameters[QBChatMessage.Key.messageAction]
72+
= QBChatMessage.ActionValue.reply
73+
customParameters[QBChatMessage.Key.originSenderName]
74+
= value.originSenderName
75+
if value.originalMessages.isEmpty == false {
76+
customParameters[QBChatMessage.Key.originalMessages]
77+
= originalMessages(value.originalMessages)
78+
}
79+
default:
80+
break
81+
}
82+
6083
delayed = value.delayed
6184
markable = value.markable
6285

@@ -65,4 +88,23 @@ extension QBChatMessage {
6588
deliveredIDs = toSend == true ? [NSNumber(value: QBSession.current.currentUserID)]
6689
: value.deliveredIds.compactMap { NSNumber(value: UInt($0) ?? 0) }
6790
}
91+
92+
private func originalMessages(_ messages: [RemoteMessageDTO]) -> String {
93+
let payload = messages.compactMap{ RemoteOriginalMessageDTO($0) }
94+
95+
let jsonEncoder = JSONEncoder()
96+
jsonEncoder.outputFormatting = .withoutEscapingSlashes
97+
jsonEncoder.dateEncodingStrategy = .iso8601
98+
99+
do {
100+
let encodeOriginalMessages = try jsonEncoder.encode(payload)
101+
guard let endcodeString = String(data: encodeOriginalMessages, encoding: .utf8) else {
102+
return ""
103+
}
104+
return endcodeString
105+
} catch {
106+
print(error.localizedDescription)
107+
return ""
108+
}
109+
}
68110
}

Sources/QuickBloxData/Source/Remote/Extension/RemoteDialogDTO+QBChatDialog.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ extension RemoteDialogDTO {
6363
}
6464

6565
}
66-
photo = value.photo ?? "null"
66+
photo = value.avatarPath
6767
unreadMessagesCount = Int(value.unreadMessagesCount)
6868
}
6969
}
70+
71+
extension QBChatDialog {
72+
var avatarPath: String {
73+
guard let photo = photo else {
74+
return ""
75+
}
76+
return (photo == "null" || photo.isEmpty) ? "" : photo
77+
}
78+
}

0 commit comments

Comments
 (0)