-
Notifications
You must be signed in to change notification settings - Fork 375
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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