useractivitystream.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /**
  20. * Class for activity streams
  21. *
  22. * Includes objects like notices, subscriptions and from plugins.
  23. *
  24. * We extend atomusernoticefeed since it does some nice setup for us.
  25. *
  26. */
  27. class UserActivityStream extends AtomUserNoticeFeed
  28. {
  29. public $activities = array();
  30. public $after = null;
  31. const OUTPUT_STRING = 1;
  32. const OUTPUT_RAW = 2;
  33. public $outputMode = self::OUTPUT_STRING;
  34. /**
  35. *
  36. * @param User $user
  37. * @param boolean $indent
  38. * @param boolean $outputMode: UserActivityStream::OUTPUT_STRING to return a string,
  39. * or UserActivityStream::OUTPUT_RAW to go to raw output.
  40. * Raw output mode will attempt to stream, keeping less
  41. * data in memory but will leave $this->activities incomplete.
  42. */
  43. function __construct($user, $indent = true, $outputMode = UserActivityStream::OUTPUT_STRING, $after = null)
  44. {
  45. parent::__construct($user, null, $indent);
  46. $this->outputMode = $outputMode;
  47. if ($this->outputMode == self::OUTPUT_STRING) {
  48. // String buffering? Grab all the notices now.
  49. $notices = $this->getNotices();
  50. } elseif ($this->outputMode == self::OUTPUT_RAW) {
  51. // Raw output... need to restructure from the stringer init.
  52. $this->xw = new XMLWriter();
  53. $this->xw->openURI('php://output');
  54. if(is_null($indent)) {
  55. $indent = common_config('site', 'indent');
  56. }
  57. $this->xw->setIndent($indent);
  58. // We'll fetch notices later.
  59. $notices = array();
  60. } else {
  61. throw new Exception('Invalid outputMode provided to ' . __METHOD__);
  62. }
  63. $this->after = $after;
  64. // Assume that everything but notices is feasible
  65. // to pull at once and work with in memory...
  66. $subscriptions = $this->getSubscriptions();
  67. $subscribers = $this->getSubscribers();
  68. $groups = $this->getGroups();
  69. $objs = array_merge($subscriptions, $subscribers, $groups, $notices);
  70. Event::handle('AppendUserActivityStreamObjects', array($this, &$objs));
  71. $subscriptions = null;
  72. $subscribers = null;
  73. $groups = null;
  74. unset($subscriptions);
  75. unset($subscribers);
  76. unset($groups);
  77. // Sort by create date
  78. usort($objs, 'UserActivityStream::compareObject');
  79. // We'll keep these around for later, and interleave them into
  80. // the output stream with the user's notices.
  81. $this->objs = $objs;
  82. }
  83. /**
  84. * Interleave the pre-sorted objects with the user's
  85. * notices, all in reverse chron order.
  86. */
  87. function renderEntries($format=Feed::ATOM, $handle=null)
  88. {
  89. $haveOne = false;
  90. $end = time() + 1;
  91. foreach ($this->objs as $obj) {
  92. set_time_limit(10);
  93. try {
  94. $act = $obj->asActivity();
  95. } catch (Exception $e) {
  96. common_log(LOG_ERR, $e->getMessage());
  97. continue;
  98. }
  99. $start = $act->time;
  100. if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
  101. // In raw mode, we haven't pre-fetched notices.
  102. // Grab the chunks of notices between other activities.
  103. try {
  104. $notices = $this->getNoticesBetween($start, $end);
  105. foreach ($notices as $noticeAct) {
  106. try {
  107. $nact = $noticeAct->asActivity($this->user->getProfile());
  108. if ($format == Feed::ATOM) {
  109. $nact->outputTo($this, false, false);
  110. } else {
  111. if ($haveOne) {
  112. fwrite($handle, ",");
  113. }
  114. fwrite($handle, json_encode($nact->asArray()));
  115. $haveOne = true;
  116. }
  117. } catch (Exception $e) {
  118. common_log(LOG_ERR, $e->getMessage());
  119. continue;
  120. }
  121. $nact = null;
  122. unset($nact);
  123. }
  124. } catch (Exception $e) {
  125. common_log(LOG_ERR, $e->getMessage());
  126. }
  127. }
  128. $notices = null;
  129. unset($notices);
  130. try {
  131. if ($format == Feed::ATOM) {
  132. // Only show the author sub-element if it's different from default user
  133. $act->outputTo($this, false, ($act->actor->id != $this->user->getUri()));
  134. } else {
  135. if ($haveOne) {
  136. fwrite($handle, ",");
  137. }
  138. fwrite($handle, json_encode($act->asArray()));
  139. $haveOne = true;
  140. }
  141. } catch (Exception $e) {
  142. common_log(LOG_ERR, $e->getMessage());
  143. }
  144. $act = null;
  145. unset($act);
  146. $end = $start;
  147. }
  148. if ($this->outputMode == self::OUTPUT_RAW) {
  149. // Grab anything after the last pre-sorted activity.
  150. try {
  151. if (!empty($this->after)) {
  152. $notices = $this->getNoticesBetween($this->after, $end);
  153. } else {
  154. $notices = $this->getNoticesBetween(0, $end);
  155. }
  156. foreach ($notices as $noticeAct) {
  157. try {
  158. $nact = $noticeAct->asActivity($this->user->getProfile());
  159. if ($format == Feed::ATOM) {
  160. $nact->outputTo($this, false, false);
  161. } else {
  162. if ($haveOne) {
  163. fwrite($handle, ",");
  164. }
  165. fwrite($handle, json_encode($nact->asArray()));
  166. $haveOne = true;
  167. }
  168. } catch (Exception $e) {
  169. common_log(LOG_ERR, $e->getMessage());
  170. continue;
  171. }
  172. }
  173. } catch (Exception $e) {
  174. common_log(LOG_ERR, $e->getMessage());
  175. }
  176. }
  177. if (empty($this->after) || strtotime($this->user->created) > $this->after) {
  178. // We always add the registration activity at the end, even if
  179. // they have older activities (from restored backups) in their stream.
  180. try {
  181. $ract = $this->user->registrationActivity();
  182. if ($format == Feed::ATOM) {
  183. $ract->outputTo($this, false, false);
  184. } else {
  185. if ($haveOne) {
  186. fwrite($handle, ",");
  187. }
  188. fwrite($handle, json_encode($ract->asArray()));
  189. $haveOne = true;
  190. }
  191. } catch (Exception $e) {
  192. common_log(LOG_ERR, $e->getMessage());
  193. continue;
  194. }
  195. }
  196. }
  197. function compareObject($a, $b)
  198. {
  199. $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
  200. $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
  201. return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
  202. }
  203. function getSubscriptions()
  204. {
  205. $subs = array();
  206. $sub = new Subscription();
  207. $sub->subscriber = $this->user->id;
  208. if (!empty($this->after)) {
  209. $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
  210. }
  211. if ($sub->find()) {
  212. while ($sub->fetch()) {
  213. if ($sub->subscribed != $this->user->id) {
  214. $subs[] = clone($sub);
  215. }
  216. }
  217. }
  218. return $subs;
  219. }
  220. function getSubscribers()
  221. {
  222. $subs = array();
  223. $sub = new Subscription();
  224. $sub->subscribed = $this->user->id;
  225. if (!empty($this->after)) {
  226. $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
  227. }
  228. if ($sub->find()) {
  229. while ($sub->fetch()) {
  230. if ($sub->subscriber != $this->user->id) {
  231. $subs[] = clone($sub);
  232. }
  233. }
  234. }
  235. return $subs;
  236. }
  237. /**
  238. *
  239. * @param int $start unix timestamp for earliest
  240. * @param int $end unix timestamp for latest
  241. * @return array of Notice objects
  242. */
  243. function getNoticesBetween($start=0, $end=0)
  244. {
  245. $notices = array();
  246. $notice = new Notice();
  247. $notice->profile_id = $this->user->id;
  248. // Only stuff after $this->after
  249. if (!empty($this->after)) {
  250. if ($start) {
  251. $start = max($start, $this->after);
  252. }
  253. if ($end) {
  254. $end = max($end, $this->after);
  255. }
  256. }
  257. if ($start) {
  258. $tsstart = common_sql_date($start);
  259. $notice->whereAdd("created >= '$tsstart'");
  260. }
  261. if ($end) {
  262. $tsend = common_sql_date($end);
  263. $notice->whereAdd("created < '$tsend'");
  264. }
  265. $notice->orderBy('created DESC');
  266. if ($notice->find()) {
  267. while ($notice->fetch()) {
  268. $notices[] = clone($notice);
  269. }
  270. }
  271. return $notices;
  272. }
  273. function getNotices()
  274. {
  275. if (!empty($this->after)) {
  276. return $this->getNoticesBetween($this->after);
  277. } else {
  278. return $this->getNoticesBetween();
  279. }
  280. }
  281. function getGroups()
  282. {
  283. $groups = array();
  284. $gm = new Group_member();
  285. $gm->profile_id = $this->user->id;
  286. if (!empty($this->after)) {
  287. $gm->whereAdd("created > '" . common_sql_date($this->after) . "'");
  288. }
  289. if ($gm->find()) {
  290. while ($gm->fetch()) {
  291. $groups[] = clone($gm);
  292. }
  293. }
  294. return $groups;
  295. }
  296. function createdAfter($item) {
  297. $created = strtotime((empty($item->created)) ? $item->modified : $item->created);
  298. return ($created >= $this->after);
  299. }
  300. function writeJSON($handle)
  301. {
  302. require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
  303. fwrite($handle, '{"items": [');
  304. $this->renderEntries(Feed::JSON, $handle);
  305. fwrite($handle, ']}');
  306. }
  307. }