useractivitystream.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. }
  194. }
  195. }
  196. function compareObject($a, $b)
  197. {
  198. $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
  199. $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
  200. return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
  201. }
  202. function getSubscriptions()
  203. {
  204. $subs = array();
  205. $sub = new Subscription();
  206. $sub->subscriber = $this->user->id;
  207. if (!empty($this->after)) {
  208. $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
  209. }
  210. if ($sub->find()) {
  211. while ($sub->fetch()) {
  212. if ($sub->subscribed != $this->user->id) {
  213. $subs[] = clone($sub);
  214. }
  215. }
  216. }
  217. return $subs;
  218. }
  219. function getSubscribers()
  220. {
  221. $subs = array();
  222. $sub = new Subscription();
  223. $sub->subscribed = $this->user->id;
  224. if (!empty($this->after)) {
  225. $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
  226. }
  227. if ($sub->find()) {
  228. while ($sub->fetch()) {
  229. if ($sub->subscriber != $this->user->id) {
  230. $subs[] = clone($sub);
  231. }
  232. }
  233. }
  234. return $subs;
  235. }
  236. /**
  237. *
  238. * @param int $start unix timestamp for earliest
  239. * @param int $end unix timestamp for latest
  240. * @return array of Notice objects
  241. */
  242. function getNoticesBetween($start=0, $end=0)
  243. {
  244. $notices = array();
  245. $notice = new Notice();
  246. $notice->profile_id = $this->user->id;
  247. // Only stuff after $this->after
  248. if (!empty($this->after)) {
  249. if ($start) {
  250. $start = max($start, $this->after);
  251. }
  252. if ($end) {
  253. $end = max($end, $this->after);
  254. }
  255. }
  256. if ($start) {
  257. $tsstart = common_sql_date($start);
  258. $notice->whereAdd("created >= '$tsstart'");
  259. }
  260. if ($end) {
  261. $tsend = common_sql_date($end);
  262. $notice->whereAdd("created < '$tsend'");
  263. }
  264. $notice->orderBy('created DESC');
  265. if ($notice->find()) {
  266. while ($notice->fetch()) {
  267. $notices[] = clone($notice);
  268. }
  269. }
  270. return $notices;
  271. }
  272. function getNotices()
  273. {
  274. if (!empty($this->after)) {
  275. return $this->getNoticesBetween($this->after);
  276. } else {
  277. return $this->getNoticesBetween();
  278. }
  279. }
  280. function getGroups()
  281. {
  282. $groups = array();
  283. $gm = new Group_member();
  284. $gm->profile_id = $this->user->id;
  285. if (!empty($this->after)) {
  286. $gm->whereAdd("created > '" . common_sql_date($this->after) . "'");
  287. }
  288. if ($gm->find()) {
  289. while ($gm->fetch()) {
  290. $groups[] = clone($gm);
  291. }
  292. }
  293. return $groups;
  294. }
  295. function createdAfter($item) {
  296. $created = strtotime((empty($item->created)) ? $item->modified : $item->created);
  297. return ($created >= $this->after);
  298. }
  299. function writeJSON($handle)
  300. {
  301. require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
  302. fwrite($handle, '{"items": [');
  303. $this->renderEntries(Feed::JSON, $handle);
  304. fwrite($handle, ']}');
  305. }
  306. }