]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitycontext.php
Merge branch '0.9.x' into json-activities
[quix0rs-gnu-social.git] / lib / activitycontext.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * An activity
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class ActivityContext
36 {
37     public $replyToID;
38     public $replyToUrl;
39     public $location;
40     public $attention = array();
41     public $conversation;
42     public $forwardID; // deprecated, use ActivityVerb::SHARE instead
43     public $forwardUrl; // deprecated, use ActivityVerb::SHARE instead
44
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';
48
49     const INREPLYTO = 'in-reply-to';
50     const REF       = 'ref';
51     const HREF      = 'href';
52
53     const POINT     = 'point';
54
55     const ATTENTION    = 'ostatus:attention';
56     const MENTIONED    = 'mentioned';
57     const CONVERSATION = 'ostatus:conversation';
58
59     function __construct($element = null)
60     {
61         if (empty($element)) {
62             return;
63         }
64
65         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
66
67         if (!empty($replyToEl)) {
68             $this->replyToID  = $replyToEl->getAttribute(self::REF);
69             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
70         }
71
72         $this->location = $this->getLocation($element);
73
74         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
75
76         // Multiple attention links allowed
77
78         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
79
80         $attention = array();
81         for ($i = 0; $i < $links->length; $i++) {
82             $link = $links->item($i);
83
84             $linkRel = $link->getAttribute(ActivityUtils::REL);
85
86             // XXX: Deprecate this in favour of "mentioned" from Salmon spec
87             // http://salmon-protocol.googlecode.com/svn/trunk/draft-panzer-salmon-00.html#SALR
88             if ($linkRel == self::ATTENTION) {
89                 $attention[] = $link->getAttribute(self::HREF);
90             } elseif ($linkRel == self::MENTIONED) {
91                 $attention[] = $link->getAttribute(self::HREF);
92             }
93         }
94         $this->attention = array_unique($attention);
95     }
96
97     /**
98      * Parse location given as a GeoRSS-simple point, if provided.
99      * http://www.georss.org/simple
100      *
101      * @param feed item $entry
102      * @return mixed Location or false
103      */
104     function getLocation($dom)
105     {
106         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
107
108         for ($i = 0; $i < $points->length; $i++) {
109             $point = $points->item($i)->textContent;
110             return self::locationFromPoint($point);
111         }
112
113         return null;
114     }
115
116     // XXX: Move to ActivityUtils or Location?
117     static function locationFromPoint($point)
118     {
119         $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
120         $point = preg_replace('/\s+/', ' ', $point);
121         $point = trim($point);
122         $coords = explode(' ', $point);
123         if (count($coords) == 2) {
124             list($lat, $lon) = $coords;
125             if (is_numeric($lat) && is_numeric($lon)) {
126                 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
127                 return Location::fromLatLon($lat, $lon);
128             }
129         }
130         common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
131         return null;
132     }
133
134     /**
135      * Returns context (StatusNet stuff) as an array suitable for serializing
136      * in JSON. Right now context stuff is an extension to Activity.
137      *
138      * @return array the context
139      */
140
141     function asArray()
142     {
143         $context = array();
144
145         $context['replyTo']    =   $this->getInReplyToArray();
146         $context['conversation'] = $this->conversation;
147         $context['forwardId']    = $this->forwardID;
148         $context['forwardUrl']   = $this->forwardUrl;
149
150         return array_filter($context);
151     }
152
153     /**
154      * Returns an array of arrays representing Activity Objects (intended to be
155      * serialized in JSON) that represent WHO the Activity is supposed to
156      * be received by. This is not really specified but appears in an example
157      * of the current spec as an extension. We might want to figure out a JSON
158      * serialization for OStatus and use that to express mentions instead.
159      *
160      * XXX: People's ideas on how to do this are all over the place
161      *
162      * @return array the array of recipients
163      */
164
165     function getToArray()
166     {
167         $tos = array();
168
169         foreach ($this->attention as $attnUrl) {
170             $to = array(
171                 'objectType' => 'person',
172                 'id'         => $attnUrl,
173                 'url'        => $attnUrl
174             );
175             $tos[] = $to;
176         }
177
178         return $tos;
179     }
180
181     /*
182      * Show replyTo
183      */
184
185      function getInReplyToArray()
186      {
187          $replyToObj = array('objectType' => 'note');
188
189          $replyToObj['id'] = $this->replyToID;
190
191          if (!empty($this->replyToUrl)) {
192              $replyToObj['url'] = $this->replyToUrl;
193          }
194
195      }
196
197 }
198