Skip to content

Commit

Permalink
move some things back from nip29/relay to nip29
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 4, 2024
1 parent 3fecd03 commit 68970d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
15 changes: 15 additions & 0 deletions nip29/nip29.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ func (kr KindRange) Includes(kind int) bool {
_, ok := slices.BinarySearch(kr, kind)
return ok
}

var (
// used for normal members without admin powers
EmptyRole *Role = nil

PermissionsMap = map[Permission]struct{}{
PermAddUser: {},
PermEditMetadata: {},
PermDeleteEvent: {},
PermRemoveUser: {},
PermAddPermission: {},
PermRemovePermission: {},
PermEditGroupStatus: {},
}
)
41 changes: 6 additions & 35 deletions nip29/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,6 @@ import (
"github.com/nbd-wtf/go-nostr/nip29"
)

var (
// used for the default role, the actual relay, hidden otherwise
MasterRole *nip29.Role = &nip29.Role{
Name: "master",
Permissions: map[nip29.Permission]struct{}{
nip29.PermAddUser: {},
nip29.PermEditMetadata: {},
nip29.PermDeleteEvent: {},
nip29.PermRemoveUser: {},
nip29.PermAddPermission: {},
nip29.PermRemovePermission: {},
nip29.PermEditGroupStatus: {},
},
}

// used for normal members without admin powers, not displayed
EmptyRole *nip29.Role = nil

PermissionsMap = map[nip29.Permission]struct{}{
nip29.PermAddUser: {},
nip29.PermEditMetadata: {},
nip29.PermDeleteEvent: {},
nip29.PermRemoveUser: {},
nip29.PermAddPermission: {},
nip29.PermRemovePermission: {},
nip29.PermEditGroupStatus: {},
}
)

type Action interface {
Apply(group *nip29.Group)
PermissionName() nip29.Permission
Expand Down Expand Up @@ -102,7 +73,7 @@ var moderationActionFactories = map[int]func(*nostr.Event) (Action, error){
permissions := make([]nip29.Permission, 0, nTags-1)
for _, tag := range evt.Tags.GetAll([]string{"permission", ""}) {
perm := nip29.Permission(tag[1])
if _, ok := PermissionsMap[perm]; !ok {
if _, ok := nip29.PermissionsMap[perm]; !ok {
return nil, fmt.Errorf("unknown permission '%s'", tag[1])
}
permissions = append(permissions, perm)
Expand All @@ -128,7 +99,7 @@ var moderationActionFactories = map[int]func(*nostr.Event) (Action, error){
permissions := make([]nip29.Permission, 0, nTags-1)
for _, tag := range evt.Tags.GetAll([]string{"permission", ""}) {
perm := nip29.Permission(tag[1])
if _, ok := PermissionsMap[perm]; !ok {
if _, ok := nip29.PermissionsMap[perm]; !ok {
return nil, fmt.Errorf("unknown permission '%s'", tag[1])
}
permissions = append(permissions, perm)
Expand Down Expand Up @@ -204,7 +175,7 @@ type AddUser struct {
func (AddUser) PermissionName() nip29.Permission { return nip29.PermAddUser }
func (a AddUser) Apply(group *nip29.Group) {
for _, target := range a.Targets {
group.Members[target] = EmptyRole
group.Members[target] = nip29.EmptyRole
}
}

Expand Down Expand Up @@ -246,7 +217,7 @@ func (a AddPermission) Apply(group *nip29.Group) {

// if it's a normal user, create a new permissions object thing for this user
// instead of modifying the global EmptyRole
if !ok || role == EmptyRole {
if !ok || role == nip29.EmptyRole {
role = &nip29.Role{Permissions: make(map[nip29.Permission]struct{})}
group.Members[target] = role
}
Expand All @@ -267,7 +238,7 @@ func (RemovePermission) PermissionName() nip29.Permission { return nip29.PermRem
func (a RemovePermission) Apply(group *nip29.Group) {
for _, target := range a.Targets {
role, ok := group.Members[target]
if !ok || role == EmptyRole {
if !ok || role == nip29.EmptyRole {
continue
}

Expand All @@ -278,7 +249,7 @@ func (a RemovePermission) Apply(group *nip29.Group) {

// if no more permissions are available, change this guy to be a normal user
if role.Name == "" && len(role.Permissions) == 0 {
group.Members[target] = EmptyRole
group.Members[target] = nip29.EmptyRole
}
}
}
Expand Down

0 comments on commit 68970d9

Please sign in to comment.