3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
40 public $attention = array(); // 'uri' => 'type'
42 public $conversation_url;
45 const THR = 'http://purl.org/syndication/thread/1.0';
46 const GEORSS = 'http://www.georss.org/georss';
47 const OSTATUS = 'http://ostatus.org/schema/1.0';
49 const INREPLYTO = 'in-reply-to';
53 // OStatus element names with prefixes
54 const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented!
55 const CONVERSATION = 'conversation';
57 const POINT = 'point';
59 const MENTIONED = 'mentioned';
61 const ATTN_PUBLIC = 'http://activityschema.org/collection/public';
63 function __construct($element = null)
65 if (empty($element)) {
69 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
71 if (!empty($replyToEl)) {
72 $this->replyToID = $replyToEl->getAttribute(self::REF);
73 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
76 $this->location = $this->getLocation($element);
78 foreach ($element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION) as $conv) {
79 if ($conv->hasAttribute('ref')) {
80 $this->conversation = $conv->getAttribute('ref');
81 if ($conv->hasAttribute('href')) {
82 $this->conversation_url = $conv->getAttribute('href');
85 $this->conversation = $conv->textContent;
87 if (!empty($this->conversation)) {
91 if (empty($this->conversation)) {
92 // fallback to the atom:link rel="ostatus:conversation" element
93 $this->conversation = ActivityUtils::getLink($element, 'ostatus:'.self::CONVERSATION);
96 // Multiple attention links allowed
98 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
100 for ($i = 0; $i < $links->length; $i++) {
101 $link = $links->item($i);
103 $linkRel = $link->getAttribute(ActivityUtils::REL);
104 $linkHref = $link->getAttribute(self::HREF);
105 if ($linkRel == self::MENTIONED && $linkHref !== '') {
106 $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
112 * Parse location given as a GeoRSS-simple point, if provided.
113 * http://www.georss.org/simple
115 * @param feed item $entry
116 * @return mixed Location or false
118 function getLocation($dom)
120 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
122 for ($i = 0; $i < $points->length; $i++) {
123 $point = $points->item($i)->textContent;
124 return self::locationFromPoint($point);
130 // XXX: Move to ActivityUtils or Location?
131 static function locationFromPoint($point)
133 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
134 $point = preg_replace('/\s+/', ' ', $point);
135 $point = trim($point);
136 $coords = explode(' ', $point);
137 if (count($coords) == 2) {
138 list($lat, $lon) = $coords;
139 if (is_numeric($lat) && is_numeric($lon)) {
140 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
141 return Location::fromLatLon($lat, $lon);
144 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
149 * Returns context (StatusNet stuff) as an array suitable for serializing
150 * in JSON. Right now context stuff is an extension to Activity.
152 * @return array the context
159 $context['inReplyTo'] = $this->getInReplyToArray();
160 $context['conversation'] = $this->conversation;
161 $context['conversation_url'] = $this->conversation_url;
163 return array_filter($context);
167 * Returns an array of arrays representing Activity Objects (intended to be
168 * serialized in JSON) that represent WHO the Activity is supposed to
169 * be received by. This is not really specified but appears in an example
170 * of the current spec as an extension. We might want to figure out a JSON
171 * serialization for OStatus and use that to express mentions instead.
173 * XXX: People's ideas on how to do this are all over the place
175 * @return array the array of recipients
178 function getToArray()
182 foreach ($this->attention as $attnUrl => $attnType) {
184 'objectType' => $attnType, // can be empty
194 * Return an array for the notices this notice is a reply to
195 * suitable for serializing as JSON note objects.
197 * @return array the array of notes
200 function getInReplyToArray()
202 if (empty($this->replyToID) && empty($this->replyToUrl)) {
206 $replyToObj = array('objectType' => 'note');
208 // XXX: Possibly shorten this to just the numeric ID?
209 // Currently, it's the full URI of the notice.
210 if (!empty($this->replyToID)) {
211 $replyToObj['id'] = $this->replyToID;
213 if (!empty($this->replyToUrl)) {
214 $replyToObj['url'] = $this->replyToUrl;