Fetching new instagram medias from a realtime update - real-time

I don't think that is a bug but rather an issue for me, lemme explain...
I have a subscription to the realtime updates for a particular tag.
When receiving the POST request from instagram (notifying new medias have arrived), I get the following:
[
{
changed_aspect: 'media',
object: 'tag',
object_id: 'nofilter',
time: 1401648280,
subscription_id: 5165151,
data: {}
}
]
Then, in order to fetch the corresponding new content, I fetch recent medias and filter them by comparing their created_time to the subscription's time, ie: media.created_time >= 1401648280
Unfortunately, sometimes, the media.created_time is 1ms behind: 1401648279 ... So that media isn't return in my filtered list of "new" medias.
--
I understand why subscription's time can be later and I can give a tolerance for my filter comparison: time - media.created_time <= X but I don't know how to set X: 10ms ? 100ms? 2s?
Do you know that value? A guaranteed amount of time within the subscription will be generated after new media updates?
Thank you in advance and best regards

Related

Paginate firestore query while still listening to new documents

As many others, I have an app with chat functionality. I have a StreamBuilder<QuerySnapshot> that listens to new chat messages, and a ListView.builder that shows them. I'm looking for a good way to paginate the messages, but none of the solutions I have found works for me, since I always have to listen to new messages.
One of the things I tried was this code, but that didn't seem to work even if the author said he had updated the code.
Each chat message has it's own document in a collection, so right now I just fetch all the documents in the collection and order them by a time field. However this only works for about 20 messages before performance suffers, so I would be very glad if someone could help me with this.
I guess you are showing the messages sorted by creation date, right? Why dont you paginate using the date then?
chatcollectionref.
where("creationdate", "<", enddate)
... // Add more conditions if needed
.orderBy("creationdate","desc").limit(20).get()
Start with enddate far in the future then update it to the creation date of the last message you retrieve and just paginate.
EDIT: a bit more details...
First run of the query: enddate = new Date(2100, 1, 1) and you get the first 20 messages and display them
The user scrolls down and reach the last message
Run again the query: enddate = creationdate of the last message you have already retrieved, and you get the 20 next
Loop back to 2
Works well in my chat app

Firestore idempotency for a virtual wallet

I'm coming to you guys again for help. I have an app, in which you can top up your own virtual wallet from your credit card.
My structure is as followers:
collection transactions/{transactionId} -- contains an owner UID and the top up amount.
collection users/{userId}/personal/wallet containing { funds: 0 }
I am keeping the wallet into a subcollection of personal info because I don't want other users to see it.
Anyway, I have an onCreate trigger called walletSync for new documents under transactions collection, from which I take the amount and add it to the user's current funds via runTransaction.
Problem is, the function sometime triggers three times ... for one document. Here's a screenshot. I have logged the transactionId ( which is just one document ) and as you can see, onCreate, is being processed three times, therefore, topping up my wallet with three times the actual value.
Here's my function code, in which I am marking any processed transaction with processed: true so I can ignore it next time it triggers. As you can see in the logs, that processed_already message never pops up, which means the trigger does not fetch the new data, next time it triggers.
I am out of ideas here, please advise.
EDIT: Here's my function: https://pastebin.com/PRA7CbxL
I have managed to figure it out, with the help of the mighty #Doug and the mentioned similar questions.
After all, the issue with my code was that while I was marking my transaction as processed, I was assuming that the second / third time the function triggers, snapshot.data() would be the value of the document, ( took me some time to realize ) it was actually the change data sent towards the database trigger. I know it sounds confusing, but the function is not being sent the value of the document ( for that you could get snapshot.ref and fetch the data from there, in order to get the updated value in a transaction ).
So here's my updated function on Pastebin. Here's a screenshot of the code in action: https://i.imgur.com/fbQSGT6.png

Facebook Marketing API: Is it possible to filter insights by status

