Skip to content

Commit

Permalink
imp: Import Pocket tags as links tags
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Oct 31, 2024
1 parent 0010f42 commit 670bc17
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 119 deletions.
2 changes: 0 additions & 2 deletions src/controllers/importations/Pocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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'),
];
Expand Down
48 changes: 24 additions & 24 deletions src/jobs/PocketImportator.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public function perform(int $importation_id): void
*
* @param array<array<string, mixed>> $items
* @param array{
* 'ignore_tags': bool,
* 'import_bookmarks': bool,
* 'import_favorites': bool,
* } $options
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions src/models/Importation.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public function fail(string $error): void

/**
* @return array{
* 'ignore_tags': bool,
* 'import_bookmarks': bool,
* 'import_favorites': bool,
* }
Expand All @@ -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'];
}
Expand Down
2 changes: 2 additions & 0 deletions src/models/dao/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
trait Message
{
use BulkQueries;

/**
* Return the link messages, orderer by creation date
*
Expand Down
26 changes: 0 additions & 26 deletions src/views/importations/pocket/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,6 @@
<form method="post" action="<?= url('import pocket') ?>">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />

<div class="form-group" data-controller="caption-switcher">
<input
type="checkbox"
id="ignore-tags"
name="ignore_tags"
checked
aria-describedby="ignore-tags-caption"
data-action="caption-switcher#switch"
data-caption-switcher-target="switch"
/>

<label class="label--checkbox" for="ignore-tags">
<?= _('Import your links by ignoring the tags') ?>
</label>

<p class="form-group__caption form-group__caption--alt" id="ignore-tags-caption" aria-live="polite">
<span data-caption-switcher-target="caption" data-caption-value="on">
<?= _f('%s will import your links in a collection named “Pocket links”, your tags will be ignored.', $brand) ?>
</span>

<span data-caption-switcher-target="caption" data-caption-unchecked>
<?= _f('%s will import your links in a collection named “Pocket links” and in collections named after your tags (not recommended if you have a lot of tags).', $brand) ?>
</span>
</p>
</div>

<div class="form-group" data-controller="caption-switcher">
<input
type="checkbox"
Expand Down
66 changes: 5 additions & 61 deletions tests/jobs/PocketImportatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function testImportPocketItemsImportInBookmarks(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down Expand Up @@ -68,7 +67,6 @@ public function testImportPocketItemsDoesNotImportInBookmarksIfOption(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => false,
'import_favorites' => true,
];
Expand Down Expand Up @@ -99,7 +97,6 @@ public function testImportPocketItemsImportInFavorite(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down Expand Up @@ -132,7 +129,6 @@ public function testImportPocketItemsDoesNotKeepFavoriteCollectionIfEmpty(): voi
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand All @@ -158,7 +154,6 @@ public function testImportPocketItemsDoesNotImportInFavoriteIfOption(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => false,
];
Expand Down Expand Up @@ -186,7 +181,6 @@ public function testImportPocketItemsImportInDefaultCollection(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand All @@ -212,7 +206,6 @@ public function testImportPocketItemsImportDoesNotKeepDefaultCollectionIfEmpty()
$url = $this->fake('url');
$items = [];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -323,7 +272,6 @@ public function testImportPocketItemsUsesTimeAddedIfItExists(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down Expand Up @@ -365,7 +313,6 @@ public function testImportPocketItemsDoesNotDuplicateAGivenUrlAlreadyThere(): vo
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down Expand Up @@ -412,7 +359,6 @@ public function testImportPocketItemsDoesNotDuplicateAResolvedUrlAlreadyThere():
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down Expand Up @@ -449,7 +395,6 @@ public function testImportPocketItemsSetsResolvedTitle(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down Expand Up @@ -479,7 +424,6 @@ public function testImportPocketItemsSetsGivenTitle(): void
],
];
$options = [
'ignore_tags' => true,
'import_bookmarks' => true,
'import_favorites' => true,
];
Expand Down

0 comments on commit 670bc17

Please sign in to comment.