From a6aa22bd1cffc76e96151fdf8db37af8959acde1 Mon Sep 17 00:00:00 2001 From: Bruno Muniz Azevedo Filho <bruno@elixxir.io> Date: Sun, 14 Aug 2022 00:24:02 -0300 Subject: [PATCH] Implemented missing requirements except for APi reporting --- .../Controllers/GroupChatController.swift | 26 +++++++++++++- .../Controllers/SingleChatController.swift | 10 +----- Sources/Integration/Session/Session.swift | 1 + Sources/LaunchFeature/LaunchViewModel.swift | 35 +++++++++++++++---- Sources/PushFeature/PushHandler.swift | 15 ++++---- 5 files changed, 62 insertions(+), 25 deletions(-) diff --git a/Sources/ChatFeature/Controllers/GroupChatController.swift b/Sources/ChatFeature/Controllers/GroupChatController.swift index 24117868..ae732094 100644 --- a/Sources/ChatFeature/Controllers/GroupChatController.swift +++ b/Sources/ChatFeature/Controllers/GroupChatController.swift @@ -317,7 +317,7 @@ extension GroupChatController: UICollectionViewDataSource { cellForItemAt indexPath: IndexPath ) -> UICollectionViewCell { - let item = sections[indexPath.section].elements[indexPath.item] + var item = sections[indexPath.section].elements[indexPath.item] let canReply: () -> Bool = { (item.status == .sent || item.status == .received) && item.networkId != nil } @@ -330,7 +330,31 @@ extension GroupChatController: UICollectionViewDataSource { let showRound: (String?) -> Void = viewModel.showRoundFrom(_:) let replyContent: (Data) -> (String, String) = viewModel.getReplyContent(for:) + var isSenderBanned = false + + if let database = try? DependencyInjection.Container.shared.resolve() as Database, + let sender = try? database.fetchContacts(.init(id: [item.senderId])).first { + isSenderBanned = sender.isBanned + } + if item.status == .received { + guard isSenderBanned == false else { + item.text = "This user has been banned" + + let cell: IncomingGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) + Bubbler.buildGroup( + bubble: cell.leftView, + with: item, + with: "Banned user" + ) + + cell.canReply = false + cell.performReply = {} + cell.leftView.didTapShowRound = {} + + return cell + } + if let replyMessageId = item.replyMessageId { let cell: IncomingGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) diff --git a/Sources/ChatFeature/Controllers/SingleChatController.swift b/Sources/ChatFeature/Controllers/SingleChatController.swift index ebc78095..3d2a35d1 100644 --- a/Sources/ChatFeature/Controllers/SingleChatController.swift +++ b/Sources/ChatFeature/Controllers/SingleChatController.swift @@ -456,15 +456,7 @@ public final class SingleChatController: UIViewController { fatalError("[takeAppScreenshot]: Unable to get foreground window scene") } - let keyWindow: UIWindow? - - if #available(iOS 15.0, *) { - keyWindow = foregroundWindowScene.keyWindow - } else { - keyWindow = UIApplication.shared.keyWindow - } - - guard let keyWindow = keyWindow else { + guard let keyWindow = foregroundWindowScene.windows.first(where: \.isKeyWindow) else { fatalError("[takeAppScreenshot]: Unable to get key window") } diff --git a/Sources/Integration/Session/Session.swift b/Sources/Integration/Session/Session.swift index fb14ed89..1dec54de 100644 --- a/Sources/Integration/Session/Session.swift +++ b/Sources/Integration/Session/Session.swift @@ -442,6 +442,7 @@ public final class Session: SessionType { client.messages .sink { [unowned self] in if var contact = try? dbManager.fetchContacts(.init(id: [$0.senderId])).first { + guard contact.isBanned == false else { return } contact.isRecent = false _ = try? dbManager.saveContact(contact) } diff --git a/Sources/LaunchFeature/LaunchViewModel.swift b/Sources/LaunchFeature/LaunchViewModel.swift index 5d85f07c..0f0f7619 100644 --- a/Sources/LaunchFeature/LaunchViewModel.swift +++ b/Sources/LaunchFeature/LaunchViewModel.swift @@ -87,8 +87,17 @@ final class LaunchViewModel { do { network.writeLogs() - // TODO: Retry inifitely if fails - let _ = try await fetchBannedList() + let bannedUids = try await fetchBannedList() // TODO: Retry inifitely if fails + + if let database = try? DependencyInjection.Container.shared.resolve() as Database { + if let bannedContacts = try? database.fetchContacts(Contact.Query(id: bannedUids, isBanned: false)) { + bannedContacts.forEach { + var contact = $0 + contact.isBanned = true + _ = try? database.saveContact(contact) + } + } + } network.updateNDF { [weak self] in guard let self = self else { return } @@ -211,16 +220,30 @@ final class LaunchViewModel { } } - private func fetchBannedList() async throws -> Data { - let url = URL(string: "https://elixxir-bins.s3.us-west-1.amazonaws.com/client/bannedUsers/banned.csv") + private func fetchBannedList() async throws -> Set<Data> { + let url = URL(string: "https://elixxir-bins.s3.us-west-1.amazonaws.com/client/bannedUsers/bannedTesting.csv") return try await withCheckedThrowingContinuation { continuation in URLSession.shared.dataTask(with: url!) { data, _, error in if let error = error { return continuation.resume(throwing: error) } - guard let data = data else { fatalError("?") } - return continuation.resume(returning: data) + guard let data = data else { + fatalError("No data was downloaded as banned users csv") + } + + guard let csvRaw = String(data: data, encoding: .utf8), + let csv = try? CSV<Enumerated>(string: csvRaw) else { + fatalError("CSV content couldn't be parsed correctly") + } + + guard let bannedUids = csv.columns?[0].rows else { + fatalError("It wasn't possible to get CSV uid array") + } + + return continuation.resume(returning: Set<Data>( + bannedUids.compactMap { $0.data(using: .utf8) } + )) }.resume() } } diff --git a/Sources/PushFeature/PushHandler.swift b/Sources/PushFeature/PushHandler.swift index 2e6dd801..9afec926 100644 --- a/Sources/PushFeature/PushHandler.swift +++ b/Sources/PushFeature/PushHandler.swift @@ -96,13 +96,6 @@ public final class PushHandler: PushHandling { return } - guard let showSender = defaults.value(forKey: Constants.usernamesSetting) as? Bool, showSender == true else { - pushes.map { ($0.type.unknownSenderContent!, $0) } - .map(contentsBuilder.build) - .forEach { completion($0) } - return - } - let dbPath = FileManager.default .containerURL(forSecurityApplicationGroupIdentifier: "group.elixxir.messenger")! .appendingPathComponent("xxm_database") @@ -119,8 +112,12 @@ public final class PushHandler: PushHandling { return nil } - let name = (contact.nickname ?? contact.username) ?? "" - return ($0.type.knownSenderContent(name)!, $0) + if let showSender = defaults.value(forKey: Constants.usernamesSetting) as? Bool, showSender == true { + let name = (contact.nickname ?? contact.username) ?? "" + return ($0.type.knownSenderContent(name)!, $0) + } else { + return ($0.type.unknownSenderContent!, $0) + } } tuples -- GitLab