Announcement Announcement Module
Collapse
No announcement yet.
Finding facebook friends that already use the application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Finding facebook friends that already use the application

    What I want to do is to find the friends that are already users of the samme application. Is that possible ?

    The use case is that X of my friends are already users of the app and I want to easily know that so I can interact with them in the app !

    Facebook fb = usersConnectionRepository.createConnectionReposito ry(searcher.getId()).getPrimaryConnection(Facebook .class).getApi();
    List<Reference> friends = fb.friendOperations().getFriends();
    for (Reference friend : friends) {
    if (friendIsUser(friend.getId()) {
    //do something with him/her
    }
    }

    how to code friendIsUser ?

    that is, is there already a method that does
    "select userId from " + tablePrefix + "UserConnection where providerId = ? and providerUserId = ?"

  • #2
    There is a way to do it, but it's not as straightforward as a simple API call and there's not (currently) a Spring Social operation to support it directly. However...

    The Facebook Graph API endpoint you want is /me/friends, which is the same endpoint that FriendOperations.getFriends() uses. But you have to ask that endpoint to return the "installed" field to know whether or not they are a user of the application (that is, whether or not they are users of the application who is asking for the list of friends--I do not think you can ask which friends are users of another application).

    To do that, the proper endpoint request should look like: https://graph.facebook.com/me/friend...name,installed

    Although there's not an API binding method in Spring Social that supports this directly, you can still do it via the fetchConnections() method on Facebook (actually, it's on the GraphApi interface, which Facebook extends):

    Code:
    Facebook fb = usersConnectionRepository.createConnectionReposito ry(searcher.getId()).getPrimaryConnection(Facebook .class).getApi();
    List<MyFriend> myFriends = fb.fetchConnections("me", "friends", MyFriend.class, "name", "installed");
    Note that the MyFriend class isn't part of Spring Social. You'll need to create that yourself--possibly by simply extending Reference and adding a boolean installed property. Then you'll need to cycle through the list of MyFriend you get and look for the ones where installed == true. Those are the friends who have the app installed.

    This sounds like a nice addition to Spring Social's Facebook API binding. That is, it would be nice if Spring Social Facebook had an API binding method that would just return a list of friends who have the same app installed so that you wouldn't have to sift through the list yourself. Would you mind creating an issue for the API binding to support it directly at https://jira.springsource.org/browse/SOCIALFB ?

    Comment


    • #3
      Wahou : that is what I call "awesome support" !

      I'm going to try this right now and tell you my progress !

      ATM, https://jira.springsource.org/browse/SOCIALFB-107

      Originally posted by habuma View Post
      There is a way to do it, but it's not as straightforward as a simple API call and there's not (currently) a Spring Social operation to support it directly. However...

      The Facebook Graph API endpoint you want is /me/friends, which is the same endpoint that FriendOperations.getFriends() uses. But you have to ask that endpoint to return the "installed" field to know whether or not they are a user of the application (that is, whether or not they are users of the application who is asking for the list of friends--I do not think you can ask which friends are users of another application).

      To do that, the proper endpoint request should look like: https://graph.facebook.com/me/friend...name,installed

      Although there's not an API binding method in Spring Social that supports this directly, you can still do it via the fetchConnections() method on Facebook (actually, it's on the GraphApi interface, which Facebook extends):

      Code:
      Facebook fb = usersConnectionRepository.createConnectionReposito ry(searcher.getId()).getPrimaryConnection(Facebook .class).getApi();
      List<MyFriend> myFriends = fb.fetchConnections("me", "friends", MyFriend.class, "name", "installed");
      Note that the MyFriend class isn't part of Spring Social. You'll need to create that yourself--possibly by simply extending Reference and adding a boolean installed property. Then you'll need to cycle through the list of MyFriend you get and look for the ones where installed == true. Those are the friends who have the app installed.

      This sounds like a nice addition to Spring Social's Facebook API binding. That is, it would be nice if Spring Social Facebook had an API binding method that would just return a list of friends who have the same app installed so that you wouldn't have to sift through the list yourself. Would you mind creating an issue for the API binding to support it directly at https://jira.springsource.org/browse/SOCIALFB ?

      Comment


      • #4
        This works PERFECTLY !

        The only gotcha is the MyFriend class : we have to create it from scratch with getter and setter instead of extending Reference

        Or they will be a problem with JSON deserialisation :

        Error deserializing data from Facebook: No suitable constructor found for type [simple type, class social.MyFriend]: can not instantiate from JSON object (need to add/enable type information?)

        Comment


        • #5
          You should be able to create a MyFriend class that extends Reference, but you'll also probably need to properly annotate it with Jackson annotations to guide the creation of it.

          I saw that you created an issue in JIRA for this and I'll definitely consider it for a future release. It's much to close to 1.1.0.M1 for me to work it in for that release, but maybe for 1.1.0.M2. When that happens, the custom MyFriend class won't be necessary.

          Comment


          • #6
            Related to this, is there a way to get the above friends' local accounts?

            Let's say that from the above example, I got a reference to a friend and I want to query my local database for information associated to that friend -- how should I do this?

            During sign-up, I can manually map the Facebook Id to the local user Id. But if the user connected to a provider after sign-up, then I need to hack the ConnectController to apply the mapping. I'm hoping that there's a better way to do this (like how UsersConnectionRepository associates a ConnectionRepository to a local account).

            To give you an overview, I'm planning to do something similar to TripAdvisor's Facebook integration wherein you can see the reviews of your friends which are also using TripAdvisor.
            Last edited by jramoyo; Dec 28th, 2012, 01:10 AM. Reason: additional information

            Comment


            • #7
              I realized I can do the linking from a ConnectInterceptor.

              Code:
              Profile profile = profileService.findByUsername(request
              	.getUserPrincipal().getName());
              profile.setFacebookId(connection.fetchUserProfile().getUsername());
              profileService.save(profile);
              I noticed however that interceptors (both Connect and Disconnect) do not get triggered when annotated with @Transactional

              Originally posted by jramoyo View Post
              Related to this, is there a way to get the above friends' local accounts?

              Let's say that from the above example, I got a reference to a friend and I want to query my local database for information associated to that friend -- how should I do this?

              During sign-up, I can manually map the Facebook Id to the local user Id. But if the user connected to a provider after sign-up, then I need to hack the ConnectController to apply the mapping. I'm hoping that there's a better way to do this (like how UsersConnectionRepository associates a ConnectionRepository to a local account).

              To give you an overview, I'm planning to do something similar to TripAdvisor's Facebook integration wherein you can see the reviews of your friends which are also using TripAdvisor.

              Comment


              • #8
                if you are not afraid of sql, you can simply do a request to the userconnection table :
                something like (I do not have the db under my eyes at the moment)
                select userid from userconnection where provider='facebook' and providerid = ?

                Comment

                Working...
                X