From 3356c7a31031a5fcfacc8cc3bd5c7d10108e2b34 Mon Sep 17 00:00:00 2001 From: Hariom Verma Date: Thu, 29 Feb 2024 22:43:19 +0530 Subject: [PATCH] refactor!: split `r/demo/users` (#1433) Splits `r/demo/users` into `p/demo/users` and `r/demo/users` Related #1393
Contributors' checklist... - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [x] Provided any useful hints for running manual tests - [x] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
--- .../grc/grc1155/basic_grc1155_token_test.gno | 32 +++--- examples/gno.land/p/demo/grc/grc1155/gno.mod | 2 +- .../p/demo/grc/grc721/basic_nft_test.gno | 104 +++++++++--------- examples/gno.land/p/demo/grc/grc721/gno.mod | 2 +- examples/gno.land/p/demo/microblog/gno.mod | 2 - .../gno.land/p/demo/microblog/microblog.gno | 58 ---------- examples/gno.land/p/demo/users/gno.mod | 1 + .../gno.land/{r => p}/demo/users/types.gno | 12 -- examples/gno.land/p/demo/users/users.gno | 31 ++++++ examples/gno.land/p/demo/users/users_test.gno | 1 + examples/gno.land/r/demo/boards/misc.gno | 5 +- .../gno.land/r/demo/boards/z_4_filetest.gno | 7 -- examples/gno.land/r/demo/foo1155/foo1155.gno | 44 ++++---- .../gno.land/r/demo/foo1155/foo1155_test.gno | 2 +- examples/gno.land/r/demo/foo1155/gno.mod | 1 + examples/gno.land/r/demo/foo20/foo20.gno | 34 +++--- examples/gno.land/r/demo/foo20/foo20_test.gno | 2 +- examples/gno.land/r/demo/foo20/gno.mod | 1 + examples/gno.land/r/demo/foo721/foo721.gno | 26 +++-- .../gno.land/r/demo/foo721/foo721_test.gno | 8 +- examples/gno.land/r/demo/foo721/gno.mod | 1 + examples/gno.land/r/demo/groups/misc.gno | 4 +- examples/gno.land/r/demo/microblog/gno.mod | 3 + .../gno.land/r/demo/microblog/microblog.gno | 58 +++++++++- .../demo/microblog/microblog_test.gno | 30 +++-- examples/gno.land/r/demo/users/gno.mod | 5 +- examples/gno.land/r/demo/users/users.gno | 83 ++++++-------- gno.land/pkg/gnoweb/gnoweb_test.go | 2 +- 28 files changed, 282 insertions(+), 279 deletions(-) create mode 100644 examples/gno.land/p/demo/users/gno.mod rename examples/gno.land/{r => p}/demo/users/types.gno (54%) create mode 100644 examples/gno.land/p/demo/users/users.gno create mode 100644 examples/gno.land/p/demo/users/users_test.gno rename examples/gno.land/{p => r}/demo/microblog/microblog_test.gno (52%) diff --git a/examples/gno.land/p/demo/grc/grc1155/basic_grc1155_token_test.gno b/examples/gno.land/p/demo/grc/grc1155/basic_grc1155_token_test.gno index 43298cb1136..d7abf8e1153 100644 --- a/examples/gno.land/p/demo/grc/grc1155/basic_grc1155_token_test.gno +++ b/examples/gno.land/p/demo/grc/grc1155/basic_grc1155_token_test.gno @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/users" + "gno.land/p/demo/users" ) const dummyURI = "ipfs://xyz" @@ -35,8 +35,8 @@ func TestBalanceOf(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj").Resolve() + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") tid1 := TokenID("1") tid2 := TokenID("2") @@ -86,8 +86,8 @@ func TestBalanceOfBatch(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj").Resolve() + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") tid1 := TokenID("1") tid2 := TokenID("2") @@ -126,8 +126,8 @@ func TestIsApprovedForAll(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj").Resolve() + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") isApprovedForAll := dummy.IsApprovedForAll(addr1, addr2) if isApprovedForAll != false { @@ -142,7 +142,7 @@ func TestSetApprovalForAll(t *testing.T) { } caller := std.GetOrigCaller() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") isApprovedForAll := dummy.IsApprovedForAll(caller, addr) if isApprovedForAll != false { @@ -177,7 +177,7 @@ func TestSafeTransferFrom(t *testing.T) { } caller := std.GetOrigCaller() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") tid := TokenID("1") @@ -224,7 +224,7 @@ func TestSafeBatchTransferFrom(t *testing.T) { } caller := std.GetOrigCaller() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") tid1 := TokenID("1") tid2 := TokenID("2") @@ -280,8 +280,8 @@ func TestSafeMint(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj").Resolve() + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") tid1 := TokenID("1") tid2 := TokenID("2") @@ -327,8 +327,8 @@ func TestSafeBatchMint(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj").Resolve() + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") tid1 := TokenID("1") tid2 := TokenID("2") @@ -374,7 +374,7 @@ func TestBurn(t *testing.T) { t.Errorf("should not be nil") } - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") tid1 := TokenID("1") tid2 := TokenID("2") @@ -417,7 +417,7 @@ func TestBatchBurn(t *testing.T) { t.Errorf("should not be nil") } - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm").Resolve() + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") tid1 := TokenID("1") tid2 := TokenID("2") diff --git a/examples/gno.land/p/demo/grc/grc1155/gno.mod b/examples/gno.land/p/demo/grc/grc1155/gno.mod index 0b2b85d8e86..b8db3675cf0 100644 --- a/examples/gno.land/p/demo/grc/grc1155/gno.mod +++ b/examples/gno.land/p/demo/grc/grc1155/gno.mod @@ -3,5 +3,5 @@ module gno.land/p/demo/grc/grc1155 require ( gno.land/p/demo/avl v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest - gno.land/r/demo/users v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno b/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno index 925f1fca44b..70255c5e9d1 100644 --- a/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno +++ b/examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno @@ -5,7 +5,7 @@ import ( "testing" "gno.land/p/demo/testutils" - "gno.land/r/demo/users" + "gno.land/p/demo/users" ) var ( @@ -68,10 +68,10 @@ func TestBalanceOf(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") - balanceAddr1, err := dummy.BalanceOf(addr1.Resolve()) + balanceAddr1, err := dummy.BalanceOf(addr1) if err != nil { t.Errorf("should not result in error") } @@ -79,15 +79,15 @@ func TestBalanceOf(t *testing.T) { t.Errorf("expected: (%d), got: (%d)", 0, balanceAddr1) } - dummy.mint(addr1.Resolve(), TokenID("1")) - dummy.mint(addr1.Resolve(), TokenID("2")) - dummy.mint(addr2.Resolve(), TokenID("3")) + dummy.mint(addr1, TokenID("1")) + dummy.mint(addr1, TokenID("2")) + dummy.mint(addr2, TokenID("3")) - balanceAddr1, err = dummy.BalanceOf(addr1.Resolve()) + balanceAddr1, err = dummy.BalanceOf(addr1) if err != nil { t.Errorf("should not result in error") } - balanceAddr2, err := dummy.BalanceOf(addr2.Resolve()) + balanceAddr2, err := dummy.BalanceOf(addr2) if err != nil { t.Errorf("should not result in error") } @@ -106,24 +106,24 @@ func TestOwnerOf(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") owner, err := dummy.OwnerOf(TokenID("invalid")) if err == nil { t.Errorf("should result in error") } - dummy.mint(addr1.Resolve(), TokenID("1")) - dummy.mint(addr2.Resolve(), TokenID("2")) + dummy.mint(addr1, TokenID("1")) + dummy.mint(addr2, TokenID("2")) // Checking for token id "1" owner, err = dummy.OwnerOf(TokenID("1")) if err != nil { t.Errorf("should not result in error") } - if owner != addr1.Resolve() { - t.Errorf("expected: (%s), got: (%s)", addr1.Resolve().String(), owner.String()) + if owner != addr1 { + t.Errorf("expected: (%s), got: (%s)", addr1.String(), owner.String()) } // Checking for token id "2" @@ -131,8 +131,8 @@ func TestOwnerOf(t *testing.T) { if err != nil { t.Errorf("should not result in error") } - if owner != addr2.Resolve() { - t.Errorf("expected: (%s), got: (%s)", addr2.Resolve().String(), owner.String()) + if owner != addr2 { + t.Errorf("expected: (%s), got: (%s)", addr2.String(), owner.String()) } } @@ -142,10 +142,10 @@ func TestIsApprovedForAll(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") - isApprovedForAll := dummy.IsApprovedForAll(addr1.Resolve(), addr2.Resolve()) + isApprovedForAll := dummy.IsApprovedForAll(addr1, addr2) if isApprovedForAll != false { t.Errorf("expected: (%v), got: (%v)", false, isApprovedForAll) } @@ -158,19 +158,19 @@ func TestSetApprovalForAll(t *testing.T) { } caller := std.PrevRealm().Addr() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - isApprovedForAll := dummy.IsApprovedForAll(caller, addr.Resolve()) + isApprovedForAll := dummy.IsApprovedForAll(caller, addr) if isApprovedForAll != false { t.Errorf("expected: (%v), got: (%v)", false, isApprovedForAll) } - err := dummy.SetApprovalForAll(addr.Resolve(), true) + err := dummy.SetApprovalForAll(addr, true) if err != nil { t.Errorf("should not result in error") } - isApprovedForAll = dummy.IsApprovedForAll(caller, addr.Resolve()) + isApprovedForAll = dummy.IsApprovedForAll(caller, addr) if isApprovedForAll != true { t.Errorf("expected: (%v), got: (%v)", false, isApprovedForAll) } @@ -195,7 +195,7 @@ func TestApprove(t *testing.T) { } caller := std.PrevRealm().Addr() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") dummy.mint(caller, TokenID("1")) @@ -204,7 +204,7 @@ func TestApprove(t *testing.T) { t.Errorf("should result in error") } - err = dummy.Approve(addr.Resolve(), TokenID("1")) + err = dummy.Approve(addr, TokenID("1")) if err != nil { t.Errorf("should not result in error") } @@ -213,8 +213,8 @@ func TestApprove(t *testing.T) { if err != nil { t.Errorf("should not result in error") } - if approvedAddr != addr.Resolve() { - t.Errorf("expected: (%s), got: (%s)", addr.Resolve().String(), approvedAddr.String()) + if approvedAddr != addr { + t.Errorf("expected: (%s), got: (%s)", addr.String(), approvedAddr.String()) } } @@ -225,12 +225,12 @@ func TestTransferFrom(t *testing.T) { } caller := std.PrevRealm().Addr() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") dummy.mint(caller, TokenID("1")) dummy.mint(caller, TokenID("2")) - err := dummy.TransferFrom(caller, addr.Resolve(), TokenID("1")) + err := dummy.TransferFrom(caller, addr, TokenID("1")) if err != nil { t.Errorf("should not result in error") } @@ -245,7 +245,7 @@ func TestTransferFrom(t *testing.T) { } // Check balance of addr after transfer - balanceOfAddr, err := dummy.BalanceOf(addr.Resolve()) + balanceOfAddr, err := dummy.BalanceOf(addr) if err != nil { t.Errorf("should not result in error") } @@ -258,8 +258,8 @@ func TestTransferFrom(t *testing.T) { if err != nil { t.Errorf("should not result in error") } - if owner != addr.Resolve() { - t.Errorf("expected: (%s), got: (%s)", addr.Resolve().String(), owner.String()) + if owner != addr { + t.Errorf("expected: (%s), got: (%s)", addr.String(), owner.String()) } } @@ -270,12 +270,12 @@ func TestSafeTransferFrom(t *testing.T) { } caller := std.PrevRealm().Addr() - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") dummy.mint(caller, TokenID("1")) dummy.mint(caller, TokenID("2")) - err := dummy.SafeTransferFrom(caller, addr.Resolve(), TokenID("1")) + err := dummy.SafeTransferFrom(caller, addr, TokenID("1")) if err != nil { t.Errorf("should not result in error") } @@ -290,7 +290,7 @@ func TestSafeTransferFrom(t *testing.T) { } // Check balance of addr after transfer - balanceOfAddr, err := dummy.BalanceOf(addr.Resolve()) + balanceOfAddr, err := dummy.BalanceOf(addr) if err != nil { t.Errorf("should not result in error") } @@ -303,8 +303,8 @@ func TestSafeTransferFrom(t *testing.T) { if err != nil { t.Errorf("should not result in error") } - if owner != addr.Resolve() { - t.Errorf("expected: (%s), got: (%s)", addr.Resolve().String(), owner.String()) + if owner != addr { + t.Errorf("expected: (%s), got: (%s)", addr.String(), owner.String()) } } @@ -314,24 +314,24 @@ func TestMint(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") - err := dummy.Mint(addr1.Resolve(), TokenID("1")) + err := dummy.Mint(addr1, TokenID("1")) if err != nil { t.Errorf("should not result in error") } - err = dummy.Mint(addr1.Resolve(), TokenID("2")) + err = dummy.Mint(addr1, TokenID("2")) if err != nil { t.Errorf("should not result in error") } - err = dummy.Mint(addr2.Resolve(), TokenID("3")) + err = dummy.Mint(addr2, TokenID("3")) if err != nil { t.Errorf("should not result in error") } // Try minting duplicate token id - err = dummy.Mint(addr2.Resolve(), TokenID("1")) + err = dummy.Mint(addr2, TokenID("1")) if err == nil { t.Errorf("should result in error") } @@ -341,8 +341,8 @@ func TestMint(t *testing.T) { if err != nil { t.Errorf("should not result in error") } - if owner != addr1.Resolve() { - t.Errorf("expected: (%s), got: (%s)", addr1.Resolve().String(), owner.String()) + if owner != addr1 { + t.Errorf("expected: (%s), got: (%s)", addr1.String(), owner.String()) } } @@ -352,10 +352,10 @@ func TestBurn(t *testing.T) { t.Errorf("should not be nil") } - addr := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - dummy.mint(addr.Resolve(), TokenID("1")) - dummy.mint(addr.Resolve(), TokenID("2")) + dummy.mint(addr, TokenID("1")) + dummy.mint(addr, TokenID("2")) err := dummy.Burn(TokenID("1")) if err != nil { @@ -375,13 +375,13 @@ func TestSetTokenURI(t *testing.T) { t.Errorf("should not be nil") } - addr1 := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") - addr2 := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") + addr1 := std.Address("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") tokenURI := "http://example.com/token" std.TestSetOrigCaller(std.Address(addr1)) // addr1 - dummy.mint(addr1.Resolve(), TokenID("1")) + dummy.mint(addr1, TokenID("1")) _, derr := dummy.SetTokenURI(TokenID("1"), TokenURI(tokenURI)) if derr != nil { diff --git a/examples/gno.land/p/demo/grc/grc721/gno.mod b/examples/gno.land/p/demo/grc/grc721/gno.mod index 952e0cb8ee4..1eada28e4ee 100644 --- a/examples/gno.land/p/demo/grc/grc721/gno.mod +++ b/examples/gno.land/p/demo/grc/grc721/gno.mod @@ -4,5 +4,5 @@ require ( gno.land/p/demo/avl v0.0.0-latest gno.land/p/demo/testutils v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest - gno.land/r/demo/users v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/p/demo/microblog/gno.mod b/examples/gno.land/p/demo/microblog/gno.mod index 5964679efa6..9bbcfa19e31 100644 --- a/examples/gno.land/p/demo/microblog/gno.mod +++ b/examples/gno.land/p/demo/microblog/gno.mod @@ -2,7 +2,5 @@ module gno.land/p/demo/microblog require ( gno.land/p/demo/avl v0.0.0-latest - gno.land/p/demo/testutils v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest - gno.land/r/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/p/demo/microblog/microblog.gno b/examples/gno.land/p/demo/microblog/microblog.gno index f12fc6b5ece..f6d6709f20b 100644 --- a/examples/gno.land/p/demo/microblog/microblog.gno +++ b/examples/gno.land/p/demo/microblog/microblog.gno @@ -9,7 +9,6 @@ import ( "gno.land/p/demo/avl" "gno.land/p/demo/ufmt" - "gno.land/r/demo/users" ) var ( @@ -48,47 +47,6 @@ func (m *Microblog) GetPages() []*Page { return pages } -func (m *Microblog) RenderHome() string { - output := ufmt.Sprintf("# %s\n\n", m.Title) - output += "# pages\n\n" - - for _, page := range m.GetPages() { - if u := users.GetUserByAddress(page.Author); u != nil { - output += ufmt.Sprintf("- [%s (%s)](%s%s)\n", u.Name(), page.Author.String(), m.Prefix, page.Author.String()) - } else { - output += ufmt.Sprintf("- [%s](%s%s)\n", page.Author.String(), m.Prefix, page.Author.String()) - } - } - - return output -} - -func (m *Microblog) RenderUser(user string) string { - silo, found := m.Pages.Get(user) - if !found { - return StatusNotFound - } - - return (silo.(*Page)).String() -} - -func (m *Microblog) Render(path string) string { - parts := strings.Split(path, "/") - - isHome := path == "" - isUser := len(parts) == 1 - - switch { - case isHome: - return m.RenderHome() - - case isUser: - return m.RenderUser(parts[0]) - } - - return StatusNotFound -} - func (m *Microblog) NewPost(text string) error { author := std.GetOrigCaller() _, found := m.Pages.Get(author.String()) @@ -131,22 +89,6 @@ func (a byLastPosted) Len() int { return len(a) } func (a byLastPosted) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byLastPosted) Less(i, j int) bool { return a[i].LastPosted.After(a[j].LastPosted) } -func (p *Page) String() string { - o := "" - if u := users.GetUserByAddress(p.Author); u != nil { - o += ufmt.Sprintf("# [%s](/r/demo/users:%s)\n\n", u.Name(), u.Name()) - o += ufmt.Sprintf("%s\n\n", u.Profile()) - } - o += ufmt.Sprintf("## [%s](/r/demo/microblog:%s)\n\n", p.Author, p.Author) - - o += ufmt.Sprintf("joined %s, last updated %s\n\n", p.CreatedAt.Format("2006-02-01"), p.LastPosted.Format("2006-02-01")) - o += "## feed\n\n" - for _, u := range p.GetPosts() { - o += u.String() + "\n\n" - } - return o -} - func (p *Page) NewPost(text string) error { now := time.Now() p.LastPosted = now diff --git a/examples/gno.land/p/demo/users/gno.mod b/examples/gno.land/p/demo/users/gno.mod new file mode 100644 index 00000000000..ad652803fb8 --- /dev/null +++ b/examples/gno.land/p/demo/users/gno.mod @@ -0,0 +1 @@ +module gno.land/p/demo/users diff --git a/examples/gno.land/r/demo/users/types.gno b/examples/gno.land/p/demo/users/types.gno similarity index 54% rename from examples/gno.land/r/demo/users/types.gno rename to examples/gno.land/p/demo/users/types.gno index b5fd6a8f6bf..d28b6a8ee42 100644 --- a/examples/gno.land/r/demo/users/types.gno +++ b/examples/gno.land/p/demo/users/types.gno @@ -1,7 +1,5 @@ package users -import "std" - type AddressOrName string func (aon AddressOrName) IsName() bool { @@ -14,13 +12,3 @@ func (aon AddressOrName) GetName() (string, bool) { } return "", false } - -func (aon AddressOrName) Resolve() std.Address { - name, isName := aon.GetName() - if isName { - user := GetUserByName(name) - return user.address - } else { - return std.Address(aon) // TODO check validity - } -} diff --git a/examples/gno.land/p/demo/users/users.gno b/examples/gno.land/p/demo/users/users.gno new file mode 100644 index 00000000000..204eaf19918 --- /dev/null +++ b/examples/gno.land/p/demo/users/users.gno @@ -0,0 +1,31 @@ +package users + +import ( + "std" + "strconv" +) + +//---------------------------------------- +// Types + +type User struct { + Address std.Address + Name string + Profile string + Number int + Invites int + Inviter std.Address +} + +func (u *User) Render() string { + str := "## user " + u.Name + "\n" + + "\n" + + " * address = " + string(u.Address) + "\n" + + " * " + strconv.Itoa(u.Invites) + " invites\n" + if u.Inviter != "" { + str = str + " * invited by " + string(u.Inviter) + "\n" + } + str = str + "\n" + + u.Profile + "\n" + return str +} diff --git a/examples/gno.land/p/demo/users/users_test.gno b/examples/gno.land/p/demo/users/users_test.gno new file mode 100644 index 00000000000..82abcb9fccb --- /dev/null +++ b/examples/gno.land/p/demo/users/users_test.gno @@ -0,0 +1 @@ +package users diff --git a/examples/gno.land/r/demo/boards/misc.gno b/examples/gno.land/r/demo/boards/misc.gno index ccb6af0b6a9..bc561ca7d22 100644 --- a/examples/gno.land/r/demo/boards/misc.gno +++ b/examples/gno.land/r/demo/boards/misc.gno @@ -82,7 +82,7 @@ func displayAddressMD(addr std.Address) string { if user == nil { return "[" + addr.String() + "](/r/demo/users:" + addr.String() + ")" } else { - return "[@" + user.Name() + "](/r/demo/users:" + user.Name() + ")" + return "[@" + user.Name + "](/r/demo/users:" + user.Name + ")" } } @@ -90,7 +90,6 @@ func usernameOf(addr std.Address) string { user := users.GetUserByAddress(addr) if user == nil { return "" - } else { - return user.Name() } + return user.Name } diff --git a/examples/gno.land/r/demo/boards/z_4_filetest.gno b/examples/gno.land/r/demo/boards/z_4_filetest.gno index 3b168ed5c58..2acafe05470 100644 --- a/examples/gno.land/r/demo/boards/z_4_filetest.gno +++ b/examples/gno.land/r/demo/boards/z_4_filetest.gno @@ -47,7 +47,6 @@ func main() { // Realm: // switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] // switchrealm["gno.land/r/demo/boards"] // u[f6dbf411da22e67d74cd7ddba6a76cd7e14a4822:101]={ // "Fields": [ @@ -962,11 +961,5 @@ func main() { // switchrealm["gno.land/r/demo/users"] // switchrealm["gno.land/r/demo/users"] // switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] -// switchrealm["gno.land/r/demo/users"] // switchrealm["gno.land/r/demo/boards"] // switchrealm["gno.land/r/boards_test"] diff --git a/examples/gno.land/r/demo/foo1155/foo1155.gno b/examples/gno.land/r/demo/foo1155/foo1155.gno index c3fbf389434..2bd3b7a84c0 100644 --- a/examples/gno.land/r/demo/foo1155/foo1155.gno +++ b/examples/gno.land/r/demo/foo1155/foo1155.gno @@ -6,6 +6,8 @@ import ( "gno.land/p/demo/grc/grc1155" "gno.land/p/demo/ufmt" "gno.land/r/demo/users" + + pusers "gno.land/p/demo/users" ) var ( @@ -27,8 +29,8 @@ func mintGRC1155Token(owner std.Address) { // Getters -func BalanceOf(user users.AddressOrName, tid grc1155.TokenID) uint64 { - balance, err := foo.BalanceOf(user.Resolve(), tid) +func BalanceOf(user pusers.AddressOrName, tid grc1155.TokenID) uint64 { + balance, err := foo.BalanceOf(users.Resolve(user), tid) if err != nil { panic(err) } @@ -36,11 +38,11 @@ func BalanceOf(user users.AddressOrName, tid grc1155.TokenID) uint64 { return balance } -func BalanceOfBatch(users []users.AddressOrName, batch []grc1155.TokenID) []uint64 { +func BalanceOfBatch(ul []pusers.AddressOrName, batch []grc1155.TokenID) []uint64 { var usersResolved []std.Address - for i := 0; i < len(users); i++ { - usersResolved[i] = users[i].Resolve() + for i := 0; i < len(ul); i++ { + usersResolved[i] = users.Resolve(ul[i]) } balanceBatch, err := foo.BalanceOfBatch(usersResolved, batch) if err != nil { @@ -50,28 +52,28 @@ func BalanceOfBatch(users []users.AddressOrName, batch []grc1155.TokenID) []uint return balanceBatch } -func IsApprovedForAll(owner, user users.AddressOrName) bool { - return foo.IsApprovedForAll(owner.Resolve(), user.Resolve()) +func IsApprovedForAll(owner, user pusers.AddressOrName) bool { + return foo.IsApprovedForAll(users.Resolve(owner), users.Resolve(user)) } // Setters -func SetApprovalForAll(user users.AddressOrName, approved bool) { - err := foo.SetApprovalForAll(user.Resolve(), approved) +func SetApprovalForAll(user pusers.AddressOrName, approved bool) { + err := foo.SetApprovalForAll(users.Resolve(user), approved) if err != nil { panic(err) } } -func TransferFrom(from, to users.AddressOrName, tid grc1155.TokenID, amount uint64) { - err := foo.SafeTransferFrom(from.Resolve(), to.Resolve(), tid, amount) +func TransferFrom(from, to pusers.AddressOrName, tid grc1155.TokenID, amount uint64) { + err := foo.SafeTransferFrom(users.Resolve(from), users.Resolve(to), tid, amount) if err != nil { panic(err) } } -func BatchTransferFrom(from, to users.AddressOrName, batch []grc1155.TokenID, amounts []uint64) { - err := foo.SafeBatchTransferFrom(from.Resolve(), to.Resolve(), batch, amounts) +func BatchTransferFrom(from, to pusers.AddressOrName, batch []grc1155.TokenID, amounts []uint64) { + err := foo.SafeBatchTransferFrom(users.Resolve(from), users.Resolve(to), batch, amounts) if err != nil { panic(err) } @@ -79,37 +81,37 @@ func BatchTransferFrom(from, to users.AddressOrName, batch []grc1155.TokenID, am // Admin -func Mint(to users.AddressOrName, tid grc1155.TokenID, amount uint64) { +func Mint(to pusers.AddressOrName, tid grc1155.TokenID, amount uint64) { caller := std.GetOrigCaller() assertIsAdmin(caller) - err := foo.SafeMint(to.Resolve(), tid, amount) + err := foo.SafeMint(users.Resolve(to), tid, amount) if err != nil { panic(err) } } -func MintBatch(to users.AddressOrName, batch []grc1155.TokenID, amounts []uint64) { +func MintBatch(to pusers.AddressOrName, batch []grc1155.TokenID, amounts []uint64) { caller := std.GetOrigCaller() assertIsAdmin(caller) - err := foo.SafeBatchMint(to.Resolve(), batch, amounts) + err := foo.SafeBatchMint(users.Resolve(to), batch, amounts) if err != nil { panic(err) } } -func Burn(from users.AddressOrName, tid grc1155.TokenID, amount uint64) { +func Burn(from pusers.AddressOrName, tid grc1155.TokenID, amount uint64) { caller := std.GetOrigCaller() assertIsAdmin(caller) - err := foo.Burn(from.Resolve(), tid, amount) + err := foo.Burn(users.Resolve(from), tid, amount) if err != nil { panic(err) } } -func BurnBatch(from users.AddressOrName, batch []grc1155.TokenID, amounts []uint64) { +func BurnBatch(from pusers.AddressOrName, batch []grc1155.TokenID, amounts []uint64) { caller := std.GetOrigCaller() assertIsAdmin(caller) - err := foo.BatchBurn(from.Resolve(), batch, amounts) + err := foo.BatchBurn(users.Resolve(from), batch, amounts) if err != nil { panic(err) } diff --git a/examples/gno.land/r/demo/foo1155/foo1155_test.gno b/examples/gno.land/r/demo/foo1155/foo1155_test.gno index 30663ab2c95..edd17d708fb 100644 --- a/examples/gno.land/r/demo/foo1155/foo1155_test.gno +++ b/examples/gno.land/r/demo/foo1155/foo1155_test.gno @@ -4,7 +4,7 @@ import ( "testing" "gno.land/p/demo/grc/grc1155" - "gno.land/r/demo/users" + "gno.land/p/demo/users" ) func TestFoo721(t *testing.T) { diff --git a/examples/gno.land/r/demo/foo1155/gno.mod b/examples/gno.land/r/demo/foo1155/gno.mod index 6fdf18a1658..0a405c5b4a2 100644 --- a/examples/gno.land/r/demo/foo1155/gno.mod +++ b/examples/gno.land/r/demo/foo1155/gno.mod @@ -3,5 +3,6 @@ module gno.land/r/demo/foo1155 require ( gno.land/p/demo/grc/grc1155 v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest gno.land/r/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/r/demo/foo20/foo20.gno b/examples/gno.land/r/demo/foo20/foo20.gno index 39ab9260d3a..162454800e8 100644 --- a/examples/gno.land/r/demo/foo20/foo20.gno +++ b/examples/gno.land/r/demo/foo20/foo20.gno @@ -7,6 +7,8 @@ import ( "gno.land/p/demo/grc/grc20" "gno.land/p/demo/ufmt" "gno.land/r/demo/users" + + pusers "gno.land/p/demo/users" ) var ( @@ -29,16 +31,16 @@ func TotalSupply() uint64 { return foo.TotalSupply() } -func BalanceOf(owner users.AddressOrName) uint64 { - balance, err := foo.BalanceOf(owner.Resolve()) +func BalanceOf(owner pusers.AddressOrName) uint64 { + balance, err := foo.BalanceOf(users.Resolve(owner)) if err != nil { panic(err) } return balance } -func Allowance(owner, spender users.AddressOrName) uint64 { - allowance, err := foo.Allowance(owner.Resolve(), spender.Resolve()) +func Allowance(owner, spender pusers.AddressOrName) uint64 { + allowance, err := foo.Allowance(users.Resolve(owner), users.Resolve(spender)) if err != nil { panic(err) } @@ -47,25 +49,25 @@ func Allowance(owner, spender users.AddressOrName) uint64 { // setters. -func Transfer(to users.AddressOrName, amount uint64) { +func Transfer(to pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() - err := foo.Transfer(caller, to.Resolve(), amount) + err := foo.Transfer(caller, users.Resolve(to), amount) if err != nil { panic(err) } } -func Approve(spender users.AddressOrName, amount uint64) { +func Approve(spender pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() - err := foo.Approve(caller, spender.Resolve(), amount) + err := foo.Approve(caller, users.Resolve(spender), amount) if err != nil { panic(err) } } -func TransferFrom(from, to users.AddressOrName, amount uint64) { +func TransferFrom(from, to pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() - err := foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount) + err := foo.TransferFrom(caller, users.Resolve(from), users.Resolve(to), amount) if err != nil { panic(err) } @@ -85,19 +87,19 @@ func Faucet() { // administration. -func Mint(address users.AddressOrName, amount uint64) { +func Mint(address pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) - err := foo.Mint(address.Resolve(), amount) + err := foo.Mint(users.Resolve(address), amount) if err != nil { panic(err) } } -func Burn(address users.AddressOrName, amount uint64) { +func Burn(address pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) - err := foo.Burn(address.Resolve(), amount) + err := foo.Burn(users.Resolve(address), amount) if err != nil { panic(err) } @@ -114,8 +116,8 @@ func Render(path string) string { case path == "": return foo.RenderHome() case c == 2 && parts[0] == "balance": - owner := users.AddressOrName(parts[1]) - balance, _ := foo.BalanceOf(owner.Resolve()) + owner := pusers.AddressOrName(parts[1]) + balance, _ := foo.BalanceOf(users.Resolve(owner)) return ufmt.Sprintf("%d\n", balance) default: return "404\n" diff --git a/examples/gno.land/r/demo/foo20/foo20_test.gno b/examples/gno.land/r/demo/foo20/foo20_test.gno index 74c7648c2e5..9db0d5f7a6e 100644 --- a/examples/gno.land/r/demo/foo20/foo20_test.gno +++ b/examples/gno.land/r/demo/foo20/foo20_test.gno @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/users" + "gno.land/p/demo/users" ) func TestReadOnlyPublicMethods(t *testing.T) { diff --git a/examples/gno.land/r/demo/foo20/gno.mod b/examples/gno.land/r/demo/foo20/gno.mod index 516690ee66c..96fefcf6163 100644 --- a/examples/gno.land/r/demo/foo20/gno.mod +++ b/examples/gno.land/r/demo/foo20/gno.mod @@ -3,5 +3,6 @@ module gno.land/r/demo/foo20 require ( gno.land/p/demo/grc/grc20 v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest gno.land/r/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/r/demo/foo721/foo721.gno b/examples/gno.land/r/demo/foo721/foo721.gno index 45dcd5b4ef3..f7364d4185f 100644 --- a/examples/gno.land/r/demo/foo721/foo721.gno +++ b/examples/gno.land/r/demo/foo721/foo721.gno @@ -6,6 +6,8 @@ import ( "gno.land/p/demo/grc/grc721" "gno.land/p/demo/ufmt" "gno.land/r/demo/users" + + pusers "gno.land/p/demo/users" ) var ( @@ -28,8 +30,8 @@ func mintNNFT(owner std.Address, n uint64) { // Getters -func BalanceOf(user users.AddressOrName) uint64 { - balance, err := foo.BalanceOf(user.Resolve()) +func BalanceOf(user pusers.AddressOrName) uint64 { + balance, err := foo.BalanceOf(users.Resolve(user)) if err != nil { panic(err) } @@ -46,8 +48,8 @@ func OwnerOf(tid grc721.TokenID) std.Address { return owner } -func IsApprovedForAll(owner, user users.AddressOrName) bool { - return foo.IsApprovedForAll(owner.Resolve(), user.Resolve()) +func IsApprovedForAll(owner, user pusers.AddressOrName) bool { + return foo.IsApprovedForAll(users.Resolve(owner), users.Resolve(user)) } func GetApproved(tid grc721.TokenID) std.Address { @@ -61,22 +63,22 @@ func GetApproved(tid grc721.TokenID) std.Address { // Setters -func Approve(user users.AddressOrName, tid grc721.TokenID) { - err := foo.Approve(user.Resolve(), tid) +func Approve(user pusers.AddressOrName, tid grc721.TokenID) { + err := foo.Approve(users.Resolve(user), tid) if err != nil { panic(err) } } -func SetApprovalForAll(user users.AddressOrName, approved bool) { - err := foo.SetApprovalForAll(user.Resolve(), approved) +func SetApprovalForAll(user pusers.AddressOrName, approved bool) { + err := foo.SetApprovalForAll(users.Resolve(user), approved) if err != nil { panic(err) } } -func TransferFrom(from, to users.AddressOrName, tid grc721.TokenID) { - err := foo.TransferFrom(from.Resolve(), to.Resolve(), tid) +func TransferFrom(from, to pusers.AddressOrName, tid grc721.TokenID) { + err := foo.TransferFrom(users.Resolve(from), users.Resolve(to), tid) if err != nil { panic(err) } @@ -84,10 +86,10 @@ func TransferFrom(from, to users.AddressOrName, tid grc721.TokenID) { // Admin -func Mint(to users.AddressOrName, tid grc721.TokenID) { +func Mint(to pusers.AddressOrName, tid grc721.TokenID) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) - err := foo.Mint(to.Resolve(), tid) + err := foo.Mint(users.Resolve(to), tid) if err != nil { panic(err) } diff --git a/examples/gno.land/r/demo/foo721/foo721_test.gno b/examples/gno.land/r/demo/foo721/foo721_test.gno index 8fb5c6d4f94..93a7e33f71d 100644 --- a/examples/gno.land/r/demo/foo721/foo721_test.gno +++ b/examples/gno.land/r/demo/foo721/foo721_test.gno @@ -5,11 +5,13 @@ import ( "gno.land/p/demo/grc/grc721" "gno.land/r/demo/users" + + pusers "gno.land/p/demo/users" ) func TestFoo721(t *testing.T) { - admin := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") - hariom := users.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") + admin := pusers.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") + hariom := pusers.AddressOrName("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm") for i, tc := range []struct { name string @@ -18,7 +20,7 @@ func TestFoo721(t *testing.T) { }{ {"BalanceOf(admin)", 10, func() interface{} { return BalanceOf(admin) }}, {"BalanceOf(hariom)", 5, func() interface{} { return BalanceOf(hariom) }}, - {"OwnerOf(0)", admin.Resolve(), func() interface{} { return OwnerOf(grc721.TokenID("0")) }}, + {"OwnerOf(0)", users.Resolve(admin), func() interface{} { return OwnerOf(grc721.TokenID("0")) }}, {"IsApprovedForAll(admin, hariom)", false, func() interface{} { return IsApprovedForAll(admin, hariom) }}, } { t.Run(tc.name, func(t *testing.T) { diff --git a/examples/gno.land/r/demo/foo721/gno.mod b/examples/gno.land/r/demo/foo721/gno.mod index 46c19e6ae55..e013677379d 100644 --- a/examples/gno.land/r/demo/foo721/gno.mod +++ b/examples/gno.land/r/demo/foo721/gno.mod @@ -3,5 +3,6 @@ module gno.land/r/demo/foo721 require ( gno.land/p/demo/grc/grc721 v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest gno.land/r/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/r/demo/groups/misc.gno b/examples/gno.land/r/demo/groups/misc.gno index 07543ba4a45..2d578627632 100644 --- a/examples/gno.land/r/demo/groups/misc.gno +++ b/examples/gno.land/r/demo/groups/misc.gno @@ -80,7 +80,7 @@ func displayAddressMD(addr std.Address) string { if user == nil { return "[" + addr.String() + "](/r/users:" + addr.String() + ")" } - return "[@" + user.Name() + "](/r/users:" + user.Name() + ")" + return "[@" + user.Name + "](/r/users:" + user.Name + ")" } func usernameOf(addr std.Address) string { @@ -88,7 +88,7 @@ func usernameOf(addr std.Address) string { if user == nil { panic("user not found") } - return user.Name() + return user.Name } func isValidPermission(perm Permission) bool { diff --git a/examples/gno.land/r/demo/microblog/gno.mod b/examples/gno.land/r/demo/microblog/gno.mod index f496b1008ce..3285127b025 100644 --- a/examples/gno.land/r/demo/microblog/gno.mod +++ b/examples/gno.land/r/demo/microblog/gno.mod @@ -1,6 +1,9 @@ module gno.land/r/demo/microblog require ( + gno.land/p/demo/avl v0.0.0-latest gno.land/p/demo/microblog v0.0.0-latest + gno.land/p/demo/testutils v0.0.0-latest + gno.land/p/demo/ufmt v0.0.0-latest gno.land/r/demo/users v0.0.0-latest ) diff --git a/examples/gno.land/r/demo/microblog/microblog.gno b/examples/gno.land/r/demo/microblog/microblog.gno index f399b3be5d9..1c3cd5e7d68 100644 --- a/examples/gno.land/r/demo/microblog/microblog.gno +++ b/examples/gno.land/r/demo/microblog/microblog.gno @@ -7,8 +7,10 @@ package microblog import ( "std" + "strings" "gno.land/p/demo/microblog" + "gno.land/p/demo/ufmt" "gno.land/r/demo/users" ) @@ -22,9 +24,61 @@ func init() { m = microblog.NewMicroblog(title, prefix) } -// Render calls the microblog renderer +func renderHome() string { + output := ufmt.Sprintf("# %s\n\n", m.Title) + output += "# pages\n\n" + + for _, page := range m.GetPages() { + if u := users.GetUserByAddress(page.Author); u != nil { + output += ufmt.Sprintf("- [%s (%s)](%s%s)\n", u.Name, page.Author.String(), m.Prefix, page.Author.String()) + } else { + output += ufmt.Sprintf("- [%s](%s%s)\n", page.Author.String(), m.Prefix, page.Author.String()) + } + } + + return output +} + +func renderUser(user string) string { + silo, found := m.Pages.Get(user) + if !found { + return "404" // StatusNotFound + } + + return PageToString((silo.(*microblog.Page))) +} + func Render(path string) string { - return m.Render(path) + parts := strings.Split(path, "/") + + isHome := path == "" + isUser := len(parts) == 1 + + switch { + case isHome: + return renderHome() + + case isUser: + return renderUser(parts[0]) + } + + return "404" // StatusNotFound +} + +func PageToString(p *microblog.Page) string { + o := "" + if u := users.GetUserByAddress(p.Author); u != nil { + o += ufmt.Sprintf("# [%s](/r/demo/users:%s)\n\n", u, u) + o += ufmt.Sprintf("%s\n\n", u.Profile) + } + o += ufmt.Sprintf("## [%s](/r/demo/microblog:%s)\n\n", p.Author, p.Author) + + o += ufmt.Sprintf("joined %s, last updated %s\n\n", p.CreatedAt.Format("2006-02-01"), p.LastPosted.Format("2006-02-01")) + o += "## feed\n\n" + for _, u := range p.GetPosts() { + o += u.String() + "\n\n" + } + return o } // NewPost takes a single argument (post markdown) and diff --git a/examples/gno.land/p/demo/microblog/microblog_test.gno b/examples/gno.land/r/demo/microblog/microblog_test.gno similarity index 52% rename from examples/gno.land/p/demo/microblog/microblog_test.gno rename to examples/gno.land/r/demo/microblog/microblog_test.gno index 6d20b55e5cb..83b20286fee 100644 --- a/examples/gno.land/p/demo/microblog/microblog_test.gno +++ b/examples/gno.land/r/demo/microblog/microblog_test.gno @@ -7,60 +7,58 @@ import ( "testing" "gno.land/p/demo/avl" + "gno.land/p/demo/microblog" "gno.land/p/demo/testutils" ) func TestMicroblog(t *testing.T) { const ( - title string = "test microblog" - prefix string = "/r/test" author1 std.Address = testutils.TestAddress("author1") author2 std.Address = testutils.TestAddress("author2") ) std.TestSetOrigCaller(author1) - d := NewMicroblog(title, prefix) - if d.Render("/wrongpath") != "404" { + if Render("/wrongpath") != "404" { t.Fatalf("rendering not giving 404") } - if d.Render("") == "404" { + if Render("") == "404" { t.Fatalf("rendering / should not give 404") } - if err := d.NewPost("goodbyte, web2"); err != nil { + if err := m.NewPost("goodbyte, web2"); err != nil { t.Fatalf("could not create post") } - if _, err := d.GetPage(author1.String()); err != nil { + if _, err := m.GetPage(author1.String()); err != nil { t.Fatalf("silo should exist") } - if _, err := d.GetPage("no such author"); err == nil { + if _, err := m.GetPage("no such author"); err == nil { t.Fatalf("silo should not exist") } std.TestSetOrigCaller(author2) - if err := d.NewPost("hello, web3"); err != nil { + if err := m.NewPost("hello, web3"); err != nil { t.Fatalf("could not create post") } - if err := d.NewPost("hello again, web3"); err != nil { + if err := m.NewPost("hello again, web3"); err != nil { t.Fatalf("could not create post") } - if err := d.NewPost("hi again,\n web4?"); err != nil { + if err := m.NewPost("hi again,\n web4?"); err != nil { t.Fatalf("could not create post") } println("--- MICROBLOG ---\n\n") - if rendering := d.Render(""); rendering != `# test microblog + if rendering := Render(""); rendering != `# gno-based microblog # pages -- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/testg1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) -- [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/testg1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) +- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/microblog:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) +- [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/microblog:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) ` { t.Fatalf("incorrect rendering /: '%s'", rendering) } - if rendering := strings.TrimSpace(d.Render(author1.String())); rendering != `## [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/microblog:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) + if rendering := strings.TrimSpace(Render(author1.String())); rendering != `## [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/microblog:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) joined 2009-13-02, last updated 2009-13-02 @@ -72,7 +70,7 @@ joined 2009-13-02, last updated 2009-13-02 t.Fatalf("incorrect rendering /: '%s'", rendering) } - if rendering := strings.TrimSpace(d.Render(author2.String())); rendering != `## [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/microblog:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) + if rendering := strings.TrimSpace(Render(author2.String())); rendering != `## [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/microblog:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) joined 2009-13-02, last updated 2009-13-02 diff --git a/examples/gno.land/r/demo/users/gno.mod b/examples/gno.land/r/demo/users/gno.mod index edd20eb2721..a2ee2ea86ba 100644 --- a/examples/gno.land/r/demo/users/gno.mod +++ b/examples/gno.land/r/demo/users/gno.mod @@ -1,3 +1,6 @@ module gno.land/r/demo/users -require gno.land/p/demo/avl v0.0.0-latest +require ( + gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest +) diff --git a/examples/gno.land/r/demo/users/users.gno b/examples/gno.land/r/demo/users/users.gno index 5f492f1662f..4fe486a71d0 100644 --- a/examples/gno.land/r/demo/users/users.gno +++ b/examples/gno.land/r/demo/users/users.gno @@ -7,44 +7,16 @@ import ( "strings" "gno.land/p/demo/avl" + "gno.land/p/demo/users" ) -//---------------------------------------- -// Types - -type User struct { - address std.Address - name string - profile string - number int - invites int - inviter std.Address -} - -func (u *User) Render() string { - str := "## user " + u.name + "\n" + - "\n" + - " * address = " + string(u.address) + "\n" + - " * " + strconv.Itoa(u.invites) + " invites\n" - if u.inviter != "" { - str = str + " * invited by " + string(u.inviter) + "\n" - } - str = str + "\n" + - u.profile + "\n" - return str -} - -func (u User) Name() string { return u.name } -func (u User) Profile() string { return u.profile } -func (u User) Address() std.Address { return u.address } - //---------------------------------------- // State var ( admin std.Address = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj" - name2User avl.Tree // Name -> *User - addr2User avl.Tree // std.Address -> *User + name2User avl.Tree // Name -> *users.User + addr2User avl.Tree // std.Address -> *users.User invites avl.Tree // string(inviter+":"+invited) -> true counter int // user id counter minFee int64 = 200 * 1000000 // minimum gnot must be paid to register. @@ -108,13 +80,13 @@ func Register(inviter std.Address, name string, profile string) { } // register. counter++ - user := &User{ - address: caller, - name: name, - profile: profile, - number: counter, - invites: invites, - inviter: inviter, + user := &users.User{ + Address: caller, + Name: name, + Profile: profile, + Number: counter, + Invites: invites, + Inviter: inviter, } name2User.Set(name, user) addr2User.Set(caller.String(), user) @@ -137,12 +109,12 @@ func Invite(invitee string) { if !ok { panic("user unknown") } - user := userI.(*User) - if user.invites <= 0 { + user := userI.(*users.User) + if user.Invites <= 0 { panic("user has no invite tokens") } - user.invites -= len(lines) - if user.invites < 0 { + user.Invites -= len(lines) + if user.Invites < 0 { panic("user has insufficient invite tokens") } } @@ -204,8 +176,8 @@ func GrantInvites(invites string) { panic("invalid user " + name) } } - user := userI.(*User) - user.invites += invites + user := userI.(*users.User) + user.Invites += invites } } @@ -238,24 +210,24 @@ func SetMaxFeeMultiple(newMaxFeeMult int64) { //---------------------------------------- // Exposed public functions -func GetUserByName(name string) *User { +func GetUserByName(name string) *users.User { userI, ok := name2User.Get(name) if !ok { return nil } - return userI.(*User) + return userI.(*users.User) } -func GetUserByAddress(addr std.Address) *User { +func GetUserByAddress(addr std.Address) *users.User { userI, ok := addr2User.Get(addr.String()) if !ok { return nil } - return userI.(*User) + return userI.(*users.User) } // unlike GetUserByName, input must be "@" prefixed for names. -func GetUserByAddressOrName(input AddressOrName) *User { +func GetUserByAddressOrName(input users.AddressOrName) *users.User { name, isName := input.GetName() if isName { return GetUserByName(name) @@ -263,6 +235,15 @@ func GetUserByAddressOrName(input AddressOrName) *User { return GetUserByAddress(std.Address(input)) } +func Resolve(input users.AddressOrName) std.Address { + name, isName := input.GetName() + if !isName { + return std.Address(input) // TODO check validity + } + user := GetUserByName(name) + return user.Address +} + //---------------------------------------- // Constants @@ -297,8 +278,8 @@ func Render(path string) string { func renderHome() string { doc := "" name2User.Iterate("", "", func(key string, value interface{}) bool { - user := value.(*User) - doc += " * [" + user.name + "](/r/demo/users:" + user.name + ")\n" + user := value.(*users.User) + doc += " * [" + user.Name + "](/r/demo/users:" + user.Name + ")\n" return false }) return doc diff --git a/gno.land/pkg/gnoweb/gnoweb_test.go b/gno.land/pkg/gnoweb/gnoweb_test.go index 82c76266683..86a739b4a36 100644 --- a/gno.land/pkg/gnoweb/gnoweb_test.go +++ b/gno.land/pkg/gnoweb/gnoweb_test.go @@ -32,7 +32,7 @@ func TestRoutes(t *testing.T) { {"/r/gnoland/blog/admin.gno", ok, "func "}, {"/r/demo/users:administrator", ok, "address"}, {"/r/demo/users", ok, "manfred"}, - {"/r/demo/users/types.gno", ok, "type "}, + {"/r/demo/users/users.gno", ok, "// State"}, {"/r/demo/deep/very/deep", ok, "it works!"}, {"/r/demo/deep/very/deep:bob", ok, "hi bob"}, {"/r/demo/deep/very/deep?help", ok, "exposed"},