Facebook Cookbook - Jay Goldman [122]
Solution
Assuming the current loggedinuser is friends with both of these users (or otherwise allowed to see their events):
SELECT name FROM event WHERE eid IN (SELECT eid FROM
event_member WHERE uid = $user1 AND eid IN (SELECT eid FROM event_member
WHERE uid= $user2))
You’ll need to plug the two IDs in as user1 and user2.
Discussion
This should be pretty easy to extend for more than two users if you need to, and it shouldn’t affect performance considerably, since the heavy lifting is done by the database.
Friend Table
Problem
What’s the schema of the friend table?
Solution
The friend table records the friendship between two users. Its fields are listed in Table 8-7. Queries to this table will only return data the current user is allowed to see (meaning you can’t request friendships for users the current loggedinuser isn’t friends with). More information about this table, including an up-to-date listing of fields, can be found at http://wiki.developers.facebook.com/index.php/Friend_(FQL).
Table 8-7. friend table fields
Name
Type
Index
Description
uid1
int
•
uid (user ID) of the first user
uid2
int
•
uid (user ID) of the second user
Discussion
If you’d rather use the API to access friends, try the Friends.get() and the Friends.areFriends() methods.
This table isn’t all that useful on its own, since it contains only two data points, but it is very powerful when combined with the user table (see User Table).
Friend Request Table
Problem
What’s the schema for the friend_request table?
Solution
The friend_request table records the friendship requests between two users. Its fields are listed in Table 8-8. Queries to this table will only return data the current user is allowed to see (meaning you can’t request friendships for users the current loggedinuser isn’t friends with). More information about this table, including an up-to-date listing of fields, can be found at http://wiki.developers.facebook.com/index.php/Friend_request_(FQL).
Table 8-8. friend_request table fields
Name
Type
Index
Description
uid_to
int
•
uid (user ID) of the user this request went to. You can query only on the current loggedinuser.
uid_from
int
uid (user ID) of the user this request came from.
Note that only the field marked as “Index” in this table can be used in an FQL query’s WHERE clause, but any of the fields can appear in the SELECT.
Discussion
This table isn’t all that useful on its own, since it contains only two data points, but you can combine it with other tables as a subquery.
Retrieving a User’s Friends
Problem
I need to retrieve all of a specific user’s friends using FQL.
Solution
This is a really simple query:
SELECT uid2 FROM friend WHERE uid1 = $uid;
Discussion
Luckily for FQL users everywhere, this query returns exactly the same result if you run it the other way around:
SELECT uid1 FROM friend WHERE uid2 = $uid;
You can run this query only when you’re looking for the friends of the current loggedinuser, or you’ll get an “Error 604: Can’t lookup all friends of 12345; only for the logged in user” error (see 600 Errors for more info).
Checking Whether Two Users are Friends
Problem
I need to check to see whether two users are friends using FQL.
Solution
If this query returns a result, user1 and user2 are friends:
SELECT uid1 FROM friend WHERE uid1 = $user1 AND uid2 = $user2;
Discussion
There are no privacy restrictions on this query in terms of errors, but you will get back an empty set if the current loggedinuser isn’t allowed to see the friends for one of the two users.
Retrieving a User’s Pending Friend Requests
Problem
I need to retrieve all of the pending friend requests for a specific user using FQL.
Solution
This query will return an empty set if there are no pending requests:
SELECT uid_from FROM friend_request WHERE uid_to = $uid;
Discussion
You can run this query only for the current loggedinuser or you’ll get an “Error 604, Message: Can only lookup