commit aa88925e6f072c0adbb032f15de5764eae3cc721
parent e3598faba0c86742d80ea8a7d3c2891692d37f81
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Thu, 17 Apr 2025 23:18:06 +0000
utils-nostr: edit classified events tags utils
Diffstat:
2 files changed, 38 insertions(+), 21 deletions(-)
diff --git a/utils-nostr/src/lib/tags.ts b/utils-nostr/src/lib/tags.ts
@@ -1,4 +1,4 @@
-import { INostrClassified, INostrJobRequest, NostrEventTagClient, NostrEventTagLocation, NostrEventTagMediaUpload, NostrEventTagPrice, NostrEventTagQuantity, type INostrFollow, type NostrEventTag, type NostrEventTags } from "$root";
+import { INostrClassified, INostrJobRequest, NostrEventTagClient, NostrEventTagLocation, NostrEventTagMediaUpload, NostrEventTagPrice, NostrEventTagPriceTier, NostrEventTagQuantity, type INostrFollow, type NostrEventTag, type NostrEventTags } from "$root";
import { ngeotags, type InputData as NostrGeotagsInputData } from "nostr-geotags";
export const tag_client = (opts: NostrEventTagClient, d_tag?: string): NostrEventTag => {
@@ -22,9 +22,13 @@ export const tag_classified_quantity = (opts: NostrEventTagQuantity): NostrEvent
if (opts.label) tag.push(opts.label);
return tag;
};
+export const tag_classified_price_tier = (tier: NostrEventTagPriceTier, price_key: string): NostrEventTag => {
+ const tag = [`price-tier`, tier.type, tier.value, price_key, tier.qty_min.toString()];
+ return tag;
+};
-export const tag_classified_price = (opts: NostrEventTagPrice): NostrEventTag => {
- const tag = [`price`, opts.amt, opts.currency, opts.qty_amt, opts.qty_unit];
+export const tag_classified_price = (tag_price: NostrEventTagPrice, tag_qty: NostrEventTagQuantity, quantity_key: string): NostrEventTag => {
+ const tag = [`price`, tag_price.amt, tag_price.currency, tag_qty.amt, tag_qty.unit, quantity_key];
return tag;
};
@@ -63,12 +67,21 @@ export const tags_classified_location_geotags = (opts: NostrEventTagLocation): N
export const tags_classified = (opts: INostrClassified): NostrEventTags => {
- const { d_tag, listing, quantity, price, location } = opts;
+ const { d_tag, listing, quantities, location } = opts;
const tags: NostrEventTags = [[`d`, d_tag]];
if (opts.client) tags.push(tag_client(opts.client, opts.d_tag));
for (const [k, v] of Object.entries(listing)) if (v) tags.push([k, v]);
- for (const quantity_tags of quantity) tags.push(tag_classified_quantity(quantity_tags));
- for (const price_tags of price) tags.push(tag_classified_price(price_tags));
+ for (const quantity of quantities) {
+ const quantity_key = `${quantity.amt}-${quantity.unit}${quantity.label ? `-${quantity.label}` : ``}`.toLowerCase();
+ tags.push(tag_classified_quantity(quantity));
+ for (const price of quantity.prices) {
+ const price_key = `${price.amt}-${price.currency}${quantity.amt}-${quantity.unit}`.toLowerCase();
+ tags.push(tag_classified_price(price, quantity, quantity_key));
+ for (const tier of price.tiers || []) {
+ tags.push(tag_classified_price_tier(tier, price_key));
+ }
+ }
+ }
tags.push(tag_classified_location(location));
if (opts.images) for (const image_tags of opts.images) tags.push(tag_classified_image(image_tags));
tags.push(...tags_classified_location_geotags(location));
diff --git a/utils-nostr/src/lib/types.ts b/utils-nostr/src/lib/types.ts
@@ -19,11 +19,28 @@ export type INostrFollow = {
contact_name?: string;
};
+export type NostrEventTagQuantity = {
+ amt: string;
+ unit: string;
+ label?: string;
+ prices: NostrEventTagPrice[];
+};
+
+export type NostrEventTagPriceTier = {
+ type: string;
+ value: string;
+ qty_min: number;
+}
+export type NostrEventTagPrice = {
+ amt: string;
+ currency: string;
+ tiers?: NostrEventTagPriceTier[];
+};
+
export type INostrClassified = {
d_tag: string;
listing: NostrEventTagListing;
- quantity: NostrEventTagQuantity[];
- price: NostrEventTagPrice[];
+ quantities: NostrEventTagQuantity[];
location: NostrEventTagLocation;
images?: NostrEventTagMediaUpload[];
client?: NostrEventTagClient;
@@ -85,19 +102,6 @@ export type NostrEventTagListing = {
year?: string;
};
-export type NostrEventTagPrice = {
- amt: string;
- currency: string;
- qty_amt: string;
- qty_unit: string;
-};
-
-export type NostrEventTagQuantity = {
- amt: string;
- unit: string;
- label?: string;
-};
-
export type NostrEventTagLocation = {
city?: string;
region?: string;