twittersitestream.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * PHP version 5
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Plugin
  21. * @package StatusNet
  22. * @author Brion Vibber <brion@status.net>
  23. * @copyright 2010 StatusNet, Inc.
  24. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link http://status.net/
  26. */
  27. /**
  28. * Multiuser stream listener for Twitter Site Streams API
  29. * http://dev.twitter.com/pages/site_streams
  30. *
  31. * The site streams API allows listening to updates for multiple users.
  32. * Pass in the user IDs to listen to in via followUser() -- note they
  33. * must each have a valid OAuth token for the application ID we're
  34. * connecting as.
  35. *
  36. * You'll need to be connecting with the auth keys for the user who
  37. * owns the application registration.
  38. *
  39. * The user each message is destined for will be passed to event handlers
  40. * in $context['for_user_id'].
  41. */
  42. class TwitterSiteStream extends TwitterStreamReader
  43. {
  44. protected $userIds;
  45. public function __construct(TwitterOAuthClient $auth, $baseUrl='https://sitestream.twitter.com')
  46. {
  47. parent::__construct($auth, $baseUrl);
  48. }
  49. public function connect($method='2b/site.json')
  50. {
  51. $params = array();
  52. if ($this->userIds) {
  53. $params['follow'] = implode(',', $this->userIds);
  54. }
  55. return parent::connect($method, $params);
  56. }
  57. /**
  58. * Set the users whose home streams should be pulled.
  59. * They all must have valid oauth tokens for this application.
  60. *
  61. * Must be called before connect().
  62. *
  63. * @param array $userIds
  64. */
  65. function followUsers($userIds)
  66. {
  67. $this->userIds = $userIds;
  68. }
  69. /**
  70. * Each message in the site stream tells us which user ID it should be
  71. * routed to; we'll need that to let the caller know what to do.
  72. *
  73. * @param array $data
  74. */
  75. function routeMessage(stdClass $data)
  76. {
  77. $context = array(
  78. 'source' => 'sitestream',
  79. 'for_user' => $data->for_user
  80. );
  81. parent::handleMessage($data->message, $context);
  82. }
  83. }