Get last message of each conversation with XEP-031

2019-03-26 19:08发布

问题:

I'm using an XMPP server that implements XEP-0313 for retrieving conversation history. I would like to fetch only the last message of each conversation, so that I can build a list of your most recent conversations previewing the last message.

I've managed to fetch all messages of all conversations and based on that I could build the list, but it's a big waste of data and not an option. I'm not sure this is the right extension for accomplishing this, so if there is another extension I should be looking at, please guide me in the right direction.

回答1:

One thing you can do easily is first retrieve the user's roster and then for each contact retrieve the latest message.

<iq from='juliet@example.com/balcony'
    id='bv1bs71f'
    type='get'>
  <query xmlns='jabber:iq:roster'/>
</iq>

Result:

<iq id='bv1bs71f'
    to='juliet@example.com/chamber'
    type='result'>
   <query xmlns='jabber:iq:roster' ver='ver7'>
     <item jid='nurse@example.com'/>
     <item jid='romeo@example.net'/>
   </query>
 </iq>

Retrieve the last message from or to nurse@example.com:

<iq type='set' id='juliet1'>
  <query xmlns='urn:xmpp:mam:1'>
    <x xmlns='jabber:x:data' type='submit'>
      <field var='FORM_TYPE' type='hidden'>
        <value>urn:xmpp:mam:1</value>
      </field>
      <field var='with'>
        <value>nurse@example.com</value>
      </field>
    </x>
    <set xmlns='http://jabber.org/protocol/rsm'>
      <max>1</max>
      <before/>
    </set>
  </query>
</iq>

Of course users can have conversations with people not on their roster, but in practice this is quite rare on XMPP.



标签: xmpp