-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathNCChatMessage.swift
273 lines (209 loc) Β· 10.7 KB
/
NCChatMessage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
// SPDX-License-Identifier: GPL-3.0-or-later
//
import Foundation
import SwiftyAttributes
@objc extension NCChatMessage {
public var isSystemMessage: Bool {
return self.systemMessage != nil && !self.systemMessage.isEmpty
}
public var isEmojiMessage: Bool {
return self.message != nil && self.message.containsOnlyEmoji && self.message.emojiCount <= 3
}
public var isUpdateMessage: Bool {
return self.systemMessage == "message_deleted" ||
self.systemMessage == "reaction" ||
self.systemMessage == "reaction_revoked" ||
self.systemMessage == "reaction_deleted" ||
self.systemMessage == "poll_voted" ||
self.systemMessage == "message_edited"
}
public var isDeletedMessage: Bool {
return self.messageType == kMessageTypeCommentDeleted
}
public var isVoiceMessage: Bool {
return self.messageType == kMessageTypeVoiceMessage
}
public var isCommandMessage: Bool {
return self.messageType == kMessageTypeCommand
}
public func isMessage(from userId: String) -> Bool {
return self.actorType == "users" && self.actorId == userId
}
public func isDeletable(for account: TalkAccount, in room: NCRoom) -> Bool {
guard !self.isDeleting else { return false }
let sixHoursAgoTimestamp = Int(Date().timeIntervalSince1970 - (6 * 3600))
// Check server capability for normal messages
var commentDeletion = NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityDeleteMessages, forAccountId: account.accountId)
commentDeletion = commentDeletion && self.messageType == kMessageTypeComment
commentDeletion = commentDeletion && self.file() == nil
commentDeletion = commentDeletion && !self.isObjectShare
// Check server capability for files or shared objects
var objectDeletion = NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityRichObjectDelete)
objectDeletion = objectDeletion && (self.file() != nil || self.isVoiceMessage || self.isObjectShare)
// Check if user is allowed to delete a message
let sameUser = self.isMessage(from: account.userId)
let moderatorUser = (room.type != .oneToOne && room.type != .formerOneToOne) && (room.participantType == .owner || room.participantType == .moderator)
let serverCanDeleteMessage = commentDeletion || objectDeletion
let userCanDeleteMessage = sameUser || moderatorUser
let noTimeLimitForMessageDeletion = NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityDeleteMessagesUnlimited, forAccountId: account.accountId)
let deletionAllowedByTime = noTimeLimitForMessageDeletion || (self.timestamp >= sixHoursAgoTimestamp)
return serverCanDeleteMessage && userCanDeleteMessage && deletionAllowedByTime
}
public func isEditable(for account: TalkAccount, in room: NCRoom) -> Bool {
guard !self.isDeleting else { return false }
let twentyFourHoursAgoTimestamp = Int(Date().timeIntervalSince1970 - (24 * 3600))
var serverCanEditMessage = NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityEditMessages, forAccountId: account.accountId)
serverCanEditMessage = serverCanEditMessage && self.messageType == kMessageTypeComment && !self.isObjectShare
let sameUser = self.isMessage(from: account.userId)
let moderatorUser = (room.type != .oneToOne && room.type != .formerOneToOne) && (room.participantType == .owner || room.participantType == .moderator)
let userCanDeleteMessage = sameUser || moderatorUser
let noTimeLimitForMessageEdit = (room.type == .noteToSelf) && NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityEditMessagesNoteToSelf, forAccountId: account.accountId)
let editAllowedByTime = noTimeLimitForMessageEdit || (self.timestamp >= twentyFourHoursAgoTimestamp)
return serverCanEditMessage && userCanDeleteMessage && editAllowedByTime
}
public var isObjectShare: Bool {
return self.message != nil && self.message == "{object}" && self.messageParameters["object"] != nil
}
public var richObjectFromObjectShare: [AnyHashable: Any] {
guard self.isObjectShare,
let objectDict = self.messageParameters["object"] as? [AnyHashable: Any],
let jsonData = try? JSONSerialization.data(withJSONObject: objectDict),
let jsonString = String(data: jsonData, encoding: .utf8),
let parameter = NCMessageParameter(dictionary: objectDict)
else { return [:] }
return [
"objectType": parameter.type!,
"objectId": parameter.parameterId!,
"metaData": jsonString
]
}
public var poll: NCMessageParameter? {
guard let objectParameter, objectParameter.type == "talk-poll"
else { return nil }
return objectParameter
}
public var objectParameter: NCMessageParameter? {
guard self.isObjectShare,
let objectDict = self.messageParameters["object"] as? [AnyHashable: Any],
let objectParameter = NCMessageParameter(dictionary: objectDict)
else { return nil }
return objectParameter
}
public var messageParameters: [AnyHashable: Any] {
guard let data = self.messageParametersJSONString?.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: data) as? [AnyHashable: Any]
else { return [:] }
return dict
}
// TODO: Should probably be an optional?
public var systemMessageFormat: NSMutableAttributedString {
guard let message = self.parsedMessage() else { return NSMutableAttributedString(string: "") }
return message.withTextColor(.tertiaryLabel)
}
// TODO: Should probably be an optional?
public var sendingMessage: String {
guard var resultMessage = self.message else { return "" }
resultMessage = resultMessage.trimmingCharacters(in: .whitespacesAndNewlines)
for case let (key as String, value as [AnyHashable: Any]) in self.messageParameters {
if let parameter = NCMessageParameter(dictionary: value) {
resultMessage = resultMessage.replacingOccurrences(of: "{\(key)}", with: parameter.mentionId)
}
}
return resultMessage
}
public var parent: NCChatMessage? {
guard !self.isDeletedMessage, self.parentId != nil else { return nil }
var unmanagedChatMessage: NCChatMessage?
if let managedChatMessage = NCChatMessage.objects(where: "internalId = %@", self.parentId).firstObject() {
unmanagedChatMessage = NCChatMessage(value: managedChatMessage)
}
return unmanagedChatMessage
}
public var parentMessageId: Int {
return self.parent?.messageId ?? -1
}
public func isReactionBeingModified(_ reaction: String) -> Bool {
return self.temporaryReactions().first(where: { ($0 as? NCChatReaction)?.reaction == reaction }) != nil
}
public func removeReactionFromTemporaryReactions(_ reaction: String) {
if let removeReaction = self.temporaryReactions().first(where: { ($0 as? NCChatReaction)?.reaction == reaction }) {
self.temporaryReactions().remove(removeReaction)
}
}
public func addTemporaryReaction(_ reaction: String) {
let temporaryReaction = NCChatReaction()
temporaryReaction.reaction = reaction
temporaryReaction.state = .adding
self.temporaryReactions().add(temporaryReaction)
}
public func removeReactionTemporarily(_ reaction: String) {
let temporaryReaction = NCChatReaction()
temporaryReaction.reaction = reaction
temporaryReaction.state = .removing
self.temporaryReactions().add(temporaryReaction)
}
internal var isReferenceApiSupported: Bool {
// Check capabilities directly, otherwise NCSettingsController introduces new dependencies in NotificationServiceExtension
if let accountId, let serverCapabilities = NCDatabaseManager.sharedInstance().serverCapabilities(forAccountId: accountId) {
return serverCapabilities.referenceApiSupported
}
return false
}
public func isSameMessage(_ message: NCChatMessage) -> Bool {
if self.isTemporary {
return self.referenceId == message.referenceId
}
return self.messageId == message.messageId
}
public var collapsedMessageParameters: [AnyHashable: Any] {
guard let data = self.collapsedMessageParametersJSONString.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: data) as? [AnyHashable: Any]
else { return [:] }
return dict
}
public func setCollapsedMessageParameters(_ messageParameters: [AnyHashable: Any]) {
guard let jsonData = try? JSONSerialization.data(withJSONObject: messageParameters),
let jsonString = String(data: jsonData, encoding: .utf8)
else { return }
self.collapsedMessageParametersJSONString = jsonString
}
public var actor: TalkActor {
return TalkActor(actorId: self.actorId, actorType: self.actorType, actorDisplayName: self.actorDisplayName)
}
public var account: TalkAccount? {
guard let accountId else { return nil }
return NCDatabaseManager.sharedInstance().talkAccount(forAccountId: accountId)
}
public var messageIconName: String? {
if let file = self.file() {
if NCUtils.isImage(fileType: file.mimetype) {
return "photo"
} else if NCUtils.isVideo(fileType: file.mimetype) {
return "movieclapper"
} else if NCUtils.isVCard(fileType: file.mimetype) {
return "person.text.rectangle"
} else if self.isVoiceMessage {
return "mic"
} else if NCUtils.isAudio(fileType: file.mimetype) {
return "music.note"
} else {
return "doc"
}
} else if poll != nil {
return "chart.bar"
} else if deckCard() != nil {
return "rectangle.stack"
} else if geoLocation() != nil {
return "location"
}
return nil
}
public var isAnimatableGif: Bool {
guard let accountId, let file = self.file() else { return false }
let capabilities = NCDatabaseManager.sharedInstance().serverCapabilities(forAccountId: accountId)
guard NCUtils.isGif(fileType: file.mimetype), let maxGifSize = capabilities?.maxGifSize, maxGifSize > 0 else { return false }
return file.size <= maxGifSize
}
}