All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threerings.facebook.FacebookSessionApi Maven / Gradle / Ivy

The newest version!
//
// $Id$

package com.threerings.facebook;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJaxbRestClient;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import static com.threerings.facebook.Log.log;

/**
 * A class to perform common requests given a view into the Facebook API with a given person's
 * sessionKey.
 */
public class FacebookSessionApi
{
    public static FQL.Field UID = new FQL.Field("uid");
    public static FQL.Field NAME = new FQL.Field("name");

    public static Function, String> FRIEND_FIELDS_TO_UID =
        new Function, String>() {
            public String apply (Map map) {
                return map.get(UID);
            }
        };

    public FacebookSessionApi (String apiKey, String appSecret, String sessionKey)
    {
        _client = new FacebookJaxbRestClient(apiKey, appSecret, sessionKey);
    }

    /**
     * Returns the name of the person that owns this session
     */
    public String getMyName ()
    {
        return getMyInfo(NAME).get(NAME);
    }

    public Map getMyInfo (FQL.Field... userFields)
    {
        FQLQuery query = new FQLQuery(USER_TABLE, userFields, new FQL.Where(UID.is("me()")));
        Map values = Maps.newHashMap();
        try {
            FQLQuery.Record rec = query.run(_client).iterator().next();
            if (rec != null) {
                for (FQL.Field field : userFields) {
                    values.put(field, rec.getField(field));
                }
            }
        } catch (FacebookException fe) {
            log.warning("FacebookException fetching my user info", fe);
        }
        return values;
    }

    /**
     * Get the collection of friends of this session that also play this app.
     */
    public Collection getFriendsInApp ()
    {
        return Sets.newHashSet(Iterables.transform(getFriendsInApp(UID), FRIEND_FIELDS_TO_UID));
    }

    public Set> getFriendsInApp (FQL.Field... userFields)
    {
        FQLQuery friendQuery = new FQLQuery(FRIEND_TABLE, new FQL.Field[]{UID2},
            new FQL.Where(UID1.is("me()")));
        Set userFieldSet = Sets.newHashSet(userFields);
        userFieldSet.add(IS_APP_USER);
        FQLQuery userQuery = new FQLQuery(USER_TABLE, userFieldSet.toArray(new FQL.Field[] {}),
            new FQL.Where(UID.in(Collections.singleton(friendQuery))));
        try {
            Set> friends = Sets.newHashSet();
            for (FQLQuery.Record rec : Iterables.filter(userQuery.run(_client), IS_APP_USER_PRED)) {
                Map friendValues = Maps.newHashMap();
                for (FQL.Field field : userFields) {
                    friendValues.put(field, rec.getField(field));
                }
                friends.add(friendValues);
            }
            return friends;

        } catch (FacebookException fe) {
            log.warning("FacebookException fetching session friends.", "message", fe.getMessage());
        }
        return Collections.emptySet();
    }

    protected static Predicate IS_APP_USER_PRED =
        new Predicate() {
            public boolean apply (FQLQuery.Record rec) {
                return "1".equals(rec.getField(IS_APP_USER));
            }
        };

    protected static String USER_TABLE = "user";
    protected static String FRIEND_TABLE = "friend";

    protected static FQL.Field IS_APP_USER = new FQL.Field("is_app_user");
    protected static FQL.Field UID1 = new FQL.Field("uid1");
    protected static FQL.Field UID2 = new FQL.Field("uid2");

    protected FacebookJaxbRestClient _client;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy