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
|
<?php
namespace Digitigrade\Model;
use Digitigrade\Db;
class Note extends FetchableModel implements \JsonSerializable {
public ?int $id;
public string $uri;
public \DateTimeImmutable $created;
public ?\DateTimeImmutable $modified;
public Actor $author;
public ?string $summary;
public string $plainContent;
/**
* @var array<string, string>
*/
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 = [];
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]);
}
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): array {
return self::findAllWhere('author = ?', [$author->id]);
}
public function jsonSerialize(): array {
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' => []
];
}
}
|