Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat: initial r/profile dapp #181

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions examples/gno.land/r/profile/profile.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package profile

import (
"std"

"gno.land/p/ufmt"
// "gno.land/r/users"
)

// FIXME: manage privacy?

func newProfile(addr std.Address) *Profile {
now := std.GetTimestamp()
return &Profile{
address: addr,
created: now,
}
}

type Profile struct {
dict map[string]interface{}
address std.Address
created std.Time
updated std.Time
}

func (p *Profile) save() {
now := std.GetTimestamp()
if p.created == 0 {
p.created = now
}
p.updated = now
}

func (p *Profile) update(dict map[string]interface{}) {
for k, v := range dict {
// TODO: additional checks here
// TODO: check v.Type()
// TODO: check v.Len()
if k == "address" || k == "created" || k == "updated" {
panic("reserved profile key")
}
p.dict[k] = v
}
p.save()
}

func (p *Profile) Render() string {
output := ""
output += ufmt.Sprintf("* address: %q\n", p.address)
output += ufmt.Sprintf("* created: %v\n", p.created)
if p.updated > 0 {
output += ufmt.Sprintf("* updated: %v\n", p.updated)
}
for k, v := range p.dict {
output += ufmt.Sprintf("* %s: %v\n", k, v)
}
return output
}
66 changes: 66 additions & 0 deletions examples/gno.land/r/profile/profiles.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package profile

import (
"std"
"strings"

"gno.land/p/avl"
"gno.land/p/ufmt"
"gno.land/r/users"
)

// TODO: makes sense to implement an allowance system?

var profiles *avl.MutTree // std.Address.String() -> *Profile
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the old avl API


func init() {
// profiles = avl.NewMutTree()
}

func Update(dict map[string]interface{}) {
// FIXME: ask a price per stored data length?
currentUser := std.GetOrigCaller()
profile := getOrCreateProfileByAddress(currentUser)
profile.update(dict)
}

func GetByAddressOrName(aon users.AddressOrName) *Profile {
addr := aon.Resolve()
profile, found := profiles.Get(addr.String())
if !found {
return nil
}
return profile.(*Profile)
}

func getOrCreateProfileByAddress(addr std.Address) *Profile {
// lookup existing profile
profile, found := profiles.Get(addr.String())
if found {
return profile.(*Profile)
}

// create
newProfile := &Profile{address: addr}
profiles.Set(addr.String(), newProfile)
return newProfile
}

func Render(path string) string {
parts := strings.Split(path, "/")

switch {
case path == "":
output := ufmt.Sprintf("stats: %d known profiles\n", profiles.Size())
return output
case len(parts) == 1:
aon := users.AddressOrName(parts[0])
profile := GetByAddressOrName(aon)
if profile != nil {
return profile.Render()
}
return "404: no such profile"
}

return "404: invalid URL"
}
17 changes: 17 additions & 0 deletions examples/gno.land/r/profile/profiles_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package profile

import (
"fmt"
"testing"

"gno.land/r/profile"
)

func TestRender(t *testing.T) {
got := profile.Render("")
fmt.Println(got)
//_ = profile.Render
// various data types
// avatar
// ip address
}
Loading