read.ts (8552B)
1 import { markerPartition } from './partition.ts'; 2 import { 3 projectImages, 4 type InboundImage, 5 type ImageDiagnostic 6 } from './media.ts'; 7 import { 8 boundedUtf8, 9 foodContentPresent, 10 foodIdentifierValid, 11 foodTextValid, 12 incomingAmount 13 } from './text.ts'; 14 import { 15 foodCurrency, 16 foodUnit, 17 type FoodPrice, 18 type FoodQuantity 19 } from './values.ts'; 20 import { 21 boundedEnvelopeNumbers, 22 foodPublishedAt 23 } from '../../nostr/envelope-bounds.ts'; 24 import { 25 boundedEnvelopeTags, 26 verifyEnvelope, 27 verifiedEnvelopeSnapshot, 28 type VerifiedEnvelope 29 } from '../../nostr/verified-envelope.ts'; 30 export type FoodParts = Readonly<{ 31 kind: number; 32 created_at: number; 33 content: string; 34 tags: readonly (readonly string[])[]; 35 }>; 36 export type FoodProjection = Readonly<{ 37 content: string; 38 identifier: string; 39 title: string; 40 summary: string; 41 published_at: number; 42 location: string; 43 price: FoodPrice; 44 quantity: FoodQuantity | null; 45 status: 'active' | 'sold'; 46 images: readonly InboundImage[]; 47 diagnostics: readonly ImageDiagnostic[]; 48 }>; 49 export type FoodError = Readonly<{ code: string; message: string }>; 50 export type FoodProjectionOutcome = 51 | Readonly<{ outcome: 'focused'; projection: FoodProjection }> 52 | Readonly<{ 53 outcome: 'excluded'; 54 partition: 'operational_listing' | 'generic_nip99'; 55 }> 56 | Readonly<{ outcome: 'rejected'; error: FoodError }>; 57 export type FoodAdmission = 58 | Readonly<{ 59 outcome: 'admitted'; 60 event_id: string; 61 verified: VerifiedEnvelope; 62 projection: FoodProjection; 63 }> 64 | Readonly<{ 65 outcome: 'excluded'; 66 event_id: string; 67 verified: VerifiedEnvelope; 68 partition: 'operational_listing' | 'generic_nip99'; 69 }> 70 | Readonly<{ outcome: 'rejected'; error: FoodError }>; 71 const prohibited = [ 72 'buyer', 73 'checkout', 74 'delivery', 75 'exception', 76 'group', 77 'invite', 78 'order', 79 'payment', 80 'pickup', 81 'proof', 82 'provenance', 83 'receipt', 84 'route', 85 'route_stop', 86 'task' 87 ]; 88 function rejected(code: string, message: string): FoodProjectionOutcome { 89 return { outcome: 'rejected', error: { code, message } }; 90 } 91 // Pure untrusted-parts projection for contract conformance. Its result carries 92 // no event id or verified proof and cannot substitute for readFoodEvent admission. 93 export function projectFoodParts(event: FoodParts): FoodProjectionOutcome { 94 if (!boundedEnvelopeNumbers(event.kind, event.created_at)) 95 return rejected( 96 'envelope_invalid', 97 'invalid or unsupported bounded NIP-01 envelope' 98 ); 99 if (!boundedUtf8(event.content, 262144) || !boundedEnvelopeTags(event.tags)) 100 return rejected( 101 'envelope_invalid', 102 'invalid or unsupported bounded NIP-01 envelope' 103 ); 104 if (event.kind !== 30402) 105 return rejected( 106 'invalid_kind', 107 `FoodAvailability event kind must be 30402, got ${event.kind}` 108 ); 109 const partition = markerPartition(event.tags); 110 if (partition === 'ambiguous') 111 return rejected( 112 'food_profile_ambiguous', 113 'classified listing mixes focused and operational markers' 114 ); 115 if (partition !== 'focused_food_availability') 116 return { outcome: 'excluded', partition }; 117 for (const tag of event.tags) 118 if (prohibited.includes(tag[0])) 119 return rejected( 120 'prohibited_capability', 121 `FoodAvailability tag \`${tag[0]}\` is a prohibited capability` 122 ); 123 const matching = (name: string) => 124 event.tags.filter((tag) => tag[0] === name); 125 const names = ['d', 'title', 'summary', 'published_at', 'location', 'status']; 126 for (const name of names) { 127 const tags = matching(name); 128 if (tags.length !== 1 || tags[0].length !== 2) 129 return rejected( 130 'food_tag_invalid', 131 'FoodAvailability core tag shape is invalid' 132 ); 133 } 134 const [identifier, title, summary, publishedValue, location, status] = 135 names.map((name) => matching(name)[0][1]); 136 if (!foodContentPresent(event.content)) 137 return rejected( 138 'food_content_missing', 139 'FoodAvailability content must be non-whitespace' 140 ); 141 if (!boundedUtf8(event.content, 131072)) 142 return rejected( 143 'food_content_too_large', 144 'FoodAvailability content exceeds 131072 bytes' 145 ); 146 if (!foodIdentifierValid(identifier)) 147 return rejected( 148 'food_identifier_invalid', 149 'FoodAvailability identifier must be nonempty and contain no whitespace, control, or format characters' 150 ); 151 if (!foodTextValid(title) || !foodTextValid(summary)) 152 return rejected( 153 'food_text_invalid', 154 'FoodAvailability text must be trimmed, nonempty, and contain no control or format characters' 155 ); 156 const publishedAt = foodPublishedAt(publishedValue); 157 if (publishedAt === undefined) 158 return rejected( 159 'food_published_at_invalid', 160 'FoodAvailability published_at must be a canonical nonzero u64 timestamp' 161 ); 162 if (publishedAt > event.created_at) 163 return rejected( 164 'food_published_at_future', 165 `FoodAvailability published_at ${publishedAt} exceeds created_at ${event.created_at}` 166 ); 167 if (!foodTextValid(location)) 168 return rejected( 169 'food_text_invalid', 170 'FoodAvailability text must be trimmed, nonempty, and contain no control or format characters' 171 ); 172 const prices = matching('price'); 173 if (prices.length !== 1) 174 return rejected( 175 'price_invalid', 176 'FoodAvailability price must be a canonical unsigned decimal with at most 28 digits' 177 ); 178 if (prices[0].length > 3) 179 return rejected( 180 'price_frequency_forbidden', 181 'FoodAvailability price frequency is forbidden' 182 ); 183 const amount = 184 prices[0].length === 3 ? incomingAmount(prices[0][1]) : undefined; 185 if (amount === undefined) 186 return rejected( 187 'price_invalid', 188 'FoodAvailability price must be a canonical unsigned decimal with at most 28 digits' 189 ); 190 const currency = foodCurrency( 191 prices[0][2].length === 3 && /^[a-zA-Z]{3}$/u.test(prices[0][2]) 192 ? prices[0][2].toUpperCase() 193 : '' 194 ); 195 if (currency === undefined) 196 return rejected( 197 'price_currency_invalid', 198 'FoodAvailability price currency must be three uppercase ASCII letters' 199 ); 200 const units = matching('radroots:price_unit'); 201 if (units.length === 0) 202 return rejected( 203 'price_unit_missing', 204 'FoodAvailability price unit is missing' 205 ); 206 const unit = 207 units.length === 1 && units[0].length === 2 208 ? foodUnit(units[0][1]) 209 : undefined; 210 if (unit === undefined) 211 return rejected( 212 'price_unit_invalid', 213 'FoodAvailability price unit is not governed' 214 ); 215 const quantities = matching('radroots:quantity'); 216 let quantity: FoodQuantity | null = null; 217 if (quantities.length > 0) { 218 const tag = quantities[0]; 219 const value = 220 quantities.length === 1 && tag.length === 3 221 ? incomingAmount(tag[1]) 222 : undefined; 223 if (value === undefined || foodUnit(tag[2]) !== unit) 224 return rejected( 225 'quantity_invalid', 226 'FoodAvailability quantity must be canonical and use the price unit' 227 ); 228 if (value === '0') 229 return rejected( 230 'quantity_zero', 231 'FoodAvailability quantity must be positive' 232 ); 233 quantity = { amount: value, unit }; 234 } 235 if (status !== 'active' && status !== 'sold') 236 return rejected( 237 'food_status_invalid', 238 'FoodAvailability status must be active or sold' 239 ); 240 const media = projectImages(matching('image')); 241 return { 242 outcome: 'focused', 243 projection: { 244 content: event.content, 245 identifier, 246 title, 247 summary, 248 published_at: publishedAt, 249 location, 250 price: { amount, currency, unit }, 251 quantity, 252 status, 253 images: media.images, 254 diagnostics: media.diagnostics 255 } 256 }; 257 } 258 export function readFoodEvent(raw: unknown): FoodAdmission { 259 const result = verifyEnvelope(raw); 260 if (!result.ok) return { outcome: 'rejected', error: result.error }; 261 const event = verifiedEnvelopeSnapshot(result.value); 262 if (!event) 263 return { 264 outcome: 'rejected', 265 error: { 266 code: 'envelope_invalid', 267 message: 'invalid verified envelope proof' 268 } 269 }; 270 const projected = projectFoodParts(event); 271 if (projected.outcome === 'focused') 272 return { 273 outcome: 'admitted', 274 event_id: event.id, 275 verified: result.value, 276 projection: projected.projection 277 }; 278 if (projected.outcome === 'excluded') 279 return { ...projected, event_id: event.id, verified: result.value }; 280 return projected; 281 }