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 8d36730
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 162 deletions.
Binary file modified locales/fr_FR/LC_MESSAGES/main.mo
Binary file not shown.
62 changes: 19 additions & 43 deletions locales/fr_FR/LC_MESSAGES/main.po
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
msgid ""
msgstr ""
"Project-Id-Version: Flus\n"
"POT-Creation-Date: 2024-10-31 18:43+0100\n"
"PO-Revision-Date: 2024-10-31 18:43+0100\n"
"POT-Creation-Date: 2024-10-31 19:23+0100\n"
"PO-Revision-Date: 2024-10-31 19:23+0100\n"
"Last-Translator: Marien Fressinaud <dev@marienfressinaud.fr>\n"
"Language-Team: \n"
"Language: fr_FR\n"
Expand Down Expand Up @@ -79,9 +79,9 @@ msgstr "L’une des thématiques associées n’existe pas."
#: controllers/Messages.php:94 controllers/Messages.php:145
#: controllers/Sessions.php:206 controllers/collections/Read.php:87
#: controllers/collections/Read.php:168 controllers/collections/Read.php:243
#: controllers/importations/Opml.php:79 controllers/importations/Pocket.php:110
#: controllers/importations/Pocket.php:157
#: controllers/importations/Pocket.php:258
#: controllers/importations/Opml.php:79 controllers/importations/Pocket.php:109
#: controllers/importations/Pocket.php:155
#: controllers/importations/Pocket.php:256
#: controllers/links/Collections.php:159 controllers/links/Read.php:46
#: controllers/links/Read.php:95 controllers/links/Read.php:144
#: controllers/links/Read.php:192 controllers/my/Avatar.php:44
Expand Down Expand Up @@ -199,11 +199,11 @@ msgstr "Vous avez déjà une importation OPML en cours."
msgid "This file cannot be uploaded."
msgstr "Ce fichier ne peut pas être téléversé."

#: controllers/importations/Pocket.php:93
#: controllers/importations/Pocket.php:92
msgid "You already have an ongoing Pocket importation."
msgstr "Vous avez déjà une importation Pocket en cours."

#: controllers/importations/Pocket.php:101
#: controllers/importations/Pocket.php:100
msgid "You didn’t authorize us to access your Pocket data."
msgstr "Vous n’avez pas autorisé l’accès à vos données Pocket."

Expand Down Expand Up @@ -249,19 +249,19 @@ msgstr ""
"Nous avons essayé d’importer depuis Pocket, mais vous n’avez pas autorisé "
"l’accès à vos données Pocket."

#: jobs/PocketImportator.php:119
#: jobs/PocketImportator.php:118
msgid "Pocket links"
msgstr "Liens Pocket"

#: jobs/PocketImportator.php:123
#: jobs/PocketImportator.php:122
msgid "All your links imported from Pocket."
msgstr "Tous vos liens importés depuis Pocket."

#: jobs/PocketImportator.php:127
#: jobs/PocketImportator.php:126
msgid "Pocket favorite"
msgstr "Favoris Pocket"

#: jobs/PocketImportator.php:131
#: jobs/PocketImportator.php:130
msgid "All your favorites imported from Pocket."
msgstr "Tous vos favoris importés depuis Pocket."

Expand Down Expand Up @@ -1360,48 +1360,24 @@ msgstr ""
"nouveaux liens dans vos collections et/ou signets."

#: views/importations/pocket/show.phtml:86
msgid "Import your links by ignoring the tags"
msgstr "Importer vos liens en ignorant les tags"

#: views/importations/pocket/show.phtml:91
#, php-format
msgid ""
"%s will import your links in a collection named “Pocket links”, your tags "
"will be ignored."
msgstr ""
"%s importera vos liens dans une collection nommée « Liens Pocket », vos tags "
"seront ignorés."

#: views/importations/pocket/show.phtml:95
#, php-format
msgid ""
"%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)."
msgstr ""
"%s importera vos liens dans une collection nommée « Liens Pocket » et dans "
"des collections nommées d’après vos tags (déconseillé si vous avez de "
"nombreux tags)."

#: views/importations/pocket/show.phtml:112
msgid "Import non-archived links in your bookmarks"
msgstr "Importer vos liens non-archivés dans vos signets"

#: views/importations/pocket/show.phtml:117
#: views/importations/pocket/show.phtml:91
#, php-format
msgid "%s will import your non-archived Pocket links in your bookmarks."
msgstr "%s importera vos liens Pocket non-archivés dans vos signets."

#: views/importations/pocket/show.phtml:121
#: views/importations/pocket/show.phtml:95
#, php-format
msgid "%s will leave your bookmarks unchanged."
msgstr "%s ne modifiera pas vos signets."

#: views/importations/pocket/show.phtml:138
#: views/importations/pocket/show.phtml:112
msgid "Import your Pocket favorites in a dedicated collection"
msgstr "Importer vos favoris Pocket dans une collection dédiée"

#: views/importations/pocket/show.phtml:143
#: views/importations/pocket/show.phtml:117
#, php-format
msgid ""
"%s doesn’t provide favorites, but it’ll import them in a collection named "
Expand All @@ -1410,21 +1386,21 @@ msgstr ""
"%s ne propose pas de système de favoris, mais il les importera dans une "
"collection appelée « Favoris Pocket »."

#: views/importations/pocket/show.phtml:147
#: views/importations/pocket/show.phtml:121
#, php-format
msgid "%s doesn’t provide favorites, so they’ll be ignored."
msgstr "%s ne propose pas de système de favoris, ils seront donc ignorés."

#: views/importations/pocket/show.phtml:154
#: views/importations/pocket/show.phtml:128
msgid "Start the import"
msgstr "Démarrer l’importation"

#: views/importations/pocket/show.phtml:161 views/mastodon/show.phtml:93
#: views/importations/pocket/show.phtml:135 views/mastodon/show.phtml:93
#, php-format
msgid "connected with account <em>%s</em>"
msgstr "connecté‧e avec le compte <em>%s</em>"

#: views/importations/pocket/show.phtml:170
#: views/importations/pocket/show.phtml:144
msgid "Connect to Pocket"
msgstr "Se connecter à Pocket"

Expand Down
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
Loading

0 comments on commit 8d36730

Please sign in to comment.