From 38d52ea4b3fa1c1a114be96b83f347f57e457262 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Thu, 26 Sep 2024 01:31:29 +0200
Subject: [PATCH] Fix collection selection in qutebrowser-linkwarden

---
 .../userscripts/qutebrowser-linkwarden         | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/.config/qutebrowser/userscripts/qutebrowser-linkwarden b/.config/qutebrowser/userscripts/qutebrowser-linkwarden
index 78a04de..56e4111 100755
--- a/.config/qutebrowser/userscripts/qutebrowser-linkwarden
+++ b/.config/qutebrowser/userscripts/qutebrowser-linkwarden
@@ -7,6 +7,13 @@ get_collections() {
     | jq -r '.response | map_values(.name)[]'
 }
 
+get_collection_id() {
+  curl -H "Authorization: Bearer $LINKWARDEN_TOKEN" \
+    -H "Accept: application/json" \
+    "$LINKWARDEN_URL/api/v1/collections" 2>/dev/null \
+    | jq -r --arg c "$1" '.response[] | select(.name==$c).id'
+}
+
 collection=$(get_collections | $DMENU -p "Collection: ")
 url=$QUTE_URL
 name=$(echo "$QUTE_TITLE" | $DMENU -p "Title: ")
@@ -20,14 +27,21 @@ fi
 payload=$(jq -n \
   --arg u "$url" \
   --arg n "$name" \
-  --arg c "$collection" \
+  --arg cn "$collection" \
+  --argjson ci "$(get_collection_id $collection)" \
   '{
     url: $u,
     name: $n,
-    collection: {name: $c},
+    collection: {
+      name: $cn,
+      id: $ci
+    },
     tags: []
   }')
 
+echo "$payload" > /tmp/linkwarden-payload.json
+echo "$(get_collection_id "Werbung")" >> /tmp/linkwarden-payload.json
+
 response=$(curl -sS --fail-with-body \
   -H "Authorization: Bearer $LINKWARDEN_TOKEN" \
   -H "Accept: application/json" \