From 08204334f30aff3657d75ce1242db6adeab22247 Mon Sep 17 00:00:00 2001
From: Callum Hopkins <callum.e.hopkins@gmail.com>
Date: Wed, 1 Aug 2018 17:11:19 +0100
Subject: [PATCH] new getUserWithField method (#74)

* Removing extra profile fields

Removed Gender, Locale, and Timezone as these need extra verification and submission to Facebook's app team even if it's not needed for just getting a users name. Basic user approval gives access to name.

* new method getUserWithFields()

* adding back in FB fields for getUser()

* Update FacebookDriver.php
---
 src/FacebookDriver.php       | 26 ++++++++++++++++++++++++++
 tests/FacebookDriverTest.php | 15 +++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/src/FacebookDriver.php b/src/FacebookDriver.php
index eb153c4..3c63e14 100644
--- a/src/FacebookDriver.php
+++ b/src/FacebookDriver.php
@@ -407,6 +407,32 @@ public function isConfigured()
         return ! empty($this->config->get('token'));
     }
 
+    /**
+     * Retrieve specific User field information.
+     *
+     * @param array $fields
+     * @param IncomingMessage $matchingMessage
+     * @return User
+     * @throws FacebookException
+     */
+    public function getUserWithFields(array $fields, IncomingMessage $matchingMessage)
+    {
+        $messagingDetails = $this->event->get('messaging')[0];
+        // implode field array to create concatinated comma string
+        $fields = implode (",", $fields);
+        // WORKPLACE (Facebook for companies)
+        // if community isset in sender Object, it is a request done by workplace
+        if (isset($messagingDetails['sender']['community'])) {
+            $fields = 'first_name,last_name,email,title,department,employee_number,primary_phone,primary_address,picture,link,locale,name,name_format,updated_time';
+        }
+        $userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields='.$fields.'&access_token='.$this->config->get('token'));
+        $this->throwExceptionIfResponseNotOk($userInfoData);
+        $userInfo = json_decode($userInfoData->getContent(), true);
+        $firstName = $userInfo['first_name'] ?? null;
+        $lastName = $userInfo['last_name'] ?? null;
+        return new User($matchingMessage->getSender(), $firstName, $lastName, null, $userInfo);
+    }
+
     /**
      * Retrieve User information.
      *
diff --git a/tests/FacebookDriverTest.php b/tests/FacebookDriverTest.php
index 334c0f1..1965a14 100644
--- a/tests/FacebookDriverTest.php
+++ b/tests/FacebookDriverTest.php
@@ -221,6 +221,21 @@ public function it_returns_the_user_object()
         $this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
     }
 
+    /** @test */
+    public function it_returns_the_user_first_name()
+    {
+        $request = '{"object":"page","entry":[{"id":"111899832631525","time":1480279487271,"messaging":[{"sender":{"id":"1433960459967306"},"recipient":{"id":"111899832631525"},"timestamp":1480279487147,"message":{"mid":"mid.1480279487147:4388d3b344","seq":36,"text":"Hi Julia"}}]}]}';
+        $facebookResponse = '{"first_name":"John"}';
+        $htmlInterface = m::mock(Curl::class);
+        $htmlInterface->shouldReceive('get')->once()->with('https://graph.facebook.com/v3.0/1433960459967306?fields=first_name&access_token=Foo')->andReturn(new Response($facebookResponse));
+        $driver = $this->getDriver($request, null, '', $htmlInterface);
+        $message = $driver->getMessages()[0];
+        $user = $driver->getUserWithFields(['first_name'],$message);
+        $this->assertSame($user->getId(), '1433960459967306');
+        $this->assertEquals('John', $user->getFirstName());
+        $this->assertEquals(json_decode($facebookResponse, true), $user->getInfo());
+    }
+
     /** @test */
     public function it_throws_exception_in_get_user()
     {