Merge pull request #8040 from MrPetovan/bug/notices
[friendica.git/.git] / src / Module / Objects.php
1 <?php
2 /**
3  * @file src/Module/Objects.php
4  */
5 namespace Friendica\Module;
6
7 use Friendica\BaseModule;
8 use Friendica\Database\DBA;
9 use Friendica\DI;
10 use Friendica\Model\Item;
11 use Friendica\Protocol\ActivityPub;
12
13 /**
14  * ActivityPub Objects
15  */
16 class Objects extends BaseModule
17 {
18         public static function rawContent(array $parameters = [])
19         {
20                 $a = DI::app();
21
22                 if (empty($a->argv[1])) {
23                         throw new \Friendica\Network\HTTPException\NotFoundException();
24                 }
25
26                 if (!ActivityPub::isRequest()) {
27                         DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
28                 }
29
30                 /// @todo Add Authentication to enable fetching of non public content
31                 // $requester = HTTPSignature::getSigner('', $_SERVER);
32
33                 // At first we try the original post with that guid
34                 // @TODO: Replace with parameter from router
35                 $item = Item::selectFirst(['id'], ['guid' => $a->argv[1], 'origin' => true, 'private' => false]);
36                 if (!DBA::isResult($item)) {
37                         // If no original post could be found, it could possibly be a forum post, there we remove the "origin" field.
38                         // @TODO: Replace with parameter from router
39                         $item = Item::selectFirst(['id', 'author-link'], ['guid' => $a->argv[1], 'private' => false]);
40                         if (!DBA::isResult($item) || !strstr($item['author-link'], DI::baseUrl()->get())) {
41                                 throw new \Friendica\Network\HTTPException\NotFoundException();
42                         }
43                 }
44
45                 $activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
46                 // Only display "Create" activity objects here, no reshares or anything else
47                 if (!is_array($activity['object']) || ($activity['type'] != 'Create')) {
48                         throw new \Friendica\Network\HTTPException\NotFoundException();
49                 }
50
51                 $data = ['@context' => ActivityPub::CONTEXT];
52                 $data = array_merge($data, $activity['object']);
53
54                 header('Content-Type: application/activity+json');
55                 echo json_encode($data);
56                 exit();
57         }
58 }