Skip to content

Commit 557c1a4

Browse files
covered the whole friends API in the SDK
1 parent 16a224c commit 557c1a4

File tree

3 files changed

+171
-5
lines changed

3 files changed

+171
-5
lines changed

examples/friend-service.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
$potentialFriendId = 21025;
29+
30+
try {
31+
$response = $service->addFriend('A friend request via the API SDK', $potentialFriendId);
32+
var_dump($response);
33+
} catch (HubIdApiException $ex) {
34+
$service->removeFriend($potentialFriendId);
35+
$response = $service->addFriend('A friend request via the API SDK', $potentialFriendId);
36+
}
37+
var_dump($service->getPendingFriends());
38+
var_dump($service->removeFriend($potentialFriendId));
39+
var_dump($service->getPendingFriends());
40+
}

src/Service/FriendService.php

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,122 @@
88

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

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)