useractivitystream.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. try {
  93. $act = $obj->asActivity();
  94. } catch (Exception $e) {
  95. common_log(LOG_ERR, $e->getMessage());
  96. continue;
  97. }
  98. $start = $act->time;
  99. if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
  100. // In raw mode, we haven't pre-fetched notices.
  101. // Grab the chunks of notices between other activities.
  102. try {
  103. $notices = $this->getNoticesBetween($start, $end);
  104. foreach ($notices as $noticeAct) {
  105. try {
  106. $nact = $noticeAct->asActivity($this->user->getProfile());
  107. if ($format == Feed::ATOM) {
  108. $nact->outputTo($this, false, false);
  109. } else {
  110. if ($haveOne) {
  111. fwrite($handle, ",");
  112. }
  113. fwrite($handle, json_encode($nact->asArray()));
  114. $haveOne = true;
  115. }
  116. } catch (Exception $e) {
  117. common_log(LOG_ERR, $e->getMessage());
  118. continue;
  119. }
  120. $nact = null;
  121. unset($nact);
  122. }
  123. } catch (Exception $e) {
  124. common_log(LOG_ERR, $e->getMessage());
  125. }
  126. }
  127. $notices = null;
  128. unset($notices);
  129. try {
  130. if ($format == Feed::ATOM) {
  131. // Only show the author sub-element if it's different from default user
  132. $act->outputTo($this, false, ($act->actor->id != $this->user->getUri()));
  133. } else {
  134. if ($haveOne) {
  135. fwrite($handle, ",");
  136. }
  137. fwrite($handle, json_encode($act->asArray()));
  138. $haveOne = true;
  139. }
  140. } catch (Exception $e) {
  141. common_log(LOG_ERR, $e->getMessage());
  142. }
  143. $act = null;
  144. unset($act);
  145. $end = $start;
  146. }
  147. if ($this->outputMode == self::OUTPUT_RAW) {
  148. // Grab anything after the last pre-sorted activity.
  149. try {
  150. if (!empty($this->after)) {
  151. $notices = $this->getNoticesBetween($this->after, $end);
  152. } else {
  153. $notices = $this->getNoticesBetween(0, $end);
  154. }
  155. foreach ($notices as $noticeAct) {
  156. try {
  157. $nact = $noticeAct->asActivity($this->user->getProfile());
  158. if ($format == Feed::ATOM) {
  159. $nact->outputTo($this, false, false);
  160. } else {
  161. if ($haveOne) {
  162. fwrite($handle, ",");
  163. }
  164. fwrite($handle, json_encode($nact->asArray()));
  165. $haveOne = true;
  166. }
  167. } catch (Exception $e) {
  168. common_log(LOG_ERR, $e->getMessage());
  169. continue;
  170. }
  171. }
  172. } catch (Exception $e) {
  173. common_log(LOG_ERR, $e->getMessage());
  174. }
  175. }
  176. if (empty($this->after) || strtotime($this->user->created) > $this->after) {
  177. // We always add the registration activity at the end, even if
  178. // they have older activities (from restored backups) in their stream.
  179. try {
  180. $ract = $this->user->registrationActivity();
  181. if ($format == Feed::ATOM) {
  182. $ract->outputTo($this, false, false);
  183. } else {
  184. if ($haveOne) {
  185. fwrite($handle, ",");
  186. }
  187. fwrite($handle, json_encode($ract->asArray()));
  188. $haveOne = true;
  189. }
  190. } catch (Exception $e) {
  191. common_log(LOG_ERR, $e->getMessage());
  192. continue;
  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. }