]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitycontext.php
Replies and Conversation noticestreams should only get POSTs by default
[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();    // 'uri' => 'type'
41     public $conversation;
42     public $scope;
43
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';
47
48     const INREPLYTO  = 'in-reply-to';
49     const REF        = 'ref';
50     const HREF       = 'href';
51     const OBJECTTYPE = 'ostatus:object-type';   // FIXME: Undocumented!
52
53     const POINT     = 'point';
54
55     const MENTIONED    = 'mentioned';
56     const CONVERSATION = 'ostatus:conversation';
57
58     const ATTN_PUBLIC  = 'http://activityschema.org/collection/public';
59
60     function __construct($element = null)
61     {
62         if (empty($element)) {
63             return;
64         }
65
66         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
67
68         if (!empty($replyToEl)) {
69             $this->replyToID  = $replyToEl->getAttribute(self::REF);
70             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
71         }
72
73         $this->location = $this->getLocation($element);
74
75         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
76
77         // Multiple attention links allowed
78
79         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
80
81         for ($i = 0; $i < $links->length; $i++) {
82             $link = $links->item($i);
83
84             $linkRel  = $link->getAttribute(ActivityUtils::REL);
85             $linkHref = $link->getAttribute(self::HREF);
86             if ($linkRel == self::MENTIONED && $linkHref !== '') {
87                 $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
88             }
89         }
90     }
91
92     /**
93      * Parse location given as a GeoRSS-simple point, if provided.
94      * http://www.georss.org/simple
95      *
96      * @param feed item $entry
97      * @return mixed Location or false
98      */
99     function getLocation($dom)
100     {
101         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
102
103         for ($i = 0; $i < $points->length; $i++) {
104             $point = $points->item($i)->textContent;
105             return self::locationFromPoint($point);
106         }
107
108         return null;
109     }
110
111     // XXX: Move to ActivityUtils or Location?
112     static function locationFromPoint($point)
113     {
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);
123             }
124         }
125         common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
126         return null;
127     }
128
129     /**
130      * Returns context (StatusNet stuff) as an array suitable for serializing
131      * in JSON. Right now context stuff is an extension to Activity.
132      *
133      * @return array the context
134      */
135
136     function asArray()
137     {
138         $context = array();
139
140         $context['inReplyTo']    = $this->getInReplyToArray();
141         $context['conversation'] = $this->conversation;
142
143         return array_filter($context);
144     }
145
146     /**
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.
152      *
153      * XXX: People's ideas on how to do this are all over the place
154      *
155      * @return array the array of recipients
156      */
157
158     function getToArray()
159     {
160         $tos = array();
161
162         foreach ($this->attention as $attnUrl => $attnType) {
163             $to = array(
164                 'objectType' => $attnType,  // can be empty
165                 'id'         => $attnUrl,
166             );
167             $tos[] = $to;
168         }
169
170         return $tos;
171     }
172
173     /**
174      * Return an array for the notices this notice is a reply to 
175      * suitable for serializing as JSON note objects.
176      *
177      * @return array the array of notes
178      */
179
180      function getInReplyToArray()
181      {
182          if (empty($this->replyToID) && empty($this->replyToUrl)) {
183              return null;
184          }
185
186          $replyToObj = array('objectType' => 'note');
187
188          // XXX: Possibly shorten this to just the numeric ID?
189          //      Currently, it's the full URI of the notice.
190          if (!empty($this->replyToID)) {
191              $replyToObj['id'] = $this->replyToID;
192          }
193          if (!empty($this->replyToUrl)) {
194              $replyToObj['url'] = $this->replyToUrl;
195          }
196
197          return $replyToObj;
198      }
199
200 }
201