Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

[NEW] message deletion syncing #2738

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions Rocket.Chat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@
99F8FAED20B66F210025FEEF /* ImageCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99F8FAEC20B66F210025FEEF /* ImageCoordinator.swift */; };
A0FFC1611E8A9D8D00A1B5EA /* TintedTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0FFC1601E8A9D8D00A1B5EA /* TintedTextField.swift */; };
AB2AC99226FB9C7C9DA45E94 /* Pods_Rocket_Chat_ShareExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6787D07A45177F339DB234DD /* Pods_Rocket_Chat_ShareExtension.framework */; };
B3B47AC322DCCE13006E1EF7 /* RoomDeletedMessagesRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3B47AC222DCCE13006E1EF7 /* RoomDeletedMessagesRequest.swift */; };
B5893BF41F6C4A5F00365768 /* UserReviewManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5893BF31F6C4A5E00365768 /* UserReviewManager.swift */; };
B5893BF61F6C4B1D00365768 /* UserReviewManagerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5893BF51F6C4B1D00365768 /* UserReviewManagerSpec.swift */; };
D10E9C1A1F643457007F1796 /* Channel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D10E9C191F643457007F1796 /* Channel.swift */; };
Expand Down Expand Up @@ -1899,6 +1900,7 @@
99F8FAEC20B66F210025FEEF /* ImageCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageCoordinator.swift; sourceTree = "<group>"; };
A0FFC1601E8A9D8D00A1B5EA /* TintedTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TintedTextField.swift; sourceTree = "<group>"; };
AAC8D92081FF042E089F3063 /* Pods-Rocket.Chat.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Rocket.Chat.beta.xcconfig"; path = "Pods/Target Support Files/Pods-Rocket.Chat/Pods-Rocket.Chat.beta.xcconfig"; sourceTree = "<group>"; };
B3B47AC222DCCE13006E1EF7 /* RoomDeletedMessagesRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoomDeletedMessagesRequest.swift; sourceTree = "<group>"; };
B5893BF31F6C4A5E00365768 /* UserReviewManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserReviewManager.swift; sourceTree = "<group>"; };
B5893BF51F6C4B1D00365768 /* UserReviewManagerSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserReviewManagerSpec.swift; sourceTree = "<group>"; };
BCF38696E8EC385488688390 /* Pods-Rocket.Chat.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Rocket.Chat.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Rocket.Chat/Pods-Rocket.Chat.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3368,6 +3370,7 @@
80235D1C1F740AAC00A56CA5 /* Room */ = {
isa = PBXGroup;
children = (
B3B47AC222DCCE13006E1EF7 /* RoomDeletedMessagesRequest.swift */,
8062E32B20A1F8100044F407 /* RoomsRequest.swift */,
89AEB7F71F7D225D00112A09 /* RoomCreateRequest.swift */,
80A63C4E1F719F9600FE5AC4 /* RoomInfoRequest.swift */,
Expand Down Expand Up @@ -4482,6 +4485,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
"pt-BR",
Expand Down Expand Up @@ -5341,6 +5345,7 @@
4162E1531D651A8800AAAE49 /* UserManager.swift in Sources */,
800FCD4C1F728EC800D9A692 /* ChannelInfoCellProtocol.swift in Sources */,
800E22841F8500A200DA84F1 /* MessagesListViewController.swift in Sources */,
B3B47AC322DCCE13006E1EF7 /* RoomDeletedMessagesRequest.swift in Sources */,
33D08E2720BD5EDC008D03EF /* NotThemeableViews.swift in Sources */,
41778EB920ED4C2C007F72F9 /* SubscriptionCellCondensed.swift in Sources */,
99805D7520C191BE00741B8E /* StyledTextField.swift in Sources */,
Expand Down
33 changes: 33 additions & 0 deletions Rocket.Chat/API/Clients/SubscriptionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,37 @@ extension SubscriptionsClient {
}
}
}

func loadDeletedMessages(subscription: Subscription,
realm: Realm? = Realm.current,
completion: (([String]) -> Void)? = nil) {

let sinceDate = subscription.deletedMessagesLastUpdated
let request = RoomDeletedMessagesRequest(roomId: subscription.rid, since: sinceDate)
let identifier = subscription.identifier ?? ""
api.fetch(request, completion: {response in
switch response {
case .resource(let resource):
realm?.execute({ realm in
let deletedIDs = resource.messages?.compactMap { $0 } ?? []

if let sub = realm.object(ofType: Subscription.self, forPrimaryKey: identifier) {
sub.deletedMessagesLastUpdated = Date()

let toDelete = sub.messages?.filter({ message in
return deletedIDs.contains(message.identifier ?? "")
})

if let toDelete = toDelete {
realm.delete(toDelete)
}
completion?(deletedIDs)
}
completion?([])
})
case .error:
completion?([])
}
})
}
}
55 changes: 55 additions & 0 deletions Rocket.Chat/API/Requests/Room/RoomDeletedMessagesRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// RoomDeletedMessagesRequest.swift
// Rocket.Chat
//
// Created by Luís Machado on 29/03/2019.
// Copyright © 2019 Rocket.Chat. All rights reserved.
//

import SwiftyJSON

struct RoomDeletedMessagesRequest: APIRequest {
typealias APIResourceType = RoomDeletedMessagesResource

let path = "/api/v1/chat.getDeletedMessages"
let requiredVersion = Version(0, 73, 0)

var query: String?
let roomId: String?
let since: Date?

init(roomId: String, since: Date) {
self.roomId = roomId
self.since = since

let dateFormatter = ISO8601DateFormatter()
let dateString = dateFormatter.string(from: since)

if let encodedString = dateString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
self.query = "roomId=\(roomId)&since=\(encodedString)"
}
}
}

final class RoomDeletedMessagesResource: APIResource {
var messages: [String?]? {
return raw?["messages"].arrayValue.map {
if let removedMsg = $0.dictionaryObject, let msgId = removedMsg["_id"] as? String {
return msgId
}
return nil
}
}

var count: Int? {
return raw?["count"].int
}

var offset: Int? {
return raw?["offset"].int
}

var total: Int? {
return raw?["total"].int
}
}
2 changes: 2 additions & 0 deletions Rocket.Chat/Controllers/Chat/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ extension MessagesViewController: SocketConnectionHandler {
if state == .connected {
viewModel.requestingData = .none

viewModel.fetchDeletedMessages()

if viewModel.threadIdentifier != nil {
viewModel.fetchThreadMessages(from: nil)
} else {
Expand Down
11 changes: 11 additions & 0 deletions Rocket.Chat/Controllers/Chat/MessagesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ final class MessagesViewModel {
})

fetchMessages(from: nil)
fetchDeletedMessages()
}
}

Expand Down Expand Up @@ -648,6 +649,16 @@ final class MessagesViewModel {
}
}

func fetchDeletedMessages() {
guard let subscription = self.subscription,
let client = API.current()?.client(SubscriptionsClient.self) else {
return
}
client.loadDeletedMessages(subscription: subscription) { _ in
self.updateData()
}
}

// MARK: Data Manipulation

/**
Expand Down
5 changes: 5 additions & 0 deletions Rocket.Chat/Models/Subscription/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ final class Subscription: BaseModel {
let usersRoles = List<RoomRoles>()

// MARK: Internal

//Indicates when deleted messages were last updated.
// On new subscription objects our messages are up to date.
@objc dynamic var deletedMessagesLastUpdated: Date = Date()

@objc dynamic var privateOtherUserStatus: String?
var otherUserStatus: UserStatus? {
if let privateOtherUserStatus = privateOtherUserStatus {
Expand Down