]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitycontext.php
Merge remote-tracking branch 'statusnet/master'
[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 $attentionType = array();
42     public $conversation;
43     public $forwardID; // deprecated, use ActivityVerb::SHARE instead
44     public $forwardUrl; // deprecated, use ActivityVerb::SHARE instead
45     public $scope;
46
47     const THR     = 'http://purl.org/syndication/thread/1.0';
48     const GEORSS  = 'http://www.georss.org/georss';
49     const OSTATUS = 'http://ostatus.org/schema/1.0';
50
51     const INREPLYTO = 'in-reply-to';
52     const REF       = 'ref';
53     const HREF      = 'href';
54
55     const POINT     = 'point';
56
57     const ATTENTION    = 'ostatus:attention';
58     const MENTIONED    = 'mentioned';
59     const CONVERSATION = 'ostatus:conversation';
60
61     function __construct($element = null)
62     {
63         if (empty($element)) {
64             return;
65         }
66
67         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
68
69         if (!empty($replyToEl)) {
70             $this->replyToID  = $replyToEl->getAttribute(self::REF);
71             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
72         }
73
74         $this->location = $this->getLocation($element);
75
76         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
77
78         // Multiple attention links allowed
79
80         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
81
82         $attention = array();
83         for ($i = 0; $i < $links->length; $i++) {
84             $link = $links->item($i);
85
86             $linkRel = $link->getAttribute(ActivityUtils::REL);
87
88             // XXX: Deprecate this in favour of "mentioned" from Salmon spec
89             // http://salmon-protocol.googlecode.com/svn/trunk/draft-panzer-salmon-00.html#SALR
90             if ($linkRel == self::ATTENTION) {
91                 $attention[] = $link->getAttribute(self::HREF);
92             } elseif ($linkRel == self::MENTIONED) {
93                 $attention[] = $link->getAttribute(self::HREF);
94             }
95         }
96         $this->attention = array_unique($attention);
97     }
98
99     /**
100      * Parse location given as a GeoRSS-simple point, if provided.
101      * http://www.georss.org/simple
102      *
103      * @param feed item $entry
104      * @return mixed Location or false
105      */
106     function getLocation($dom)
107     {
108         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
109
110         for ($i = 0; $i < $points->length; $i++) {
111             $point = $points->item($i)->textContent;
112             return self::locationFromPoint($point);
113         }
114
115         return null;
116     }
117
118     // XXX: Move to ActivityUtils or Location?
119     static function locationFromPoint($point)
120     {
121         $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
122         $point = preg_replace('/\s+/', ' ', $point);
123         $point = trim($point);
124         $coords = explode(' ', $point);
125         if (count($coords) == 2) {
126             list($lat, $lon) = $coords;
127             if (is_numeric($lat) && is_numeric($lon)) {
128                 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
129                 return Location::fromLatLon($lat, $lon);
130             }
131         }
132         common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
133         return null;
134     }
135
136     /**
137      * Returns context (StatusNet stuff) as an array suitable for serializing
138      * in JSON. Right now context stuff is an extension to Activity.
139      *
140      * @return array the context
141      */
142
143     function asArray()
144     {
145         $context = array();
146
147         $context['inReplyTo']    = $this->getInReplyToArray();
148         $context['conversation'] = $this->conversation;
149         $context['forwardId']    = $this->forwardID;
150         $context['forwardUrl']   = $this->forwardUrl;
151
152         return array_filter($context);
153     }
154
155     /**
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.
161      *
162      * XXX: People's ideas on how to do this are all over the place
163      *
164      * @return array the array of recipients
165      */
166
167     function getToArray()
168     {
169         $tos = array();
170
171         foreach ($this->attention as $attnUrl) {
172             if (array_key_exists($attnUrl, $this->attentionType)) {
173                 $type = ActivityObject::canonicalType($this->attentionType[$attnUrl]);
174             } else {
175                 $type = ActivityObject::canonicalType(ActivityObject::PERSON);
176             }
177             $to = array(
178                 'objectType' => $type,
179                 'id'         => $attnUrl
180             );
181             $tos[] = $to;
182         }
183
184         return $tos;
185     }
186
187     /**
188      * Return an array for the notices this notice is a reply to 
189      * suitable for serializing as JSON note objects.
190      *
191      * @return array the array of notes
192      */
193
194      function getInReplyToArray()
195      {
196          if (empty($this->replyToID) && empty($this->replyToUrl)) {
197              return null;
198          }
199
200          $replyToObj = array('objectType' => 'note');
201
202          // XXX: Possibly shorten this to just the numeric ID?
203          //      Currently, it's the full URI of the notice.
204          if (!empty($this->replyToID)) {
205              $replyToObj['id'] = $this->replyToID;
206          }
207          if (!empty($this->replyToUrl)) {
208              $replyToObj['url'] = $this->replyToUrl;
209          }
210
211          return $replyToObj;
212      }
213
214 }
215