Fix correctness bugs found by code review

- matrix: drop cryptohelper LoginAs so device.json creds aren't
  invalidated on every restart by Init's re-login
- matrix: thread ctx through PostThreadedReply; bot dispatcher derives
  per-command ctx from app lifecycle so SIGTERM cancels in-flight work
- matrix: isTokenValid verifies /whoami user_id matches configured one
- matrix: loadDevice distinguishes missing file from corrupt parse;
  refuse to silently overwrite a corrupt device.json
- matrix: Start checks Syncer type assertion and returns error
- arr: parseLookup skips empty-title items and extracts existing id
- bot: skip Add when Result.Exists; reply "already in library"
This commit is contained in:
prosolis
2026-05-24 20:50:51 -07:00
parent 8c295d183b
commit 035089c159
5 changed files with 137 additions and 52 deletions

View File

@@ -19,9 +19,10 @@ import (
"maunium.net/go/mautrix/id"
)
// Replier sends a threaded reply into roomID under rootEventID.
// Replier sends a threaded reply into roomID under rootEventID. The ctx
// governs cancellation/deadline so shutdown propagates through replies.
type Replier interface {
PostThreadedReply(roomID id.RoomID, rootEventID id.EventID, plain, htmlBody string) error
PostThreadedReply(ctx context.Context, roomID id.RoomID, rootEventID id.EventID, plain, htmlBody string) error
}
// Services holds the configured *arr clients. Any field may be nil; commands
@@ -34,6 +35,7 @@ type Services struct {
// Dispatcher routes parsed commands to services and posts replies.
type Dispatcher struct {
baseCtx context.Context
prefix string
services Services
replier Replier
@@ -41,12 +43,17 @@ type Dispatcher struct {
}
// New builds a Dispatcher. prefix is the leading character(s) on a command
// (e.g. "!"); a zero value defaults to "!".
func New(prefix string, services Services, replier Replier) *Dispatcher {
// (e.g. "!"); a zero value defaults to "!". baseCtx becomes the parent of
// every per-command context so app shutdown cancels in-flight handlers.
func New(baseCtx context.Context, prefix string, services Services, replier Replier) *Dispatcher {
if prefix == "" {
prefix = "!"
}
if baseCtx == nil {
baseCtx = context.Background()
}
return &Dispatcher{
baseCtx: baseCtx,
prefix: prefix,
services: services,
replier: replier,
@@ -64,11 +71,11 @@ func (d *Dispatcher) Handle(roomID id.RoomID, eventID id.EventID, sender id.User
slog.Info("bot: command received", "room", roomID, "sender", sender, "cmd", cmd, "query", query)
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
ctx, cancel := context.WithTimeout(d.baseCtx, d.timeout)
defer cancel()
plain, htmlBody := d.dispatch(ctx, cmd, query)
if err := d.replier.PostThreadedReply(roomID, eventID, plain, htmlBody); err != nil {
if err := d.replier.PostThreadedReply(ctx, roomID, eventID, plain, htmlBody); err != nil {
slog.Error("bot: reply failed", "room", roomID, "err", err)
}
}
@@ -130,6 +137,10 @@ func (d *Dispatcher) run(ctx context.Context, cmd, service string, client arr.Cl
}
top := results[0]
if top.Exists {
msg := fmt.Sprintf("%s already has %s in the library.", service, formatTitle(top))
return msg, html.EscapeString(msg)
}
if err := client.Add(ctx, top); err != nil {
slog.Error("bot: add failed", "service", service, "title", top.Title, "err", err)
msg := fmt.Sprintf("%s: failed to add %s: %v", service, formatTitle(top), err)