|
8 | 8 |
|
9 | 9 | class FriendService extends TokenRefreshingService |
10 | 10 | { |
| 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 | + |
11 | 43 | /** |
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 | + * |
13 | 47 | * @return array |
14 | 48 | */ |
15 | | - public function getFriends() |
| 49 | + public function getFriendRequests() |
16 | 50 | { |
| 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 | + |
17 | 151 | return $this->createResponse( |
18 | | - $this->get("/friends") |
| 152 | + $this->get("/v2/friends/search?search={$searchKeyword}&offset={$offset}&limit={$limit}") |
19 | 153 | ); |
20 | 154 | } |
21 | 155 | } |
0 commit comments