Add the notify and ntfy modules and scoped tokens
Breaking: a job with a raw element prints only with a token issued with uberbringer token --raw. Every other token, every existing one included, gets 403 token_scope for it, with the raw elements in details, and keeps every other route. The notify module prints a notification card from a title, message, priority and tags, with topic, click, up to three actions, image, markdown and time as options; it decodes strictly and refuses a payload over its length caps. The ntfy module takes one ntfy message as ntfy sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does not know, cuts text over the caps, and answers 204 for other events and binary bodies. Both print every message plain: one message cannot tell the first of a sequence from a replacement, since a publisher may choose the sequence ID. internal/pkg/ntfy holds ntfy's message types. Both print the same card. The priority picks the form: a dark URGENT rule and a double box, a double box headed HIGH, a single box with the title in its border, or a rule with no box. Tags print as markers in front of the title, the rest on a Tags row; links print as QR codes with their labels; attached PNG, JPEG, GIF and WebP images print after the card. A line-level markdown subset is laid out without a dependency, keeping marks inside code spans and URLs, and emoji are removed after a few typed ones become markers. A card that cannot be laid out prints as a plain receipt, and the server logs the fault once at ERROR and reports it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the markers table is a setting with the user's own keys, also settable as UBERBRINGER_NTFY_MARKERS=name=value,... uberbringer token --modules issues a token limited to those modules' print and preview routes, job status and the module list; each route in the table says which tokens it takes, and a scope claim that could read as absent makes the token invalid. image elements take optional, which also shrinks a too-wide image to the paper; a QR code that could be too wide for the paper prints with smaller modules; box takes title_bold. Submit takes a Meta whose source shows in job status and, with a copy of the request ID, in every worker log line of the job. The four ntfy cards passed on the TM-T88IV, posted as the client recipe posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md describes the modules, the card and the client recipe, with tokens read from a header file; docs/swagger.json is regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa15cb27b1
commit
1335ed0687
119 files changed
+5626
-468
No files matched your search
@@ -471,3 +471,32 @@ func TestLookupProfile(t *testing.T) {
|
||||
t.Errorf("unknown model err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQRModules(t *testing.T) {
|
||||
tests := []struct {
|
||||
n int
|
||||
level escpos.QRCorrection
|
||||
want int
|
||||
}{
|
||||
{1, escpos.QRCorrectionM, 21},
|
||||
{14, escpos.QRCorrectionM, 21},
|
||||
{15, escpos.QRCorrectionM, 25},
|
||||
{213, escpos.QRCorrectionM, 57},
|
||||
{504, escpos.QRCorrectionM, 85},
|
||||
{505, escpos.QRCorrectionM, 89},
|
||||
{17, escpos.QRCorrectionL, 21},
|
||||
{7, escpos.QRCorrectionH, 21},
|
||||
{2953, escpos.QRCorrectionL, 177},
|
||||
{2954, escpos.QRCorrectionL, 177},
|
||||
{2331, escpos.QRCorrectionM, 177},
|
||||
{1663, escpos.QRCorrectionQ, 177},
|
||||
{1273, escpos.QRCorrectionH, 177},
|
||||
{1274, escpos.QRCorrectionH, 177},
|
||||
{10, escpos.QRCorrection(0), 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := escpos.QRModules(tt.n, tt.level); got != tt.want {
|
||||
t.Errorf("QRModules(%d, %d) = %d, want %d", tt.n, tt.level, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,40 @@ const (
|
||||
MaxQRModuleSize = 16
|
||||
)
|
||||
|
||||
// qrByteCapacity holds, for each QR version from 1 to 40, how many bytes a
|
||||
// code of that version holds in byte mode at correction levels L, M, Q and
|
||||
// H (ISO/IEC 18004).
|
||||
var qrByteCapacity = [40][4]int{
|
||||
{17, 14, 11, 7}, {32, 26, 20, 14}, {53, 42, 32, 24}, {78, 62, 46, 34},
|
||||
{106, 84, 60, 44}, {134, 106, 74, 58}, {154, 122, 86, 64}, {192, 152, 108, 84},
|
||||
{230, 180, 130, 98}, {271, 213, 151, 119}, {321, 251, 177, 137}, {367, 287, 203, 155},
|
||||
{425, 331, 241, 177}, {458, 362, 258, 194}, {520, 412, 292, 220}, {586, 450, 322, 250},
|
||||
{644, 504, 364, 280}, {718, 560, 394, 310}, {792, 624, 442, 338}, {858, 666, 482, 382},
|
||||
{929, 711, 509, 403}, {1003, 779, 565, 439}, {1091, 857, 611, 461}, {1171, 911, 661, 511},
|
||||
{1273, 997, 715, 535}, {1367, 1059, 751, 593}, {1465, 1125, 805, 625}, {1528, 1190, 868, 658},
|
||||
{1628, 1264, 908, 698}, {1732, 1370, 982, 742}, {1840, 1452, 1030, 790}, {1952, 1538, 1112, 842},
|
||||
{2068, 1628, 1168, 898}, {2188, 1722, 1228, 958}, {2303, 1809, 1283, 983}, {2431, 1911, 1351, 1051},
|
||||
{2563, 1989, 1423, 1093}, {2699, 2099, 1499, 1139}, {2809, 2213, 1579, 1219}, {2953, 2331, 1663, 1273},
|
||||
}
|
||||
|
||||
// QRModules returns how many modules wide a QR code holding n bytes at
|
||||
// level is at most, or 0 for a level that is not one. It assumes byte mode,
|
||||
// the least dense, so the printer's own code is never wider; data that
|
||||
// only a denser mode holds, such as thousands of digits, counts as the
|
||||
// largest version, 177 modules.
|
||||
func QRModules(n int, level QRCorrection) int {
|
||||
if level < QRCorrectionL || level > QRCorrectionH {
|
||||
return 0
|
||||
}
|
||||
column := int(level - QRCorrectionL)
|
||||
for v, capacity := range qrByteCapacity {
|
||||
if n <= capacity[column] {
|
||||
return 17 + 4*(v+1)
|
||||
}
|
||||
}
|
||||
return 17 + 4*len(qrByteCapacity)
|
||||
}
|
||||
|
||||
// ParseQRCorrection returns the correction level named by name: L, M, Q or H.
|
||||
func ParseQRCorrection(name string) (QRCorrection, error) {
|
||||
switch strings.ToUpper(strings.TrimSpace(name)) {
|
||||
|
||||
Reference in new issue
Block a user