diff --git a/submodules/ChatListUI/Sources/ChatListSearchListPaneNode.swift b/submodules/ChatListUI/Sources/ChatListSearchListPaneNode.swift index cf24611bd4..ff248d5245 100644 --- a/submodules/ChatListUI/Sources/ChatListSearchListPaneNode.swift +++ b/submodules/ChatListUI/Sources/ChatListSearchListPaneNode.swift @@ -1690,7 +1690,7 @@ final class ChatListSearchListPaneNode: ASDisplayNode, ChatListSearchPaneNode { }) let foundThreads: Signal<[EngineChatList.Item], NoError> - if case .forum = location, (key == .topics || key == .chats) { + if case let .forum(peerId) = location, (key == .topics || key == .chats) { foundThreads = chatListViewForLocation(chatListLocation: location, location: .initial(count: 1000, filter: nil), account: context.account) |> map { view -> [EngineChatList.Item] in var filteredItems: [EngineChatList.Item] = [] @@ -1708,6 +1708,24 @@ final class ChatListSearchListPaneNode: ASDisplayNode, ChatListSearchPaneNode { return filteredItems } + |> mapToSignal { local -> Signal<[EngineChatList.Item], NoError> in + return .single(local) + |> then(context.engine.messages.searchForumTopics(peerId: peerId, query: finalQuery) + |> map { remoteResult in + var mergedResult = local + for item in remoteResult { + guard case let .forum(threadId) = item.id else { + continue + } + if !mergedResult.contains(where: { $0.id == .forum(threadId) }) { + mergedResult.append(item) + } + } + + return mergedResult + }) + } + |> distinctUntilChanged } else { foundThreads = .single([]) } diff --git a/submodules/TelegramCore/Sources/ForumChannels.swift b/submodules/TelegramCore/Sources/ForumChannels.swift index 83d83a290c..db874759a9 100644 --- a/submodules/TelegramCore/Sources/ForumChannels.swift +++ b/submodules/TelegramCore/Sources/ForumChannels.swift @@ -570,7 +570,7 @@ enum LoadMessageHistoryThreadsError { case generic } -func _internal_requestMessageHistoryThreads(accountPeerId: PeerId, postbox: Postbox, network: Network, peerId: PeerId, offsetIndex: StoredPeerThreadCombinedState.Index?, limit: Int) -> Signal { +func _internal_requestMessageHistoryThreads(accountPeerId: PeerId, postbox: Postbox, network: Network, peerId: PeerId, query: String?, offsetIndex: StoredPeerThreadCombinedState.Index?, limit: Int) -> Signal { let signal: Signal = postbox.transaction { transaction -> Api.InputChannel? in guard let channel = transaction.getPeer(peerId) as? TelegramChannel else { return nil @@ -585,7 +585,12 @@ func _internal_requestMessageHistoryThreads(accountPeerId: PeerId, postbox: Post guard let inputChannel = inputChannel else { return .fail(.generic) } - let flags: Int32 = 0 + var flags: Int32 = 0 + + if query != nil { + flags |= 1 << 0 + } + var offsetDate: Int32 = 0 var offsetId: Int32 = 0 var offsetTopic: Int32 = 0 @@ -597,7 +602,7 @@ func _internal_requestMessageHistoryThreads(accountPeerId: PeerId, postbox: Post let signal: Signal = network.request(Api.functions.channels.getForumTopics( flags: flags, channel: inputChannel, - q: nil, + q: query, offsetDate: offsetDate, offsetId: offsetId, offsetTopic: offsetTopic, @@ -839,6 +844,59 @@ func _internal_forumChannelTopicNotificationExceptions(account: Account, id: Eng } } +public func _internal_searchForumTopics(account: Account, peerId: EnginePeer.Id, query: String) -> Signal<[EngineChatList.Item], NoError> { + return _internal_requestMessageHistoryThreads(accountPeerId: account.peerId, postbox: account.postbox, network: account.network, peerId: peerId, query: query, offsetIndex: nil, limit: 100) + |> map(Optional.init) + |> `catch` { _ -> Signal in + return .single(nil) + } + |> mapToSignal { result -> Signal<[EngineChatList.Item], NoError> in + guard let result else { + return .single([]) + } + return account.postbox.transaction { transcation -> [EngineChatList.Item] in + guard let peer = transcation.getPeer(peerId) else { + return [] + } + + var items: [EngineChatList.Item] = [] + for item in result.items { + guard let index = item.index else { + continue + } + items.append(EngineChatList.Item( + id: .forum(item.threadId), + index: .forum(pinnedIndex: .none, timestamp: index.timestamp, threadId: index.threadId, namespace: Namespaces.Message.Cloud, id: index.messageId), + messages: [], + readCounters: nil, + isMuted: false, + draft: nil, + threadData: item.data, + renderedPeer: EngineRenderedPeer(peer: EnginePeer(peer)), + presence: nil, + hasUnseenMentions: false, + hasUnseenReactions: false, + forumTopicData: EngineChatList.ForumTopicData( + id: item.threadId, + title: item.data.info.title, + iconFileId: item.data.info.icon, + iconColor: item.data.info.iconColor, + maxOutgoingReadMessageId: EngineMessage.Id(peerId: peerId, namespace: Namespaces.Message.Cloud, id: item.data.maxOutgoingReadId), + isUnread: false + ), + topForumTopicItems: [], + hasFailed: false, + isContact: false, + autoremoveTimeout: nil, + storyStats: nil + )) + } + + return items + } + } +} + public final class ForumChannelTopics { private final class Impl { private let queue: Queue @@ -859,8 +917,6 @@ public final class ForumChannelTopics { self.account = account self.peerId = peerId - //let _ = _internal_loadMessageHistoryThreads(account: self.account, peerId: peerId, offsetIndex: nil, limit: 100).start() - self.updateDisposable.set(account.viewTracker.polledChannel(peerId: peerId).start()) } diff --git a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift index c3f96a731f..6a49ad4c1e 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift @@ -2833,7 +2833,7 @@ func resetChannels(accountPeerId: PeerId, postbox: Postbox, network: Network, pe var resetTopicsSignals: [Signal] = [] for resetForumTopicPeerId in resetForumTopics { - resetTopicsSignals.append(_internal_requestMessageHistoryThreads(accountPeerId: accountPeerId, postbox: postbox, network: network, peerId: resetForumTopicPeerId, offsetIndex: nil, limit: 20) + resetTopicsSignals.append(_internal_requestMessageHistoryThreads(accountPeerId: accountPeerId, postbox: postbox, network: network, peerId: resetForumTopicPeerId, query: nil, offsetIndex: nil, limit: 20) |> map(StateResetForumTopics.result) |> `catch` { _ -> Signal in return .single(.error(resetForumTopicPeerId)) @@ -3113,7 +3113,7 @@ private func pollChannel(accountPeerId: PeerId, postbox: Postbox, network: Netwo var resetTopicsSignals: [Signal] = [] for resetForumTopicPeerId in resetForumTopics { - resetTopicsSignals.append(_internal_requestMessageHistoryThreads(accountPeerId: accountPeerId, postbox: postbox, network: network, peerId: resetForumTopicPeerId, offsetIndex: nil, limit: 20) + resetTopicsSignals.append(_internal_requestMessageHistoryThreads(accountPeerId: accountPeerId, postbox: postbox, network: network, peerId: resetForumTopicPeerId, query: nil, offsetIndex: nil, limit: 20) |> map(StateResetForumTopics.result) |> `catch` { _ -> Signal in return .single(.error(resetForumTopicPeerId)) diff --git a/submodules/TelegramCore/Sources/State/ManagedChatListHoles.swift b/submodules/TelegramCore/Sources/State/ManagedChatListHoles.swift index 8f115d7816..84ade74719 100644 --- a/submodules/TelegramCore/Sources/State/ManagedChatListHoles.swift +++ b/submodules/TelegramCore/Sources/State/ManagedChatListHoles.swift @@ -143,7 +143,7 @@ func managedForumTopicListHoles(network: Network, postbox: Postbox, accountPeerI } for (entry, disposable) in added { - disposable.set((_internal_requestMessageHistoryThreads(accountPeerId: accountPeerId, postbox: postbox, network: network, peerId: entry.peerId, offsetIndex: entry.index, limit: 100) + disposable.set((_internal_requestMessageHistoryThreads(accountPeerId: accountPeerId, postbox: postbox, network: network, peerId: entry.peerId, query: nil, offsetIndex: entry.index, limit: 100) |> mapToSignal { result -> Signal in return postbox.transaction { transaction in return applyLoadMessageHistoryThreadsResults(accountPeerId: accountPeerId, transaction: transaction, results: [result]) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift index 7c02bb0c4f..2d0215fd49 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift @@ -637,6 +637,10 @@ public extension TelegramEngine { |> ignoreValues } + public func searchForumTopics(peerId: EnginePeer.Id, query: String) -> Signal<[EngineChatList.Item], NoError> { + return _internal_searchForumTopics(account: self.account, peerId: peerId, query: query) + } + public func debugAddHoles() -> Signal { return self.account.postbox.transaction { transaction -> Void in transaction.addHolesEverywhere(peerNamespaces: [Namespaces.Peer.CloudUser, Namespaces.Peer.CloudGroup, Namespaces.Peer.CloudChannel], holeNamespace: Namespaces.Message.Cloud)