From ab1120d3f73dbd99adc0d0a48c31b02d73873f70 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Tue, 5 Oct 2021 17:54:26 +0400 Subject: [PATCH] Update API --- .../Peers/InvitationLinks.swift | 28 +++++++++++++------ .../TelegramEngine/Peers/JoinLink.swift | 11 ++++++-- .../Peers/TelegramEnginePeers.swift | 4 --- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/InvitationLinks.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/InvitationLinks.swift index 86ae48f477..1f909313f0 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/InvitationLinks.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/InvitationLinks.swift @@ -4,7 +4,7 @@ import SwiftSignalKit import TelegramApi import MtProtoKit -func _internal_updateInvitationRequest(account: Account, peerId: PeerId, userId: PeerId, approve: Bool) -> Signal { +private func _internal_updateInvitationRequest(account: Account, peerId: PeerId, userId: PeerId, approve: Bool) -> Signal { return account.postbox.transaction { transaction -> Signal in if let peer = transaction.getPeer(peerId), let user = transaction.getPeer(userId), let inputPeer = apiInputPeer(peer), let inputUser = apiInputUser(user) { var flags: Int32 = 0 @@ -716,6 +716,7 @@ private final class PeerInvitationImportersContextImpl { private let link: String? private let disposable = MetaDisposable() private let updateDisposable = MetaDisposable() + private let actionDisposables = DisposableSet() private var isLoadingMore: Bool = false private var hasLoadedOnce: Bool = false private var canLoadMore: Bool = true @@ -736,9 +737,9 @@ private final class PeerInvitationImportersContextImpl { self.count = count self.isLoadingMore = true - self.disposable.set((account.postbox.transaction { transaction -> (peers: [PeerInvitationImportersState.Importer], canLoadMore: Bool)? in + self.disposable.set((account.postbox.transaction { transaction -> (peers: [PeerInvitationImportersState.Importer], count: Int32, canLoadMore: Bool)? in let cachedResult = transaction.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedPeerInvitationImporters, key: CachedPeerInvitationImporters.key(peerId: peerId, link: invite?.link ?? "requests")))?.get(CachedPeerInvitationImporters.self) - if let cachedResult = cachedResult, Int(cachedResult.count) == count { + if let cachedResult = cachedResult, (Int(cachedResult.count) == count || invite == nil) { var result: [PeerInvitationImportersState.Importer] = [] for peerId in cachedResult.peerIds { if let peer = transaction.getPeer(peerId), let date = cachedResult.dates[peerId] { @@ -747,18 +748,19 @@ private final class PeerInvitationImportersContextImpl { return nil } } - return (result, Int(cachedResult.count) > result.count) + return (result, cachedResult.count, Int(cachedResult.count) > result.count || invite == nil) } else { return nil } } - |> deliverOn(self.queue)).start(next: { [weak self] cachedPeersAndCanLoadMore in + |> deliverOn(self.queue)).start(next: { [weak self] cachedPeersCountAndCanLoadMore in guard let strongSelf = self else { return } strongSelf.isLoadingMore = false - if let (cachedPeers, canLoadMore) = cachedPeersAndCanLoadMore { + if let (cachedPeers, cachedCount, canLoadMore) = cachedPeersCountAndCanLoadMore { strongSelf.results = cachedPeers + strongSelf.count = cachedCount strongSelf.hasLoadedOnce = true strongSelf.canLoadMore = canLoadMore strongSelf.loadedFromCache = true @@ -772,6 +774,7 @@ private final class PeerInvitationImportersContextImpl { deinit { self.disposable.dispose() self.updateDisposable.dispose() + self.actionDisposables.dispose() } func loadMore() { @@ -881,7 +884,9 @@ private final class PeerInvitationImportersContextImpl { self.updateState() } - func remove(_ peerId: EnginePeer.Id) { + func update(_ peerId: EnginePeer.Id, action: PeerInvitationImportersContext.UpdateAction) { + self.actionDisposables.add(_internal_updateInvitationRequest(account: self.account, peerId: self.peerId, userId: peerId, approve: action == .approve).start()) + var results = self.results results.removeAll(where: { $0.peer.peerId == peerId}) self.results = results @@ -911,6 +916,11 @@ private final class PeerInvitationImportersContextImpl { } public final class PeerInvitationImportersContext { + public enum UpdateAction { + case approve + case deny + } + private let queue: Queue = Queue() private let impl: QueueLocalObject @@ -939,9 +949,9 @@ public final class PeerInvitationImportersContext { } } - public func remove(_ peerId: EnginePeer.Id) { + public func update(_ peerId: EnginePeer.Id, action: UpdateAction) { self.impl.with { impl in - impl.remove(peerId) + impl.update(peerId, action: action) } } } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift index 6f6fd69ab1..f5b31d4a67 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/JoinLink.swift @@ -22,7 +22,8 @@ func apiUpdatesGroups(_ updates: Api.Updates) -> [Api.Chat] { } public enum ExternalJoiningChatState { - case invite(title: String, about: String?, photoRepresentation: TelegramMediaImageRepresentation?, participantsCount: Int32, participants: [Peer]?) + case invite(title: String, photoRepresentation: TelegramMediaImageRepresentation?, participantsCount: Int32, participants: [Peer]?) + case request(title: String, about: String?, photoRepresentation: TelegramMediaImageRepresentation?, participantsCount: Int32, isGroup: Bool) case alreadyJoined(PeerId) case invalidHash case peek(PeerId, Int32) @@ -67,9 +68,13 @@ func _internal_joinLinkInformation(_ hash: String, account: Account) -> Signal mapToSignal { (result) -> Signal in if let result = result { switch result { - case let .chatInvite(_, title, about, invitePhoto, participantsCount, participants): + case let .chatInvite(flags, title, about, invitePhoto, participantsCount, participants): let photo = telegramMediaImageFromApiPhoto(invitePhoto).flatMap({ smallestImageRepresentation($0.representations) }) - return .single(.invite(title: title, about: about, photoRepresentation: photo, participantsCount: participantsCount, participants: participants?.map({TelegramUser(user: $0)}))) + if (flags & (1 << 6)) != 0 { + return .single(.request(title: title, about: about, photoRepresentation: photo, participantsCount: participantsCount, isGroup: (flags & (1 << 1)) == 0)) + } else { + return .single(.invite(title: title, photoRepresentation: photo, participantsCount: participantsCount, participants: participants?.map({TelegramUser(user: $0)}))) + } case let .chatInviteAlready(chat): if let peer = parseTelegramGroupOrChannel(chat: chat) { return account.postbox.transaction({ (transaction) -> ExternalJoiningChatState in diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index 8623f813b7..a9bc529b64 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -450,10 +450,6 @@ public extension TelegramEngine { return PeerExportedInvitationsContext(account: self.account, peerId: peerId, adminId: adminId, revoked: revoked, forceUpdate: forceUpdate) } - public func updateInvitationRequest(peerId: PeerId, userId: PeerId, approve: Bool) -> Signal { - return _internal_updateInvitationRequest(account: self.account, peerId: peerId, userId: userId, approve: approve) - } - public func revokePersistentPeerExportedInvitation(peerId: PeerId) -> Signal { return _internal_revokePersistentPeerExportedInvitation(account: self.account, peerId: peerId) }