diff --git a/src/controllers/importations/Pocket.php b/src/controllers/importations/Pocket.php index e81ae10f..44828681 100644 --- a/src/controllers/importations/Pocket.php +++ b/src/controllers/importations/Pocket.php @@ -51,7 +51,6 @@ public function show(Request $request): Response * Initialize a new Pocket importation and register a PocketImportator job. * * @request_param string $csrf - * @request_param boolean $ignore_tags * @request_param boolean $import_bookmarks * @request_param boolean $import_favorites * @@ -112,7 +111,6 @@ public function import(Request $request): Response } $options = [ - 'ignore_tags' => $request->paramBoolean('ignore_tags'), 'import_bookmarks' => $request->paramBoolean('import_bookmarks'), 'import_favorites' => $request->paramBoolean('import_favorites'), ]; diff --git a/src/jobs/PocketImportator.php b/src/jobs/PocketImportator.php index bf1adc59..5f5b0954 100644 --- a/src/jobs/PocketImportator.php +++ b/src/jobs/PocketImportator.php @@ -107,7 +107,6 @@ public function perform(int $importation_id): void * * @param array> $items * @param array{ - * 'ignore_tags': bool, * 'import_bookmarks': bool, * 'import_favorites': bool, * } $options @@ -144,6 +143,7 @@ public function importPocketItems(models\User $user, array $items, array $option $links_to_create = []; $collections_to_create = []; $links_to_collections_to_create = []; + $messages = []; foreach ($items as $item) { /** @var string */ @@ -163,29 +163,12 @@ public function importPocketItems(models\User $user, array $items, array $option $collection_ids[] = $bookmarks_collection->id; } - if (is_array($item['tags'] ?? null) && !$options['ignore_tags']) { - // we want to create a collection per tag - $tags = array_keys($item['tags']); - foreach ($tags as $tag) { - if (isset($collection_ids_by_names[$tag])) { - // a collection named by the current tag already - // exists, just pick its id - $collection_ids[] = $collection_ids_by_names[$tag]; - } else { - // the collection needs to be created - $collection = models\Collection::init($user->id, $tag, '', false); - $collection->created_at = \Minz\Time::now(); - - $collections_to_create[] = $collection; - - // add the collection to the map array to avoid - // creating it again next time we find it - $collection_ids_by_names[$collection->name] = $collection->id; - - // and add the collection id to the array which stores - // the link collections - $collection_ids[] = $collection->id; - } + $tags = []; + + if (is_array($item['tags'] ?? null)) { + foreach (array_keys($item['tags']) as $tag) { + $tag = str_replace(' ', '_', $tag); + $tags[] = $tag; } } @@ -208,6 +191,8 @@ public function importPocketItems(models\User $user, array $items, array $option $link->title = $item['given_title']; } + $link->setTags($tags); + // In normal cases, created_at is set on save() call. Since we // add links via the bulkInsert call, we have to set created_at // first, or it would fail because of the not-null constraint. @@ -245,12 +230,27 @@ public function importPocketItems(models\User $user, array $items, array $option $link_to_collection->created_at = $published_at; $links_to_collections_to_create[] = $link_to_collection; } + + // We create a message containing the list of tags if any. + if ($tags) { + $formatted_tags = array_map(function ($tag) { + return "#{$tag}"; + }, $tags); + + $content = implode(' ', $formatted_tags); + + $message = new models\Message($user->id, $link_id, $content); + $message->created_at = $published_at; + + $messages[] = $message; + } } // Finally, let the big import (in DB) begin! models\Link::bulkInsert($links_to_create); models\Collection::bulkInsert($collections_to_create); models\LinkToCollection::bulkInsert($links_to_collections_to_create); + models\Message::bulkInsert($messages); // Delete the collections if they are empty at the end of the // importation. diff --git a/src/models/Importation.php b/src/models/Importation.php index 17dc3ea7..06330662 100644 --- a/src/models/Importation.php +++ b/src/models/Importation.php @@ -86,7 +86,6 @@ public function fail(string $error): void /** * @return array{ - * 'ignore_tags': bool, * 'import_bookmarks': bool, * 'import_favorites': bool, * } @@ -98,15 +97,10 @@ public function pocketOptions(): array } $clean_options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; - if (is_bool($this->options['ignore_tags'])) { - $clean_options['ignore_tags'] = $this->options['ignore_tags']; - } - if (is_bool($this->options['import_bookmarks'])) { $clean_options['import_bookmarks'] = $this->options['import_bookmarks']; } diff --git a/src/models/dao/Message.php b/src/models/dao/Message.php index 360a939d..768684b3 100644 --- a/src/models/dao/Message.php +++ b/src/models/dao/Message.php @@ -12,6 +12,8 @@ */ trait Message { + use BulkQueries; + /** * Return the link messages, orderer by creation date * diff --git a/src/views/importations/pocket/show.phtml b/src/views/importations/pocket/show.phtml index b5886254..56a01662 100644 --- a/src/views/importations/pocket/show.phtml +++ b/src/views/importations/pocket/show.phtml @@ -71,32 +71,6 @@
-
- - - - -

- - - - - - - -

-
-
true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -68,7 +67,6 @@ public function testImportPocketItemsDoesNotImportInBookmarksIfOption(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => false, 'import_favorites' => true, ]; @@ -99,7 +97,6 @@ public function testImportPocketItemsImportInFavorite(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -132,7 +129,6 @@ public function testImportPocketItemsDoesNotKeepFavoriteCollectionIfEmpty(): voi ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -158,7 +154,6 @@ public function testImportPocketItemsDoesNotImportInFavoriteIfOption(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => false, ]; @@ -186,7 +181,6 @@ public function testImportPocketItemsImportInDefaultCollection(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -212,7 +206,6 @@ public function testImportPocketItemsImportDoesNotKeepDefaultCollectionIfEmpty() $url = $this->fake('url'); $items = []; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -223,40 +216,7 @@ public function testImportPocketItemsImportDoesNotKeepDefaultCollectionIfEmpty() $this->assertNull($collection); } - public function testImportPocketItemsDoesNotImportTags(): void - { - $importator = new PocketImportator(); - $user = UserFactory::create(); - /** @var string */ - $url = $this->fake('url'); - /** @var string */ - $tag = $this->fake('word'); - $items = [ - [ - 'given_url' => $url, - 'resolved_url' => $url, - 'favorite' => '0', - 'status' => '1', - 'tags' => [ - $tag => ['item_id' => 'some id', 'tag' => $tag], - ], - ], - ]; - $options = [ - 'ignore_tags' => true, - 'import_bookmarks' => true, - 'import_favorites' => true, - ]; - - $importator->importPocketItems($user, $items, $options); - - $link = models\Link::findBy(['url' => $url]); - $this->assertNotNull($link); - $collection = models\Collection::findBy(['name' => $tag]); - $this->assertNull($collection); - } - - public function testImportPocketItemsImportTagsIfOption(): void + public function testImportPocketItemsImportTags(): void { $importator = new PocketImportator(); $user = UserFactory::create(); @@ -279,29 +239,18 @@ public function testImportPocketItemsImportTagsIfOption(): void ], ]; $options = [ - 'ignore_tags' => false, 'import_bookmarks' => true, 'import_favorites' => true, ]; $importator->importPocketItems($user, $items, $options); - $collection1 = models\Collection::findBy(['name' => $tag1]); - $collection2 = models\Collection::findBy(['name' => $tag2]); - $this->assertNotNull($collection1); - $this->assertNotNull($collection2); $link = models\Link::findBy(['url' => $url]); $this->assertNotNull($link); - $db_links_to_collection1 = models\LinkToCollection::findBy([ - 'link_id' => $link->id, - 'collection_id' => $collection1->id, - ]); - $db_links_to_collection2 = models\LinkToCollection::findBy([ - 'link_id' => $link->id, - 'collection_id' => $collection2->id, - ]); - $this->assertNotNull($db_links_to_collection1); - $this->assertNotNull($db_links_to_collection2); + $this->assertEquals([$tag1 => $tag1, $tag2 => $tag2], $link->tags); + $messages = $link->messages(); + $this->assertSame(1, count($messages)); + $this->assertSame("#{$tag1} #{$tag2}", $messages[0]->content); } public function testImportPocketItemsUsesTimeAddedIfItExists(): void @@ -323,7 +272,6 @@ public function testImportPocketItemsUsesTimeAddedIfItExists(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -365,7 +313,6 @@ public function testImportPocketItemsDoesNotDuplicateAGivenUrlAlreadyThere(): vo ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -412,7 +359,6 @@ public function testImportPocketItemsDoesNotDuplicateAResolvedUrlAlreadyThere(): ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -449,7 +395,6 @@ public function testImportPocketItemsSetsResolvedTitle(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ]; @@ -479,7 +424,6 @@ public function testImportPocketItemsSetsGivenTitle(): void ], ]; $options = [ - 'ignore_tags' => true, 'import_bookmarks' => true, 'import_favorites' => true, ];