]> git.mxchange.org Git - friendica.git/blob - src/Module/Objects.php
Speed improvement
[friendica.git] / src / Module / Objects.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use Friendica\Network\HTTPException;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Util\Network;
32
33 /**
34  * ActivityPub Objects
35  */
36 class Objects extends BaseModule
37 {
38         public static function rawContent(array $parameters = [])
39         {
40                 if (empty($parameters['guid'])) {
41                         throw new HTTPException\BadRequestException();
42                 }
43
44                 if (!ActivityPub::isRequest()) {
45                         DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
46                 }
47
48                 /// @todo Add Authentication to enable fetching of non public content
49                 // $requester = HTTPSignature::getSigner('', $_SERVER);
50
51                 $item = Item::selectFirst(
52                         ['id', 'origin', 'author-link', 'changed'],
53                         [
54                                 'guid' => $parameters['guid'],
55                                 'private' => [Item::PUBLIC, Item::UNLISTED]
56                         ],
57                         ['order' => ['origin' => true]]
58                 );
59                 // Valid items are original post or posted from this node (including in the case of a forum)
60                 if (!DBA::isResult($item) || !$item['origin'] && (parse_url($item['author-link'], PHP_URL_HOST) != parse_url(DI::baseUrl()->get(), PHP_URL_HOST))) {
61                         throw new HTTPException\NotFoundException();
62                 }
63
64                 $etag          = md5($parameters['guid'] . '-' . $item['changed']);
65                 $last_modified = $item['changed'];
66                 Network::checkEtagModified($etag, $last_modified);
67
68                 if (empty($parameters['activity'])) {
69                         $activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
70                         $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
71
72                         // Only display "Create" activity objects here, no reshares or anything else
73                         if (empty($activity['object']) || ($activity['type'] != 'Create')) {
74                                 throw new HTTPException\NotFoundException();
75                         }
76
77                         $data = ['@context' => ActivityPub::CONTEXT];
78                         $data = array_merge($data, $activity['object']);
79                 } elseif (in_array($parameters['activity'], ['Create', 'Announce', 'Update', 
80                         'Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept', 'Follow', 'Add'])) {
81                         $data = ActivityPub\Transmitter::createActivityFromItem($item['id']);
82                         if (empty($data)) {
83                                 throw new HTTPException\NotFoundException();
84                         }
85                         if ($parameters['activity'] != 'Create') {
86                                 $data['type'] = $parameters['activity'];
87                                 $data['id'] = str_replace('/Create', '/' . $parameters['activity'], $data['id']);
88                         }
89                 } else {
90                         throw new HTTPException\NotFoundException();
91                 }
92
93                 // Relaxed CORS header for public items
94                 header('Access-Control-Allow-Origin: *');
95                 System::jsonExit($data, 'application/activity+json');
96         }
97 }