From 1f834bd7109dc3987202b6c5779142fc7d4f3eda Mon Sep 17 00:00:00 2001 From: Bruno Muniz Azevedo Filho <bruno@elixxir.io> Date: Wed, 22 Jun 2022 01:43:48 -0300 Subject: [PATCH] Cleaned all integration except FT and mocks --- Sources/Integration/Extensions.swift | 46 ++-- .../Implementations/GroupManager.swift | 17 +- Sources/Integration/Listeners.swift | 10 +- .../Integration/Session/Session+Group.swift | 220 +++++++++--------- Sources/Integration/Session/SessionType.swift | 1 - 5 files changed, 148 insertions(+), 146 deletions(-) diff --git a/Sources/Integration/Extensions.swift b/Sources/Integration/Extensions.swift index 74835b71..6fd221f6 100644 --- a/Sources/Integration/Extensions.swift +++ b/Sources/Integration/Extensions.swift @@ -5,16 +5,16 @@ import Bindings extension Contact { init(with contact: BindingsContact, status: Contact.AuthStatus) { self.init( - photo: nil, - userId: contact.getID()!, - email: contact.retrieve(fact: .email), - phone: contact.retrieve(fact: .phone), - status: status, + id: contact.getID()!, marshaled: try! contact.marshal(), username: contact.retrieve(fact: .username) ?? "", + email: contact.retrieve(fact: .email), + phone: contact.retrieve(fact: .phone), nickname: nil, - createdAt: Date(), - isRecent: false + photo: nil, + authStatus: status, + isRecent: false, + createdAt: Date() ) } } @@ -24,14 +24,17 @@ extension Message { guard let payload = try? Payload(with: message.getPayload()!) else { fatalError() } self.init( - sender: message.getSender()!, - receiver: meMarshalled, - payload: payload, - unread: true, - timestamp: Int(message.getTimestampNano()), - uniqueId: message.getID()!, + networkId: message.getID()!, + senderId: message.getSender()!, + recipientId: meMarshalled, + groupId: nil, + date: Date.fromTimestamp(Int(message.getTimestampNano())), status: .received, - roundURL: message.getRoundURL() + isUnread: true, + text: payload.text, + replyMessageId: payload.reply?.messageId, + roundURL: message.getRoundURL(), + fileTransferId: nil ) } @@ -39,14 +42,17 @@ extension Message { guard let payload = try? Payload(with: message.getPayload()!) else { fatalError() } self.init( - sender: message.getSenderID()!, + networkId: message.getMessageID()!, + senderId: message.getSenderID()!, + recipientId: nil, groupId: message.getGroupID()!, - payload: payload, - unread: true, - timestamp: Int(message.getTimestampNano()), - uniqueId: message.getMessageID()!, + date: Date.fromTimestamp(Int(message.getTimestampNano())), status: .received, - roundURL: message.getRoundURL() + isUnread: true, + text: payload.text, + replyMessageId: payload.reply?.messageId, + roundURL: message.getRoundURL(), + fileTransferId: nil ) } } diff --git a/Sources/Integration/Implementations/GroupManager.swift b/Sources/Integration/Implementations/GroupManager.swift index 1fcdbccf..4b3dc4ef 100644 --- a/Sources/Integration/Implementations/GroupManager.swift +++ b/Sources/Integration/Implementations/GroupManager.swift @@ -68,15 +68,14 @@ extension BindingsGroupChat: GroupManagerInterface { return } - completion(.success( - .init( - leader: me, - name: name, - groupId: group.getID()!, - status: .participating, - createdAt: Date(), - serialize: group.serialize()! - ))) + completion(.success(.init( + id: group.getID()!, + name: name, + leaderId: me, + createdAt: Date(), + authStatus: .participating, + serialized: group.serialize()! + ))) return default: break diff --git a/Sources/Integration/Listeners.swift b/Sources/Integration/Listeners.swift index 3859566e..9ade68b4 100644 --- a/Sources/Integration/Listeners.swift +++ b/Sources/Integration/Listeners.swift @@ -133,16 +133,16 @@ public extension BindingsClient { } groupRequests(.init( - leader: members.first!, + id: id, name: String(data: name, encoding: .utf8)!, - groupId: id, - status: .pending, + leaderId: members.first!, createdAt: Date(), - serialize: serialize + authStatus: .pending, + serialized: serialize ), members, welcomeMessage) } - let messageCallback = GroupMessageCallback { groupMessages(GroupMessage(with: $0)) } + let messageCallback = GroupMessageCallback { groupMessages(Message(with: $0)) } let groupManager = BindingsNewGroupManager(self, requestCallback, messageCallback, &error) guard let error = error else { return groupManager } diff --git a/Sources/Integration/Session/Session+Group.swift b/Sources/Integration/Session/Session+Group.swift index 7829a545..ced2ada3 100644 --- a/Sources/Integration/Session/Session+Group.swift +++ b/Sources/Integration/Session/Session+Group.swift @@ -42,56 +42,59 @@ extension Session { @discardableResult func processGroupCreation(_ group: Group, memberIds: [Data], welcome: String?) -> [GroupMember] { - try! dbManager.saveGroup(group) - - var members: [GroupMember] = [] - - if let contactsOnGroup: [Contact] = try? dbManager.fetch(.withUserIds(memberIds)) { - contactsOnGroup.forEach { members.append(GroupMember(contact: $0, group: group)) } - } - - let strangersOnGroup = memberIds - .filter { !members.map { $0.contactId }.contains($0) } - .filter { $0 != client.bindings.myId } - - if !strangersOnGroup.isEmpty { - for stranger in strangersOnGroup.enumerated() { - members.append(GroupMember( - userId: stranger.element, - groupId: group.groupId, - status: .pendingUsername, - username: "Fetching username...", - photo: nil - )) - } - } - - members.forEach { try! dbManager.saveGroupMember($0) } - - if group.leaderId != client.bindings.meMarshalled, inappnotifications { - DeviceFeedback.sound(.contactAdded) - DeviceFeedback.shake(.notification) - } - - scanStrangers {} - - if let welcome = welcome { - _ = try? dbManager.saveMessage(.init( - networkId: nil, - senderId: group.leaderId, - recipientId: client.bindings.meMarshalled, - groupId: group.id, - date: Date(), - status: .received, - isUnread: true, - text: welcome, - replyMessageId: nil, - roundURL: nil, - fileTransferId: nil - )) - } - - return members + // TODO: Implement this checking on which members of the group are my members etc. + +// try! dbManager.saveGroup(group) +// +// var members: [GroupMember] = [] +// +// if let contactsOnGroup: [Contact] = try? dbManager.fetchContacts(.init(id: Set(memberIds)) { +// //contactsOnGroup.forEach { members.append(GroupMember(contact: $0, group: group)) } +// } +// +// let strangersOnGroup = memberIds +// .filter { !members.map { $0.contactId }.contains($0) } +// .filter { $0 != client.bindings.myId } +// +// if !strangersOnGroup.isEmpty { +// for stranger in strangersOnGroup.enumerated() { +// members.append(GroupMember( +// userId: stranger.element, +// groupId: group.groupId, +// status: .pendingUsername, +// username: "Fetching username...", +// photo: nil +// )) +// } +// } +// +// members.forEach { try! dbManager.saveGroupMember($0) } +// +// if group.leaderId != client.bindings.meMarshalled, inappnotifications { +// DeviceFeedback.sound(.contactAdded) +// DeviceFeedback.shake(.notification) +// } +// +// scanStrangers {} +// +// if let welcome = welcome { +// _ = try? dbManager.saveMessage(.init( +// networkId: nil, +// senderId: group.leaderId, +// recipientId: client.bindings.meMarshalled, +// groupId: group.id, +// date: Date(), +// status: .received, +// isUnread: true, +// text: welcome, +// replyMessageId: nil, +// roundURL: nil, +// fileTransferId: nil +// )) +// } +// +// return members + fatalError() } } @@ -100,13 +103,16 @@ extension Session { extension Session { public func send(_ payload: Payload, toGroup group: Group) { var message = Message( - sender: client.bindings.meMarshalled, - groupId: group.groupId, - payload: payload, - unread: false, - timestamp: Date.asTimestamp, - uniqueId: nil, - status: .sending + senderId: client.bindings.meMarshalled, + recipientId: nil, + groupId: group.id, + date: Date(), + status: .sending, + isUnread: false, + text: payload.text, + replyMessageId: payload.reply?.messageId, + roundURL: nil, + fileTransferId: nil ) do { @@ -117,13 +123,6 @@ extension Session { } } - public func retryGroupMessage(_ id: Int64) { - guard var message: GroupMessage = try? dbManager.fetch(withId: id) else { return } - message.timestamp = Date.asTimestamp - message.status = .sending - send(groupMessage: try! dbManager.saveMessage(message)) - } - private func send(message: Message) { guard let manager = client.groupManager else { fatalError("A group manager was not created") } var message = message @@ -163,52 +162,51 @@ extension Session { } public func scanStrangers(_ completion: @escaping () -> Void) { - DispatchQueue.global().async { [weak self] in - guard let self = self, let ud = self.client.userDiscovery else { return } - - guard let strangers: [GroupMember] = try? self.dbManager.fetch(.strangers) else { - DispatchQueue.main.async { - completion() - } - - return - } - - let ids = strangers.map { $0.userId } - - var updatedStrangers: [GroupMember] = [] - - ud.lookup(idList: ids) { - switch $0 { - case .success(let contacts): - strangers.forEach { stranger in - if let found = contacts.first(where: { contact in contact.userId == stranger.userId }) { - var updatedStranger = stranger - updatedStranger.status = .usernameSet - updatedStranger.username = found.username - updatedStrangers.append(updatedStranger) - } - } - - DispatchQueue.main.async { - updatedStrangers.forEach { - do { - try self.dbManager.save($0) - } catch { - log(string: error.localizedDescription, type:.error) - } - } - - log(string: "Scanned unknown group members", type: .info) - completion() - } - case .failure(let error): - DispatchQueue.main.async { - log(string: error.localizedDescription, type: .error) - completion() - } - } - } - } + // TODO: How this will work? + +// DispatchQueue.global().async { [weak self] in +// guard let self = self, let ud = self.client.userDiscovery else { return } +// +// guard let strangers = try? self.dbManager.fetchContacts(.init(authStatus: [.stranger])), +// strangers.isEmpty == false else { +// DispatchQueue.main.async { completion() } +// return +// } +// +// let ids = strangers.map { $0.id } +// +// var updatedStrangers: [GroupMember] = [] +// +// ud.lookup(idList: ids) { +// switch $0 { +// case .success(let contacts): +// strangers.forEach { stranger in +// if let found = contacts.first(where: { contact in contact.id == stranger.id }) { +// var updatedStranger = stranger +// updatedStranger.username = found.username +// updatedStrangers.append(updatedStranger) +// } +// } +// +// DispatchQueue.main.async { +// updatedStrangers.forEach { +// do { +// try self.dbManager.saveContact($0) +// } catch { +// log(string: error.localizedDescription, type:.error) +// } +// } +// +// log(string: "Scanned unknown group members", type: .info) +// completion() +// } +// case .failure(let error): +// DispatchQueue.main.async { +// log(string: error.localizedDescription, type: .error) +// completion() +// } +// } +// } +// } } } diff --git a/Sources/Integration/Session/SessionType.swift b/Sources/Integration/Session/SessionType.swift index f2badbfa..b77fe940 100644 --- a/Sources/Integration/Session/SessionType.swift +++ b/Sources/Integration/Session/SessionType.swift @@ -40,7 +40,6 @@ public protocol SessionType { // Messages func retryMessage(_: Int64) - func retryGroupMessage(_: Int64) func send(_: Payload, toContact: Contact) // Contacts -- GitLab