In what order does threads.get() and messages.get() return a list of items?
Asked Answered
R

5

5

It does not seem to be documented by how does threads.get() and messages.get() sort the results they return.

Is the order by a descending date/time?

Rex answered 4/11, 2014 at 4:50 Comment(1)
@Eric DeFriez any hint?Factional
L
1

Id just like to say that using the javascript library -- messages.list does NOT return in date ASC or date DESC. They are returned randomly as far as I can tell.

At first I thought it may be my code since I was using jquery $.each to parse through the JSON but even using a native javascript for loop they are still not in Date order. It seems like the returned messages are mostly in date DESC, but every now and then one is thrown in out of order. I have done a fair amount of manipulation trying to diagnose, thinking...maybe these messages belong to the same thread...but this is not the case.

If anyone has a tip for the proper way to proceed using the javascript library please post. I'd rather deal simply with messages as my application is a simple record of rather than full CRUD on the messages...so just a simple list of messages in reverse date order is all I need.

ADDITION: I've also used threads.list and threads.get to return the messages and they are even more randomly sorted on the return. Really love for someone to post the proper way to retrieve messages sorted by date. Copied the code here for reference to any/all willing to take a look

function makeApiCall() {
gapi.client.load('gmail', 'v1', function() {
    //console.log('inside call: '+myquery);
  var request = gapi.client.gmail.users.threads.list({
    'userId': 'me',
    'q': myquery
  });
  request.execute(function(resp) {
      //$('.ASAP-emailhouse').append(message+'<br>');
    jQuery(document).ready(function($) {
         var nummessages = resp.threads.length;
             for (i = 0; i < resp.threads.length; i++) { 
                //$('.ASAP-emailhouse').append(resp.messages[i].id+'<br>');
                var threadId = resp.threads[i].id;
                var messagerequest = gapi.client.gmail.users.threads.get({
                    'userId': 'me',
                    'id': threadId
                  });//end var message request
                messagerequest.execute(function(messageresp) {
                    for (m = 0; m < messageresp.messages.length; m++) {
                        //$('.ASAP-emailhouse').append(messageresp.messages[m].payload.headers.length+'<br>');
                         for (n = 0; n < messageresp.messages[m].payload.headers.length; n++) { 
                            //$('.ASAP-emailhouse').append(messageresp.messages[m].payload.headers[n].name+'<br>');
                            if( messageresp.messages[m].payload.headers[n].name == 'Date'){
                                $('.ASAP-emailhouse').append(messageresp.messages[m].payload.headers[n].value+'<br>');
                            }
                         }
                    }
                });
             }//end for each message
      });//end jquery wrapper for wordpress
  });//end request execute list messages
});//end gapi client load gmail

}

Lizettelizotte answered 5/2, 2015 at 15:36 Comment(3)
I have also integrated the Gmail API and can confirm that the threads and emails are returned in random order.Rex
Yay! For me not begin crazy -- not so great for the API returning in random order. So I suppose we have to make the messages.list call, loop through those making the messages.get call, grab the date, put ID, Date into a new object, reorder, and them make the messages.get call again...CRAZY?Lizettelizotte
You can also use the search syntax to filter by emails date: support.google.com/mail/answer/7190?hl=enRex
A
4

Came here through Google because I was looking for this myself.

After some more digging around, it seems the messages.list returns messages according to historyId DESC. So the last modified message is on top.

This also seems to be stated in the docs here in point 3:

Merge the updates into your cached results. Your application should store the historyId of the most recent message (the first message in the list response) for future partial synchronization.

Actualize answered 1/8, 2017 at 15:27 Comment(0)
K
2

Threads are always sorted date DESC, and messages are always sorted date ASC. There aren't currently any parameters to change this.

Kasper answered 17/11, 2014 at 16:16 Comment(2)
This doesn't seem to be true anymore (at least for messages). I've tried the messages.list api and it returns messages sorted by date DESC, more or less. I say 'more or less', because when iterating over the messages and printing their dates, every now and then a message from a day after gets 'inserted' into the sequence of printed message dates.Appoint
It looks like threads are sorted by the date the thread was created, regardless of when the most recent message in the thread was received.Fichtean
L
1

Id just like to say that using the javascript library -- messages.list does NOT return in date ASC or date DESC. They are returned randomly as far as I can tell.

At first I thought it may be my code since I was using jquery $.each to parse through the JSON but even using a native javascript for loop they are still not in Date order. It seems like the returned messages are mostly in date DESC, but every now and then one is thrown in out of order. I have done a fair amount of manipulation trying to diagnose, thinking...maybe these messages belong to the same thread...but this is not the case.

If anyone has a tip for the proper way to proceed using the javascript library please post. I'd rather deal simply with messages as my application is a simple record of rather than full CRUD on the messages...so just a simple list of messages in reverse date order is all I need.

ADDITION: I've also used threads.list and threads.get to return the messages and they are even more randomly sorted on the return. Really love for someone to post the proper way to retrieve messages sorted by date. Copied the code here for reference to any/all willing to take a look

function makeApiCall() {
gapi.client.load('gmail', 'v1', function() {
    //console.log('inside call: '+myquery);
  var request = gapi.client.gmail.users.threads.list({
    'userId': 'me',
    'q': myquery
  });
  request.execute(function(resp) {
      //$('.ASAP-emailhouse').append(message+'<br>');
    jQuery(document).ready(function($) {
         var nummessages = resp.threads.length;
             for (i = 0; i < resp.threads.length; i++) { 
                //$('.ASAP-emailhouse').append(resp.messages[i].id+'<br>');
                var threadId = resp.threads[i].id;
                var messagerequest = gapi.client.gmail.users.threads.get({
                    'userId': 'me',
                    'id': threadId
                  });//end var message request
                messagerequest.execute(function(messageresp) {
                    for (m = 0; m < messageresp.messages.length; m++) {
                        //$('.ASAP-emailhouse').append(messageresp.messages[m].payload.headers.length+'<br>');
                         for (n = 0; n < messageresp.messages[m].payload.headers.length; n++) { 
                            //$('.ASAP-emailhouse').append(messageresp.messages[m].payload.headers[n].name+'<br>');
                            if( messageresp.messages[m].payload.headers[n].name == 'Date'){
                                $('.ASAP-emailhouse').append(messageresp.messages[m].payload.headers[n].value+'<br>');
                            }
                         }
                    }
                });
             }//end for each message
      });//end jquery wrapper for wordpress
  });//end request execute list messages
});//end gapi client load gmail

}

Lizettelizotte answered 5/2, 2015 at 15:36 Comment(3)
I have also integrated the Gmail API and can confirm that the threads and emails are returned in random order.Rex
Yay! For me not begin crazy -- not so great for the API returning in random order. So I suppose we have to make the messages.list call, loop through those making the messages.get call, grab the date, put ID, Date into a new object, reorder, and them make the messages.get call again...CRAZY?Lizettelizotte
You can also use the search syntax to filter by emails date: support.google.com/mail/answer/7190?hl=enRex
T
1

Contrary to what the documentation implies (see @Allard Stijnman's answer), based on actual testing, messages.list sorts by descending internalDate.

Interestingly enough, threads sort by descending internalDate of their first message, not their last one as you'd expect to see it inside the Gmail inbox.

Toomin answered 15/2 at 6:33 Comment(0)
L
0

I have implemented the new gmail API via javascript library. The response from users.messages:list method comes back as json object in what seems to be Date ASC order. I have not come across any way in the documentation to sort the response before it is delivered as a part of the optional query parameters.

Lizettelizotte answered 6/11, 2014 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.