/libraries/joomla/twitter/friends.php

https://github.com/pjwiseman/joomla-cms · PHP · 459 lines · 217 code · 55 blank · 187 comment · 27 complexity · 0fa542897762dc71ebad9b789f4c7d09 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Twitter
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. /**
  11. * Twitter API Friends class for the Joomla Platform.
  12. *
  13. * @since 12.3
  14. */
  15. class JTwitterFriends extends JTwitterObject
  16. {
  17. /**
  18. * Method to get an array of user IDs the specified user follows.
  19. *
  20. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  21. * @param integer $cursor Causes the list of connections to be broken into pages of no more than 5000 IDs at a time.
  22. * The number of IDs returned is not guaranteed to be 5000 as suspended users are filtered out
  23. * after connections are queried. If no cursor is provided, a value of -1 will be assumed, which is the first "page."
  24. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  25. * @param integer $count Specifies the number of IDs attempt retrieval of, up to a maximum of 5,000 per distinct request.
  26. *
  27. * @return array The decoded JSON response
  28. *
  29. * @since 12.3
  30. * @throws RuntimeException
  31. */
  32. public function getFriendIds($user, $cursor = null, $string_ids = null, $count = 0)
  33. {
  34. // Check the rate limit for remaining hits
  35. $this->checkRateLimit('friends', 'ids');
  36. // Determine which type of data was passed for $user
  37. if (is_numeric($user))
  38. {
  39. $data['user_id'] = $user;
  40. }
  41. elseif (is_string($user))
  42. {
  43. $data['screen_name'] = $user;
  44. }
  45. else
  46. {
  47. // We don't have a valid entry
  48. throw new RuntimeException('The specified username is not in the correct format; must use integer or string');
  49. }
  50. // Check if cursor is specified
  51. if (!is_null($cursor))
  52. {
  53. $data['cursor'] = $cursor;
  54. }
  55. // Check if string_ids is true
  56. if ($string_ids)
  57. {
  58. $data['stringify_ids'] = $string_ids;
  59. }
  60. // Check if count is specified
  61. if ($count > 0)
  62. {
  63. $data['count'] = $count;
  64. }
  65. // Set the API path
  66. $path = '/friends/ids.json';
  67. // Send the request.
  68. return $this->sendRequest($path, 'GET', $data);
  69. }
  70. /**
  71. * Method to display detailed friend information between two users.
  72. *
  73. * @param mixed $user_a Either an integer containing the user ID or a string containing the screen name of the first user.
  74. * @param mixed $user_b Either an integer containing the user ID or a string containing the screen name of the second user.
  75. *
  76. * @return array The decoded JSON response
  77. *
  78. * @since 12.3
  79. * @throws RuntimeException
  80. */
  81. public function getFriendshipDetails($user_a, $user_b)
  82. {
  83. // Check the rate limit for remaining hits
  84. $this->checkRateLimit('friendships', 'show');
  85. // Determine which type of data was passed for $user_a
  86. if (is_numeric($user_a))
  87. {
  88. $data['source_id'] = $user_a;
  89. }
  90. elseif (is_string($user_a))
  91. {
  92. $data['source_screen_name'] = $user_a;
  93. }
  94. else
  95. {
  96. // We don't have a valid entry
  97. throw new RuntimeException('The first specified username is not in the correct format; must use integer or string');
  98. }
  99. // Determine which type of data was passed for $user_b
  100. if (is_numeric($user_b))
  101. {
  102. $data['target_id'] = $user_b;
  103. }
  104. elseif (is_string($user_b))
  105. {
  106. $data['target_screen_name'] = $user_b;
  107. }
  108. else
  109. {
  110. // We don't have a valid entry
  111. throw new RuntimeException('The second specified username is not in the correct format; must use integer or string');
  112. }
  113. // Set the API path
  114. $path = '/friendships/show.json';
  115. // Send the request.
  116. return $this->sendRequest($path, 'GET', $data);
  117. }
  118. /**
  119. * Method to get an array of user IDs the specified user is followed by.
  120. *
  121. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  122. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned
  123. * is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor
  124. * is provided, a value of -1 will be assumed, which is the first "page."
  125. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  126. * @param integer $count Specifies the number of IDs attempt retrieval of, up to a maximum of 5,000 per distinct request.
  127. *
  128. * @return array The decoded JSON response
  129. *
  130. * @since 12.3
  131. * @throws RuntimeException
  132. */
  133. public function getFollowerIds($user, $cursor = null, $string_ids = null, $count = 0)
  134. {
  135. // Check the rate limit for remaining hits
  136. $this->checkRateLimit('followers', 'ids');
  137. // Determine which type of data was passed for $user
  138. if (is_numeric($user))
  139. {
  140. $data['user_id'] = $user;
  141. }
  142. elseif (is_string($user))
  143. {
  144. $data['screen_name'] = $user;
  145. }
  146. else
  147. {
  148. // We don't have a valid entry
  149. throw new RuntimeException('The specified username is not in the correct format; must use integer or string');
  150. }
  151. // Set the API path
  152. $path = '/followers/ids.json';
  153. // Check if cursor is specified
  154. if (!is_null($cursor))
  155. {
  156. $data['cursor'] = $cursor;
  157. }
  158. // Check if string_ids is specified
  159. if (!is_null($string_ids))
  160. {
  161. $data['stringify_ids'] = $string_ids;
  162. }
  163. // Check if count is specified
  164. if (!is_null($count))
  165. {
  166. $data['count'] = $count;
  167. }
  168. // Send the request.
  169. return $this->sendRequest($path, 'GET', $data);
  170. }
  171. /**
  172. * Method to determine pending requests to follow the authenticating user.
  173. *
  174. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned
  175. * is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor
  176. * is provided, a value of -1 will be assumed, which is the first "page."
  177. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  178. *
  179. * @return array The decoded JSON response
  180. *
  181. * @since 12.3
  182. */
  183. public function getFriendshipsIncoming($cursor = null, $string_ids = null)
  184. {
  185. // Check the rate limit for remaining hits
  186. $this->checkRateLimit('friendships', 'incoming');
  187. $data = array();
  188. // Check if cursor is specified
  189. if (!is_null($cursor))
  190. {
  191. $data['cursor'] = $cursor;
  192. }
  193. // Check if string_ids is specified
  194. if (!is_null($string_ids))
  195. {
  196. $data['stringify_ids'] = $string_ids;
  197. }
  198. // Set the API path
  199. $path = '/friendships/incoming.json';
  200. // Send the request.
  201. return $this->sendRequest($path, 'GET', $data);
  202. }
  203. /**
  204. * Method to determine every protected user for whom the authenticating user has a pending follow request.
  205. *
  206. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned
  207. * is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor
  208. * is provided, a value of -1 will be assumed, which is the first "page."
  209. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  210. *
  211. * @return array The decoded JSON response
  212. *
  213. * @since 12.3
  214. */
  215. public function getFriendshipsOutgoing($cursor = null, $string_ids = null)
  216. {
  217. // Check the rate limit for remaining hits
  218. $this->checkRateLimit('friendships', 'outgoing');
  219. $data = array();
  220. // Check if cursor is specified
  221. if (!is_null($cursor))
  222. {
  223. $data['cursor'] = $cursor;
  224. }
  225. // Check if string_ids is specified
  226. if (!is_null($string_ids))
  227. {
  228. $data['stringify_ids'] = $string_ids;
  229. }
  230. // Set the API path
  231. $path = '/friendships/outgoing.json';
  232. // Send the request.
  233. return $this->sendRequest($path, 'GET', $data);
  234. }
  235. /**
  236. * Allows the authenticating users to follow the user specified in the ID parameter.
  237. *
  238. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  239. * @param boolean $follow Enable notifications for the target user.
  240. *
  241. * @return array The decoded JSON response
  242. *
  243. * @since 12.3
  244. * @throws RuntimeException
  245. */
  246. public function follow($user, $follow = false)
  247. {
  248. // Determine which type of data was passed for $user
  249. if (is_numeric($user))
  250. {
  251. $data['user_id'] = $user;
  252. }
  253. elseif (is_string($user))
  254. {
  255. $data['screen_name'] = $user;
  256. }
  257. else
  258. {
  259. // We don't have a valid entry
  260. throw new RuntimeException('The specified username is not in the correct format; must use integer or string');
  261. }
  262. // Check if follow is true
  263. if ($follow)
  264. {
  265. $data['follow'] = $follow;
  266. }
  267. // Set the API path
  268. $path = '/friendships/create.json';
  269. // Send the request.
  270. return $this->sendRequest($path, 'POST', $data);
  271. }
  272. /**
  273. * Allows the authenticating users to unfollow the user specified in the ID parameter.
  274. *
  275. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  276. *
  277. * @return array The decoded JSON response
  278. *
  279. * @since 12.3
  280. * @throws RuntimeException
  281. */
  282. public function unfollow($user)
  283. {
  284. // Determine which type of data was passed for $user
  285. if (is_numeric($user))
  286. {
  287. $data['user_id'] = $user;
  288. }
  289. elseif (is_string($user))
  290. {
  291. $data['screen_name'] = $user;
  292. }
  293. else
  294. {
  295. // We don't have a valid entry
  296. throw new RuntimeException('The specified username is not in the correct format; must use integer or string');
  297. }
  298. // Set the API path
  299. $path = '/friendships/destroy.json';
  300. // Send the request.
  301. return $this->sendRequest($path, 'POST', $data);
  302. }
  303. /**
  304. * Method to get the relationship of the authenticating user to the comma separated list of up to 100 screen_names or user_ids provided.
  305. *
  306. * @param string $screen_name A comma separated list of screen names, up to 100 are allowed in a single request.
  307. * @param string $id A comma separated list of user IDs, up to 100 are allowed in a single request.
  308. *
  309. * @return array The decoded JSON response
  310. *
  311. * @since 12.3
  312. * @throws RuntimeException
  313. */
  314. public function getFriendshipsLookup($screen_name = null, $id = null)
  315. {
  316. // Check the rate limit for remaining hits
  317. $this->checkRateLimit('friendships', 'lookup');
  318. // Set user IDs and screen names.
  319. if ($id)
  320. {
  321. $data['user_id'] = $id;
  322. }
  323. if ($screen_name)
  324. {
  325. $data['screen_name'] = $screen_name;
  326. }
  327. if ($id == null && $screen_name == null)
  328. {
  329. // We don't have a valid entry
  330. throw new RuntimeException('You must specify either a comma separated list of screen names, user IDs, or a combination of the two');
  331. }
  332. // Set the API path
  333. $path = '/friendships/lookup.json';
  334. // Send the request.
  335. return $this->sendRequest($path, 'GET', $data);
  336. }
  337. /**
  338. * Allows one to enable or disable retweets and device notifications from the specified user.
  339. *
  340. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  341. * @param boolean $device Enable/disable device notifications from the target user.
  342. * @param boolean $retweets Enable/disable retweets from the target user.
  343. *
  344. * @return array The decoded JSON response
  345. *
  346. * @since 12.3
  347. * @throws RuntimeException
  348. */
  349. public function updateFriendship($user, $device = null, $retweets = null)
  350. {
  351. // Determine which type of data was passed for $user
  352. if (is_numeric($user))
  353. {
  354. $data['user_id'] = $user;
  355. }
  356. elseif (is_string($user))
  357. {
  358. $data['screen_name'] = $user;
  359. }
  360. else
  361. {
  362. // We don't have a valid entry
  363. throw new RuntimeException('The specified username is not in the correct format; must use integer or string');
  364. }
  365. // Check if device is specified.
  366. if (!is_null($device))
  367. {
  368. $data['device'] = $device;
  369. }
  370. // Check if retweets is specified.
  371. if (!is_null($retweets))
  372. {
  373. $data['retweets'] = $retweets;
  374. }
  375. // Set the API path
  376. $path = '/friendships/update.json';
  377. // Send the request.
  378. return $this->sendRequest($path, 'POST', $data);
  379. }
  380. /**
  381. * Method to get the user ids that currently authenticated user does not want to see retweets from.
  382. *
  383. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  384. *
  385. * @return array The decoded JSON response
  386. *
  387. * @since 12.3
  388. */
  389. public function getFriendshipNoRetweetIds($string_ids = null)
  390. {
  391. // Check the rate limit for remaining hits
  392. $this->checkRateLimit('friendships', 'no_retweets/ids');
  393. $data = array();
  394. // Check if string_ids is specified
  395. if (!is_null($string_ids))
  396. {
  397. $data['stringify_ids'] = $string_ids;
  398. }
  399. // Set the API path
  400. $path = '/friendships/no_retweets/ids.json';
  401. // Send the request.
  402. return $this->sendRequest($path, 'GET', $data);
  403. }
  404. }