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?
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?
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
}
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.
Threads are always sorted date DESC, and messages are always sorted date ASC. There aren't currently any parameters to change this.
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 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
}
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.
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.
© 2022 - 2024 — McMap. All rights reserved.