Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

relrender.gno

2.91 Kb · 111 lines
  1package kourt
  2import (
  3	"strconv"
  4	"strings"
  5)
  6func writeRelations(b *strings.Builder, c *Court, claimID uint64) {
  7	base := "/r/g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6/kourt:" + c.id + "/"
  8	sup := writeSupersedes(b, c, claimID, base)
  9	arg := writeArguments(b, c, claimID, base)
 10	if sup || arg {
 11		b.WriteString("\n")
 12	}
 13}
 14func writeSupersedes(b *strings.Builder, c *Court, claimID uint64, base string) bool {
 15	wrote := false
 16	if c.supBy != nil {
 17		p := beClaimKey(claimID)
 18		n := 0
 19		c.supBy.Iterate(p, "", func(key string, v any) bool {
 20			if !strings.HasPrefix(key, p) {
 21				return true
 22			}
 23			from := claimIDFromKey(key[len(p):])
 24			if from == 0 || from > c.nextID || assocTextGone(c, from) {
 25				return false
 26			}
 27			if n == 0 {
 28				b.WriteString("> **This question was asked again.** It died here " +
 29					"without an answer and was re-filed as:\n")
 30			}
 31			n++
 32			cs := mustClaim(c, from)
 33			b.WriteString(">\n> - [" + claimTitleFor(c, cs) + "](" + base +
 34				strconv.FormatUint(from, 10) + ") — " + claimStatus(cs) + "\n")
 35			return n >= renderPageSize
 36		})
 37		if n > 0 {
 38			b.WriteString("\n")
 39			wrote = true
 40		}
 41	}
 42	if c.supOf != nil {
 43		if v := c.supOf.Get(beClaimKey(claimID)); v != nil {
 44			e := v.(*supEdge)
 45			if e.to != 0 && e.to <= c.nextID && !assocTextGone(c, e.to) {
 46				cs := mustClaim(c, e.to)
 47				b.WriteString("_Re-files [" + claimTitleFor(c, cs) + "](" + base +
 48					strconv.FormatUint(e.to, 10) + "), which died unanswered._\n\n")
 49				wrote = true
 50			}
 51		}
 52	}
 53	return wrote
 54}
 55func writeArguments(b *strings.Builder, c *Court, claimID uint64, base string) bool {
 56	var out, in strings.Builder
 57	nOut := writeEdgeSide(&out, c, claimID, base, true)
 58	nIn := writeEdgeSide(&in, c, claimID, base, false)
 59	if nOut+nIn == 0 {
 60		return false
 61	}
 62	b.WriteString("## Related claims\n\n")
 63	b.WriteString("_Anyone may draw these links, and the court does not rule on " +
 64		"them — they are what readers have asserted, not findings._\n\n")
 65	if nOut > 0 {
 66		b.WriteString("This claim says:\n\n")
 67		b.WriteString(out.String() + "\n")
 68	}
 69	if nIn > 0 {
 70		b.WriteString("Others say about this claim:\n\n")
 71		b.WriteString(in.String() + "\n")
 72	}
 73	return true
 74}
 75func writeEdgeSide(b *strings.Builder, c *Court, claimID uint64, base string, outgoing bool) int {
 76	t := c.assocIn
 77	if outgoing {
 78		t = c.assocOut
 79	}
 80	if t == nil {
 81		return 0
 82	}
 83	p := beClaimKey(claimID)
 84	n := 0
 85	t.Iterate(p, "", func(key string, v any) bool {
 86		if !strings.HasPrefix(key, p) {
 87			return true
 88		}
 89		e, ok := v.(*assocEdge)
 90		if !ok {
 91			return false
 92		}
 93		other := claimIDFromKey(key[len(p):])
 94		if other == 0 || other > c.nextID || assocTextGone(c, other) {
 95			return false
 96		}
 97		verb := "supports"
 98		if e.stance == assocContests {
 99			verb = "contests"
100		}
101		if !outgoing {
102			verb += " it"
103		}
104		n++
105		cs := mustClaim(c, other)
106		b.WriteString("- " + verb + " [" + claimTitleFor(c, cs) + "](" + base +
107			strconv.FormatUint(other, 10) + ")\n")
108		return n >= renderPageSize
109	})
110	return n
111}