I need to get the list of comments for each item in a user's news feed including comments for any media in the feed items.
I'm trying to avoid multiple FQL query roundtrips, so I'd like to use multiquery.
I can get the comments for each post_id in the feed:
{
posts: 'SELECT post_id, attachment FROM stream WHERE filter_key = "nf"',
post_comments: 'SELECT post_id, fromid, text FROM comment WHERE post_id IN (SELECT post_id FROM #posts)'
}
However, there doesn't seem to be a way to reference content in the media elements so I can request the comments for media fbids, e.g.:
photo_comments: 'SELECT object_id, fromid, text FROM comment WHERE object_id in (SELECT attachment.media.fbid FROM #posts)'
This question (How to query FQL Stream by Attachment.Media.Type?) is very similar, but the answer was a bit uncertain & wasn't accepted.
Any suggestions or definitive answer?
Figures -- once I posted the question, I came across an answer to another question (http://facebook.stackoverflow.com/questions/8003581/get-photos-from-stream) that gave me what I needed:
photo_comments: 'SELECT object_id, fromid, text from comment where object_id in (SELECT attachment.media.photo.fbid from #posts)'
The example in my answer was a generic description, but turned out to be close to the actual technique (with the photo object missing in the path).
Hope this helps somebody else doing a similarly narrow search.
Related
I've been looking at many permutations of this query on StackOverflow, but nothing quite like this, and I can't figure it out.
I'm after the posts, in a specific group, that have been commented on by me() (the current user).
I can see how to get all the posts (and comments) which is simple enough:
SELECT post_id, comments FROM stream WHERE source_id=GROUP_ID
But to then reach into the comments to further filter on the fromid of me() is alluding me. Is this a job for a multi-query? Or is there some kind of IN clause I need to run?
Many thanks!
You shouldn't need a multiquery for this, if all you're getting is the post id and comments. Try this:
SELECT post_id, fromid, text FROM comment WHERE post_id IN
(SELECT post_id FROM stream WHERE source_id=GROUP_IDENTIFIER)
AND fromid=me()
I'm using the Facebook comment box plugin and am also subscribing the comment.create JavaScript event so that I can retrieve the details of the comment from Facebook using FQL and store that comment data in my application's database.
So far, when I receive the ID and href of the new comment (passed into the comment.create event handler), I am able to use the following FQL to retrieve the details of that comment:
SELECT object_id, post_id, fromid, time, text, username
FROM comment
WHERE post_fbid='*** EVENT COMMENT_ID ***' AND object_id
IN (SELECT comments_fbid FROM link_stat WHERE url='*** EVENT HREF ***')
All of this works great.
The problem occurs when a user replies to a comment. In this case, the query above fails to retrieve the comment.
I've done some digging around and have found that the Facebook 'comment' table apparently has a 'comments' field (or relationship, not sure) that contains the replies to the comment. You can even use the following query to retrieve all of the replies to a comment:
SELECT comments
FROM comment
WHERE post_fbid='*** EVENT COMMENT_ID ***' AND object_id
IN (SELECT comments_fbid FROM link_stat WHERE url='*** EVENT HREF ***')
After tinkering with the FQL test console, I can see that the replies are being added to this 'comments' field. However, rather than trying to retrieve ALL of the replies, I'm trying to lookup the specific one whose ID I received in the comment.create event handler. The problem is, since 'comments' appears to be a field rather than a linked table, I don't think I can query it.
Anyone else run into this? Any advice would be greatly appreciated.
Thanks.
Try this:
SELECT text, time, id, likes, fromid, comments FROM comment WHERE is_private = 0 AND
object_id IN (SELECT comments_fbid FROM link_stat WHERE url ='YOUR_URL') or
object_id in (select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='YOUR_URL'))
ORDER BY time DESC limit 0,10
Yes, I know that it's unusual for a user to "like" their own posts but that's what I'm looking for. I also know that you can't query the like table by user to get everything they've liked but that's not what I'm after. I just want their own posts (and later, photos) that they've liked.
I've clearly gotten pathetically rusty in my SQL skills--playing around with statements based off of the examples I've found:
SELECT source_id,message,created_time FROM stream WHERE source_id=me()
and
SELECT actor_id, post_id, message FROM stream WHERE uid IN (SELECT uid2 FROM like WHERE uid1=me())
I imagine what I want is either trivial to do or currently impossible. I expect the former.
To get the post ids that the user likes, it would be something like:
SELECT post_id
FROM like
WHERE object_id in (SELECT source_id FROM stream WHERE source_id=me())
AND user_id = me()
You could expand this to also include photo ids. And you could nest it another level deep to get more post info by using the like's post_id field.
I'm struggling with a very simple problem. The facebook documentation, as always, didn't give me enough explanation.
I attached a facebook comments plugin to my site. And using a callback of "comment.create" event, I can get information of a just-create comment.
FB.Event.subscribe('comment.create', function(response) {
alert(JSON.stringify(response));
});
The json reponse looks like:
{"href":"http://siteaddress.com/page.htm", "commentID":"111122223333" }
What I like to do now is to retrieve the data of the single comment with the commentID. Although I expected that the following way should work:
https://graph.facebook.com/111122223333
it just gave me "False". I can retrieve all comments attached to that page using:
https://graph.facebook.com/comments?ids=http://siteaddress.com/page.htm
But, what is the right way to retrieve the single comment data just created using the commentID?
I was also facing the same problem... so what i did was , I queried the last posted comment or reply from the fb comments table using fql. Here I sort the comments by time in descending order and pick the top one. though one may think that if two comments are posted at same time, it may cause ambiguity, But in my case i tried and tested it invoving more than 2 users but i always got the expected result.
FB.Event.subscribe('comment.create', function(response) {
FB.api({
method: 'fql.query',
query: "select post_fbid, fromid, object_id, text, time from comment where object_id in (select comments_fbid from link_stat where url ='URL_OF_THE_COMMENT_BOX') or object_id in (select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='URL_OF_THE_COMMENT_BOX')) order by time desc limit 1"
},
function(response) {
var feed = response[0];
alert(feed.text)
}
);
});
Hi if you are having comment id then why you dont use FQL and query to Comment Table to get all comment related data?
I am having this same problem. What seems to be happening is that the commentID and parentCommentID are actually just returning the unique ID of that page, and not a unique ID for the comment itself.
The unique ID for an individual comment is the unique ID of the page (that is, the value currently being returned as "commentID") with an underscore followed by another number (8 digits long in the tests I've done). You can look this up directly from the graph the response provided it.
I have logged a bug with Facebook to hopefully get this fixed! Bug at address below:
http://bugs.developers.facebook.net/show_bug.cgi?id=16535
Hm. I can retreive comment data by id (id format like this: 1234567890123456_12345678). Ids i retreive from url like this:
https://graph.facebook.com/comments?ids={$url}
I combined a couple approaches (including from Charsee).
// this query takes a "commentID" and "href". The commentID is returned on comment.create "response" object
// this code requires an escape function "addslashes(str)" to handle single quotes.
var query = "SELECT text, fromid FROM comment WHERE post_fbid='"+addslashes(commentID)+"' AND ( object_id in (select comments_fbid from link_stat where url ='"+addslashes(href)+"') or object_id in (select post_fbid from comment where object_id in (select comments_fbid from link_stat where url ='"+addslashes(href)+"')))";
I am trying to write a Facebook query that will return all comments posted by a user to his friends,
however I can't seem to find the correct schema. Its as if there are no 'indexable' fields to build this.
Any suggestions please?
With thanks,
Wineshtain
The indirect path for stream comments would be something like
select * from comments where fromid = <my_id> and object_id in (
select post_id from stream where sourceid in (
select uid1 from friend where uid2 = <my_id> ) )
for photos, substitute the middle query with
SELECT pid FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner IN ( ...
Unfortunately the security settings may restrict the querying your friends wall posts and photos.
I don't believe you can accomplish this in a direct manner as you describe. FQL tables are generally only indexed on a limited criteria (for performance reasons I'm sure). In the case of the Comments FQL Table, you can only select comments via a post ID or an xid.
Unfortunately, this means that you have to know the objects that a user has commented on before you can get the comments for it. You would have to have previously selected all the posts, photos, etc. that you wished to get the comments for before you could retrieve them.