Recent Posts XMPP Archive

I am reading http://xmpp.org/extensions/xep-0313.html for an Ejabberd request for messages archived with a specific user.

This is the xml I'm posting:

<iq type='get' id='get_archive_user1'> <query xmlns='urn:xmpp:mam:tmp'> <with> user1@localhost </with> <set xmlns='http://jabber.org/protocol/rsm'> <max>20</max> </set> </query> </iq> 

I get the first 20 messages correctly. To request again, I add a tag:

 <after>(id in element "Last" from last request)</after> 

and that works fine too. I need to get the last 20 posts, not the first 20 posts. How can I achieve this?

+4
source share
3 answers

Managing message archives XEP-0313 relies on XEP-0059 Managing a result set for pagination.

The RSM specification explains how to get the last page in a result set :

the requesting entity MAY request the last page in the result set by including the empty <before/> element and the maximum number of returned elements in its request.

This means that you need to add an empty <before/> element to your result set query.

Here is an example based on XEP-0313 version 0.4 on how to get the last 20 messages in a conversation with a given user. The request limit is determined by the max parameter (it determines the page size).

 <iq type='set' id='q29302'> <query xmlns='urn:xmpp:mam:0'>  <x xmlns='jabber:x:data' type='submit'>   <field var='FORM_TYPE' type='hidden'>    <value>urn:xmpp:mam:0</value>   </field>   <field var='with'>    <value> juliet@capulet.lit </value>   </field>  </x>  <set xmlns='http://jabber.org/protocol/rsm'> <max>20</max> <before/>  </set> </query> </iq> 
+8
source

You must add an empty <before/> element:

 <iq type='get' id='get_archive_user1'> <query xmlns='urn:xmpp:mam:tmp'> <with> user1@localhost </with> <set xmlns='http://jabber.org/protocol/rsm'> <max>20</max> <before/> </set> </query> </iq> 

See here .

+4
source

People who would like to use Smack to extract can use the code below

  public MamManager.MamQueryResult getArchivedMessages(String jid, int maxResults) { MamManager mamManager = MamManager.getInstanceFor(connection); try { DataForm form = new DataForm(DataForm.Type.submit); FormField field = new FormField(FormField.FORM_TYPE); field.setType(FormField.Type.hidden); field.addValue(MamElements.NAMESPACE); form.addField(field); FormField formField = new FormField("with"); formField.addValue(jid); form.addField(formField); // "" empty string for before RSMSet rsmSet = new RSMSet(maxResults, "", RSMSet.PageDirection.before); MamManager.MamQueryResult mamQueryResult = mamManager.page(form, rsmSet); return mamQueryResult; } catch (Exception e) { e.printStackTrace(); } return null; } 
+1
source

All Articles