5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
11 use \Instagram\Collection\MediaCollection;
12 use \Instagram\Collection\LikedMediaCollection;
13 use \Instagram\Collection\UserCollection;
18 * Holds the currently logged in user
20 * @see \Instagram\Instagram->getCurrentUser()
21 * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/current_user.php}
22 * {@link http://galengrover.com/projects/instagram/?example=current_user.php}
24 class CurrentUser extends \Instagram\User {
27 * Holds relationship info for the current user
32 protected $relationships = array();
35 * Holds liked info for the current user
37 * Current user likes are stored in media objects
38 * If a media is liked after a media has been fetched the like will not be a part of the media object
43 protected $liked = array();
46 * Holds unliked info for the current user
51 protected $unliked = array();
56 * @return string Returns "self"
59 public function getApiId() {
64 * Does the current use like a media
66 * @param \Instagram\Media $media Media to query for a like from the current user
69 public function likes( \Instagram\Media $media ) {
71 isset( $this->liked[$media->getId()] ) ||
73 isset( $media->getData()->user_has_liked ) &&
74 (bool)$media->getData()->user_has_liked === true &&
75 !isset( $this->unliked[$media->getId()] )
80 * Add like from current user
82 * @param \Instagram\Media|string $media Media to add a like to from the current user
85 public function addLike( $media ) {
86 if ( $media instanceof \Instagram\Media ) {
87 $media = $media->getId();
89 $this->proxy->like( $media );
90 unset( $this->unliked[$media] );
91 $this->liked[$media] = true;
95 * Delete like from current user
97 * @param \Instagram\Media|string $media Media to delete a like to from the current user
100 public function deleteLike( $media ) {
101 if ( $media instanceof \Instagram\Media ) {
102 $media = $media->getId();
104 $this->proxy->unLike( $media );
105 unset( $this->liked[$media] );
106 $this->unliked[$media] = true;
112 * @param \Instagram\Media|string Media to add a comment to
113 * @param string $text Comment text
116 public function addMediaComment( $media, $text ) {
117 if ( $media instanceof \Instagram\Media ) {
118 $media = $media->getId();
120 $this->proxy->addMediaComment( $media, $text );
126 * @param \Instagram\Media|string Media to delete a comment from
127 * @param string $text Comment text
130 public function deleteMediaComment( $media, $comment ) {
131 if ( $media instanceof \Instagram\Media ) {
132 $media = $media->getId();
134 if ( $comment instanceof \Instagram\Comment ) {
135 $comment = $comment->getId();
137 $this->proxy->deleteMediaComment( $media, $comment );
141 * Update relationship with a user
143 * Internal function that updates relationships
145 * @see \Instagram\CurrentUser::follow
146 * @see \Instagram\CurrentUser::unFollow
147 * @see \Instagram\CurrentUser::getRelationship
148 * @see \Instagram\CurrentUser::approveFollowRequest
149 * @see \Instagram\CurrentUser::ignoreFollowRequest
150 * @see \Instagram\CurrentUser::block
151 * @see \Instagram\CurrentUser::unblock
153 * @param \Instagram\User|string User object or user id whose relationship you'd like to update
156 protected function updateRelationship( $user ) {
157 if ( $user instanceof \Instagram\User ) {
158 $user = $user->getId();
160 if ( !isset( $this->relationships[ $user ] ) ) {
161 $this->relationships[ $user ] = $this->proxy->getRelationshipToCurrentUser( $user );
168 * @param \Instagram\User|string User object or user id who should be followed
171 public function follow( $user ) {
172 if ( $user instanceof \Instagram\User ) {
173 $user = $user->getId();
175 $this->updateRelationship( $user );
176 $response = $this->proxy->modifyRelationship( $user, 'follow' );
177 foreach( $response as $r => $v ) {
178 $this->relationships[ $user ]->$r = $v;
186 * @param \Instagram\User|string $user User object or user id who should be approved for following
189 public function approveFollowRequest( $user ) {
190 if ( $user instanceof \Instagram\User ) {
191 $user = $user->getId();
193 $this->updateRelationship( $user );
194 $response = $this->proxy->modifyRelationship( $user, 'approve' );
195 foreach( $response as $r => $v ) {
196 $this->relationships[ $user ]->$r = $v;
204 * @param \Instagram\User|string $user User object or user id who should be ignored
207 public function ignoreFollowRequest( $user ) {
208 if ( $user instanceof \Instagram\User ) {
209 $user = $user->getId();
211 $this->updateRelationship( $user );
212 $response = $this->proxy->modifyRelationship( $user, 'ignore' );
213 foreach( $response as $r => $v ) {
214 $this->relationships[ $user ]->$r = $v;
222 * @param \Instagram\User|string $user User object or user id who should be unfollowed
225 public function unFollow( $user ) {
226 if ( $user instanceof \Instagram\User ) {
227 $user = $user->getId();
229 $this->updateRelationship( $user );
230 $response = $this->proxy->modifyRelationship( $user, 'unfollow' );
231 foreach( $response as $r => $v ) {
232 $this->relationships[ $user ]->$r = $v;
240 * @param \Instagram\User|string $user User object or user id who should be blocked
243 public function block( $user ) {
244 if ( $user instanceof \Instagram\User ) {
245 $user = $user->getId();
247 $this->updateRelationship( $user );
248 $response = $this->proxy->modifyRelationship( $user, 'block' );
249 foreach( $response as $r => $v ) {
250 $this->relationships[ $user ]->$r = $v;
258 * @param \Instagram\User|string $user User object or user id who should be unblocked
261 public function unblock( $user ) {
262 if ( $user instanceof \Instagram\User ) {
263 $user = $user->getId();
265 $this->updateRelationship( $user );
266 $response = $this->proxy->modifyRelationship( $user, 'unblock' );
267 foreach( $response as $r => $v ) {
268 $this->relationships[ $user ]->$r = $v;
274 * Check if the current user can view a user
279 public function canViewUser( $user_id ) {
280 $relationship = $this->proxy->getRelationshipToCurrentUser( $user_id );
281 if ( $this->getId() == $user_id || !(bool)$relationship->target_user_is_private || $relationship->outgoing_status == 'follows' ) {
290 * Get the complete relationship to a user
292 * @param \Instagram\User|string $user User object or user id to get the relationship details of
295 public function getRelationship( $user ) {
296 if ( $user instanceof \Instagram\User ) {
297 $user = $user->getId();
299 $this->updateRelationship( $user );
300 return $this->relationships[ $user ];
306 * Get the users that have requested to follow the current user
308 * @return \Instagram\Collection\UserCollection
310 public function getFollowRequests() {
311 return new UserCollection( $this->proxy->getFollowRequests(), $this->proxy );
315 * Check outgoing request status
317 * Check if the current user is currently waiting approval of a follow request
319 * @param \Instagram\User|string $user User object or user id to check the status of
322 public function isAwaitingFollowApprovalFrom( $user ) {
323 if ( $user instanceof \Instagram\User ) {
324 $user = $user->getId();
326 return $this->getRelationship( $user )->outgoing_status == 'requested';
330 * Check incoming request status
332 * Check if the current user has been requested to be followed by a user
334 * @param \Instagram\User|string $user User object or user id to check the status of
337 public function hasBeenRequestedBy( $user ) {
338 if ( $user instanceof \Instagram\User ) {
339 $user = $user->getId();
341 return $this->getRelationship( $user )->incoming_status == 'requested_by';
345 * Check following status
347 * Check if the current user is blocking a user
349 * @param \Instagram\User|string $user User object or user id to check the following status of
352 public function isBlocking( $user ) {
353 if ( $user instanceof \Instagram\User ) {
354 $user = $user->getId();
356 return $this->getRelationship( $user )->incoming_status == 'blocked_by_you';
360 * Check following status
362 * Check if hte current user is following a user
364 * @param \Instagram\User|string $user User object or user id to check the following status of
367 public function isFollowing( $user ) {
368 if ( $user instanceof \Instagram\User ) {
369 $user = $user->getId();
371 return $this->getRelationship( $user )->outgoing_status == 'follows';
375 * Check followed by status
377 * Check if the current user is followed by a user
379 * @param \Instagram\User|string $user User object or user id to check the followed by status of
382 public function isFollowedBy( $user ) {
383 if ( $user instanceof \Instagram\User ) {
384 $user = $user->getId();
386 return $this->getRelationship( $user )->incoming_status == 'followed_by';
390 * Get the current user's feed
392 * This can be paginated with the next_max_id param obtained from MediaCollection->getNext()
394 * @param array $params Optional params to pass to the endpoint
395 * @return \Instagram\Collection\MediaCollection
398 public function getFeed( array $params = null ) {
399 return new MediaCollection( $this->proxy->getFeed( $params ), $this->proxy );
403 * Get the current user's liked media
405 * This can be paginated with the next_max_like_id param obtained from MediaCollection->getNext()
407 * @param array $params Optional params to pass to the endpoint
408 * @return \Instagram\Collection\MediaCollection
411 public function getLikedMedia( array $params = null ) {
412 return new LikedMediaCollection( $this->proxy->getLikedMedia( $params ), $this->proxy );