<commit_before>﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.AspNet.Mvc.Facebook.Attributes;
using Microsoft.AspNet.Mvc.Facebook.Services;

namespace Microsoft.AspNet.Mvc.Facebook.Models
{
    public class FacebookObjectModelBinder : IModelBinder
    {
        private readonly IFacebookService facebookService;
        private readonly IFacebookUserStorageService facebookUserStorageService;
        private readonly IFacebookObjectStorageService facebookObjectStorageService;

        public FacebookObjectModelBinder()
            : this(DefaultFacebookService.Instance, FacebookSettings.DefaultUserStorageService, FacebookSettings.DefaultObjectStorageService)
        {
        }

        public FacebookObjectModelBinder(IFacebookService facebookService, IFacebookUserStorageService facebookUserStorageService, IFacebookObjectStorageService facebookObjectStorageService)
        {
            this.facebookService = facebookService;
            this.facebookUserStorageService = facebookUserStorageService;
            this.facebookObjectStorageService = facebookObjectStorageService;
        }

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var client = facebookService.CreateClient();

            var requestParam = controllerContext.HttpContext.Request.Params["signed_request"];
            if (requestParam != null)
            {
                dynamic sr = client.ParseSignedRequest(FacebookSettings.AppSecret, requestParam);
                client.AccessToken = sr.oauth_token;

                return GetObjects(bindingContext.ModelType, (string)sr.user_id, client);
            }

            return null;
        }

        private object GetObjects(Type modelType, string userFacebookId, global::Facebook.FacebookClient client)
        {
            var typeName = GetTypeName(modelType);
            if (string.IsNullOrEmpty(typeName))
            {
                return null;
            }

            //TODO: (ErikPo) Total HACK but it will have to do for now. Not sure how to call a generic method with only the modelType
            var mi = facebookObjectStorageService.GetType().GetMethod("GetObjects");
            var objects = (FacebookObjectList<FacebookObject>)mi.Invoke(facebookObjectStorageService, new object[] { userFacebookId }); //facebookObjectStorageService.GetObjects(userFacebookId);

            if (objects == null || objects.Count == 0/* || Some other time period has been met and we should sync again */)
            {
                return LoadObjects(client, modelType, userFacebookId, "me/friends" + facebookService.GetFields(modelType));
            }

            var castedObjects = (IList)Activator.CreateInstance(modelType);
            foreach (var obj in objects)
            {
                castedObjects.Add(obj);
            }
            return castedObjects;<commit_msg>Removing the comment that was causing policheck failure<commit_after>﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.AspNet.Mvc.Facebook.Attributes;
using Microsoft.AspNet.Mvc.Facebook.Services;

namespace Microsoft.AspNet.Mvc.Facebook.Models
{
    public class FacebookObjectModelBinder : IModelBinder
    {
        private readonly IFacebookService facebookService;
        private readonly IFacebookUserStorageService facebookUserStorageService;
        private readonly IFacebookObjectStorageService facebookObjectStorageService;

        public FacebookObjectModelBinder()
            : this(DefaultFacebookService.Instance, FacebookSettings.DefaultUserStorageService, FacebookSettings.DefaultObjectStorageService)
        {
        }

        public FacebookObjectModelBinder(IFacebookService facebookService, IFacebookUserStorageService facebookUserStorageService, IFacebookObjectStorageService facebookObjectStorageService)
        {
            this.facebookService = facebookService;
            this.facebookUserStorageService = facebookUserStorageService;
            this.facebookObjectStorageService = facebookObjectStorageService;
        }

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var client = facebookService.CreateClient();

            var requestParam = controllerContext.HttpContext.Request.Params["signed_request"];
            if (requestParam != null)
            {
                dynamic sr = client.ParseSignedRequest(FacebookSettings.AppSecret, requestParam);
                client.AccessToken = sr.oauth_token;

                return GetObjects(bindingContext.ModelType, (string)sr.user_id, client);
            }

            return null;
        }

        private object GetObjects(Type modelType, string userFacebookId, global::Facebook.FacebookClient client)
        {
            var typeName = GetTypeName(modelType);
            if (string.IsNullOrEmpty(typeName))
            {
                return null;
            }

            //TODO: (ErikPo) Figure out how to call a generic method with only the modelType
            var mi = facebookObjectStorageService.GetType().GetMethod("GetObjects");
            var objects = (FacebookObjectList<FacebookObject>)mi.Invoke(facebookObjectStorageService, new object[] { userFacebookId }); //facebookObjectStorageService.GetObjects(userFacebookId);

            if (objects == null || objects.Count == 0/* || Some other time period has been met and we should sync again */)
            {
                return LoadObjects(client, modelType, userFacebookId, "me/friends" + facebookService.GetFields(modelType));
            }

            var castedObjects = (IList)Activator.CreateInstance(modelType);
            foreach (var obj in objects)
            {
                castedObjects.Add(obj);
            }
            return castedObjects;
{'fraction_non_alphanumeric': 0.06976744186046512, 'fraction_numerical': 0.00031640563202024995, 'mean_word_length': 2.9291485394655066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2893614', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1419, 'n_words': 358}