aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/Actor.php
blob: 4f3aab243a015d445a3b7d7e7f6d5221aa46c661 (plain)
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
<?php
namespace Digitigrade\Model;

class Actor extends PushableModel {
    public ?int $id;
    public string $uri;
    public bool $isLocal = false;
    public \DateTimeImmutable $created;
    public ?\DateTimeImmutable $modified;
    public ?string $homepage;
    public string $handle;
    public string $displayName;
    public ?string $avatar;
    public ?string $bio;
    public ?string $pronouns;
    public bool $automated = false;
    public bool $requestToFollow = false;
    public ActorEndpoints $endpoints;
    public array $extensions = [];

    /**
     * Creates and saves a new local actor with the specified fields pre-set
     * @param string $handle handle local part
     * @param ?string $displayName display name (default = same as handle)
     * @param ?string $bio biography / description
     * @param bool $automated is this account automated? (e.g. a bot)
     * @param bool $requestToFollow does this account check follow requests manually?
     * @return self
     */
    public static function create(
        string $handle,
        ?string $displayName = null,
        ?string $bio = null,
        bool $automated = false,
        bool $requestToFollow = true // setting this true by default as i think it's better for privacy
    ): self {
        if (self::findLocalByHandle($handle) != null) {
            throw new \RuntimeException('an actor with that local handle already exists!');
        }
        $actor = new self();
        $actor->uri = path_to_uri("/user/$handle");
        $actor->isLocal = true;
        $date = new \DateTimeImmutable();
        $actor->created = $date;
        $actor->modified = $date;
        // don't really need to set this as it's generated by the view at /user/:handle
        $actor->homepage = path_to_uri("/@/$handle");
        $actor->handle = $handle;
        $actor->displayName = $displayName ?? $handle;
        $actor->bio = $bio;
        $actor->automated = $automated;
        $actor->requestToFollow = $requestToFollow;
        // don't actually need to set these either
        // just need to set the basicFeed since it's a required field
        $actor->endpoints = new ActorEndpoints();
        $actor->endpoints->basicFeed = path_to_uri("/user/$handle/basicFeed");
        $actor->save();
        return $actor;
    }

    protected function getUpdateWhereClause(\PDO $db): ?string {
        if (self::findWhere('uri = ?', [$this->uri]) != null)
            return 'uri = ' . $db->quote($this->uri);
        if (self::findWhere('id = ?', [$this->id]) != null)
            return "id = $this->id";
        return null;
    }

    public static function findLocalByHandle(string $handle): ?self {
        return self::findWhere('is_local = true AND handle = ?', [$handle]);
    }

    public static function findByWebfinger(string $acct, bool $autoSave = true, bool $forceRefetch = false): ?self {
        // normalise uri
        if (!str_starts_with($acct, 'acct:')) {
            if (str_starts_with($acct, '@')) {
                $acct = substr($acct, 1);
            }
            $acct = "acct:$acct";
        }

        // fetch it
        return ActorWebfinger::findByAcct($acct, $autoSave, $forceRefetch)->actor;
    }

    public function jsonSerialize(): array {
        return [
            'type' => 'actor',
            'self' => path_to_uri("/user/$this->handle"),
            'created' => $this->created?->format('c'),
            'modified' => $this->modified?->format('c'),
            'homepage' => path_to_uri("/@/$this->handle"),
            'handle' => $this->handle,
            'displayName' => $this->displayName,
            'bio' => $this->bio,
            'pronouns' => $this->pronouns,
            'automated' => $this->automated,
            'endpoints' => [
                'basicFeed' => path_to_uri("/user/$this->handle/basicFeed"),
                'fullFeed' => path_to_uri("/user/$this->handle/fullFeed")
            ]
        ];
    }

    public function hydrate() {
        $this->endpoints = ActorEndpoints::findWhere('actor_id = ?', [$this->id]);
    }
}