famedly / matrix-dart-sdk

Matrix SDK written in pure Dart.
GNU Affero General Public License v3.0
60 stars 31 forks source link

Add ability to retrieve attachments from a room #1838

Closed gustavo434 closed 3 months ago

gustavo434 commented 4 months ago

Preflight Checklist

Describe your problem

I'm currently having a hard time figuring out a good solution to retrieve the attachments of a room without having to pull every single event that's not an attachment in the process.

Describe your ideal solution

It would be ideal if we could get a new function similar to getTimeline but for attachments only.

Another possible solution would be to let us pass an optional List<MessageTypes>? messageTypesFilter as a parameter on getTimeline so that we have the option to receive a filtered timeline.

In my case I would call:

    final timeline = await matrixRoom?.getTimeline(
      onUpdate: () async {...},
      messageTypesFilter: [
        MessageTypes.Image,
        MessageTypes.Video,
        MessageTypes.Audio,
        MessageTypes.File,
      ],
    );

Version

No response

Security requirements

No response

Additional Context

No response

nico-famedly commented 3 months ago

You can currently do:

     return timeline.startSearch(
       requestHistoryCount: 1000,
       limit: 32,
       searchFunc: (Event event) => event.messageType == MessageTypes.Image,
     );

Is that sufficient for your usecase? (You obviously can extend that filter to more message types)

gustavo434 commented 3 months ago

Thank you for your response. The approach using timeline.startSearch with a custom filter is helpful, but it doesn't fully address the core issue I mentioned.

If I understand correctly, this method involves fetching every message in the room and then filtering based on the searchFunc. This can be quite resource-intensive in rooms with thousands of messages, especially when we only need a specific subset of those messages (attachments). This approach can significantly impact performance, particularly on mobile devices.

Implementing a way to directly retrieve only certain message types would greatly enhance performance. This is why a messageTypesFilter parameter within the getTimeline function would be highly beneficial, as it would allow us to pull only the necessary messages from the start.

Thanks for your support!

krille-chan commented 3 months ago

Hello, I think the Timeline.startSearch method is the only way we can do this in an environment with encrypted messages.

For unencrypted rooms, there is this API endpoint: https://spec.matrix.org/v1.10/client-server-api/#get_matrixclientv3roomsroomidmessages but it looks like the RoomEventFilter has no way to filter for msgtype so this is not gonna help. So afaik Matrix just does not offer a better solution yet 😟

Closing this as this is a question and not an issue

nico-famedly commented 3 months ago

(The roomfilter can filter by contains_url, which often is equivalent in unencrypted rooms)