query.gno
4.33 Kb · 180 lines
1package board
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/nt/markdown/sanitize/v0"
9)
10
11const maxPage = 50
12
13// ListingJSON returns one listing, or null.
14func ListingJSON(id uint64) string {
15 v := listings.Get(listingKey(id))
16 if v == nil {
17 return "null"
18 }
19 return v.(*listing).json(time.Now().Unix())
20}
21
22// OpenJSON lists open listings, newest first. side filters by buy, sell or
23// swap; "" means all.
24func OpenJSON(side string, offset, limit int) string {
25 offset, limit = page(offset, limit)
26 now := time.Now().Unix()
27 items := []string{}
28 skipped := 0
29 more := false
30 oldest := now - MaxDays*day
31 listings.ReverseIterate("", "", func(_ string, v any) bool {
32 l := v.(*listing)
33 if l.createdAt < oldest {
34 return true // nothing older can still be open
35 }
36 if !l.open(now) || (side != "" && l.side != side) {
37 return false
38 }
39 if skipped < offset {
40 skipped++
41 return false
42 }
43 if len(items) == limit {
44 more = true
45 return true
46 }
47 items = append(items, l.json(now))
48 return false
49 })
50 return jsonObject(
51 "listings", jsonArray(items),
52 "more", jsonBool(more),
53 "total", jsonUint(lastID),
54 "now", jsonInt(now),
55 )
56}
57
58// ListingsOfJSON lists every listing of one owner, newest first.
59func ListingsOfJSON(owner string, offset, limit int) string {
60 offset, limit = page(offset, limit)
61 now := time.Now().Unix()
62 items := []string{}
63 var ids []uint64
64 if v := byOwner.Get(strings.TrimSpace(owner)); v != nil {
65 ids = v.([]uint64)
66 }
67 more := false
68 for i := len(ids) - 1 - offset; i >= 0; i-- {
69 if len(items) == limit {
70 more = true
71 break
72 }
73 items = append(items, mustListing(ids[i]).json(now))
74 }
75 return jsonObject(
76 "listings", jsonArray(items),
77 "more", jsonBool(more),
78 "now", jsonInt(now),
79 )
80}
81
82// json describes the listing at block time now (unix seconds).
83func (l *listing) json(now int64) string {
84 return jsonObject(
85 "id", jsonUint(l.id),
86 "owner", jsonString(l.owner.String()),
87 "side", jsonString(l.side),
88 "give", jsonString(l.give),
89 "want", jsonString(l.want),
90 "note", jsonString(l.note),
91 "open", jsonBool(l.open(now)),
92 "createdAt", jsonInt(l.createdAt),
93 "expiresAt", jsonInt(l.expiresAt),
94 "closedAt", jsonInt(l.closedAt),
95 )
96}
97
98func page(offset, limit int) (int, int) {
99 if offset < 0 {
100 offset = 0
101 }
102 if limit <= 0 || limit > maxPage {
103 limit = maxPage
104 }
105 return offset, limit
106}
107
108// Render shows open listings on gnoweb, and one listing at listing/<id>.
109func Render(path string) string {
110 now := time.Now().Unix()
111 path = strings.TrimSpace(path)
112 if rest, ok := strings.CutPrefix(path, "listing/"); ok {
113 if id, err := strconv.ParseUint(rest, 10, 64); err == nil {
114 if v := listings.Get(listingKey(id)); v != nil {
115 return renderListing(v.(*listing), now)
116 }
117 }
118 return "# Not found\n\nNo listing at this address.\n"
119 }
120 if path != "" {
121 return "# Not found\n\nNo listing at this address.\n"
122 }
123
124 var b strings.Builder
125 b.WriteString("# Trade Window board\n\n")
126 b.WriteString("Public listings: what someone gives and what they want. ")
127 b.WriteString("A listing moves nothing; deals settle in a trade room.\n\n")
128 n := 0
129 oldest := now - MaxDays*day
130 listings.ReverseIterate("", "", func(_ string, v any) bool {
131 l := v.(*listing)
132 if l.createdAt < oldest {
133 return true
134 }
135 if !l.open(now) {
136 return false
137 }
138 b.WriteString("- [" + sideLabel(l.side) + "](:listing/" + jsonUint(l.id) + "): " +
139 sanitize.InlineText(l.give) + " for " + sanitize.InlineText(l.want) + "\n")
140 n++
141 return n >= 30
142 })
143 if n == 0 {
144 b.WriteString("No open listings.\n")
145 }
146 return b.String()
147}
148
149func renderListing(l *listing, now int64) string {
150 var b strings.Builder
151 b.WriteString("# " + sideLabel(l.side) + ": " + sanitize.InlineText(l.give) + "\n\n")
152 b.WriteString("Wants: " + sanitize.InlineText(l.want) + "\n\n")
153 if l.note != "" {
154 b.WriteString(sanitize.InlineText(l.note) + "\n\n")
155 }
156 b.WriteString("Posted by " + sanitize.InlineCode(l.owner.String()) + " on " +
157 date(l.createdAt) + ". ")
158 if l.open(now) {
159 b.WriteString("Open until " + date(l.expiresAt) + ".\n")
160 } else {
161 b.WriteString("Closed.\n")
162 }
163 return b.String()
164}
165
166func sideLabel(side string) string {
167 switch side {
168 case SideBuy:
169 return "Buying"
170 case SideSell:
171 return "Selling"
172 default:
173 return "Swapping"
174 }
175}
176
177// date writes unix seconds as a UTC calendar date.
178func date(unix int64) string {
179 return time.Unix(unix, 0).UTC().Format("2006-01-02")
180}