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'
44 const THR = 'http://purl.org/syndication/thread/1.0';
45 const GEORSS = 'http://www.georss.org/georss';
46 const OSTATUS = 'http://ostatus.org/schema/1.0';
48 const INREPLYTO = 'in-reply-to';
52 // OStatus element names with prefixes
53 const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented!
54 const CONVERSATION = 'ostatus:conversation';
56 const POINT = 'point';
58 const MENTIONED = 'mentioned';
60 const ATTN_PUBLIC = 'http://activityschema.org/collection/public';
62 function __construct($element = null)
64 if (empty($element)) {
68 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
70 if (!empty($replyToEl)) {
71 $this->replyToID = $replyToEl->getAttribute(self::REF);
72 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
75 $this->location = $this->getLocation($element);
77 $convs = $element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION);
78 foreach ($convs as $conv) {
79 $this->conversation = $conv->textContent;
81 if (empty($this->conversation)) {
82 // fallback to the atom:link rel="ostatus:conversation" element
83 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
86 // Multiple attention links allowed
88 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
90 for ($i = 0; $i < $links->length; $i++) {
91 $link = $links->item($i);
93 $linkRel = $link->getAttribute(ActivityUtils::REL);
94 $linkHref = $link->getAttribute(self::HREF);
95 if ($linkRel == self::MENTIONED && $linkHref !== '') {
96 $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
102 * Parse location given as a GeoRSS-simple point, if provided.
103 * http://www.georss.org/simple
105 * @param feed item $entry
106 * @return mixed Location or false
108 function getLocation($dom)
110 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
112 for ($i = 0; $i < $points->length; $i++) {
113 $point = $points->item($i)->textContent;
114 return self::locationFromPoint($point);
120 // XXX: Move to ActivityUtils or Location?
121 static function locationFromPoint($point)
123 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
124 $point = preg_replace('/\s+/', ' ', $point);
125 $point = trim($point);
126 $coords = explode(' ', $point);
127 if (count($coords) == 2) {
128 list($lat, $lon) = $coords;
129 if (is_numeric($lat) && is_numeric($lon)) {
130 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
131 return Location::fromLatLon($lat, $lon);
134 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
139 * Returns context (StatusNet stuff) as an array suitable for serializing
140 * in JSON. Right now context stuff is an extension to Activity.
142 * @return array the context
149 $context['inReplyTo'] = $this->getInReplyToArray();
150 $context['conversation'] = $this->conversation;
152 return array_filter($context);
156 * Returns an array of arrays representing Activity Objects (intended to be
157 * serialized in JSON) that represent WHO the Activity is supposed to
158 * be received by. This is not really specified but appears in an example
159 * of the current spec as an extension. We might want to figure out a JSON
160 * serialization for OStatus and use that to express mentions instead.
162 * XXX: People's ideas on how to do this are all over the place
164 * @return array the array of recipients
167 function getToArray()
171 foreach ($this->attention as $attnUrl => $attnType) {
173 'objectType' => $attnType, // can be empty
183 * Return an array for the notices this notice is a reply to
184 * suitable for serializing as JSON note objects.
186 * @return array the array of notes
189 function getInReplyToArray()
191 if (empty($this->replyToID) && empty($this->replyToUrl)) {
195 $replyToObj = array('objectType' => 'note');
197 // XXX: Possibly shorten this to just the numeric ID?
198 // Currently, it's the full URI of the notice.
199 if (!empty($this->replyToID)) {
200 $replyToObj['id'] = $this->replyToID;
202 if (!empty($this->replyToUrl)) {
203 $replyToObj['url'] = $this->replyToUrl;