aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/Note.php
diff options
context:
space:
mode:
authorwinter2025-02-16 19:36:51 +0000
committerwinter2025-02-16 19:36:51 +0000
commit5a44137ddea7b3a6d7f68cbe71594f7633d6e9a6 (patch)
tree1583032971f5fbde6e333a4c377c2b6672dc296c /Digitigrade/Model/Note.php
parent65dfe7aff06dfd3b044a5deb47b03d643c651ec1 (diff)
link previews
Diffstat (limited to 'Digitigrade/Model/Note.php')
-rw-r--r--Digitigrade/Model/Note.php45
1 files changed, 44 insertions, 1 deletions
diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php
index d5d4e69..ac9cc3c 100644
--- a/Digitigrade/Model/Note.php
+++ b/Digitigrade/Model/Note.php
@@ -4,12 +4,16 @@ namespace Digitigrade\Model;
use Digitigrade\Db;
use Digitigrade\FormattingProvider\CommonMark;
use Digitigrade\FormattingProvider\SanitisedHtml;
+use Digitigrade\LinkPreview;
use Digitigrade\Model\HomeTimelineItem;
use Digitigrade\Notification\Notifyable;
use Digitigrade\Timeline\TimelineIncludeable;
use Digitigrade\Timeline\TimelineItem;
+use JsonMapper;
class Note extends PushableModel implements TimelineIncludeable, Notifyable {
+ public const EXTENSION_LINK_PREVIEWS = 'https://pawpub.entities.org.uk/extension/link-previews';
+
public ?int $id;
public string $uri;
public \DateTimeImmutable $created;
@@ -265,6 +269,45 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
return htmlspecialchars($this->plainContent);
}
+ /**
+ * Gets a list of LinkPreviews applicable to this note
+ * @return LinkPreview[]
+ */
+ public function getLinkPreviews(): array {
+ if (($this->extensions[self::EXTENSION_LINK_PREVIEWS] ?? []) == []) {
+ return [];
+ }
+ $mapper = new JsonMapper();
+ $results = [];
+ foreach ($this->extensions[self::EXTENSION_LINK_PREVIEWS] as $preview) {
+ $results[] = $mapper->map($preview, LinkPreview::class);
+ }
+ return $results;
+ }
+
+ /**
+ * Generates new LinkPreviews for this note (but does not save them automatically)
+ * @return LinkPreview[] the new link previews
+ */
+ public function generateLinkPreviews(): array {
+ $matches = [];
+ preg_match_all(
+ '`[a-z+]+://[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(([-a-zA-Z0-9@:(%_\+.~#?&/=]|\)[^\s])*)`',
+ $this->getFormattedContent('text/markdown') ?? $this->plainContent,
+ $matches
+ );
+ $urls = $matches[0] ?? [];
+ $previews = array_filter(
+ array_map(fn($url) => LinkPreview::generate($url), $urls),
+ fn($p) => $p != null
+ );
+ $this->extensions[self::EXTENSION_LINK_PREVIEWS] = array_map(
+ fn($p) => json_decode(json_encode($p)),
+ $previews
+ );
+ return $previews;
+ }
+
public function renderAsHtml() {
render_template('note', ['note' => $this]);
}
@@ -345,7 +388,7 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable {
'description' => $attachment->description
];
}, $this->attachments),
- //'extensions' => []
+ 'extensions' => $this->extensions
];
}
} \ No newline at end of file