Skip to content

Commit 9eee63e

Browse files
Merge pull request #8 from hub/feature/friend-service
Feature : Friend service APIs exposed
2 parents 16a224c + 251aab1 commit 9eee63e

File tree

9 files changed

+209
-12
lines changed

9 files changed

+209
-12
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ composer require hub/hubid-api-client
1414

1515
## Authentication
1616

17+
Refer to the [https://hubculture.com/developer/home](https://hubculture.com/developer/home) for obtaining the private and public keys.
18+
1719
```php
1820
include '/vendor/autoload.php';
1921

@@ -57,9 +59,9 @@ var_dump($user);
5759
Please run the following command to run a PHP server serving examples.
5860

5961
```bash
60-
make demo
62+
HUBID_PRIVATE_KEY=[your-private-key] HUBID_PUBLIC_KEY=[your-public-key] make demo
6163
```
6264

63-
Browse to [http://localhost:8085/message-service.php](http://localhost:8085/message-service.php).
65+
Browse to [http://localhost:8085/friend-service.php](http://localhost:8085/friend-service.php).
6466

65-
You may look at examples under examples directory.
67+
You may look at examples under `examples` directory.

examples/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
$config = array(
88
'base_path' => 'https://id.hubculture.com:466',
99
'verify' => false,
10-
'private_key' => 'private_5d265de1d9204f6235830ce2',
11-
'public_key' => 'public_153222247f4cbe2511208120a',
10+
'private_key' => getenv('HUBID_PRIVATE_KEY'), // __YOUR_KEY__
11+
'public_key' => getenv('HUBID_PUBLIC_KEY'), // __YOUR_KEY__
1212
'client_id' => 10014,
1313
'debug' => true,
1414
);

examples/event-service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
$redirectUrl = 'http://localhost:8085/event-service.php';
1313
if (empty($_GET['access_token'])) {
14-
$redirectLoginHelper->getAccessToken($redirectUrl);
14+
$redirectLoginHelper->redirectToLoginUrl($redirectUrl);
1515
} else {
1616
$accessToken = $_GET['access_token'];
1717
$refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken);

examples/friend-service.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author Tharanga Kothalawala <tharanga.kothalawala@hubculture.com>
4+
* @copyright (c) 2020 by HubCulture Ltd.
5+
*/
6+
7+
use Hub\HubAPI\Service\Exception\HubIdApiException;
8+
use Hub\HubAPI\Service\FriendService;
9+
10+
include __DIR__ . '/config.php';
11+
12+
$redirectUrl = 'http://localhost:8085/friend-service.php';
13+
if (empty($_GET['access_token'])) {
14+
$redirectLoginHelper->redirectToLoginUrl($redirectUrl);
15+
} else {
16+
$accessToken = $_GET['access_token'];
17+
$refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken);
18+
echo <<<HTML
19+
<pre>
20+
<br/><b>Access Token</b>: '{$accessToken}'
21+
<br/><b>Refresh Token</b>: '{$refreshedToken}'
22+
</pre>
23+
HTML;
24+
$config['token'] = $accessToken;
25+
// example event creation and retrieval
26+
$service = new FriendService($config);
27+
28+
29+
var_dump($service->getFriends());
30+
var_dump($service->getPendingFriends());
31+
var_dump($service->getFriendRequests());
32+
var_dump($service->searchFriends('user'));
33+
//
34+
// $potentialFriendId = 123456789;
35+
// try {
36+
// $response = $service->addFriend('A friend request via the API SDK', $potentialFriendId);
37+
// var_dump($response);
38+
// } catch (HubIdApiException $ex) {
39+
// $service->removeFriend($potentialFriendId);
40+
// $response = $service->addFriend('A friend request via the API SDK', $potentialFriendId);
41+
// }
42+
// var_dump($service->removeFriend($potentialFriendId));
43+
}

examples/message-service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$redirectUrl = 'http://localhost:8085/message-service.php';
1414

1515
if (empty($_GET['access_token'])) {
16-
$redirectLoginHelper->getAccessToken($redirectUrl);
16+
$redirectLoginHelper->redirectToLoginUrl($redirectUrl);
1717
} else {
1818
$accessToken = $_GET['access_token'];
1919
$refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken);

examples/ultra-service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
$redirectUrl = 'http://localhost:8085/ultra-service.php';
1212

1313
if (empty($_GET['access_token'])) {
14-
$redirectLoginHelper->getAccessToken($redirectUrl);
14+
$redirectLoginHelper->redirectToLoginUrl($redirectUrl);
1515
} else {
1616
$accessToken = $_GET['access_token'];
1717
$refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken);

examples/user-service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$redirectUrl = 'http://localhost:8085/user-service.php';
1313

1414
if (empty($_GET['access_token'])) {
15-
$redirectLoginHelper->getAccessToken($redirectUrl);
15+
$redirectLoginHelper->redirectToLoginUrl($redirectUrl);
1616
} else {
1717
$accessToken = $_GET['access_token'];
1818
$refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken);

src/Service/FriendService.php

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,148 @@
88

99
class FriendService extends TokenRefreshingService
1010
{
11+
const BASE = '/friends';
12+
const DEFAULT_PAGINATION_LIMIT = 10;
13+
14+
/**
15+
* This returns all the friends of the current authenticated user.
16+
*
17+
* @param int $offset [optional] offset for pagination
18+
* @param int $limit [optional] limit for pagination
19+
*
20+
* @return array
21+
* @see UserService::getFriends()
22+
*/
23+
public function getFriends($offset = 0, $limit = self::DEFAULT_PAGINATION_LIMIT)
24+
{
25+
$offset = intval($offset) === 0 ? 0 : intval($offset);
26+
$limit = intval($limit) === 0 ? self::DEFAULT_PAGINATION_LIMIT : intval($limit);
27+
28+
return $this->createResponse($this->get(self::BASE . "/?offset={$offset}&limit={$limit}"));
29+
}
30+
31+
/**
32+
* Returns the pending friends associated to the current authenticated user (you).
33+
* These are the list of users for whom you have sent a friend request to.
34+
* However they haven't yet added you as a friend yet
35+
*
36+
* @return array
37+
*/
38+
public function getPendingFriends()
39+
{
40+
return $this->createResponse($this->get(self::BASE . "/pending"));
41+
}
42+
1143
/**
12-
* Returns the friends associated to the current authenticated user.
44+
* Returns the users who have sent the current authenticated user (you) friend requests.
45+
* You are yet to approve them as your friends.
46+
*
1347
* @return array
1448
*/
15-
public function getFriends()
49+
public function getFriendRequests()
1650
{
51+
return $this->createResponse($this->get(self::BASE . "/requests"));
52+
}
53+
54+
/**
55+
* Returns the full user information of a friend. This returns 'No such friend' if the given user id is not a
56+
* friend yet.
57+
*
58+
* @param int $friendUserId A valid user id of a friend user.
59+
*
60+
* @return array
61+
* @see UserService::getUserById() For retriving any user.
62+
*/
63+
public function getFriendInfo($friendUserId)
64+
{
65+
return $this->createResponse($this->get(self::BASE . "/{$friendUserId}"));
66+
}
67+
68+
/**
69+
* Use this to approve a friend request that the current authenticated user (you) has received.
70+
*
71+
* @param int $friendUserId A valid user id of a pending user / friend.
72+
*
73+
* @return array
74+
*/
75+
public function approveFriendRequest($friendUserId)
76+
{
77+
return $this->createResponse($this->put("/friend/request/{$friendUserId}"));
78+
}
79+
80+
/**
81+
* Use this to decline a friend request that the current authenticated user (you) has received.
82+
*
83+
* @param int $friendUserId A valid user id of a pending user / friend.
84+
*
85+
* @return array
86+
*/
87+
public function declineFriendRequest($friendUserId)
88+
{
89+
return $this->createResponse($this->delete("/friend/request/{$friendUserId}"));
90+
}
91+
92+
/**
93+
* Use this to add a new user as a friend.
94+
*
95+
* The following errors will be thrown:
96+
* 'You are already friend with that user'
97+
* 'A friend request already exists for that user'
98+
*
99+
* @param string $message A personal note to the receiver of the request.
100+
* Message length must be between 4 to 500 characters long.
101+
* @param null|int $potentialFriendId A valid user id
102+
* @param null|string $potentialFriendEmail A valid email address of an existing user in the platform
103+
*
104+
* @return array
105+
*/
106+
public function addFriend($message, $potentialFriendId = null, $potentialFriendEmail = null)
107+
{
108+
$payload = ['message' => $message];
109+
if (!is_null($potentialFriendId) && intval($potentialFriendId) > 0) {
110+
$payload['id'] = $potentialFriendId;
111+
}
112+
if (!is_null($potentialFriendEmail)) {
113+
$payload['email'] = $potentialFriendEmail;
114+
}
115+
116+
return $this->createResponse($this->postFormData(self::BASE, $payload));
117+
}
118+
119+
/**
120+
* Use this to un-friend a existing friend.
121+
*
122+
* @param int $friendUserId A valid user id of a friend user.
123+
*
124+
* @return array
125+
*/
126+
public function removeFriend($friendUserId)
127+
{
128+
return $this->createResponse($this->delete("/friend/{$friendUserId}"));
129+
}
130+
131+
/**
132+
* Use this to search for friends in the current authenticated user's (your) friend list.
133+
*
134+
* @param string $searchKeyword The search term to search for friends in your friend list.
135+
* This can be part of a name or an email address.
136+
* @param int $offset [optional] offset for pagination
137+
* @param int $limit [optional] limit for pagination
138+
*
139+
* @return array
140+
*/
141+
public function searchFriends($searchKeyword, $offset = 0, $limit = self::DEFAULT_PAGINATION_LIMIT)
142+
{
143+
if (empty($searchKeyword)) {
144+
return [];
145+
}
146+
147+
$offset = intval($offset) === 0 ? 0 : intval($offset);
148+
$limit = intval($limit) === 0 ? self::DEFAULT_PAGINATION_LIMIT : intval($limit);
149+
$searchKeyword = urlencode($searchKeyword);
150+
17151
return $this->createResponse(
18-
$this->get("/friends")
152+
$this->get("/v2/friends/search?search={$searchKeyword}&offset={$offset}&limit={$limit}")
19153
);
20154
}
21155
}

src/Service/UserService.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class UserService extends TokenRefreshingService
1212
{
1313
const BASE = '/user';
14+
const DEFAULT_PAGINATION_LIMIT = 10;
1415

1516
/**
1617
* Use this to provision a new user in the Hub Culture platform.
@@ -82,4 +83,21 @@ public function getSelf()
8283
$this->get(self::BASE)
8384
);
8485
}
86+
87+
/**
88+
* This returns all the friends of the current authenticated user.
89+
*
90+
* @param int $offset [optional] offset for pagination
91+
* @param int $limit [optional] limit for pagination
92+
*
93+
* @return array
94+
* @see FriendService::getFriends()
95+
*/
96+
public function getFriends($offset = 0, $limit = self::DEFAULT_PAGINATION_LIMIT)
97+
{
98+
$offset = intval($offset) === 0 ? 0 : intval($offset);
99+
$limit = intval($limit) === 0 ? self::DEFAULT_PAGINATION_LIMIT : intval($limit);
100+
101+
return $this->createResponse($this->get("/friends?offset={$offset}&limit={$limit}"));
102+
}
85103
}

0 commit comments

Comments
 (0)