ostatusqueuehandler.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Prepare PuSH and Salmon distributions for an outgoing message.
  24. *
  25. * @package OStatusPlugin
  26. * @author Brion Vibber <brion@status.net>
  27. */
  28. class OStatusQueueHandler extends QueueHandler
  29. {
  30. // If we have more than this many subscribing sites on a single feed,
  31. // break up the PuSH distribution into smaller batches which will be
  32. // rolled into the queue progressively. This reduces disruption to
  33. // other, shorter activities being enqueued while we work.
  34. const MAX_UNBATCHED = 50;
  35. // Each batch (a 'hubprep' entry) will have this many items.
  36. // Selected to provide a balance between queue packet size
  37. // and number of batches that will end up getting processed.
  38. // For 20,000 target sites, 1000 should work acceptably.
  39. const BATCH_SIZE = 1000;
  40. function transport()
  41. {
  42. return 'ostatus';
  43. }
  44. function handle($notice)
  45. {
  46. assert($notice instanceof Notice);
  47. $this->notice = $notice;
  48. $this->user = User::getKV('id', $notice->profile_id);
  49. try {
  50. $profile = $this->notice->getProfile();
  51. } catch (Exception $e) {
  52. common_log(LOG_ERR, "Can't get profile for notice; skipping: " . $e->getMessage());
  53. return true;
  54. }
  55. if ($notice->isLocal()) {
  56. // Notices generated on remote sites will have already
  57. // been pushed to user's subscribers by their origin sites.
  58. $this->pushUser();
  59. }
  60. foreach ($notice->getGroups() as $group) {
  61. $oprofile = Ostatus_profile::getKV('group_id', $group->id);
  62. if ($oprofile) {
  63. // remote group
  64. if ($notice->isLocal()) {
  65. $this->pingReply($oprofile);
  66. }
  67. } else {
  68. // local group
  69. $this->pushGroup($group->id);
  70. }
  71. }
  72. if ($notice->isLocal()) {
  73. // Notices generated on other sites will have already
  74. // pinged their reply-targets.
  75. foreach ($notice->getReplies() as $profile_id) {
  76. $oprofile = Ostatus_profile::getKV('profile_id', $profile_id);
  77. if ($oprofile) {
  78. $this->pingReply($oprofile);
  79. }
  80. }
  81. if (!empty($this->notice->reply_to)) {
  82. $replyTo = Notice::getKV('id', $this->notice->reply_to);
  83. if (!empty($replyTo)) {
  84. foreach($replyTo->getReplies() as $profile_id) {
  85. $oprofile = Ostatus_profile::getKV('profile_id', $profile_id);
  86. if ($oprofile) {
  87. $this->pingReply($oprofile);
  88. }
  89. }
  90. }
  91. }
  92. foreach ($notice->getProfileTags() as $ptag) {
  93. $oprofile = Ostatus_profile::getKV('peopletag_id', $ptag->id);
  94. if (!$oprofile) {
  95. $this->pushPeopletag($ptag);
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. function pushUser()
  102. {
  103. if ($this->user) {
  104. // For local posts, ping the PuSH hub to update their feed.
  105. // http://identi.ca/api/statuses/user_timeline/1.atom
  106. $feed = common_local_url('ApiTimelineUser',
  107. array('id' => $this->user->id,
  108. 'format' => 'atom'));
  109. $this->pushFeed($feed, array($this, 'userFeedForNotice'));
  110. }
  111. }
  112. function pushGroup($group_id)
  113. {
  114. // For a local group, ping the PuSH hub to update its feed.
  115. // Updates may come from either a local or a remote user.
  116. $feed = common_local_url('ApiTimelineGroup',
  117. array('id' => $group_id,
  118. 'format' => 'atom'));
  119. $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id);
  120. }
  121. function pushPeopletag($ptag)
  122. {
  123. // For a local people tag, ping the PuSH hub to update its feed.
  124. // Updates may come from either a local or a remote user.
  125. $feed = common_local_url('ApiTimelineList',
  126. array('id' => $ptag->id,
  127. 'user' => $ptag->tagger,
  128. 'format' => 'atom'));
  129. $this->pushFeed($feed, array($this, 'peopletagFeedForNotice'), $ptag);
  130. }
  131. function pingReply($oprofile)
  132. {
  133. if ($this->user) {
  134. // For local posts, send a Salmon ping to the mentioned
  135. // remote user or group.
  136. // @fixme as an optimization we can skip this if the
  137. // remote profile is subscribed to the author.
  138. $oprofile->notifyDeferred($this->notice, $this->user);
  139. }
  140. }
  141. /**
  142. * @param string $feed URI to the feed
  143. * @param callable $callback function to generate Atom feed update if needed
  144. * any additional params are passed to the callback.
  145. */
  146. function pushFeed($feed, $callback)
  147. {
  148. $hub = common_config('ostatus', 'hub');
  149. if ($hub) {
  150. $this->pushFeedExternal($feed, $hub);
  151. }
  152. $sub = new HubSub();
  153. $sub->topic = $feed;
  154. if ($sub->find()) {
  155. $args = array_slice(func_get_args(), 2);
  156. $atom = call_user_func_array($callback, $args);
  157. $this->pushFeedInternal($atom, $sub);
  158. } else {
  159. common_log(LOG_INFO, "No PuSH subscribers for $feed");
  160. }
  161. return true;
  162. }
  163. /**
  164. * Ping external hub about this update.
  165. * The hub will pull the feed and check for new items later.
  166. * Not guaranteed safe in an environment with database replication.
  167. *
  168. * @param string $feed feed topic URI
  169. * @param string $hub PuSH hub URI
  170. * @fixme can consolidate pings for user & group posts
  171. */
  172. function pushFeedExternal($feed, $hub)
  173. {
  174. $client = new HTTPClient();
  175. try {
  176. $data = array('hub.mode' => 'publish',
  177. 'hub.url' => $feed);
  178. $response = $client->post($hub, array(), $data);
  179. if ($response->getStatus() == 204) {
  180. common_log(LOG_INFO, "PuSH ping to hub $hub for $feed ok");
  181. return true;
  182. } else {
  183. common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed with HTTP " .
  184. $response->getStatus() . ': ' .
  185. $response->getBody());
  186. }
  187. } catch (Exception $e) {
  188. common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed: " . $e->getMessage());
  189. return false;
  190. }
  191. }
  192. /**
  193. * Queue up direct feed update pushes to subscribers on our internal hub.
  194. * If there are a large number of subscriber sites, intermediate bulk
  195. * distribution triggers may be queued.
  196. *
  197. * @param string $atom update feed, containing only new/changed items
  198. * @param HubSub $sub open query of subscribers
  199. */
  200. function pushFeedInternal($atom, $sub)
  201. {
  202. common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
  203. $n = 0;
  204. $batch = array();
  205. while ($sub->fetch()) {
  206. $n++;
  207. if ($n < self::MAX_UNBATCHED) {
  208. $sub->distribute($atom);
  209. } else {
  210. $batch[] = $sub->callback;
  211. if (count($batch) >= self::BATCH_SIZE) {
  212. $sub->bulkDistribute($atom, $batch);
  213. $batch = array();
  214. }
  215. }
  216. }
  217. if (count($batch) >= 0) {
  218. $sub->bulkDistribute($atom, $batch);
  219. }
  220. }
  221. /**
  222. * Build a single-item version of the sending user's Atom feed.
  223. * @return string
  224. */
  225. function userFeedForNotice()
  226. {
  227. $atom = new AtomUserNoticeFeed($this->user);
  228. $atom->addEntryFromNotice($this->notice);
  229. $feed = $atom->getString();
  230. return $feed;
  231. }
  232. function groupFeedForNotice($group_id)
  233. {
  234. $group = User_group::getKV('id', $group_id);
  235. $atom = new AtomGroupNoticeFeed($group);
  236. $atom->addEntryFromNotice($this->notice);
  237. $feed = $atom->getString();
  238. return $feed;
  239. }
  240. function peopletagFeedForNotice($ptag)
  241. {
  242. $atom = new AtomListNoticeFeed($ptag);
  243. $atom->addEntryFromNotice($this->notice);
  244. $feed = $atom->getString();
  245. return $feed;
  246. }
  247. }