Is there any way to limit a Gmail watch to only messages added? - gmail-api

When setting up a watch for a user is there a way to limit the watch to only messages added to the inbox?
Based on the documentation (https://developers.google.com/gmail/api/v1/reference/users/watch) I see that there is the option for INBOX labelId, but I want to limit it to only messages added as well. We're currently having to handle this by passing 'history/messagesAdded' in the fields string in the subsequent history.list call.

Unfortunately you cannot. what you have to do is
Get the history when notification arrived. History returns a json and it contains a 'messagesAdded' if new message is added.
You can keep a predefined array of labels like below
predefinedLabels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX']
Now you can check, (each is the history json)
if 'messagesAdded' in each:
labels = each["messagesAdded"][0]["message"]["labelIds"]
intersectionOfTwoArrays = list(set(predefinedLabels) & set(labels))
Here you get the intersection of labels. Now you have to check that with predefined labels
if set(predefinedLabels) == set(intersectionOfTwoArrays):
#get the messageId and do what you want
finally you can filter the notification as you want!.
It is better to store histroyId and update it with every
notification and use it when you get the history. It will help you
to get updated history only.
Please note I used python when I was building my sever. So above demo code written using python

It looks like history.list added a new parameter "historyTypes". If you set that to "messageAdded", the api will only return history records of that type.

Related

When is new Gmail API History ID generated

I am trying to design an algorithm to maintain a list of new INBOX messages using a combination the watch()/push notification, with the user.history and user.messages gmail apis. HistoryID documentation is unclear.
https://developers.google.com/gmail/api/guides/sync#partial
indicates
"You can also use push notifications to trigger partial synchronization in real-time and only when necessary, thereby avoiding needless polling." -- My question is HOW?
Which event on the email account triggers an incremented HistoryID?
If I guess HistoryID I get in the push notification the latest historyId of the label specified, then I should be able to use that HistoryID as the starting HistoryId in the User.History.List call, but when I do I just get an json response which is another higher HistoryID. So, if I'm specifying messagesadded and watching INBOX, why is the call to History API with the most current HistoryID returning no HistoryList objects?
I have read Gmail API users.watch - no details for historyId and Gmail History list is not giving complete data
and I'm unclear as to how to design the algorithm to use push notifications in conjunction with the other apis to do a partial sync to maintain a running inventory on new INBOX messages, as per the documentation.
this issue really sad, google just send historyId with empty history list. But why cant add emailId or property that this history binds with new message arrived?
To solve this problem u need remember time when u last time fetch email messages with total counter messages in Inbox, next, when u get google pub/sub message and it has empty history object - u need get new total counter of messages in Inbox label - if there is differences - next what u need is fetch email messages using:
or
max count value = differences between of total counter messages,
means add query to get messages by max result
or
using date time from your last update - means add query to get messages by date time.
Really bad that google make such bad implementation for pub sub for new messages.

How to modify firebase database so that i can set a read or unread status

I need to show the count of unread messages from firebase in the notification bell.
I am done with displaying all contents in my collection in my web application.But when a new message arrives i need to give a feedback to user by showing the unread message count in notification bell .From my understanding,for that i need to set a read/unread status in firebase collection.Is there any alternative way for showing the count of unread messages in my React Web Application?
i am using firebase package of npm for displaying messages.
Below is the sample data in my firebase collection.
name:"John Doe",
message:"you got a new appointment"
i am new to firebase.Please help me with some logic to sort out this problem.
If I understand correctly, you would want to use a status flag (read: boolean), unless you would not mind writing all new messages to a particular path and moving them to another path after it is read.
Something like below
/user/unread-msg => all new messages come here
/user/read-msg => messages are moved here after being read.
This I believe would be less efficient than using a simple flag.
You could use a firebase cloud function to maintain the count of child elements in a particular path like /user/unread-msg or /user/read-msg so that you don't have to download the entire data to get the count.
Refer the sample code at https://github.com/firebase/functions-samples/tree/master/child-count

Why does gmail API when using history.list method send message ids without the field what action has been preformed on them?

When using gmail API, history.list method we get "bare" message ids with no additional field from the fields in 'labelAdded', 'labelRemoved', 'messageAdded', 'messageRemoved'. Why is that? And is it possible that a new message has been added but when we use this method, the field messageAdded hasn't been used, so we receive it "bare" ?
According to the GMail history API, the list only contains the id and threadId fields.
It works this way because the objective of the history API is to provide you with the changes that happened in the mailbox, not its contents.
After you obtain the list from Users.history: list you need to call Users.messages: get or Users.messages: list to get the full messages.
If the messageAdded field is empty, it means that no new messages were added after the last historyId you examined. You might have skipped some. Make sure that every time you query the API you internally store the last historyId you processed so you can resume from that point in the future and not lose any changes.

Thread get - just metadata

I am doing full sync this way: list of /threads and then a request to get each of the thread like /threads/{id}. However this returns me every message together with it's body data -> and I just want to fetch the metadata of the messages. I can see that in get 'messages/{id}' you can specify format but not in get threads/{id}
Threads.get() now supports format=METADATA and with that you can use the new "metadataIncludeHeaders" to further limit the headers list to a select few. This is much more efficient than using "fields" as it only fetches what is necessary from the backend rather than filtering it later on:
https://developers.google.com/gmail/api/v1/reference/users/threads/get
I assume that by metadata you mean the headers (no body). You can use the fields parameter to get just that (messages/payload/headers):
https://www.googleapis.com/gmail/v1/users/me/threads/{thread-id}?fields=messages%2Fpayload%2Fheaders&key={YOUR_API_KEY}

Salesforce apex - how to determine if a field is set to be tracked in Chatter?

I'm writing triggers / classes to post field change notifications to the Chatter feeds of objects related to the object undergoing change (e.g. posting a field update of an Opportunity to its related Account is changed).
I've written the code to compare the before and after values and post feed updates for the values that have changed just fine. But I only want to post updates for fields that are set to be "tracked" in Chatter (Setup -> Customize -> Chatter -> Feed Tracking).
How can I get at these settings in Apex?
(It's possible to call isFeedEnabled() on an object to see if Chatter is enabled for that object. But I don't see any way to tell if an individual field is set to have its updates posted to the feed)
Cheers,
Ray
There's call like isFeedEnabled() for checking on field-level tracking (both for chatter and the old style field history) sadly.
You could however use a trigger on the feedItem sObject to see if an item where Type == 'FeedTrackedChange' is created, and if so query the FeedTrackedChanges related to that FeedItem; then create the chatter post from there.

Resources