<p>&xep0060; §6.5.7 allows to retrieve the "most recent items" and &xep0313; state in §3.1 that archives are ordered in "chronological order". While this order is straightforward in general use cases, it is sometimes desirable to use a different order, for instance while using &xep0277;: a spelling mistake correction should not bring an old blog post to the top of retrieved items.</p>
<p>This specification allows to explicitly change business logic to retrieve the items in a different order.</p>
<li><strong>Date of creation</strong> — date when the item has been published <strong>ONLY if the item has a new id</strong> (i.e. an id which was not already present in the node at the time of publication). If an item reuses an existing id, it overwrites the original item <strong>and the date of creation stays the date of creation of the original item</strong>.</li>
<li><strong>Date of modification</strong> — date when the item has been overwritten by a new item of the same id. If the item has never been overwritten, it is equal to the date of creation defined above.</li>
<p>To do so, her client do a regular Pubsub request, but adds the <order> element as a children of the <pubsub> element with the <tt>"urn:xmpp:order-by:1"</tt> namespace, a <tt>by</tt> attribute equal to <tt>creation</tt> and a <tt>desc</tt> attribute equal to <tt>true</tt>.</p>
<p>To check that everything is alright, she requests again the last 3 items, but this time by date of modification. To do so, the client proceeds the same way as for date of creation, except that it uses the value <tt>modification</tt> for the <tt>by</tt> attribute.</p>
<p>The Pubsub service returns again the 3 plays but the "Winter Tales" item has been overwritten recently, while the 2 others have never been overwritten, so it returns the items in the following order, with the most recently modified item on top:</p>
<p>By default, ordering MUST be done in ascending order. This can be reversed by using the <tt>desc</tt> boolean attribute, which MAY have a value of either <tt>true</tt> or <tt>1</tt>.</p>
</section2>
<section2topic="Using Order-By with RSM"anchor="use_with_rsm">
<p>This section provides a full example of using Order-By with Pubsub and RSM. For readability, we'll use a node with 4 items that will have following IDs (in order of their creation) <tt>A</tt>, <tt>B</tt>, <tt>C</tt> and <tt>D</tt>.
Items <tt>C</tt> has been overwritten after <tt>D</tt> creation, and item <tt>A</tt> has been overwritten even later. Thus, when <em>ascending</em><tt>creation</tt> order is requested, items are in order <tt>A, B, C, D</tt>. When <em>ascending</em><tt>modification</tt> order is requested, items are in order <tt>B, D, C, A</tt>.<br/>
Let's see how this work when Juliet wants to retrieve all items in ascending modification order with RSM using a page size of 2 items:
</p>
<examplecaption='Juliet Retrieves First Page of Items with RSM'><![CDATA[
Now Juliet wants to get the second and last page to complete her collection. She does this as usual with RSM, by using the value advertised in <em><last></em> element in a <em><after></em> element.
</p>
<p><strong>NOTE:</strong> in this example the value used in <em><last></em> element is the item ID, but as specified in &xep0059;, an implementation MAY use whatever makes sense to it, the requesting client MUST treat this as an opaque value.</p>
<examplecaption='Juliet Retrieves the Second (and Last) Page of Items'><![CDATA[
Juliets wonders which are the 2 last items created. To discover this, she request again the node, but this time with a <tt>creation</tt> order field, and in descending order:
</p>
<examplecaption='Juliet Retrieves Last Created Items'><![CDATA[
<p>Now she knows that last created item is <tt>D</tt>, and the one created before is <tt>C</tt>.</p>
<p>Please note that items are in descending order in the whole result set but also inside the RSM page (thus the first item here is <tt>D</tt>), and that in this order, this request returns the first page, so index is <tt>0</tt> here.</p>
<p>If Juliet wanted to retrieve the second page of items by descending order of creation, she would do like this:</p>
<examplecaption='Juliet Retrieves Second Page of Last Created Items'><![CDATA[
<p>This specification can be extended by further XEPs, proposing other kind of ordering in the 'by' attribute (e.g. ordering by filename for a file sharing service). But this is beyond the scope of this XEP, and a client should not assume that other ordering than "creation" and "modification" are available without further negotiation. Any new ordering specified in a other XEP SHOULD use the Clark notation to avoid any collision (i.e.: <tt>{some_namespace}some_ordering</tt>).</p>
<p>It is important to note the following points:</p>
<ul>
<li>Order-By affect the order of the whole archive, <strong>AND</strong> the order of the items inside a RSM result set (i.e. inside a page).</li>
<li>The order of <tt>creation</tt> or <tt>modification</tt> is the one set by the Pubsub service itself. Some Pubsub based features like &xep0277; let users specify a creation and modification date; using them would need item parsing and is NOT what <tt>creation</tt> and <tt>modification</tt> is referring to here. A future XEP extending this one could allow to order by user-specified creation or modification date, but this is beyond the scope of this XEP.</li>
<li>The semantic described here can be reused in other use cases as for Pubsub or MAM. If it is the case, the support MUST be advertised using discovery and the namespace covered, as explained in <linkurl="#disco">Discovering Support</link> below.</li>
<li>It may be hard to impossible for an implementation to be compliant with features specified at <linkurl="https://xmpp.org/extensions/xep-0059.html#forwards">Paging Forwards Through a Result Set</link> in &xep0059;. Notably for some order fields, it may be really difficult to not return duplicate items or to no omit items from pages. People interacting with this XEP must be aware of that, and services implementing this XEP SHOULD try to comply with those features, but MAY not if proven too difficult (those features are not required in RSM anyway as the term <em>MAY</em> is used).</li>
<p>If a server supports the "order by" protocol, it MUST advertize it including the "urn:xmpp:order-by:1" discovery feature &NSNOTE; in response to a &xep0030; information request.<br/>In addition to the general feature support, an entity MUST indicated on which protocols Order-By can be used, by using the notation <tt>urn:xmpp:order-by:1@<em>other_namespace</em></tt>, i.e. a concatenation of:</p>
<p>So if Order-By is implemented for &xep0060;, the service MUST advertise <tt>urn:xmpp:order-by:1@http://jabber.org/protocol/pubsub</tt>. If Order-By is implemented for &xep0313;, it is <tt>urn:xmpp:order-by:1@urn:xmpp:mam:2</tt>.<br/>
In the following example, the server <tt>example.org</tt> advertizes Order-By support, and indicates that it is implemented for Pubsub and MAM:</p>
<p>Several ordering elements may be used, this allows to solve next levels of ordering in case of equality. In this case, the first ordering (i.e. the top most <order> element) is the main one, the second <order> element is used in case of equality, then the next one if a new equality happens and so on.</p>
<p>In case of equality, if no new <order> element is specified, the item order is not guaranteed and is up to the implementation (the implementation MUST keep this order consistent across requests though).</p>
<p>It may be difficult to find a correct value for <first> and <last> elements of RSM. Indeed, internal ID of items can't be suited for all orderings. For Pubsub service using a SQL database as backend, item ID (XMPP or internal) could be used with a window function such as <tt>row_number</tt> (supported by major database engines such as PostgreSQL, MariaDB/MySQL or SQLite) over the requested ordering. For instance, on a hypothetical table where items are requested by ascending <tt>creation</tt> then <tt>modification</tt> dates after the value <tt>ABC</tt> (which correspond to XMPP item ID in our case), a request similar to this could be used:</p>
<examplecaption="SQL Query to Handle <after> value"><![CDATA[
WITH cte_1 AS
(SELECT items.id AS id, row_number() OVER (ORDER BY created ASC, modified ASC) - 1 AS item_index
FROM items
WHERE items.node_id = 123)
SELECT cte_1.item_index, items.id, items.payload
FROM items JOIN cte_1 ON items.id = cte_1.id
WHERE cte_1.item_index > (SELECT cte_1.item_index
FROM cte_1
WHERE cte_1.id = "ABC")
ORDER BY cte_1.item_index ASC
LIMIT 10;
]]></example>
<p>In this example, <tt>row_number</tt> is decreased by 1 to match RSM index (<tt>row_number</tt> starts at 1 while RSM index starts at 0), thus the <tt>item_index</tt> column can be used directly to fill RSM metadata. A Common Table Expression has been used for better readability.</p>