I am trying to filter adsets insights by adset status, but when I add a status filter, I get an empty dataset back:
curl "https://graph.facebook.com/v2.7/act_<redacted>/insights?fields=clicks,impressions,cpc,ctr,account_id&time_range%5Bsince%5D=2016-01-12&time_range%5Buntil%5D=2016-09-12&access_token=<redacted>&format=json&filtering=%5B%7B%22field%22:%22status%22,%22operator%22:%22EQUAL%22,%22value%22:%22ACTIVE%22%7D%5D&level=adset"
here is what filtering param looks like before it gets url-encoded:
[{"field":"status","operator":"EQUAL","value":"ACTIVE"}]
I have tried all valid values for status ACTIVE, PAUSED, DELETED, ARCHIVED to no avail.
When I remove filtering param - I see my data.
The question is:
Does anyone know if it is possible to filter by status, and if it is, what I am doing wrong?
It is possible. Using latest version as of March 2018.
You need to actually request all the ads that are ACTIVE and then provide insights as a 'nested' list of fields.
Replace 123456 with your ad account (but leave the 'act_' which is needed)
act_123456/ads?fields=effective_status,name,insights{total_action_value,total_actions,actions},adset_id,campaign_id&filtering=[{'field':'effective_status','operator':'IN','value':['ACTIVE']}]
This also works for /campaigns and /adsets
You can also do this (be sure to include &level=ad):
act_123456/insights?fields=ad_id,adset_id,campaign_name,action_values,campaign_id,total_action_value&level=ad&filtering=[{"field":"campaign.delivery_info","operator":"IN","value":["active"]}]
Filtering by insights isn't possible. At least in v2.7. To get insights for let's say ads I've decided to do the following:
1. Fetch ads that I need via /ads call. This allows me to filter by status.
2. Fetch insights for the same collection of ads and make sure that I ad id is included in the response.
3. Filter the second collection using the first one.
i m using this to filter campaigns with insights , working for me , start playing with it
'filtering'=>
array(
array(
"field"=>"campaign.effective_status",
"operator"=>"IN",
"value"=>$campaigncheckstatus,
),
),
$campaigncheckstatus = array("ACTIVE","PAUSED");
dont forget to json_encode $campaigncheckstatus if passed through url

How to get past/ended public events from facebook using graph api/FQL?

I tried using the since and until parameters to get past events but it doesnt seem to work. The results from using since and until parameters give only events that have an end date in the future and a start date within the specified time limit. But however i would like to retrieve a list of all public events within the specified limit irrespective of whether it is still active or not. Is this possible?
Thanks for your help
You can use FQL for that:
select eid, name, start_time from event where creator = {page_id} and start_time >= "2014-05-01T00:00:00+0200" and start_time <= "2014-05-31T23:59:59+0200"
Make sure that {page_id} is a valid Page ID (or User ID). See https://developers.facebook.com/docs/reference/fql/event/ as reference.

facebook graph api comment list sort , like 'orderby=desc'?

I use graph api to get the picture's comments, but I want to first sort the results by creating time and then return to the latest data. Similar to the sql statement 'order by create_time desc', I do not know if have such a parameter.
Currently used to offset and limit access to the latest data, but also know the total number of comments,
pagesize = 25;
offset = comments.count - pagesize;
limit = 25;
url = "https://graph.facebook.com/" + object_id + "/comments?access_token=" + access_token + "&limit=" + limit + "&offset=" + limit;
next page:
offset -= 25
but comments.ount of numerical sometimes is not accurate
and the result of the request URL to return to sometimes don't match
Whether to have very good solution
Or I used the wrong way (‘limit’ and ‘offset’ Parameter)!!!
Thank you for your answer.
"Graphics API" the existence of the cache?
i post a message and 46 comments.requests url, set the parameters:
offset=0&limit=1
Then it should return to the last comment (latest one), the actual return to the middle of a comment, and I tested a few times, set the
offset and limit. According to the returned results, the middle one is
the latest comment
If I set the limit value is greater than the 'comment.count', the returned data is all, the official website and facebook consistent
Because the cache reason?
Thanks again~
#dbau - You are still better off using FQL. In my experience, unless you are making a very simple call, you have very little control over what you get via a Graph API call.
Why don't you want to use FQL? FQL is an endpoint of the Graph API. There is still some data that can only be returned via FQL.
This will get you the result you're looking for. The query needs to be URL encoded. I left it in plain text for clarity.
https://graph.facebook.com/fql?access_token=[TOKEN]&q=
SELECT id, fromid, text, time, likes, user_likes FROM comment
WHERE object_id = [OBJECT_ID] ORDER BY time DESC LIMIT 0,[N]
You may find you don't get [N] comments returned each time, because Facebook filters out items that are not visible to the access_token owner after the query is run. You could either up the LIMIT and filter out any excess results returned or if you are using a user access_token, you could add AND can_like = TRUE to the WHERE clause to be guaranteed that, if they exist, [N] posts visible to the current user are returned.
Graph API returns latest objects first.
Facebook provides 2 keywords to filter the fetched data.
Limit : Returns "limit" number of latest records
Offset : Returns "limit" number of records from the offset position
So to retrieve latest "x" comments posted for an object
https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=0
To retrieve next "X" comments (page wise)
https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=[X*PAGENo]
Hope the answer is clear enough for you.