aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/Note.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/Model/Note.php')
-rw-r--r--Digitigrade/Model/Note.php52
1 files changed, 50 insertions, 2 deletions
diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php
index 880e3e8..b32e625 100644
--- a/Digitigrade/Model/Note.php
+++ b/Digitigrade/Model/Note.php
@@ -7,11 +7,13 @@ use Digitigrade\FormattingProvider\SanitisedHtml;
use Digitigrade\LinkPreview;
use Digitigrade\Model\HomeTimelineItem;
use Digitigrade\Notification\Notifyable;
+use Digitigrade\Page;
use Digitigrade\Timeline\TimelineIncludeable;
use Digitigrade\Timeline\TimelineItem;
class Note extends PushableModel implements TimelineIncludeable, Notifyable {
public const EXTENSION_LINK_PREVIEWS = 'https://pawpub.entities.org.uk/extension/link-previews';
+ public const EXTENSION_PAGES = 'https://pawpub.entities.org.uk/extension/pages';
public ?int $id;
public string $uri;
@@ -195,6 +197,29 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
return $notes;
}
+ public static function findAllWithExtension(string $extensionUri, ?int $limit = null, int $offset = 0): array {
+ $pdo = Db::getInstance()->getPdo();
+ $query = 'SELECT note_id FROM note_extension LEFT JOIN note ON note.id = note_id '
+ . 'WHERE note_extension.uri = ? ORDER BY note.created DESC';
+ if ($limit != null) {
+ $query .= " LIMIT $limit OFFSET $offset";
+ }
+ $stmt = $pdo->prepare($query);
+ $stmt->execute([$extensionUri]);
+ $notes = [];
+ while ($id = $stmt->fetchColumn(0)) {
+ $notes[] = self::find($id);
+ }
+ return $notes;
+ }
+
+ public static function countWithExtension(string $extensionUri): int {
+ $pdo = Db::getInstance()->getPdo();
+ $stmt = $pdo->prepare('SELECT COUNT(*) FROM note_extension WHERE uri = ?');
+ $stmt->execute([$extensionUri]);
+ return $stmt->fetchColumn(0);
+ }
+
public function getInteractions(): array {
return Interaction::findAllWithTarget($this);
}
@@ -268,6 +293,10 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
return htmlspecialchars($this->plainContent);
}
+ public function getLocalUiHref(bool $asPage = false): string {
+ return '/@/' . $this->author->getFullHandle() . ($asPage ? '/page/' : '/note/') . $this->id;
+ }
+
/**
* Gets a list of LinkPreviews applicable to this note
* @return LinkPreview[]
@@ -285,6 +314,25 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
}
/**
+ * @return bool whether this note has an attached page
+ */
+ public function hasPage(): bool {
+ return isset($this->extensions[self::EXTENSION_PAGES]);
+ }
+
+ public function getPage(): ?Page {
+ if (!$this->hasPage())
+ return null;
+ $mapper = new \JsonMapper();
+ $mapper->bStrictObjectTypeChecking = false; // so that CharacterRanges can be decoded
+ return $mapper->map($this->extensions[self::EXTENSION_PAGES], Page::class);
+ }
+
+ public function setPage(Page $page) {
+ $this->extensions[self::EXTENSION_PAGES] = json_decode(json_encode($page));
+ }
+
+ /**
* Generates new LinkPreviews for this note (but does not save them automatically)
* @return LinkPreview[] the new link previews
*/
@@ -324,7 +372,7 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
return sprintf(__("notifications.$action.title"), $this->author->displayName);
}
public function getNotificationTitleLink(): ?string {
- return '/@/' . $this->author->getFullHandle() . '/note/' . $this->id;
+ return $this->author->getLocalUiHref();
}
public function getNotificationBody(): ?string {
return isset($this->summary) ? "[$this->summary]" : $this->plainContent;
@@ -333,7 +381,7 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
return $this->author->avatar ?? '/static/default-avatar.png';
}
public function getNotificationImageLink(): ?string {
- return '/@/' . $this->author->getFullHandle();
+ return $this->author->getLocalUiHref();
}
public function processNotifications() {