Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / CurrentUser.php
1 <?php
2
3 /**
4 * Instagram PHP
5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
7 */
8
9 namespace Instagram;
10
11 use \Instagram\Collection\MediaCollection;
12 use \Instagram\Collection\LikedMediaCollection;
13 use \Instagram\Collection\UserCollection;
14
15 /**
16  * Current User
17  *
18  * Holds the currently logged in user
19  *
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}
23  */
24 class CurrentUser extends \Instagram\User {
25
26     /**
27      * Holds relationship info for the current user
28      *
29      * @access protected
30      * @var array
31      */
32     protected $relationships = array();
33
34     /**
35      * Holds liked info for the current user
36      *
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
39      *
40      * @access protected
41      * @var array
42      */
43     protected $liked = array();
44
45     /**
46      * Holds unliked info for the current user
47      *
48      * @access protected
49      * @var array
50      */
51     protected $unliked = array();
52
53     /**
54      * Get the API ID
55      *
56      * @return string Returns "self"
57      * @access public
58      */
59     public function getApiId() {
60         return 'self';
61     }
62
63     /**
64      * Does the current use like a media
65      *
66      * @param \Instagram\Media $media Media to query for a like from the current user
67      * @access public
68      */
69     public function likes( \Instagram\Media $media ) {
70         return
71             isset( $this->liked[$media->getId()] ) ||
72             (
73                 isset( $media->getData()->user_has_liked ) &&
74                 (bool)$media->getData()->user_has_liked === true &&
75                 !isset( $this->unliked[$media->getId()] )
76             );
77     }
78
79     /**
80      * Add like from current user
81      *
82      * @param \Instagram\Media|string $media Media to add a like to from the current user
83      * @access public
84      */
85     public function addLike( $media ) {
86         if ( $media instanceof \Instagram\Media ) {
87             $media = $media->getId();
88         }
89         $this->proxy->like( $media );
90         unset( $this->unliked[$media] );
91         $this->liked[$media] = true;
92     }
93
94     /**
95      * Delete like from current user
96      *
97      * @param \Instagram\Media|string $media Media to delete a like to from the current user
98      * @access public
99      */
100     public function deleteLike( $media ) {
101         if ( $media instanceof \Instagram\Media ) {
102             $media = $media->getId();
103         }
104         $this->proxy->unLike( $media );
105         unset( $this->liked[$media] );
106         $this->unliked[$media] = true;
107     }
108
109     /**
110      * Add a comment
111      *
112      * @param \Instagram\Media|string Media to add a comment to
113      * @param string $text Comment text
114      * @access public
115      */
116     public function addMediaComment( $media, $text ) {
117         if ( $media instanceof \Instagram\Media ) {
118             $media = $media->getId();
119         }
120         $this->proxy->addMediaComment( $media, $text );
121     }
122
123     /**
124      * Delete a comment
125      *
126      * @param \Instagram\Media|string Media to delete a comment from
127      * @param string $text Comment text
128      * @access public
129      */
130     public function deleteMediaComment( $media, $comment ) {
131         if ( $media instanceof \Instagram\Media ) {
132             $media = $media->getId();
133         }
134         if ( $comment instanceof \Instagram\Comment ) {
135             $comment = $comment->getId();
136         }
137         $this->proxy->deleteMediaComment( $media, $comment );
138     }
139
140     /**
141      * Update relationship with a user
142      *
143      * Internal function that updates relationships
144      *
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
152      *
153      * @param \Instagram\User|string User object or user id whose relationship you'd like to update
154      * @access protected
155      */
156     protected function updateRelationship( $user ) {
157         if ( $user instanceof \Instagram\User ) {
158             $user = $user->getId();
159         }
160         if ( !isset( $this->relationships[ $user ] ) ) {
161             $this->relationships[ $user ] = $this->proxy->getRelationshipToCurrentUser( $user );
162         }
163     }
164
165     /**
166      * Follow user
167      *
168      * @param \Instagram\User|string User object or user id who should be followed
169      * @return boolean
170      */
171     public function follow( $user ) {
172         if ( $user instanceof \Instagram\User ) {
173             $user = $user->getId();
174         }
175         $this->updateRelationship( $user );
176         $response = $this->proxy->modifyRelationship( $user, 'follow' );
177         foreach( $response as $r => $v ) {
178             $this->relationships[ $user ]->$r = $v;
179         }
180         return true;
181     }
182
183     /**
184      * Approve follower
185      *
186      * @param \Instagram\User|string $user User object or user id who should be approved for following
187      * @return boolean
188      */
189     public function approveFollowRequest( $user ) {
190         if ( $user instanceof \Instagram\User ) {
191             $user = $user->getId();
192         }
193         $this->updateRelationship( $user ); 
194         $response = $this->proxy->modifyRelationship( $user, 'approve' );
195         foreach( $response as $r => $v ) {
196             $this->relationships[ $user ]->$r = $v;
197         }
198         return true;
199     }
200
201     /**
202      * Ignore follower
203      *
204      * @param \Instagram\User|string $user User object or user id who should be ignored
205      * @return boolean
206      */
207     public function ignoreFollowRequest( $user ) {
208         if ( $user instanceof \Instagram\User ) {
209             $user = $user->getId();
210         }
211         $this->updateRelationship( $user ); 
212         $response = $this->proxy->modifyRelationship( $user, 'ignore' );
213         foreach( $response as $r => $v ) {
214             $this->relationships[ $user ]->$r = $v;
215         }
216         return true;
217     }
218
219     /**
220      * Unfollow user
221      *
222      * @param \Instagram\User|string $user User object or user id who should be unfollowed
223      * @return boolean
224      */
225     public function unFollow( $user ) {
226         if ( $user instanceof \Instagram\User ) {
227             $user = $user->getId();
228         }
229         $this->updateRelationship( $user ); 
230         $response = $this->proxy->modifyRelationship( $user, 'unfollow' );
231         foreach( $response as $r => $v ) {
232             $this->relationships[ $user ]->$r = $v;
233         }
234         return true;
235     }
236
237     /**
238      * Block user
239      *
240      * @param \Instagram\User|string $user User object or user id who should be blocked
241      * @return boolean
242      */
243     public function block( $user ) {
244         if ( $user instanceof \Instagram\User ) {
245             $user = $user->getId();
246         }
247         $this->updateRelationship( $user ); 
248         $response = $this->proxy->modifyRelationship( $user, 'block' );
249         foreach( $response as $r => $v ) {
250             $this->relationships[ $user ]->$r = $v;
251         }
252         return true;
253     }
254
255     /**
256      * Unblock user
257      *
258      * @param \Instagram\User|string $user User object or user id who should be unblocked
259      * @return boolean
260      */
261     public function unblock( $user ) {
262         if ( $user instanceof \Instagram\User ) {
263             $user = $user->getId();
264         }
265         $this->updateRelationship( $user ); 
266         $response = $this->proxy->modifyRelationship( $user, 'unblock' );
267         foreach( $response as $r => $v ) {
268             $this->relationships[ $user ]->$r = $v;
269         }
270         return true;
271     }
272
273     /**
274      * Check if the current user can view a user
275      *
276      * @return bool
277      * @access public
278      */
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' ) {
282             return true;
283         }
284         return false;
285     }
286
287     /**
288      * Get relationship
289      *
290      * Get the complete relationship to a user
291      *
292      * @param \Instagram\User|string $user User object or user id to get the relationship details of
293      * @return StdClass
294      */
295     public function getRelationship( $user ) {
296         if ( $user instanceof \Instagram\User ) {
297             $user = $user->getId();
298         }
299         $this->updateRelationship( $user );
300         return $this->relationships[ $user ];
301     }
302
303     /**
304      * Get follow request
305      *
306      * Get the users that have requested to follow the current user
307      *
308      * @return \Instagram\Collection\UserCollection
309      */
310     public function getFollowRequests() {
311         return new UserCollection( $this->proxy->getFollowRequests(), $this->proxy );
312     }
313
314     /**
315      * Check outgoing request status
316      *
317      * Check if the current user is currently waiting approval of a follow request
318      *
319      * @param \Instagram\User|string $user User object or user id to check the status of
320      * @return boolean
321      */
322     public function isAwaitingFollowApprovalFrom( $user ) {
323         if ( $user instanceof \Instagram\User ) {
324             $user = $user->getId();
325         }
326         return $this->getRelationship( $user )->outgoing_status == 'requested';
327     }
328
329     /**
330      * Check incoming request status
331      *
332      * Check if the current user has been requested to be followed by a user
333      *
334      * @param \Instagram\User|string $user User object or user id to check the status of
335      * @return boolean
336      */
337     public function hasBeenRequestedBy( $user ) {
338         if ( $user instanceof \Instagram\User ) {
339             $user = $user->getId();
340         }
341         return $this->getRelationship( $user )->incoming_status == 'requested_by';
342     }
343
344     /**
345      * Check following status
346      *
347      * Check if the current user is blocking a user
348      *
349      * @param \Instagram\User|string $user User object or user id to check the following status of
350      * @return boolean
351      */
352     public function isBlocking( $user ) {
353         if ( $user instanceof \Instagram\User ) {
354             $user = $user->getId();
355         }
356         return $this->getRelationship( $user )->incoming_status == 'blocked_by_you';
357     }
358
359     /**
360      * Check following status
361      *
362      * Check if hte current user is following a user
363      *
364      * @param \Instagram\User|string $user User object or user id to check the following status of
365      * @return boolean
366      */
367     public function isFollowing( $user ) {
368         if ( $user instanceof \Instagram\User ) {
369             $user = $user->getId();
370         }
371         return $this->getRelationship( $user )->outgoing_status == 'follows';
372     }
373
374     /**
375      * Check followed by status
376      *
377      * Check if the current user is followed by a user
378      *
379      * @param \Instagram\User|string $user User object or user id to check the followed by status of
380      * @return boolean
381      */
382     public function isFollowedBy( $user ) {
383         if ( $user instanceof \Instagram\User ) {
384             $user = $user->getId();
385         }
386         return $this->getRelationship( $user )->incoming_status == 'followed_by';
387     }
388
389     /**
390      * Get the current user's feed
391      *
392      * This can be paginated with the next_max_id param obtained from MediaCollection->getNext()
393      *
394      * @param array $params Optional params to pass to the endpoint
395      * @return \Instagram\Collection\MediaCollection
396      * @access public
397      */
398     public function getFeed( array $params = null ) {
399         return new MediaCollection( $this->proxy->getFeed( $params ), $this->proxy );
400     }
401
402     /**
403      * Get the current user's liked media
404      *
405      * This can be paginated with the next_max_like_id param obtained from MediaCollection->getNext()
406      *
407      * @param array $params Optional params to pass to the endpoint
408      * @return \Instagram\Collection\MediaCollection
409      * @access public
410      */
411     public function getLikedMedia( array $params = null ) {
412         return  new LikedMediaCollection( $this->proxy->getLikedMedia( $params ), $this->proxy );
413     }
414
415 }