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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
<?php
namespace Digitigrade\Model;
use Digitigrade\Db;
use Digitigrade\Job\PushObject;
class Note extends PushableModel {
public ?int $id;
public string $uri;
public \DateTimeImmutable $created;
public ?\DateTimeImmutable $modified;
public Actor $author;
public ?string $summary;
public string $plainContent;
// associative array mapping strings to strings - mimetypes to content
public array $formattedContent = [];
public string $language;
/**
* @var Actor[]
*/
public array $mentions = [];
public ?Note $inReplyTo;
public ?Note $threadApex;
public NotePrivacy $privacy;
/**
* @var NoteAttachment[]
*/
public array $attachments = [];
public array $extensions = [];
/**
* Creates and saves a new note with the specified fields pre-set
* @param Actor $author who wrote the note
* @param string $plainContent text of the note, without any markup
* @param string $language ISO 639-3 3-letter language code
* @param ?string $summary summary line or content warning
* @param NotePrivacyScope $scope who is allowed to view this note
* @param bool $indexable whether this note should be findable e.g. in search results
* @return self
*/
public static function create(
Actor $author,
string $plainContent,
string $language,
?string $summary = null,
NotePrivacyScope $scope = NotePrivacyScope::PUBLIC ,
bool $indexable = true
): self {
$note = new self();
// can't set the note uri properly until it's been saved and we have an id
$note->uri = "UNKNOWN";
$note->created = new \DateTimeImmutable();
$note->author = $author;
$note->summary = $summary;
$note->plainContent = $plainContent;
$note->language = $language;
$note->privacy = new NotePrivacy();
$note->privacy->scope = $scope;
$note->privacy->indexable = $indexable;
$note->save();
$note->uri = path_to_uri("/post/$note->id");
$note->save();
return $note;
}
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;
}
protected function hydrate() {
$this->privacy = NotePrivacy::findWhere('note_id = ?', [$this->id]);
$this->formattedContent = $this->findFormattedContents();
$this->mentions = $this->findMentions();
$this->attachments = NoteAttachment::findAllWhere('note_id = ?', [$this->id]);
}
protected function validate(): bool {
// author has to be from the same instance as the note itself
return hostname_from_uri($this->author->uri) == hostname_from_uri($this->uri);
}
private function findFormattedContents(): array {
$pdo = Db::getInstance()->getPdo();
$stmt = $pdo->prepare('SELECT mimetype, body FROM note_formatted_content WHERE note_id = ?');
$stmt->execute([$this->id]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
private function findMentions(): array {
$pdo = Db::getInstance()->getPdo();
$stmt = $pdo->prepare('SELECT actor_id FROM note_mention WHERE note_id = ?');
$stmt->execute([$this->id]);
$actors = array_map(function ($actorId) {
return Actor::find($actorId);
}, $stmt->fetchAll(\PDO::FETCH_COLUMN, 0));
return $actors;
}
public static function findAllWithAuthor(Actor $author, ?int $limit = null, int $offset = 0): array {
return self::findAllWhere('author = ?', [$author->id], $limit, $offset);
}
public function getInteractions(): array {
return Interaction::findAllWithTarget($this);
}
protected function getRelevantServers(): array {
$recipientActors = match ($this->privacy->scope) {
NotePrivacyScope::NONE => [],
NotePrivacyScope::MUTUALS => $this->author->findMutualFollows(),
default => $this->author->findFollowers()
};
$recipientActors = array_merge($recipientActors, $this->mentions, $this->privacy->alsoVisibleTo);
$recipientActors = array_unique($recipientActors);
$instances = array_map(function (Actor $a) {
return $a->findHomeInstance();
}, $recipientActors);
return array_unique($instances);
}
/**
* Gets this note's formatted content in the given mimetype or null if there isn't one
* @param string $mimetype content type to return
* @return ?string the marked up content
*/
public function getFormattedContent(string $mimetype): ?string {
return array_filter($this->formattedContent, fn($item) => $item['mimetype'] == $mimetype)[0]['body'] ?? null;
}
public function jsonSerialize(): array {
if ($this->deleted) {
return [
'type' => 'tombstone',
'self' => path_to_uri("/post/$this->id"),
'previousType' => 'note'
];
}
return [
'type' => 'note',
'self' => path_to_uri("/post/$this->id"),
'created' => $this->created->format('c'),
'modified' => $this->modified?->format('c'),
'author' => $this->author->uri,
'summary' => $this->summary,
'plainContent' => $this->plainContent,
'formattedContent' => $this->formattedContent,
'language' => $this->language,
'mentions' => array_map(function (Actor $actor) {
return $actor->uri;
}, $this->mentions),
'inReplyTo' => $this->inReplyTo?->uri,
'threadApex' => $this->threadApex?->uri,
'privacy' => $this->privacy,
'attachments' => array_map(function (NoteAttachment $attachment) {
return [
'type' => $attachment->type,
'href' => $attachment->href,
'description' => $attachment->description
];
}, $this->attachments),
//'extensions' => []
];
}
}
|