Retrieve messages from mam for muc

I have a problem retrieving messages from mam for MUC which means I receive all the messages from mam even the messages before I joined the room. How can I prevent to retrieve receiving messages from server which were sent before joining? Here is the code for retrieving messages

val mamManager = MamManager.getInstanceFor(multiUserChat)
val mamQueryArgs: MamManager.MamQueryArgs =
val mamQuery = mamManager?.queryArchive(mamQueryArgs)

As I see, in the iOS version it works perfectly.
I am using smack 4.4.5.

Your code already uses a MamQueryArgs instance to construct the the MAM request. You can make this request more specific, by adding additional configuration to it. You can, for example, request all archived messages that come ‘after’ a certain message (that is identified by an ID), or sent in a certain date range. The API is documented in javadoc.

More background in how the MAM functionality is expected to work can be found in the XEP that specifies it: XEP-0313: Message Archive Management You will find that much of the API lines up with what is written in the XEP.

I know I can add some filters to mam query, however, I don’t think it is a solution. Why should I retrieve the messages that were there in the chat before I joined the group?

I’m not sure if I understand what you are trying to do. If you do not want to receive messages from before you joined the MUC room, then you do not need to create and execute a MAM Query at all.

The request that you are creating with the code snippet that you show in your first post is a request for all archived messages in the archive. If you want to limit that somehow, you will need to add that limitation to the query.

This is the scenario:

  1. I have been in a group with user A. I am offline, and user A wrote several messages to the group.
  2. Then he removed me from the group and wrote several messages.
  3. Then he added me to the group and wrote several messages.

When I join the group again, I would like to retrieve only those messages from MAM for that MUC room that was sent during the period that I was in the group.

Setting a filter from the client side is not a solution for this problem, if you can retrieve all the messages which were sent before you joined the MUC, is a critical security issue. I want to solve that problem. I can fix this problem by ignoring some messages but it is not the solution.

Ah, I see. This is not a level of granularity that Openfire provides. A user either has access to (all the messages in) the message archive, or it does not have access at all. Openfire does not keep track on a per-message basis if a particular user at a certain point in time had access to the message.

We are using Ejabberd. Seems like this problem exists in ejabberd too

The MAM protocol seems to allow for such behavior as an optional feature. It defines in section 6.1.2:

A MUC archive MAY, if it stores historical data about previous configuration states, limit the results returned to only those that the querying user would have been authorised to see at the time (e.g. it MAY limit the results to not include results while a user was an outcast).

I am not aware of any server implementation that offers that functionality.