PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/twitter/friends.php

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