mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-16 18:40:05 +00:00
109 lines
4.1 KiB
Swift
109 lines
4.1 KiB
Swift
#if os(macOS)
|
|
import PostboxMac
|
|
import SwiftSignalKitMac
|
|
#else
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
#endif
|
|
|
|
public func importContact(account: Account, firstName: String, lastName: String, phoneNumber: String) -> Signal<PeerId?, NoError> {
|
|
|
|
let input = Api.InputContact.inputPhoneContact(clientId: 1, phone: phoneNumber, firstName: firstName, lastName: lastName)
|
|
|
|
return account.network.request(Api.functions.contacts.importContacts(contacts: [input]))
|
|
|> map(Optional.init)
|
|
|> `catch` { _ -> Signal<Api.contacts.ImportedContacts?, NoError> in
|
|
return .single(nil)
|
|
}
|
|
|> mapToSignal { result -> Signal<PeerId?, NoError> in
|
|
return account.postbox.transaction { transaction -> PeerId? in
|
|
if let result = result {
|
|
switch result {
|
|
case let .importedContacts(_, _, _, users):
|
|
if let first = users.first {
|
|
let user = TelegramUser(user: first)
|
|
let peerId = user.id
|
|
updatePeers(transaction: transaction, peers: [user], update: { _, updated in
|
|
return updated
|
|
})
|
|
var peerIds = transaction.getContactPeerIds()
|
|
if !peerIds.contains(peerId) {
|
|
peerIds.insert(peerId)
|
|
transaction.replaceContactPeerIds(peerIds)
|
|
}
|
|
return peerId
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum AddContactError {
|
|
case generic
|
|
}
|
|
|
|
public func addContactInteractively(account: Account, peer: Peer, firstName: String, lastName: String, phoneNumber: String) -> Signal<Never, AddContactError> {
|
|
guard let inputUser = apiInputUser(peer) else {
|
|
return .fail(.generic)
|
|
}
|
|
return account.network.request(Api.functions.contacts.addContact(id: inputUser, firstName: firstName, lastName: lastName, phone: phoneNumber))
|
|
|> mapError { _ -> AddContactError in
|
|
return .generic
|
|
}
|
|
|> mapToSignal { result -> Signal<Never, AddContactError> in
|
|
return account.postbox.transaction { transaction -> Void in
|
|
var peers: [Peer] = []
|
|
switch result {
|
|
case let .updates(_, users, _, _, _):
|
|
for user in users {
|
|
peers.append(TelegramUser(user: user))
|
|
}
|
|
case let .updatesCombined(_, users, _, _, _, _):
|
|
for user in users {
|
|
peers.append(TelegramUser(user: user))
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
updatePeers(transaction: transaction, peers: peers, update: { _, updated in
|
|
return updated
|
|
})
|
|
var peerIds = transaction.getContactPeerIds()
|
|
if !peerIds.contains(peer.id) {
|
|
peerIds.insert(peer.id)
|
|
transaction.replaceContactPeerIds(peerIds)
|
|
}
|
|
|
|
account.stateManager.addUpdates(result)
|
|
}
|
|
|> introduceError(AddContactError.self)
|
|
|> ignoreValues
|
|
}
|
|
}
|
|
|
|
public enum AcceptAndShareContactError {
|
|
case generic
|
|
}
|
|
|
|
public func acceptAndShareContact(account: Account, peerId: PeerId) -> Signal<Never, AcceptAndShareContactError> {
|
|
return account.postbox.transaction { transaction -> Api.InputUser? in
|
|
return transaction.getPeer(peerId).flatMap(apiInputUser)
|
|
}
|
|
|> introduceError(AcceptAndShareContactError.self)
|
|
|> mapToSignal { inputUser -> Signal<Never, AcceptAndShareContactError> in
|
|
guard let inputUser = inputUser else {
|
|
return .fail(.generic)
|
|
}
|
|
return account.network.request(Api.functions.contacts.acceptContact(id: inputUser))
|
|
|> mapError { _ -> AcceptAndShareContactError in
|
|
return .generic
|
|
}
|
|
|> mapToSignal { updates -> Signal<Never, AcceptAndShareContactError> in
|
|
account.stateManager.addUpdates(updates)
|
|
return .complete()
|
|
}
|
|
}
|
|
}
|