How to get the time at which a comment was posted - facebook

I'm trying use the facebook api to get the last posts from the newsfeed of a user along with the comments attached to it. It's easy to get information about those comments such as the text, the actor id, etc... from a query to the STREAM table,
var query = FB.Data.query('SELECT post_id, actor_id, app_data, message, attribution, created_time, comments FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid={0} AND type="newsfeed") LIMIT 20', response.id);
but it doesn't seem to include the timestamp at which those comments were posted (so I can get the timestamp for the post itself with 'created_time', but not for the comments attached to it).
Does anybody know a way to do this?
Help would be much appreciated.
Thanks a lot.

This is a sample response from a query to the table stream
You could fetch that with something like
var query = FB.Data.query('SELECT post_id, actor_id, app_data, message, attribution, created_time, comments FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid={0} AND type="newsfeed") LIMIT 20', response.id);
query.wait(function(rows) {
for (var i = 0; i < rows.length; i++) {
if (rows[i].stream_post.comments.count > 0) {
var comments_list = rows[i].stream_post.comments.comment_list;
for (var j = 0; j < comments_list.length; j++) {
var comment = comments_list[j];
alert(comment.time);//timestamp
}
}
}
});
Hope this helps.
Disclaimer: Have not debugged the JavaScript. typos may be present.

In:
$resultObject->comments->comment_list[0]->time
there is the unix timestamp from the comment created time.

Related

facebook batch/fql friendlists a friend is member of

