-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActor.php
More file actions
122 lines (112 loc) · 3.32 KB
/
Copy pathActor.php
File metadata and controls
122 lines (112 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\RequestHandlers\Api;
use FediE2EE\PKD\Crypto\Exceptions\{
CryptoException,
JsonException,
NotImplementedException
};
use FediE2EE\PKDServer\{
Exceptions\CacheException,
Exceptions\DependencyException,
Exceptions\TableException,
Interfaces\HttpCacheInterface,
Meta\Route,
Redirect,
Traits\HttpCacheTrait
};
use FediE2EE\PKDServer\Tables\Actors;
use Override;
use ParagonIE\CipherSweet\Exception\{
ArrayKeyException,
BlindIndexNotFoundException,
CipherSweetException,
CryptoOperationException,
InvalidCiphertextException
};
use Psr\SimpleCache\InvalidArgumentException;
use Psr\Http\Message\{
ResponseInterface,
ServerRequestInterface
};
use Psr\Http\Server\RequestHandlerInterface;
use SodiumException;
use Throwable;
use TypeError;
use function is_null;
class Actor implements RequestHandlerInterface, HttpCacheInterface
{
use HttpCacheTrait;
protected Actors $actorsTable;
/**
* @throws CacheException
* @throws DependencyException
* @throws TableException
*/
public function __construct()
{
$actorsTable = $this->table('Actors');
if (!($actorsTable instanceof Actors)) {
throw new TypeError('Could not load Actors table at runtime');
}
$this->actorsTable = $actorsTable;
}
#[Override]
public function getPrimaryCacheKey(): string
{
return 'api:actor';
}
/**
* @api
*
* @throws ArrayKeyException
* @throws BlindIndexNotFoundException
* @throws CipherSweetException
* @throws CryptoException
* @throws CryptoOperationException
* @throws DependencyException
* @throws InvalidArgumentException
* @throws InvalidCiphertextException
* @throws JsonException
* @throws NotImplementedException
* @throws SodiumException
* @throws TableException
*/
#[Route("api/actor/{actor_id}")]
#[Override]
public function handle(ServerRequestInterface $request): ResponseInterface
{
// If no Actor ID is given, redirect.
$actorID = $request->getAttribute('actor_id') ?? '';
if (empty($actorID)) {
return (new Redirect('/api'))->respond();
}
// Resolve canonical Actor ID
try {
$actorID = $this->webfinger()->canonicalize($actorID);
} catch (Throwable $ex) {
return $this->error('A WebFinger error occurred: ' . $ex->getMessage());
}
// Cache actor lookup and counts
$response = $this->getCache()->cacheJson(
$actorID,
function () use ($actorID) {
$actor = $this->actorsTable->searchForActor($actorID);
if (is_null($actor)) {
return null;
}
$counts = $this->actorsTable->getCounts($actor->getPrimaryKey());
return [
'!pkd-context' => 'fedi-e2ee:v1/api/actor/info',
'actor-id' => $actorID,
'count-aux' => $counts['count-aux'],
'count-keys' => $counts['count-keys'],
];
}
);
if (is_null($response)) {
return $this->error('Actor not found', 404);
}
return $this->json($response);
}
}