-
Notifications
You must be signed in to change notification settings - Fork 441
feat(boards2): change realm to use gno.land/p/gnoland/boards
#4939
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
Open
jeronimoalbi
wants to merge
12
commits into
gnolang:master
Choose a base branch
from
jeronimoalbi:refactor/boards2-use-boards-package
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cf10093
refactor: move render related methods into standalone functions
jeronimoalbi 4d82555
refactor: change boards2 realm to use boards package
jeronimoalbi 4237868
tests: add unit tests for replies storage
jeronimoalbi d76395c
feat: changed public functions to use `WithPermission`
jeronimoalbi a37c997
feat: change boards to use an identified generator for board IDs
jeronimoalbi aa8847f
chore: change `CreateRepost()` arguments order
jeronimoalbi 07f4aa5
chore: remove reply freeze support
jeronimoalbi cc0b03c
feat: add `gno.land/p/gnoland/boards/dao` package
jeronimoalbi b4c7639
refactor: change basic permissions to use a custom member storage
jeronimoalbi 9168168
feat: add suport for public permissions
jeronimoalbi 975bee3
feat: allow members to remove themselves without permissions
jeronimoalbi 7e41cbc
feat: move custom member storage to `p/nt/commondao/exts/storage`
jeronimoalbi 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 hidden or 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,2 @@ | ||
| module = "gno.land/p/nt/commondao/exts/storage" | ||
| gno = "0.9" |
164 changes: 164 additions & 0 deletions
164
examples/gno.land/p/nt/commondao/exts/storage/member_storage.gno
This file contains hidden or 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,164 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "gno.land/p/jeronimoalbi/message" | ||
| "gno.land/p/nt/avl" | ||
| "gno.land/p/nt/commondao" | ||
| ) | ||
|
|
||
| const ( | ||
| msgMemberAdd message.Topic = "MemberAdd" | ||
| msgMemberRemove = "MemberRemove" | ||
| ) | ||
|
|
||
| // GetMemberGroups returns the groups that a member belongs to. | ||
| // It returns no groups if member storage was not created using this package. | ||
| func GetMemberGroups(s commondao.MemberStorage, member address) []string { | ||
| storage, ok := s.(*memberStorage) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| v, found := storage.memberGroups.Get(member.String()) | ||
| if !found { | ||
| return nil | ||
| } | ||
|
|
||
| tree := v.(*avl.Tree) | ||
| groups := make([]string, 0, tree.Size()) | ||
| tree.Iterate("", "", func(group string, _ any) bool { | ||
| groups = append(groups, group) | ||
| return false | ||
| }) | ||
| return groups | ||
| } | ||
|
|
||
| // NewMemberStorage creates a new CommonDAO member storage with grouping support. | ||
| // | ||
| // This is a custom storage that automatically adds or removes members that are added | ||
| // or removed from any of the member groups. This allows for quick and inexpensive | ||
| // checks for the number of total unique storage users, including users added to groups, | ||
| // and also to iterate all of them without needing to iterate individual groups. | ||
| func NewMemberStorage() commondao.MemberStorage { | ||
| // Create a new broker to allow storages to publish and subscribe to messages | ||
| // It is used to add/remove users from the storage each time one or more groups change. | ||
| broker := message.NewBroker() | ||
|
|
||
| // Define a factory for creating custom member storages when groups are created. | ||
| // Custom storage publishes when a member is added or removed from a group. | ||
| innerFactory := func(group string) commondao.MemberStorage { | ||
| return &groupMemberStorage{ | ||
| MemberStorage: commondao.NewMemberStorage(), | ||
| messages: broker, | ||
| group: group, | ||
| } | ||
| } | ||
|
|
||
| // Create a member storage that automatically adds or removes members each time | ||
| // a member group changes. This allows the storage to keep all members within | ||
| // the same "root" storage for easier iteration. | ||
| storage := &memberStorage{ | ||
| MemberStorage: commondao.NewMemberStorageWithGrouping( | ||
| commondao.UseStorageFactory(innerFactory), | ||
| ), | ||
| messages: broker, | ||
| } | ||
|
|
||
| // Subscribe to messages published by member groups | ||
| storage.messages.Subscribe(msgMemberAdd, storage.handleMemberAddMsg) | ||
| storage.messages.Subscribe(msgMemberRemove, storage.handleMemberRemoveMsg) | ||
| return storage | ||
| } | ||
|
|
||
| type memberStorage struct { | ||
| commondao.MemberStorage | ||
|
|
||
| memberGroups avl.Tree // string(address) -> *avl.Tree(group -> struct{}) | ||
| messages message.Subscriber | ||
| } | ||
|
|
||
| func (s *memberStorage) handleMemberAddMsg(msg message.Message) { | ||
| data := msg.Data.(groupMemberUpdateData) | ||
| key := data.Member.String() | ||
| v, _ := s.memberGroups.Get(key) | ||
| groups, ok := v.(*avl.Tree) | ||
| if !ok { | ||
| // Create a new tree to track member's groups | ||
| groups = avl.NewTree() | ||
| s.memberGroups.Set(key, groups) | ||
|
|
||
| // Add the new member to the storage | ||
| s.MemberStorage.Add(data.Member) | ||
| } | ||
|
|
||
| // Keep track of the new member group | ||
| groups.Set(data.Group, struct{}{}) | ||
| } | ||
|
|
||
| func (s *memberStorage) handleMemberRemoveMsg(msg message.Message) { | ||
| data := msg.Data.(groupMemberUpdateData) | ||
| key := data.Member.String() | ||
| v, found := s.memberGroups.Get(key) | ||
| if !found { | ||
| // Member should always be found | ||
| return | ||
| } | ||
|
|
||
| // Remove the group from the list of groups member belongs | ||
| groups := v.(*avl.Tree) | ||
| groups.Remove(data.Group) | ||
|
|
||
| // Remove the member from the storage when it doesn't belong to any group | ||
| if groups.Size() == 0 { | ||
| s.memberGroups.Remove(key) | ||
| s.MemberStorage.Remove(data.Member) | ||
| } | ||
| } | ||
|
|
||
| // Size returns the number of members in the storage. | ||
| // It also includes unique members that belong to any number of member groups. | ||
| func (s memberStorage) Size() int { | ||
| return s.MemberStorage.Size() | ||
| } | ||
|
|
||
| // IterateByOffset iterates members starting at the given offset. | ||
| // The callback can return true to stop iteration. | ||
| // It also iterates unique members that belong to any of the member groups. | ||
| func (s memberStorage) IterateByOffset(offset, count int, fn commondao.MemberIterFn) bool { | ||
| return s.MemberStorage.IterateByOffset(offset, count, fn) | ||
| } | ||
|
|
||
| // groupMemberUpdateData defines a data type for group member updates. | ||
| type groupMemberUpdateData struct { | ||
| Group string | ||
| Member address | ||
| } | ||
|
|
||
| // groupMemberStorage defines a member storage for member groups. | ||
| // This type of storage publishes messages when a member is added or removed from a group. | ||
| type groupMemberStorage struct { | ||
| commondao.MemberStorage | ||
|
|
||
| group string | ||
| messages message.Publisher | ||
| } | ||
|
|
||
| // Add adds a member to the storage. | ||
| // Returns true if the member is added, or false if it already existed. | ||
| func (s *groupMemberStorage) Add(member address) bool { | ||
| s.messages.Publish(msgMemberAdd, groupMemberUpdateData{ | ||
| Group: s.group, | ||
| Member: member, | ||
| }) | ||
| return s.MemberStorage.Add(member) | ||
| } | ||
|
|
||
| // Remove removes a member from the storage. | ||
| // Returns true if member was removed, or false if it was not found. | ||
| func (s *groupMemberStorage) Remove(member address) bool { | ||
| s.messages.Publish(msgMemberRemove, groupMemberUpdateData{ | ||
| Group: s.group, | ||
| Member: member, | ||
| }) | ||
| return s.MemberStorage.Remove(member) | ||
| } | ||
Oops, something went wrong.
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.
I though of replacing the default
commondaomember storage implementation with this one, but I didn't because I think this one is opinionated, it uses a message broker to update the list of members in the storage when a member is added or removed from any storage group. A member storage can have multiple groups that can be used for example for tiers or roles.This custom member storage automatically updates members from group members to make it easier to iterate all members that exists within the groups, in a cheaper way that doesn't require to potentially traversing multiple AVL trees, one within each group, but instead just traverse a single storage AVL tree. It also simplifies listing all the groups a member belongs to without traversing multiple trees, or getting the total number of members.
Finally, this custom member storage is being used within Boards2 realm, in the
BasicPermissionsimplementation. It uses grouping to group board members by role.