Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/linktree/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186407
  max_deposit = "5000000ugnot"

linktree.gno

package linktree

import (
	"strconv"
	"strings"

	"chain"
	"chain/runtime/unsafe"

	"gno.land/p/nt/avl/v0"
)

// link is a single labeled URL on a profile.
type link struct {
	Label string
	URL   string
}

// profile is one address's personal links page.
type profile struct {
	Bio   string
	Links []link
}

// profiles maps an address (string key) to its *profile.
// avl.Tree keeps iteration deterministic for Render.
var profiles avl.Tree

// SetBio sets (or replaces) the calling address's bio.
func SetBio(cur realm, bio string) {
	addr := unsafe.PreviousRealm().Address()
	p := getOrCreate(addr.String())
	p.Bio = bio
	chain.Emit("BioSet", "addr", addr.String())
}

// AddLink appends a labeled URL to the calling address's profile.
func AddLink(cur realm, label string, url string) {
	if label == "" {
		panic("label must not be empty")
	}
	if url == "" {
		panic("url must not be empty")
	}
	addr := unsafe.PreviousRealm().Address()
	p := getOrCreate(addr.String())
	p.Links = append(p.Links, link{Label: label, URL: url})
	chain.Emit("LinkAdded", "addr", addr.String(), "label", label)
}

// RemoveLink deletes the link at index from the calling address's profile.
func RemoveLink(cur realm, index int) {
	addr := unsafe.PreviousRealm().Address()
	key := addr.String()
	v := profiles.Get(key)
	if v == nil {
		panic("no profile for caller")
	}
	p := v.(*profile)
	if index < 0 || index >= len(p.Links) {
		panic("index out of range")
	}
	p.Links = append(p.Links[:index], p.Links[index+1:]...)
	chain.Emit("LinkRemoved", "addr", key, "index", strconv.Itoa(index))
}

// getOrCreate returns the profile for key, creating an empty one if absent.
func getOrCreate(key string) *profile {
	if v := profiles.Get(key); v != nil {
		return v.(*profile)
	}
	p := &profile{}
	profiles.Set(key, p)
	return p
}

// Render shows all profiles at root, or a single profile at "/<address>".
func Render(path string) string {
	addr := strings.Trim(path, "/")
	if addr == "" {
		return renderIndex()
	}
	return renderProfile(addr)
}

func renderIndex() string {
	var b strings.Builder
	b.WriteString("# Linktree\n\n")
	if profiles.Size() == 0 {
		b.WriteString("_No profiles yet. Call `SetBio` or `AddLink` to create one._\n")
		return b.String()
	}
	b.WriteString("Profiles:\n\n")
	profiles.Iterate("", "", func(key string, _ interface{}) bool {
		b.WriteString("- [")
		b.WriteString(key)
		b.WriteString("](/r/REPLACE_ADDR/linktree:")
		b.WriteString(key)
		b.WriteString(")\n")
		return false
	})
	return b.String()
}

func renderProfile(addr string) string {
	var b strings.Builder
	b.WriteString("# ")
	b.WriteString(addr)
	b.WriteString("\n\n")

	v := profiles.Get(addr)
	if v == nil {
		b.WriteString("_No profile for this address._\n")
		return b.String()
	}
	p := v.(*profile)

	if p.Bio != "" {
		b.WriteString(p.Bio)
		b.WriteString("\n\n")
	}

	if len(p.Links) == 0 {
		b.WriteString("_No links yet._\n")
		return b.String()
	}

	b.WriteString("## Links\n\n")
	for _, l := range p.Links {
		b.WriteString("- [")
		b.WriteString(l.Label)
		b.WriteString("](")
		b.WriteString(l.URL)
		b.WriteString(")\n")
	}
	return b.String()
}

linktree_test.gno

package linktree

import (
	"strings"
	"testing"

	"gno.land/p/nt/testutils/v0"
	"gno.land/p/nt/uassert/v0"
)

func TestProfileLifecycle(cur realm, t *testing.T) {
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	SetBio(cross(cur), "gnome builder")
	AddLink(cross(cur), "Home", "https://alice.example")
	AddLink(cross(cur), "GitHub", "https://github.com/alice")

	out := Render("/" + alice.String())
	uassert.True(t, strings.Contains(out, "gnome builder"), "bio present")
	uassert.True(t, strings.Contains(out, "[Home](https://alice.example)"), "first link present")
	uassert.True(t, strings.Contains(out, "[GitHub](https://github.com/alice)"), "second link present")

	// Remove the first link; second must survive.
	RemoveLink(cross(cur), 0)
	out = Render("/" + alice.String())
	uassert.False(t, strings.Contains(out, "[Home]"), "removed link gone")
	uassert.True(t, strings.Contains(out, "[GitHub](https://github.com/alice)"), "kept link present")
}

func TestIndexAndOwnership(cur realm, t *testing.T) {
	alice := testutils.TestAddress("alice2")
	bob := testutils.TestAddress("bob2")

	testing.SetRealm(testing.NewUserRealm(alice))
	AddLink(cross(cur), "A", "https://a.example")

	testing.SetRealm(testing.NewUserRealm(bob))
	AddLink(cross(cur), "B", "https://b.example")

	// Root index lists both addresses.
	idx := Render("")
	uassert.True(t, strings.Contains(idx, alice.String()), "alice listed")
	uassert.True(t, strings.Contains(idx, bob.String()), "bob listed")

	// bob is current caller; his profile has only his link, not alice's.
	bobOut := Render("/" + bob.String())
	uassert.True(t, strings.Contains(bobOut, "[B](https://b.example)"), "bob has own link")
	uassert.False(t, strings.Contains(bobOut, "[A]"), "bob does not have alice's link")

	// Out-of-range removal on bob aborts.
	uassert.AbortsWithMessage(t, cur, "index out of range", func() {
		RemoveLink(cross(cur), 99)
	})
}

func TestUnknownProfile(t *testing.T) {
	out := Render("/g1doesnotexist")
	uassert.True(t, strings.Contains(out, "No profile for this address"), "unknown profile message")
}