I'm creating a javascript facebook app to have an overview of my friendlists. Actually I can retrieve my (logged in user = me) friendlists, when I click a friendlist I see all my friends in that list.
When I want now is, when I click a list, I want to see the friends belonging to that list, and a comma separated lists my friend is member of.
For example in the list "Best" I have 3 friends: Athos, Portos, Aramis. But they are also in other lists. So in my app I'd like to get something like this:
Athos: Best, Family, Close Friends, VIP
Portos: Best, VIP
Aramis: Best, Close Friends, Party Animals
I tried with the FQL multiquery, but it seems impossible to get what I want.
So I think I should use a Batch request, but since I don't have a lot of experience using batches here I am asking your help.
So here the FQL query I use to get friends belonging to the list. I need this, because I want to manage friends beginning from a list:
var listID = 123;//just enter a listID
var friendsInList = 'SELECT uid,username,profile_url,first_name,last_name,pic_square FROM user WHERE uid IN (SELECT uid FROM friendlist_member WHERE flid =' + listID +')';
Now I don't know how to proceed in a clean and efficient way. I have a working but inefficient solution:
//[...]FB.api call to get a response object with all my friends using the friendsInList query above
for (var i = 0; i < response.length; i++) {
friendID = response[i].uid;
//call FB.api with the following query:
var friendListMemberships = 'SELECT uid, flid, name FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me()) AND uid IN (select uid FROM friendlist WHERE flid =' + friendID;
}
the above solution works but is highly inefficient as I send a FB.api request for each friend, very slow for big lists... thus I'd like a pro user to help me putting this in a batch (a batch can have maximum 50 requests, thus I'd like to send just 1-2 requests and get back all what I need).
I know how to write a batch, here I put the structure of my code, if it can help you completing it with the second query:
var listID = _listID;
var _queryFriendsInList = 'SELECT uid,username,profile_url,first_name,last_name,pic_square FROM user WHERE uid IN (SELECT uid FROM friendlist_member WHERE flid =' + listID + ')';
var queryFriendsInList = _queryFriendsInList.replace(" ", "+");
var _queryFriendListMemberships = 'I have no idea what kind of query I can write here to get the lists my friends from queryFriendsInList are member of';
var queryFriendListMemberships = _queryFriendListMemberships.replace(" ", "+");
var searchArgs = {
access_token: FB.getAuthResponse().access_token,
batch: []
};
searchArgs.batch.push({
'method': 'GET',
'name': 'friendsInList',
'relative_url': 'method/fql.query?query=' + queryFriendsInList,
'omit_response_on_success': false
});
//here is where I need help
searchArgs.batch.push({
'method': 'GET',
'name': 'friendlistMememberships',
'relative_url': 'method/fql.query?query=' + queryFriendListMemberships,
'omit_response_on_success': false
});
FB.api("/", "POST", searchArgs,
function (response) {
//console.log("log");
}
);
I hope the question and code samples are clear enough. Thank you for helping!
You can get everything you want in one API call with a multiquery. You'll need to assemble this together on the client side with a few loops, but it shouldn't be too hard. Here's the query:
{
"all_friendlists":
"SELECT flid, owner, name FROM friendlist WHERE owner=me()",
"fl_members":
"SELECT uid, flid FROM friendlist_member WHERE flid IN
(SELECT flid FROM #all_friendlists)",
"fl_member_details":
"SELECT uid, name, username FROM user WHERE uid IN (SELECT uid FROM #fl_members)"
}
You can present everything to your user from this one API call. The #all_friendlists query gives all your user's friendlists. Once your user has selected a friendlist, then loop through #fl_members to find all the members of this list, retrieving their details from #fl_member_details. Make another loop through #fl_members to find what other lists they belong to.
Alternately, you could just mash these three lists together into a single presentation page, and use a jQuery data filtering scheme like Isotope to allow your user to sort/filter/etc. on the fly.

FQL count(*) Javascript SDK doesn't work

I use a fql request to find all my friends who use my application and want to see my film. I use this request :
FB.api({
method: "fql.query",
query: "SELECT actor_id FROM stream WHERE app_id = MYAPPID and source_id = "+friend.id+" and attachment.name = 'MYFILMNAME'"},
function (responseFQL) {
This returns all friends ids. But i want to count the number of results, how can i do simply ? I can't use count(*) in FQL ?
Thanks !
You can't use COUNT in FQL, it's not supported. Why don't you get the data and perform the count operation on your end ?

Get user name of comment in comments table FQL

I am getting Facebook comments data for particular posts using FQL. FQL returns only fromid however i need fromname as well. I am using batch request for getting comments of 50 posts at same time.
My query is
SELECT object_id,post_id,fromid,time,text,id,reply_xid,post_fbid,is_private FROM comment WHERE post_id='".$postID."'
How can I do this?
you need to make an additional query, something like this:
$multiQuery_postInfo = array(
"posts" => "SELECT post_id, actor_id, message, message_tags, likes, comments, share_count, attachment, action_links, type FROM stream WHERE source_id=$pageID",
"users" => "SELECT uid, username, first_name, last_name, profile_url, pic FROM user WHERE uid IN (SELECT actor_id FROM %23posts)"
);

Facebook API : Data dump of Comment/Like Data

Here is my code:
string code = Request.QueryString["code"];
string appId = "x";
string appSecret = "x";
if (code == "" || code == null)
{
//request for authentication
Response.Redirect("https://graph.facebook.com/oauth/authorize?client_id=" + appId + "&redirect_uri=http://localhost:62543/&scope=email,read_stream");
}
else
{
var fb = new MyFB();
fb.ApplicationSecret = appSecret;
fb.ApplicationID = appId;
string accessToken = fb.GetAccessToken(code);
fb.AccessToken = accessToken;
var fbc = new FacebookClient(accessToken);
dynamic streams = fbc.Query("select post_id from stream where permalink = 'http://www.facebook.com/kevin.clough/posts/882439437244'");
Response.Write(streams.Count);
dynamic results = fbc.Query("select comments, likes from Post where id = " + streams.post_id);
foreach (dynamic comment in results.comments)
{
Response.Write(comment.from.name + ", " + comment.message + "<br/>");
}
}
Getting this error "A session cannot be used when querying the stream table with a viewer_id that is a different from the session owner."
How do I authorize this session to view my own data?
So the first problem I see is that the query is not valid. You have:
select post_id from stream
where permalink = 'http://www.facebook.com/kevin.clough/posts/882439437244'
Per the Facebook documentation here you can only query the stream table using the columns post_id, app_id, source_id, filter_key, and xid.
So you should change your first query to this (using post ID '19292868552_118464504835613' as an example):
select post_id from stream where post_id = '19292868552_118464504835613'
Alternatively, you could do the same thing with the graph using the post id:
dyanmic post1 = fbc.Get("19292868552_118464504835613");
Your second query is wrong mainly because you are querying a table that doesn't exist.
select comments, likes from Post where id = " + streams.post_id
There is no such FQL table 'Post'. I am not sure what the goal of this is, but if you want to get the likes and comments I think it would be easier to use the Graph API.
Comments:
dynamic comments = fbc.Get("19292868552_118464504835613/comments");
Likes:
dynamic likes = fbc.Get("19292868552_118464504835613/likes");
Here is Facebook's documentation on the Post Graph API: http://developers.facebook.com/docs/reference/api/post/
EDIT:
To determine the stream items of a user you will want to read their feed. You can do this using the Graph API also.
dynamic feed = fbc.Get("me/feed");
foreach (dynamic post in feed.data) {
var post_id = post.id;
}
This will get you the most recent posts of the user. From there you can loop through them to find the one you want to get more details.

Facebook FQL - All fanpage Events a User is Attending

I am trying to achieve the following via Facebook's Multiquery FQL, get a list of all Fan Page Events that i am 'Attending'.
I have the below logic, and have tried to implement something similar with the FQL statement below.
get list of all events created by / from a Fan Page (using specific id)
get a list of all my (the users) events attending
Match eid's from event_member FQL Table
If they match ids, and rsvp_status is equal to attending
display result.
FQL Multi
{"query1":"SELECT eid FROM event WHERE
eid IN (SELECT eid FROM event_member
WHERE uid
=$fanpageID)","query2":"SELECT rsvp_status FROM event_member WHERE
uid = $uidAND eid IN
(SELECT eid FROM #query1)"}
I know the statement is incorrect, but i am struggling on trying to do this. I have achieved this via Graph API using a very messy set of foreach loops in my PHP code.
Using a single multiquery FQL statement would be far more efficient.
Any suggestions would be great.
EDIT
Problem im having now, is ifaour's FQL statement works within the FQL Query console (Returns Results), however when i run the following, and empty array is returned.
When i dissect the statement to just ( SELECT eid FROM event WHERE creator =$fanPageID) the FQL Console highlights that eid in non indexable.
Here is my PHP
function usersEventsPoints($uid,
$facebook, $fanpageID){
$events = "SELECT eid FROM event WHERE creator=$fanpageID AND eid
IN (SELECT eid FROM event_member WHERE
uid=$uid AND rsvp_status =
\"attending\")";
$eventsAttendance = $facebook->api(array(
'method' => 'fql.query',
'query' =>$events, ));
$eventsAttendingcount = 0;
foreach
($eventsAttendance as $eventsAttendanceFetch) {
if($eventsAttendanceFetch['eid'] == true){
$eventsAttendingcount++;
}
}
return $eventsAttendingcount;
}
Blockquote
Any Ideas?
It's a very simple task:
SELECT eid FROM event WHERE creator = $fanpageID AND eid IN (SELECT eid FROM event_member WHERE uid = me() AND rsvp_status = "attending")
This will return all events' Ids that are created/owned by the page that has $fanpageID id and you are a member in (me()) and your status is attending.
EDIT:
You are looping the returned array just to get the count?! you can simply use:
$eventsAttendingcount = count($eventsAttendance);
Also make sure that the user you are matching, has authorized your application and granted you the correct permission!
I'm not sure I understand what the problem is - I ran your FQL statement and it returns a list of events and then your RSVP status?
On another note, why not use the events.get method. You pass in the UserId and it will display the event information for you. You can filter by RSVP status.
Just checked and the events.get method is in the process of being deprecated.
You should use the new Graph API which gives you a list of attendees for your event:
http://developers.facebook.com/docs/reference/api/event
You get the list of attendees by using
https://graph.facebook.com/EVENT_ID/attending
If you want to get the next 10 events of the facebook logged user where events are set a attending then you can build your Swift query like this:
let graphPath = "/me/events?limit=10&type=attending&since=now"
let params = ["fields": "id, name, category, start_time, end_time, timezone, updated_time, type, attending_count, owner, place"]
let request = GraphRequest(graphPath: graphPath, parameters: params, accessToken: accessToken, httpMethod: .GET, apiVersion: apiVersion)
request.start {}