diff --git a/Rocket.Chat/API/Clients/MessagesClient.swift b/Rocket.Chat/API/Clients/MessagesClient.swift index bf9773bd18..ebfb7678bd 100644 --- a/Rocket.Chat/API/Clients/MessagesClient.swift +++ b/Rocket.Chat/API/Clients/MessagesClient.swift @@ -222,7 +222,6 @@ struct MessagesClient: APIClient { return true } - // swiftlint:disable function_body_length @discardableResult func reactMessage(_ message: Message, emoji: String, user: User? = AuthManager.currentUser(), realm: Realm? = Realm.current) -> Bool { guard let id = message.identifier, let username = user?.username else { diff --git a/Rocket.Chat/API/Clients/SpotlightClient.swift b/Rocket.Chat/API/Clients/SpotlightClient.swift index 2e800b583a..dad3238e26 100644 --- a/Rocket.Chat/API/Clients/SpotlightClient.swift +++ b/Rocket.Chat/API/Clients/SpotlightClient.swift @@ -27,6 +27,7 @@ struct SpotlightClient: APIClient { var subscriptions: [Subscription] = [] resource.rooms.forEach { object in + // Important note: On this API "_id" means "rid" if let roomIdentifier = object["_id"].string { if let subscription = Subscription.find(rid: roomIdentifier, realm: realm) { subscription.map(object, realm: realm) @@ -34,7 +35,7 @@ struct SpotlightClient: APIClient { subscriptions.append(subscription) } else { let subscription = Subscription() - subscription.identifier = String.random() + subscription.identifier = roomIdentifier subscription.rid = roomIdentifier subscription.name = object["name"].string ?? "" diff --git a/Rocket.Chat/API/Clients/SubscriptionsClient.swift b/Rocket.Chat/API/Clients/SubscriptionsClient.swift index b848c8fa25..54900d56d5 100644 --- a/Rocket.Chat/API/Clients/SubscriptionsClient.swift +++ b/Rocket.Chat/API/Clients/SubscriptionsClient.swift @@ -49,20 +49,40 @@ struct SubscriptionsClient: APIClient { guard let auth = AuthManager.isAuthenticated(realm: realm) else { return } func queueSubscriptionForUpdate(_ object: JSON) { - let subscription = Subscription.getOrCreate(realm: realm, values: object, updates: nil) - subscription.auth = auth - subscriptions.append(subscription) + var subscription: Subscription? + + if let rid = object["rid"].string { + subscription = Subscription.find(rid: rid, realm: realm) ?? + Subscription.getOrCreate(realm: realm, values: object, updates: nil) + } else { + subscription = Subscription.getOrCreate(realm: realm, values: object, updates: nil) + } + + if let subscription = subscription { + subscription.auth = auth + subscription.map(object, realm: realm) + subscription.mapRoom(object, realm: realm) + subscriptions.append(subscription) + } } resource.list?.forEach(queueSubscriptionForUpdate) resource.update?.forEach(queueSubscriptionForUpdate) resource.remove?.forEach { object in - let subscription = Subscription.getOrCreate(realm: realm, values: object, updates: { (obj) in - obj?.auth = nil - }) + var subscription: Subscription? - subscriptions.append(subscription) + if let rid = object["rid"].string { + subscription = Subscription.find(rid: rid, realm: realm) ?? + Subscription.getOrCreate(realm: realm, values: object, updates: nil) + } else { + subscription = Subscription.getOrCreate(realm: realm, values: object, updates: nil) + } + + if let subscription = subscription { + subscription.auth = nil + subscriptions.append(subscription) + } } auth.lastSubscriptionFetchWithLastMessage = Date.serverDate diff --git a/Rocket.Chat/Controllers/Chat/ChatSections/MessageSection.swift b/Rocket.Chat/Controllers/Chat/ChatSections/MessageSection.swift index 657b625fb8..1d69aeed08 100644 --- a/Rocket.Chat/Controllers/Chat/ChatSections/MessageSection.swift +++ b/Rocket.Chat/Controllers/Chat/ChatSections/MessageSection.swift @@ -13,6 +13,7 @@ import MobilePlayer import FLAnimatedImage import SimpleImageViewer +// swiftlint:disable type_body_length final class MessageSection: ChatSection { var object: AnyDifferentiable weak var controllerContext: UIViewController? diff --git a/Rocket.Chat/Controllers/Chat/MessagesViewControllerJoining.swift b/Rocket.Chat/Controllers/Chat/MessagesViewControllerJoining.swift index d730fe418c..e4cc17907f 100644 --- a/Rocket.Chat/Controllers/Chat/MessagesViewControllerJoining.swift +++ b/Rocket.Chat/Controllers/Chat/MessagesViewControllerJoining.swift @@ -61,6 +61,7 @@ extension MessagesViewController: ChatPreviewModeViewProtocol { Realm.executeOnMainThread({ realm in subscription.auth = auth + subscription.open = true realm.add(subscription, update: true) }) diff --git a/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManager.swift b/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManager.swift index c82a2b82f9..f89584cf2a 100644 --- a/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManager.swift +++ b/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManager.swift @@ -75,11 +75,13 @@ struct SubscriptionManager { return } - let subscription = Subscription.getOrCreate(realm: realm, values: object, updates: nil) + let subscription = Subscription.find(rid: rid, realm: realm) ?? + Subscription.getOrCreate(realm: realm, values: object, updates: nil) if msg == "removed" { realm.delete(subscription) } else { + subscription.auth = auth realm.add(subscription, update: true) } }) diff --git a/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManagerRooms.swift b/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManagerRooms.swift index 460282c052..2cd08a6c27 100644 --- a/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManagerRooms.swift +++ b/Rocket.Chat/Managers/Model/SubscriptionManager/SubscriptionManagerRooms.swift @@ -40,7 +40,7 @@ extension SubscriptionManager { "msg": "method", "method": "joinRoom", "params": [rid] - ] as [String: Any] + ] as [String: Any] SocketManager.send(request) { (response) in guard !response.isError() else { return Log.debug(response.result.string) } diff --git a/Rocket.Chat/Managers/Socket/Response/ResponseMessage.swift b/Rocket.Chat/Managers/Socket/Response/ResponseMessage.swift index 3099c3aaac..8b2e3d63c5 100644 --- a/Rocket.Chat/Managers/Socket/Response/ResponseMessage.swift +++ b/Rocket.Chat/Managers/Socket/Response/ResponseMessage.swift @@ -12,6 +12,7 @@ enum ResponseMessage: String { case ping case changed case added + case inserted case updated case removed case unknown diff --git a/Rocket.Chat/Managers/Socket/Response/SocketHandlers.swift b/Rocket.Chat/Managers/Socket/Response/SocketHandlers.swift index 6362d121ee..5bc50c9012 100644 --- a/Rocket.Chat/Managers/Socket/Response/SocketHandlers.swift +++ b/Rocket.Chat/Managers/Socket/Response/SocketHandlers.swift @@ -24,9 +24,9 @@ extension SocketManager { return self.handleConnectionMessage(result, socket: socket) case .ping: return self.handlePingMessage(result, socket: socket) - case .changed, .added, .removed: + case .changed, .added, .inserted, .updated, .removed: return self.handleModelUpdates(result, socket: socket) - case .updated, .unknown: + case .unknown: break case .error: self.handleError(result, socket: socket) diff --git a/Rocket.Chat/Models/Base/ModelHandler.swift b/Rocket.Chat/Models/Base/ModelHandler.swift index ae25a4effa..db7c19c9d7 100644 --- a/Rocket.Chat/Models/Base/ModelHandler.swift +++ b/Rocket.Chat/Models/Base/ModelHandler.swift @@ -31,7 +31,7 @@ extension ModelHandler where Self: BaseModel { } switch msg { - case .added: + case .added, .inserted: object.add(values, realm: realm) case .changed: object.update(values, realm: realm) diff --git a/Rocket.Chat/Models/Mapping/SubscriptionModelMapping.swift b/Rocket.Chat/Models/Mapping/SubscriptionModelMapping.swift index 805c1cea04..84641f0b4e 100644 --- a/Rocket.Chat/Models/Mapping/SubscriptionModelMapping.swift +++ b/Rocket.Chat/Models/Mapping/SubscriptionModelMapping.swift @@ -68,6 +68,7 @@ extension Subscription: ModelMappeable { func mapNotifications(_ values: JSON) { self.disableNotifications = values["disableNotifications"].bool ?? false self.hideUnreadStatus = values["hideUnreadStatus"].bool ?? false + if let desktopNotificationsString = values["desktopNotifications"].string { self.desktopNotifications = SubscriptionNotificationsStatus(rawValue: desktopNotificationsString) ?? .default } diff --git a/Rocket.Chat/Models/Message/UnmanagedMessage.swift b/Rocket.Chat/Models/Message/UnmanagedMessage.swift index 0203139331..450f221d0d 100644 --- a/Rocket.Chat/Models/Message/UnmanagedMessage.swift +++ b/Rocket.Chat/Models/Message/UnmanagedMessage.swift @@ -93,6 +93,8 @@ extension UnmanagedMessage { } extension UnmanagedMessage { + + // swiftlint:disable function_body_length init?(_ message: Message) { guard let messageIdentifier = message.identifier, diff --git a/Rocket.Chat/Views/Cells/Subscription/SubscriptionCell.swift b/Rocket.Chat/Views/Cells/Subscription/SubscriptionCell.swift index 9e2efb14c3..9286410649 100644 --- a/Rocket.Chat/Views/Cells/Subscription/SubscriptionCell.swift +++ b/Rocket.Chat/Views/Cells/Subscription/SubscriptionCell.swift @@ -32,7 +32,7 @@ final class SubscriptionCell: BaseSubscriptionCell { override func updateSubscriptionInformation() { guard let subscription = subscription?.managedObject else { return } - labelLastMessage.text = subscription.roomLastMessageText + labelLastMessage.text = subscription.roomLastMessageText ?? localized("subscriptions.list.no_message") if let roomLastMessage = subscription.roomLastMessage?.createdAt { labelDate.text = dateFormatted(date: roomLastMessage) diff --git a/Rocket.ChatTests/API/Clients/SubscriptionsClientSpec.swift b/Rocket.ChatTests/API/Clients/SubscriptionsClientSpec.swift index 012cd522ef..2b665b327b 100644 --- a/Rocket.ChatTests/API/Clients/SubscriptionsClientSpec.swift +++ b/Rocket.ChatTests/API/Clients/SubscriptionsClientSpec.swift @@ -12,6 +12,7 @@ import RealmSwift @testable import Rocket_Chat +// swiftlint:disable type_body_length class SubscriptionsClientSpec: XCTestCase { override func setUp() { @@ -236,6 +237,7 @@ class SubscriptionsClientSpec: XCTestCase { XCTAssertEqual(user2Object.rolesInSubscription(subscriptionObject).count, 1) } + // swiftlint:disable function_body_length func testLoadHistory() { let api = MockAPI() let client = SubscriptionsClient(api: api) diff --git a/Rocket.ChatTests/API/Requests/Room/RoomHistoryRequestSpec.swift b/Rocket.ChatTests/API/Requests/Room/RoomHistoryRequestSpec.swift index d8866e15b8..87cee70f02 100644 --- a/Rocket.ChatTests/API/Requests/Room/RoomHistoryRequestSpec.swift +++ b/Rocket.ChatTests/API/Requests/Room/RoomHistoryRequestSpec.swift @@ -46,6 +46,7 @@ class RoomHistoryRequestSpec: APITestCase { XCTAssertNil(request.httpBody, "body is nil") } + // swiftlint:disable function_body_length func testResult() { let mockResult = JSON([ "messages": [ diff --git a/Rocket.ChatTests/Extensions/NSAttributedStringExtensionsSpec.swift b/Rocket.ChatTests/Extensions/NSAttributedStringExtensionsSpec.swift index 40b9181d7a..93be66473a 100644 --- a/Rocket.ChatTests/Extensions/NSAttributedStringExtensionsSpec.swift +++ b/Rocket.ChatTests/Extensions/NSAttributedStringExtensionsSpec.swift @@ -64,6 +64,7 @@ class NSAttributedStringExtensionsSpec: XCTestCase { assert(attributedString, has: (NSAttributedString.Key.backgroundColor, color), in: wordRange, "setBackgroundColor will work") } + // swiftlint:disable function_body_length func testMentionParsing() { let string = "Hi @rafael.kellermann, how are you doing? " + "Is everyone @here having a great day? How about you @filipe.alvarenga? Wish you @all a happy Christmas :) from @matheus.cardoso" @@ -91,7 +92,7 @@ class NSAttributedStringExtensionsSpec: XCTestCase { let range0 = nsString.range(of: "Hi ") let attributes0 = result.attributes(at: range0.location, longestEffectiveRange: nil, in: range0) XCTAssertEqual(attributes0.count, 0, "Will have no attributes") - + let range1 = nsString.range(of: "\u{00a0}rafael.kellermann\u{00a0}") let attributes1 = result.attributes(at: range1.location, longestEffectiveRange: nil, in: range1) XCTAssertEqual(attributes1.count, 4, "Will have 4 attributes")