123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- if (!defined('GNUSOCIAL')) {
- exit(1);
- }
- class ActivityStreamJSONDocument extends JSONActivityCollection
- {
-
-
-
-
- const CONTENT_TYPE = 'application/json; charset=utf-8';
-
- protected $doc = [];
-
- protected $cur;
- protected $scoped = null;
-
- protected $title;
-
- protected $links;
-
-
- protected $count;
-
- public function __construct($cur = null, $title = null, array $items = [], $links = null, $url = null)
- {
- parent::__construct($items, $url);
- $this->cur = $cur ?: common_current_user();
- $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
-
- $this->title = $title;
- if (!empty($items)) {
- $this->count = count($this->items);
- }
-
- $this->links = empty($links) ? [] : $items;
-
- if (!empty($url)) {
- $this->url = $url;
- }
- }
-
- public function setTitle($title)
- {
- $this->title = $title;
- }
- public function setUrl($url)
- {
- $this->url = $url;
- }
-
- public function addItemsFromNotices($notices)
- {
- if (is_array($notices)) {
- foreach ($notices as $notice) {
- $this->addItemFromNotice($notice);
- }
- } else {
- while ($notices->fetch()) {
- $this->addItemFromNotice($notices);
- }
- }
- }
-
- public function addItemFromNotice($notice)
- {
- try {
- $act = $notice->asActivity($this->scoped);
- } catch (Exception $e) {
-
-
-
-
- return;
- }
- $act->extra[] = $notice->noticeInfo($this->scoped);
- array_push($this->items, $act->asArray());
- $this->count++;
- }
-
- public function addLink($url = null, $rel = null, $mediaType = null)
- {
- $link = new ActivityStreamsLink($url, $rel, $mediaType);
- array_push($this->links, $link->asArray());
- }
-
- public function asString()
- {
- $this->doc['generator'] = 'GNU social ' . GNUSOCIAL_VERSION;
- $this->doc['title'] = $this->title;
- $this->doc['url'] = $this->url;
- $this->doc['totalItems'] = $this->count;
- $this->doc['items'] = $this->items;
- $this->doc['links'] = $this->links;
- return json_encode(array_filter($this->doc));
- }
- }
|