Remind before LinkedIn's sign-in expires, and stop posting once it has

LinkedIn tokens last 60 days and can't be renewed without signing in
again. The expiry LinkedIn reports is now recorded (60 days from sign-in
for older connections). The LinkedIn card says until when; from two
weeks before, the Overview, the Inbox and the card remind you, with a
Reconnect button. Once expired, LinkedIn counts as not connected, due
posts come back to the Inbox saying why, and a 401 from LinkedIn reads
"reconnect LinkedIn" instead of the raw error.

Also fixes a leftover "Settings → Credentials" in a publish error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 11:43:59 -07:00
co-authored by Claude Opus 5.5
parent a2c8889599
commit 9c62680927
14 changed files with 159 additions and 9 deletions
+14 -1
View File
@@ -62,7 +62,11 @@ func (s *Server) publishers() content.Publishers {
pubs[content.ChannelX] = xPublisher{c}
}
if tok, urn := s.Secrets.Get(secrets.LinkedInAccessToken), s.Secrets.Get(secrets.LinkedInAuthorURN); tok != "" && urn != "" {
pubs[content.ChannelLinkedIn] = linkedInPublisher{linkedin.New(tok, urn)}
if e := s.linkedInExpiry(); e != nil && e.Expired() {
pubs[content.ChannelLinkedIn] = expiredPublisher{*e}
} else {
pubs[content.ChannelLinkedIn] = linkedInPublisher{linkedin.New(tok, urn)}
}
}
if c := s.redditUser(); c != nil {
pubs[content.ChannelReddit] = redditPublisher{s: s, c: c}
@@ -70,6 +74,15 @@ func (s *Server) publishers() content.Publishers {
return pubs
}
// expiredPublisher stands in for a network whose sign-in ran out: it posts
// nothing and says so, rather than letting the network answer 401.
type expiredPublisher struct{ e Expiry }
func (p expiredPublisher) Publish(context.Context, content.Post) (string, error) {
return "", fmt.Errorf("%s's sign-in expired on %s: reconnect %s under Settings → Social accounts, then publish again",
p.e.Network, p.e.At.Local().Format("2 Jan 2006"), p.e.Network)
}
type xPublisher struct{ c *twitter.Client }
// Publish appends the source link to the post, as the old app did; X shows
+2 -2
View File
@@ -148,10 +148,10 @@ func (s *Server) entryData(e *inventory.Entry, showDismissed bool) (*entryPage,
if err != nil {
return nil, err
}
pubs := s.publishers()
conn := s.connected()
var chans []channelChoice
for _, c := range content.Channels {
chans = append(chans, channelChoice{c, pubs[c.ID] != nil})
chans = append(chans, channelChoice{c, conn[c.ID]})
}
var subs []string
seen := map[string]bool{}
+5 -2
View File
@@ -72,8 +72,10 @@ func groupBatches(drafts []content.Draft) []*draftBatch {
func (s *Server) connected() map[string]bool {
out := map[string]bool{}
for id := range s.publishers() {
out[id] = true
for id, p := range s.publishers() {
if _, expired := p.(expiredPublisher); !expired {
out[id] = true
}
}
return out
}
@@ -93,6 +95,7 @@ func (s *Server) renderInbox(w http.ResponseWriter, r *http.Request, errMsg stri
sections = append(sections, inboxSection{Module: "Content", Batches: groupBatches(pending)})
}
s.render(w, r, "inbox", page{Title: "Inbox", Nav: "content/inbox", Error: errMsg, Data: map[string]any{
"Reminders": s.reminders(),
"Sections": sections,
"Reddit": s.redditViews(r.Context(), pending),
"Connected": s.connected(),
+55 -1
View File
@@ -25,6 +25,57 @@ import (
type Connection struct {
Account string `json:"account"`
At time.Time `json:"at"`
// Expires is when the sign-in stops working, for networks whose tokens
// can't be renewed (LinkedIn); zero for ones Supermarket renews itself.
Expires time.Time `json:"expires,omitempty"`
}
// linkedInTokenLife is how long LinkedIn's access tokens last, for
// sign-ins saved before the expiry was recorded.
const linkedInTokenLife = 60 * 24 * time.Hour
// expiryWarning is how long before a sign-in expires the reminders start.
const expiryWarning = 14 * 24 * time.Hour
// Expiry is when a sign-in runs out, if it does.
type Expiry struct {
Network string // e.g. LinkedIn
At time.Time
}
// Expired reports whether it has run out.
func (e Expiry) Expired() bool { return !time.Now().Before(e.At) }
// Soon reports whether it runs out within expiryWarning (or has).
func (e Expiry) Soon() bool { return time.Until(e.At) < expiryWarning }
// Days is how many whole days are left (0 on the last day).
func (e Expiry) Days() int { return int(time.Until(e.At) / (24 * time.Hour)) }
// linkedInExpiry is when the LinkedIn sign-in runs out, or nil if LinkedIn
// isn't connected.
func (s *Server) linkedInExpiry() *Expiry {
if !s.Secrets.Has(secrets.LinkedInAccessToken) {
return nil
}
c := s.connections()["linkedin"]
at := c.Expires
if at.IsZero() {
if c.At.IsZero() {
return nil // a token from the old app: when it was made is unknown
}
at = c.At.Add(linkedInTokenLife)
}
return &Expiry{Network: "LinkedIn", At: at}
}
// reminders are the sign-ins running out soon, or already, for the
// Overview and the Inbox.
func (s *Server) reminders() []Expiry {
if e := s.linkedInExpiry(); e != nil && e.Soon() {
return []Expiry{*e}
}
return nil
}
const connectionsKey = "connections"
@@ -107,6 +158,7 @@ func (s *Server) dashboard(w http.ResponseWriter, r *http.Request) {
"ImportErr": errString(s.ImportErr),
"QueuedPosts": queued,
"Scheduled": len(scheduled),
"Reminders": s.reminders(),
"Pending": pending,
}})
}
@@ -153,7 +205,9 @@ func (s *Server) connectionSummary(c map[string]Connection) string {
} else if s.Secrets.Has(secrets.XAccessToken) {
parts = append(parts, "X")
}
if s.Secrets.Has(secrets.LinkedInAccessToken) {
if e := s.linkedInExpiry(); e != nil && e.Expired() {
parts = append(parts, "LinkedIn (expired)")
} else if s.Secrets.Has(secrets.LinkedInAccessToken) {
parts = append(parts, "LinkedIn")
}
if a := c["reddit"].Account; a != "" {
+41
View File
@@ -2,6 +2,7 @@ package web
import (
"bytes"
"context"
"encoding/json"
"io"
"log/slog"
@@ -403,3 +404,43 @@ func TestProfileDraftNeedsAI(t *testing.T) {
t.Error("warning shown with an AI key saved")
}
}
func TestLinkedInExpiryReminders(t *testing.T) {
s, h := newTestServer(t, nil)
_ = s.Secrets.Set(map[string]string{secrets.LinkedInAccessToken: "tok", secrets.LinkedInAuthorURN: "urn:li:person:1"})
day := 24 * time.Hour
get := func(path string) string { return do(h, http.MethodGet, path, nil, nil).Body.String() }
// Plenty of time: the card says until when, no reminders.
_ = s.saveConnection("linkedin", Connection{At: time.Now(), Expires: time.Now().Add(50 * day)})
if !strings.Contains(get("/settings/accounts"), "The sign-in lasts until") || strings.Contains(get("/"), "Reconnect LinkedIn") {
t.Error("50 days left: wrong reminders")
}
if !s.connected()["linkedin"] {
t.Error("50 days left: should be connected")
}
// Saved before expiries were recorded, 50 days ago: 10 days left.
_ = s.saveConnection("linkedin", Connection{At: time.Now().Add(-50 * day)})
for _, path := range []string{"/", "/inbox"} {
if body := get(path); !strings.Contains(body, "LinkedIn's sign-in expires in 9 days") && !strings.Contains(body, "LinkedIn's sign-in expires in 10 days") {
t.Errorf("%s: no 10-day reminder", path)
}
}
// Expired: reminders, not connected, and publishing says why.
_ = s.saveConnection("linkedin", Connection{At: time.Now().Add(-61 * day), Expires: time.Now().Add(-day)})
if body := get("/"); !strings.Contains(body, "LinkedIn's sign-in expired on") {
t.Error("expired: no reminder on the Overview")
}
if !strings.Contains(get("/settings/accounts"), "The sign-in expired on") {
t.Error("expired: the card doesn't say so")
}
if s.connected()["linkedin"] {
t.Error("expired: still counted as connected")
}
if _, err := s.publishers()["linkedin"].Publish(context.Background(), content.Post{Text: "hi"}); err == nil ||
!strings.Contains(err.Error(), "reconnect LinkedIn") {
t.Errorf("expired publish: %v", err)
}
}
+8 -1
View File
@@ -228,6 +228,7 @@ type connectInfo struct {
Account string
At time.Time
Note string
Expiry *Expiry // when the sign-in runs out, for LinkedIn
}
func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
@@ -272,6 +273,7 @@ func (s *Server) networks() []network {
case "LinkedIn":
n.Connect = connect("linkedin", secrets.LinkedInAccessToken, "In your LinkedIn app's Auth settings, add this redirect URL:", "",
set[secrets.LinkedInClientID] && set[secrets.LinkedInClientSecret], func(string) string { return "" })
n.Connect.Expiry = s.linkedInExpiry()
case "Reddit":
n.Connect = connect("reddit", secrets.RedditAccessToken, "At reddit.com/prefs/apps, create a web app with this redirect URI:",
"Reddit's API terms need approval for commercial use: Supermarket is meant for a few hand-approved posts, not volume.",
@@ -444,7 +446,12 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
if err := s.saveConnection("linkedin", Connection{Account: urn, At: time.Now()}); err != nil {
life := res.ExpiresIn
if life <= 0 {
life = linkedInTokenLife
}
now := time.Now()
if err := s.saveConnection("linkedin", Connection{Account: urn, At: now, Expires: now.Add(life)}); err != nil {
s.Log.Error("saving connection", "err", err)
}
redirect(w, r, "/settings/accounts", "li-connected")
+4
View File
@@ -361,3 +361,7 @@ form.inline { display: inline-block; margin: 0 0 0.75rem; }
/* A key's label, with where to get it */
.label-row { display: flex; align-items: baseline; gap: 0.75rem; }
.get-it { margin-left: auto; font-size: 0.82rem; font-weight: 600; white-space: nowrap; }
/* A sign-in running out */
.reminder { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.reminder p { margin: 0; flex: 1; min-width: 16rem; }
+5
View File
@@ -33,6 +33,11 @@
<div class="signin">
<h3>Sign in to post</h3>
{{if .Signed}}<p>Connected{{with .Account}} as <strong>{{.}}</strong>{{end}}{{if not .At.IsZero}}, {{ago .At}}{{end}}.</p>{{end}}
{{with .Expiry}}
{{if .Expired}}<p class="error">The sign-in expired on {{.At.Local.Format "2 Jan 2006"}}: reconnect to publish again.</p>
{{else if .Soon}}<p class="warn-text"><strong>The sign-in expires {{if eq .Days 0}}today{{else if eq .Days 1}}tomorrow{{else}}in {{.Days}} days{{end}}, on {{.At.Local.Format "2 Jan 2006"}}.</strong> Reconnect now to keep posting.</p>
{{else}}<p class="help">The sign-in lasts until {{.At.Local.Format "2 Jan 2006"}}; {{.Network}} doesn't let it be renewed without signing in again. Supermarket reminds you two weeks before.</p>{{end}}
{{end}}
<p class="help">{{.Setup}} <code>{{.Redirect}}</code>{{with .Note}}<br>{{.}}{{end}}</p>
<form method="post" action="/auth/{{.Provider}}/start">
<button type="submit" {{if not .Ready}}disabled title="Save the client ID{{if ne .Provider "x"}} and secret{{end}} first"{{end}}>{{if .Signed}}Reconnect{{else}}Connect{{end}} {{$name}}</button>
+1
View File
@@ -3,6 +3,7 @@
<h1>Overview</h1>
<p class="lede">Supermarket's modules, and what's left to set up.</p>
</div>
{{template "reminders" .Reminders}}
<div class="grid">
{{range .Modules}}
+2
View File
@@ -3,6 +3,8 @@
<p class="lede">Everything waiting for your approval. Nothing is published until you say so: publish a draft now, or schedule it.
{{if .Tagged}}Links to your own sites get UTM tags when published.{{end}}</p>
{{template "reminders" .Reminders}}
{{if .Sections}}
{{range .Sections}}
<h2>{{.Module}}</h2>
+9
View File
@@ -94,6 +94,15 @@
</div>
{{end}}
{{/* Sign-ins running out: {{template "reminders" .Reminders}}. */}}
{{define "reminders"}}{{range .}}
<section class="card warn reminder" role="status">
<p><strong>{{if .Expired}}{{.Network}}'s sign-in expired on {{.At.Local.Format "2 Jan 2006"}}.{{else}}{{.Network}}'s sign-in expires {{if eq .Days 0}}today{{else if eq .Days 1}}tomorrow{{else}}in {{.Days}} days{{end}}, on {{.At.Local.Format "2 Jan 2006"}}.{{end}}</strong>
{{if .Expired}}Nothing can be published to {{.Network}} until you reconnect; scheduled posts for it come back to the Inbox.{{else}}After that, nothing can be published to {{.Network}} until you reconnect.{{end}}</p>
<a class="button" href="/settings/accounts#network-{{.Network}}">Reconnect {{.Network}}</a>
</section>
{{end}}{{end}}
{{/* A secret's field: {{template "key-field" (dict "F" secretField "Set" bool)}}.
Saved values are never shown; an empty field keeps what's saved. */}}
{{define "key-field"}}