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

org.springframework.social.facebook.api.impl.FriendTemplate Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.social.facebook.api.impl;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.social.facebook.api.FamilyMember;
import org.springframework.social.facebook.api.FriendList;
import org.springframework.social.facebook.api.FriendOperations;
import org.springframework.social.facebook.api.GraphApi;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.PagingParameters;
import org.springframework.social.facebook.api.Reference;
import org.springframework.social.facebook.api.User;
import org.springframework.social.facebook.api.UserInvitableFriend;
import org.springframework.social.facebook.api.UserTaggableFriend;
import org.springframework.social.support.URIBuilder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

class FriendTemplate implements FriendOperations {
	
	private final GraphApi graphApi;

	private final RestTemplate restTemplate;

	public FriendTemplate(GraphApi graphApi, RestTemplate restTemplate) {
		this.graphApi = graphApi;
		this.restTemplate = restTemplate;
	}
	
	public PagedList getFriendLists() {
		return graphApi.fetchConnections("me", "friendlists", FriendList.class);
	}

	public FriendList getFriendList(String friendListId) {
		return graphApi.fetchObject(friendListId, FriendList.class);
	}
	
	public PagedList getFriends() {
		return getFriends("me");
	}
	
	public PagedList getFriendIds() {
		return getFriendIds("me");
	}
	
	public PagedList getFriendProfiles() {
		return getFriendProfiles("me");
	}

	public PagedList getFriendProfiles(PagingParameters pagedListParameters) {
		return getFriendProfiles("me", pagedListParameters);
	}
	
	public PagedList getFriends(String userId) {
		return graphApi.fetchConnections(userId, "friends", Reference.class);
	}
	
	public PagedList getFriendIds(String userId) {
		URI uri = URIBuilder.fromUri(graphApi.getBaseGraphApiUrl() + userId + "/friends").queryParam("fields", "id").build();
		JsonNode responseNode = restTemplate.getForObject(uri, JsonNode.class);
		ArrayNode dataNode = (ArrayNode) responseNode.get("data");
		List idList = new ArrayList(dataNode.size());
		for (JsonNode entryNode : dataNode) {
			idList.add(entryNode.get("id").textValue());
		}
		
		Integer totalCount = responseNode.has("summary") && responseNode.get("summary").has("total_count") ?
				responseNode.get("summary").get("total_count").asInt() : null;
		return new PagedList(idList, null, null, totalCount);
	}
	
	public PagedList getFriendProfiles(String userId) {
		MultiValueMap parameters = new LinkedMultiValueMap();
		parameters.set("fields", FULL_PROFILE_FIELDS);
		return graphApi.fetchConnections(userId, "friends", User.class, parameters);
	}

	public PagedList getFriendProfiles(String userId, PagingParameters pagedListParameters) {
		MultiValueMap parameters = PagedListUtils.getPagingParameters(pagedListParameters);
		parameters.set("fields", FULL_PROFILE_FIELDS);
		return graphApi.fetchConnections(userId, "friends", User.class, parameters);
	}
	
	public PagedList getFamily() {
		return graphApi.fetchConnections("me", "family", FamilyMember.class);
	}

	public PagedList getFamily(String userId) {
		return graphApi.fetchConnections(userId, "family", FamilyMember.class);
	}

	public PagedList getInvitableFriends() {
		return graphApi.fetchConnections("me", "invitable_friends", UserInvitableFriend.class, 
				"id", "name" ,"first_name", "last_name", "middle_name");
	}

	public PagedList getTaggableFriends() {
		return graphApi.fetchConnections("me", "taggable_friends", UserTaggableFriend.class, 
				"id", "name" ,"picture", "first_name", "last_name", "middle_name");  
	}
	
	private static final String FULL_PROFILE_FIELDS = "id,name,first_name,last_name,gender,locale,education,work,email,third_party_id,link,timezone,updated_time,verified,about,bio,birthday,location,hometown,interested_in,religion,political,quotes,relationship_status,significant_other,website";

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy