GetStream / stream-chat-swift

💬 iOS Chat SDK in Swift - Build your own app chat experience for iOS using the official Stream Chat API
https://getstream.io/chat/sdk/ios/
Other
849 stars 204 forks source link

Question: Filtering Channel Messages #2061

Closed davefoxy closed 2 years ago

davefoxy commented 2 years ago

What are you trying to achieve?

I'm trying to filter a channel's message list to show only messages from a given user. Is this possible? I don't see anything in the docs but I do see something that seems relevant...

I see the following API when creating a channel controller: https://github.com/GetStream/stream-chat-swift/blob/develop/Sources/StreamChat/Controllers/ChannelController/ChannelController.swift#L20

I was a little confused why the method for creating a channel controller would take a channel list query. Would you be able to clarify why this is and what's possible when it comes to filtering messages?

In particular, in addition to the above requirement of "show only messages by user x", I'm also interested in filtering on a message's extraData.

If possible, how can you achieve this currently?

I'm not sure if it's possible or not.

What would be the better way?

A friendly message-filtering API.

GetStream Environment

GetStream Chat version: 4.15.1 GetStream Chat frameworks: StreamChatUI iOS version: All Swift version: All Xcode version: All Device: All

b-onc commented 2 years ago

Hey @davefoxy

ChannelController accepts ChannelListQuery for creation since Channels can be included in Channel Lists. If you pass ChannelListQuery Q when creating ChannelController CC which observes channel C, C will be included in Q

Regarding your filtering question, let me investigate and get back to you.

b-onc commented 2 years ago

Hey @davefoxy , sorry for delayed response.

I've looked into this and while it's logged as an improvement in our SDK (#1372), no work has been planned towards it, sadly. If you'd like to filter messages, you'd have to implement your own UI components, the built-in ones don't have support and doesn't seem like they will soon.

Is there anything else I can help you with?

davefoxy commented 2 years ago

Thanks @b-onc I don't mind implementing our own UI component for this but are you saying that that's currently possible? I don't see anything in the Stream SDK (not the UI one) such as the channel controller that allows fetching channel messages with queries applied to them.

b-onc commented 2 years ago

Hey @davefoxy ,

It's possible although it's tricky.

When you implement your own UI, you'll have to filter on 2 places:

  1. ChannelController.messages -> you'll most probably use this for numberOfItemsInSection, you have to filter out the messages you won't display. This "filtering" logic can create a bottleneck as the number of messages grows
  2. ChannelControllerDelegate.didUpdateMessages delegate callback -> you need to discard updates related to messages you're not displaying

Alternatively, you can fork the SDK and introduce a var messagesPredicate in ChatChannelController, which you can pass to messagesObserver in ChatChannelController.setMessagesObserver. Here's the diff of a quick hack, you'll most probably need to change this: messageFiltering.patch.zip

Keep in mind that if you go through the predicate route, you won't see performance issues but you won't be able to filter based on extraData since ExtraData is kept as Data in DB, you can't create predicates for them.

b-onc commented 2 years ago

Hey @davefoxy

Is there anything I can help you with?

davefoxy commented 2 years ago

@b-onc Thanks, I think we can close this now. I'm not quite sure which solution we'll go with but thanks for your suggestions and background into how things are working 🙇