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();
41 public $attentionType = array();
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 const POINT = 'point';
55 const MENTIONED = 'mentioned';
56 const CONVERSATION = 'ostatus:conversation';
58 function __construct($element = null)
60 if (empty($element)) {
64 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
66 if (!empty($replyToEl)) {
67 $this->replyToID = $replyToEl->getAttribute(self::REF);
68 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
71 $this->location = $this->getLocation($element);
73 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
75 // Multiple attention links allowed
77 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
80 for ($i = 0; $i < $links->length; $i++) {
81 $link = $links->item($i);
83 $linkRel = $link->getAttribute(ActivityUtils::REL);
85 if ($linkRel == self::MENTIONED) {
86 $attention[] = $link->getAttribute(self::HREF);
89 $this->attention = array_unique($attention);
93 * Parse location given as a GeoRSS-simple point, if provided.
94 * http://www.georss.org/simple
96 * @param feed item $entry
97 * @return mixed Location or false
99 function getLocation($dom)
101 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
103 for ($i = 0; $i < $points->length; $i++) {
104 $point = $points->item($i)->textContent;
105 return self::locationFromPoint($point);
111 // XXX: Move to ActivityUtils or Location?
112 static function locationFromPoint($point)
114 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
115 $point = preg_replace('/\s+/', ' ', $point);
116 $point = trim($point);
117 $coords = explode(' ', $point);
118 if (count($coords) == 2) {
119 list($lat, $lon) = $coords;
120 if (is_numeric($lat) && is_numeric($lon)) {
121 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
122 return Location::fromLatLon($lat, $lon);
125 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
130 * Returns context (StatusNet stuff) as an array suitable for serializing
131 * in JSON. Right now context stuff is an extension to Activity.
133 * @return array the context
140 $context['inReplyTo'] = $this->getInReplyToArray();
141 $context['conversation'] = $this->conversation;
143 return array_filter($context);
147 * Returns an array of arrays representing Activity Objects (intended to be
148 * serialized in JSON) that represent WHO the Activity is supposed to
149 * be received by. This is not really specified but appears in an example
150 * of the current spec as an extension. We might want to figure out a JSON
151 * serialization for OStatus and use that to express mentions instead.
153 * XXX: People's ideas on how to do this are all over the place
155 * @return array the array of recipients
158 function getToArray()
162 foreach ($this->attention as $attnUrl) {
163 if (array_key_exists($attnUrl, $this->attentionType)) {
164 $type = ActivityObject::canonicalType($this->attentionType[$attnUrl]);
166 $type = ActivityObject::canonicalType(ActivityObject::PERSON);
169 'objectType' => $type,
179 * Return an array for the notices this notice is a reply to
180 * suitable for serializing as JSON note objects.
182 * @return array the array of notes
185 function getInReplyToArray()
187 if (empty($this->replyToID) && empty($this->replyToUrl)) {
191 $replyToObj = array('objectType' => 'note');
193 // XXX: Possibly shorten this to just the numeric ID?
194 // Currently, it's the full URI of the notice.
195 if (!empty($this->replyToID)) {
196 $replyToObj['id'] = $this->replyToID;
198 if (!empty($this->replyToUrl)) {
199 $replyToObj['url'] = $this->replyToUrl;