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

白昼怎懂夜的黑 提交于 2019-12-05 12:45:44

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

}

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!