[COMPONENTS][Posting][Link][Tag] Pass the note, not just the id in ProcessNoteContent

This commit is contained in:
Hugo Sales 2021-09-20 16:16:42 +01:00
parent 7926f18f93
commit 2f3f7b8469
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 13 additions and 8 deletions

View File

@ -25,6 +25,7 @@ use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Entity; use App\Entity;
use App\Entity\Note;
use App\Entity\NoteToLink; use App\Entity\NoteToLink;
use App\Util\Common; use App\Util\Common;
use App\Util\HTML; use App\Util\HTML;
@ -35,7 +36,7 @@ class Link extends Component
/** /**
* Extract URLs from $content and create the appropriate Link and NoteToLink entities * Extract URLs from $content and create the appropriate Link and NoteToLink entities
*/ */
public function onProcessNoteContent(int $note_id, string $content) public function onProcessNoteContent(Note $note, string $content)
{ {
if (Common::config('attachments', 'process_links')) { if (Common::config('attachments', 'process_links')) {
$matched_urls = []; $matched_urls = [];
@ -44,7 +45,7 @@ class Link extends Component
foreach ($matched_urls as $match) { foreach ($matched_urls as $match) {
try { try {
$link_id = Entity\Link::getOrCreate($match)->getId(); $link_id = Entity\Link::getOrCreate($match)->getId();
DB::persist(NoteToLink::create(['link_id' => $link_id, 'note_id' => $note_id])); DB::persist(NoteToLink::create(['link_id' => $link_id, 'note_id' => $note->getId()]));
} catch (InvalidArgumentException) { } catch (InvalidArgumentException) {
continue; continue;
} }

View File

@ -156,7 +156,7 @@ class Posting extends Component
DB::persist($note); DB::persist($note);
// Need file and note ids for the next step // Need file and note ids for the next step
Event::handle('ProcessNoteContent', [$note->getId(), $content, $content_type]); Event::handle('ProcessNoteContent', [$note, $content, $content_type]);
DB::flush(); DB::flush();
if ($processed_attachments != []) { if ($processed_attachments != []) {

View File

@ -21,10 +21,12 @@
namespace Component\Tag; namespace Component\Tag;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Note;
use App\Entity\NoteTag; use App\Entity\NoteTag;
use App\Util\Formatting; use App\Util\Formatting;
use App\Util\HTML; use App\Util\HTML;
@ -51,14 +53,16 @@ class Tag extends Component
/** /**
* Process note by extracting any tags present * Process note by extracting any tags present
*/ */
public function onProcessNoteContent(int $note_id, string $content) public function onProcessNoteContent(Note $note, string $content)
{ {
$matched_tags = []; $matched_tags = [];
$processed_tags = false; $processed_tags = false;
preg_match_all(self::TAG_REGEX, $content, $matched_tags, PREG_SET_ORDER); preg_match_all(self::TAG_REGEX, $content, $matched_tags, PREG_SET_ORDER);
foreach ($matched_tags as $match) { foreach ($matched_tags as $match) {
$tag = $match[2]; $tag = $match[2];
DB::persist(NoteTag::create(['tag' => $tag, 'canonical' => $this->canonicalTag($tag), 'note_id' => $note_id])); $canonical_tag = self::canonicalTag($tag);
DB::persist(NoteTag::create(['tag' => $tag, 'canonical' => $canonical_tag, 'note_id' => $note->getId()]));
Cache::pushList("tag-{$canonical_tag}", $note);
$processed_tags = true; $processed_tags = true;
} }
if ($processed_tags) { if ($processed_tags) {
@ -73,12 +77,12 @@ class Tag extends Component
private function tagLink(string $tag): string private function tagLink(string $tag): string
{ {
$canonical = $this->canonicalTag($tag); $canonical = self::canonicalTag($tag);
$url = Router::url('tag', ['tag' => $canonical]); $url = Router::url('tag', ['tag' => $canonical]);
return HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => $tag, 'rel' => 'tag'], $tag]], options: ['indent' => false]); return HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => $tag, 'rel' => 'tag'], $tag]], options: ['indent' => false]);
} }
public function canonicalTag(string $tag): string public static function canonicalTag(string $tag): string
{ {
return substr(Formatting::slugify($tag), 0, self::MAX_TAG_LENGTH); return substr(Formatting::slugify($tag), 0, self::MAX_TAG_LENGTH);
} }