Add session mode: signing in and adding to the cart.

With cart_mode = "session", dibs signs in to the shop's account and puts
an item in the cart when it fires. The account's cookies are kept apart
from the watcher's and stored encrypted, with the key in the OS keyring.
Without a keyring, they're stored in a file only you can read.

An alert now goes out right away, without a cart link, and a second one
follows once the item is in the cart. If adding fails, the second alert
carries the cart link instead.

Amazon only accepts additions its page scripts have seen, so plugins can
now press a button in headless Chrome with ctx:click.

The TUI asks for one-time codes and shows the plugin's warning about
account locks in a modal. 'dibs login' signs in from a terminal, or
with --browser in a Chrome window, and 'dibs logout' deletes the cookies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
OverlordandClaude Bot committed 2026-10-14 17:26:26 +02:00
1 parent e2cbc1a55c
commit 15a34e1f84
39 files changed
+3529 -163

No files matched your search

+63 -18
View File
@@ -192,17 +192,38 @@ func sleep(ctx context.Context, d time.Duration) error {
}
}
// link is where tapping a notification leads: the cart link if the shop has
// one, otherwise the product page.
// link is where tapping a notification leads: the cart page once session
// mode has added the item, the cart link if the shop has one, otherwise the
// product page. While session mode is adding the item, a cart link would add
// a second copy.
func link(a alert.Alert) string {
if a.CartLink != "" { return a.CartLink }
return a.URL
switch {
case a.Cart == alert.CartAdded && a.CartURL != "":
return a.CartURL
case a.Cart == alert.CartAdding || a.Cart == alert.CartAdded:
return a.URL
case a.CartLink != "":
return a.CartLink
default:
return a.URL
}
}
func headline(a alert.Alert) string {
h := "In stock: " + a.Item
if a.Test { h = "Test alert: " + a.Item }
switch a.Cart {
case alert.CartAdding:
h += ", adding to cart"
case alert.CartAdded:
h = "In cart: " + a.Item + ", finish checkout"
case alert.CartFailed:
h += ", adding to cart failed"
default:
// Deeplink mode.
}
if a.Test { h = "Test alert: " + h }
return h
}
@@ -215,17 +236,29 @@ func summary(a alert.Alert) string {
return s
}
func desktopText(a alert.Alert) (string, string) {
lines := []string{summary(a)}
// details are the lines under the summary.
func details(a alert.Alert) []string {
var lines []string
if a.CartError != "" { lines = append(lines, "Adding to the cart failed: "+a.CartError) }
if a.Title != "" { lines = append(lines, a.Title) }
return headline(a), strings.Join(lines, "\n")
return lines
}
func desktopText(a alert.Alert) (string, string) {
return headline(a), strings.Join(append([]string{summary(a)}, details(a)...), "\n")
}
func ntfyMessage(a alert.Alert) string {
lines := []string{summary(a)}
if a.Title != "" { lines = append(lines, a.Title) }
if a.CartLink != "" { lines = append(lines, "Cart: "+a.CartLink) }
lines := append([]string{summary(a)}, details(a)...)
switch {
case a.Cart == alert.CartAdded && a.CartURL != "":
lines = append(lines, "Cart: "+a.CartURL)
case a.Cart != alert.CartAdding && a.Cart != alert.CartAdded && a.CartLink != "":
lines = append(lines, "Add to cart: "+a.CartLink)
}
lines = append(lines, "Page: "+a.URL)
return strings.Join(lines, "\n")
@@ -270,7 +303,22 @@ func discordPayload(a alert.Alert) discordMessage {
if a.PreviousPrice != nil { price += ", down from " + alert.Money(*a.PreviousPrice, a.PreviousCurrency) }
desc := "[Product page](" + a.URL + ")"
if a.CartLink != "" { desc = "[Add to cart](" + a.CartLink + ") · " + desc }
switch l := link(a); {
case a.Cart == alert.CartAdded && l != a.URL:
desc = "[Cart](" + l + ") · " + desc
case l != a.URL:
desc = "[Add to cart](" + l + ") · " + desc
}
fields := []discordField{
{Name: "Price", Value: price, Inline: true},
{Name: "Seller", Value: a.SellerText(), Inline: true},
}
if a.CartError != "" { fields = append(fields, discordField{Name: "Adding to the cart failed", Value: truncate(a.CartError, 1024)}) }
color := 0x2ecc71
if a.Cart == alert.CartFailed { color = 0xe67e22 }
return discordMessage{
Username: "dibs",
@@ -280,11 +328,8 @@ func discordPayload(a alert.Alert) discordMessage {
Title: truncate(title, 256),
URL: link(a),
Description: desc,
Color: 0x2ecc71,
Fields: []discordField{
{Name: "Price", Value: price, Inline: true},
{Name: "Seller", Value: a.SellerText(), Inline: true},
},
Color: color,
Fields: fields,
Timestamp: a.Time.UTC().Format(time.RFC3339),
}},
// Page titles never ping anyone.
+37
View File
@@ -56,3 +56,40 @@ func TestRetries(t *testing.T) {
if c.requests > 1 && waited != 7*time.Second { t.Errorf("%d: waited %s between attempts, want 7s", c.status, waited) }
}
}
// TestCartSteps checks where each session-mode notification leads. While
// the item is being added, nothing may link to the cart link, which would
// add a second copy.
func TestCartSteps(t *testing.T) {
const (
page = "https://www.amazon.de/dp/B0HMX2FSTF"
addLink = "https://www.amazon.de/gp/aws/cart/add.html?ASIN.1=B0HMX2FSTF&Quantity.1=1"
cart = "https://www.amazon.de/gp/cart/view.html"
)
base := alert.Alert{Item: "metro", Shop: "amazon", URL: page, CartLink: addLink, Time: time.Now()}
cases := []struct {
cart, cartURL, cartErr string
headline, link string
} {
{"", "", "", "In stock: metro", addLink},
{alert.CartAdding, "", "", "In stock: metro, adding to cart", page},
{alert.CartAdded, cart, "", "In cart: metro, finish checkout", cart},
{alert.CartFailed, "", "timeout", "In stock: metro, adding to cart failed", addLink},
}
for _, tc := range cases {
a := base
a.Cart, a.CartURL, a.CartError = tc.cart, tc.cartURL, tc.cartErr
if got := headline(a); got != tc.headline { t.Errorf("%q: headline %q, want %q", tc.cart, got, tc.headline) }
if got := link(a); got != tc.link { t.Errorf("%q: link %q, want %q", tc.cart, got, tc.link) }
msg := ntfyMessage(a)
desc := discordPayload(a).Embeds[0].Description
if hasAdd := strings.Contains(msg, addLink) || strings.Contains(desc, addLink); hasAdd != (tc.link == addLink) { t.Errorf("%q: cart link in ntfy %v or discord %q", tc.cart, strings.Contains(msg, addLink), desc) }
if tc.cartErr != "" && !strings.Contains(msg, tc.cartErr) { t.Errorf("%q: ntfy message without the error: %q", tc.cart, msg) }
}
}
+16
View File
@@ -27,10 +27,18 @@ const PayloadVersion = 1
// "previous_currency": null,
// "seller": "first_party",
// "cart_link": "https://www.amazon.de/gp/aws/cart/add.html?ASIN.1=B0HMX2FSTF&Quantity.1=1",
// "cart": null,
// "cart_url": null,
// "cart_error": null,
// "time": "2026-10-14T09:30:00Z"
// }
//
// Every key is always present; values that do not apply are null.
//
// In session mode, an item that fires sends up to two payloads: one with
// "cart": "adding" and no cart_link, then one with "cart": "added" and the
// cart page in cart_url, or "cart": "failed", the reason in cart_error and
// the cart link.
type Payload struct {
Version int `json:"version"`
// Test is true for the sample alert of `dibs notify test`.
@@ -55,6 +63,11 @@ type Payload struct {
// Seller is "first_party", "fulfilled", "third_party" or "unknown".
Seller string `json:"seller"`
CartLink *string `json:"cart_link"`
// Cart is null without session mode, else "adding", "added" or
// "failed".
Cart *string `json:"cart"`
CartURL *string `json:"cart_url"`
CartError *string `json:"cart_error"`
// Time is when the page was checked, in UTC.
Time time.Time `json:"time"`
}
@@ -75,6 +88,9 @@ func NewPayload(a alert.Alert) Payload {
PreviousCurrency: orNull(a.PreviousCurrency),
Seller: a.Seller,
CartLink: orNull(a.CartLink),
Cart: orNull(a.Cart),
CartURL: orNull(a.CartURL),
CartError: orNull(a.CartError),
Time: a.Time.UTC().Truncate(time.Second),
}
}