stream-chat-swift icon indicating copy to clipboard operation
stream-chat-swift copied to clipboard

Is it possible to search for messages by ids and get results in the same order as input array?

Open vadimue opened this issue 3 years ago • 6 comments

What did you do?

Fetching messages with ChatMessageSearchController and providing message ids array as an input argument of MessageSearchQuery.

What did you expect to happen?

I want to get the result of search which contains all messages with provided ids and get these messages in the same order as the input array.

What happened instead?

I get the correct response which contains all messages but they are in random order. Regardless MessageSearchSortingKey (relevance, id, createdAt, updatedAt). If I use id as a sorting option StreamSDK literally order the array alphabetically by id, which is not I want to achive.

GetStream Environment

GetStream Chat version: 4.16.0 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: iOS 15.2 Swift version: Swift 5 Xcode version: Xcode 13.2.1 Device: iPhone 13 Simulator

Additional context

import Foundation
import Resolver
import StreamChat

final class FeedPresenter {
    weak var view: FeedViewInterface?
    weak var delegate: FeedSceneDelegate?
    
    @Injected var chatClient: ChatClient
    @KeychainStorage(key: KeychainKeys.user) private var user: UserProfile?
    
    lazy var messageSearchcontroller: ChatMessageSearchController = chatClient.messageSearchController()
    
    func query(messageIds: [String]) -> MessageSearchQuery {
        let messageFilter: Filter<MessageSearchFilterScope> = .in("id", values: messageIds)
        let sort: Sorting<MessageSearchSortingKey> = .init(key: .id)
        let query = MessageSearchQuery(channelFilter: .nonEmpty, messageFilter: messageFilter, sort: [sort])
        return query
    }
}

// MARK: - FeedPresentation
extension FeedPresenter: FeedPresentation {
    func viewDidLoad() {
        view?.showLoading()
        performPostsFetchRequest()
    }
}

private extension FeedPresenter {
    func performPostsFetchRequest() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
            self?.view?.hideLoading()
            self?.handlePostsResponse()
        }
    }
    
    func handlePostsResponse() {
        let messageIds = [
            "274CB269-405C-481B-A32B-A5503B677114", // Hello
            "9FB5003B-E617-4F2A-A199-3993C3441556", // Perfect
            "A929D10E-601E-452F-98DA-A6228CB1B959", // Hi there
            "6BFDA015-DB47-4C7C-BB06-FCD7F25F677D", // Waterfall
            "3CF45739-1534-4B13-AB80-8FD96F50582C", // Squirrel
            "6672CE6C-78B8-4F5A-B98F-36B4D063E6D4", // hdhdhd
            "3BD54314-6705-441E-A74B-FF352ED9AECA", // Wow
            "11C762BA-2EBA-4138-9B1C-39CA6D44D6EC", // hi
            "135BD42E-319F-4835-99A0-5F79C3062D97", // I like mexican food :)
            "7367631A-6AC7-457C-8DEB-ED1981B2C870", // Eugene
            "3E64EAF2-8DCC-4A68-B304-2D8152ECC27F", // 9 photos
        ]
        messageSearchcontroller.delegate = self
        messageSearchcontroller.search(query: query(messageIds: messageIds)) { error in
            dump(self.messageSearchcontroller.messages.map { "\($0.id) \($0.text) - \($0.cid!)" })
        }
    }
}

extension FeedPresenter: ChatMessageSearchControllerDelegate {
    func controller(_ controller: ChatMessageSearchController, didChangeMessages changes: [ListChange<ChatMessage>]) {
        print("messages start updates \(changes)")
        view?.updateMessages(with: changes) {
            print("messages updated")
        }
    }
}

Default sort option: Screenshot 2022-06-18 at 11 42 10 Sort option by id. As you can see it sort results alphabetically: image

vadimue avatar Jun 18 '22 08:06 vadimue

Hi @vadimue,

thanks for the thorough report. We will review this request with the team and come back to you soon.

bielikb avatar Jun 20 '22 14:06 bielikb

Hi @vadimue,

could you please better describe your use case from the UX point of view of your app? Thanks

bielikb avatar Jun 23 '22 10:06 bielikb

Hi @bielikb, Thanks for taking my question. I'm trying to build Feed functionality which is actually a list of messages from different channels. My own backend sends a list of ids and I need to request messages via Stream SDK.

What I'm planning right now is to split list of ids into pages, request messages by pages, sort them by the specific rule I described here, and then build UI. So I lose part of build-in functionality of SDK which I need to cover myself.

vadimue avatar Jun 23 '22 12:06 vadimue

Thanks for the info @vadimue. I reached out to our backend team.

We will come back to you as soon as we get updates on this.

Best, Boris

bielikb avatar Jun 23 '22 13:06 bielikb

Hi @vadimue,

I talked to our backend team. We will have this functionality ready around mid of July.

Ill update this issue once the functionality is in place. Thanks for understanding.

Best, Boris.

bielikb avatar Jun 27 '22 15:06 bielikb

Thanks for the update 👍

vadimue avatar Jun 29 '22 06:06 vadimue

Hi @vadimue , sorry for not coming back to you on this, we completely forgot. This is live since July 7th 😄.

polqf avatar Sep 29 '22 09:09 polqf