~ / archivo / stackoverflow / 44635719
Sorting Gmail threads by date
-
I'm hoping someone can suggest a good technique for sorting Gmail threads by date without needing to get details on potentially thousands of threads.
Right now I use
threads.listto get a list of threads, and I'm using whatever order they're returned in. That's mostly correct in that it returns threads in reverse chronological order. Except that chronology is apparently determined by the first message in a thread rather than the thread's most recent message.That's fine for getting new messages at the top of the list but it's not good if someone has a new reply to a thread that started a few days ago. I'd like to put that at the top of the list, since it's a new message. But
threads.listleaves it sorted based on the first message.I thought the answer might be to sort threads based on
historyId. That does sort by the most recent message. But it's also affected by any other thread change. If the user updates the labels on a message (by starring it, for example),historyIdchanges. Then the message sorts to the top even though it's not new.I could use
threads.getto get details of the thread, and do more intelligent sorting based on that. But users might have thousands of threads and I don't want to have to make this call for every one of them.Does anyone have a better approach? Something I've missed?
- Miguel Ortiz yo 2017-07-01 04:26 Have you checked "users.messages" and then organize by their threadId?
- Tom Harrington 2017-07-03 15:05 @MiguelOrtiz I have, but
users.messagesis not consistently sorted, so while I can organize by thread ID this doesn't help with sorting by date.
-
I'm not a developer and never used the API before but I just readed the API documentation and it doesn't seem to have the functionality you want.
Anyway, this is what I understood in your question:
You want is organize threads by the latest message in each one.
I thought you could use a combination of users.messages and threads.list. In users.messages you'll have the ThreadID:
threadId string The ID of the thread the message belongs to.
The method would be using the date of user.messages to organize the latest messages from newer to old, then recursively obtain their original threads by threadId and print the threads with threads.list by their latest received message.
With this method you'll avoid recursion in each thread saving resources and time.
I don't know how new messages are affected by labels or starring, you'll have to find out that.
I apologize in advance if my answer isn't correct or missleading.
- Tom Harrington 2017-07-03 15:04 Listing user messages does give the thread ID, but not the message date. You don't get the date unless you call
messages.getindividually on each message. That runs into the same problem as I started with-- potentially thousands of calls to get accurate sorting. - Miguel Ortiz yo 2017-07-03 16:11 What about filtering with "q" in thread:list so you can filter by date with newer / newer than: developers.google.com/gmail/api/v1/reference/users/threads/list, i.e: newer: 2017/01/01 + is:unread I'm really curious about your question, hope you don't mind about it.
- Tom Harrington 2017-07-03 16:50 I'm not sure how I'd use that. I want to sort by date, not filter by it.
- Tom Harrington 2017-07-03 15:04 Listing user messages does give the thread ID, but not the message date. You don't get the date unless you call