-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
32 deletions.
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
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,75 @@ | ||
package nip29 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/nbd-wtf/go-nostr" | ||
) | ||
|
||
type Group struct { | ||
ID string | ||
Name string | ||
Picture string | ||
About string | ||
Members map[string]*Role | ||
Private bool | ||
Closed bool | ||
} | ||
|
||
func (group Group) MetadataEvent() *nostr.Event { | ||
evt := &nostr.Event{ | ||
Kind: 39000, | ||
CreatedAt: nostr.Now(), | ||
Content: group.About, | ||
Tags: nostr.Tags{ | ||
nostr.Tag{"d", group.ID}, | ||
}, | ||
} | ||
if group.Name != "" { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"name", group.Name}) | ||
} | ||
if group.About != "" { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"about", group.Name}) | ||
} | ||
if group.Picture != "" { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"picture", group.Picture}) | ||
} | ||
|
||
// status | ||
if group.Private { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"private"}) | ||
} else { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"public"}) | ||
} | ||
if group.Closed { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"closed"}) | ||
} else { | ||
evt.Tags = append(evt.Tags, nostr.Tag{"open"}) | ||
} | ||
|
||
return evt | ||
} | ||
|
||
func MetadataEventToGroup(evt *nostr.Event) (*Group, error) { | ||
if evt.Kind != nostr.KindSimpleGroupMetadata { | ||
return nil, fmt.Errorf("expected kind %d, got %d", nostr.KindSimpleGroupMetadata, evt.Kind) | ||
} | ||
|
||
group := &Group{} | ||
group.ID = evt.Tags.GetD() | ||
|
||
if tag := nostr.Tags.GetFirst([]string{"name", ""}); tag != nil { | ||
group.Name = (*tag)[1] | ||
} | ||
if tag := nostr.Tags.GetFirst([]string{"about", ""}); tag != nil { | ||
group.About = (*tag)[1] | ||
} | ||
if tag := nostr.Tags.GetFirst([]string{"picture", ""}); tag != nil { | ||
group.Picture = (*tag)[1] | ||
} | ||
|
||
|
||
Members map[string]*Role | ||
Private bool | ||
Closed bool | ||
} |
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,46 @@ | ||
package nip29 | ||
|
||
import ( | ||
"github.com/nbd-wtf/go-nostr" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
type Role struct { | ||
Name string | ||
Permissions map[Permission]struct{} | ||
} | ||
|
||
type Permission = string | ||
|
||
const ( | ||
PermAddUser Permission = "add-user" | ||
PermEditMetadata Permission = "edit-metadata" | ||
PermDeleteEvent Permission = "delete-event" | ||
PermRemoveUser Permission = "remove-user" | ||
PermAddPermission Permission = "add-permission" | ||
PermRemovePermission Permission = "remove-permission" | ||
PermEditGroupStatus Permission = "edit-group-status" | ||
) | ||
|
||
type KindRange []int | ||
|
||
var ModerationEventKinds = KindRange{ | ||
nostr.KindSimpleGroupAddUser, | ||
nostr.KindSimpleGroupRemoveUser, | ||
nostr.KindSimpleGroupEditMetadata, | ||
nostr.KindSimpleGroupAddPermission, | ||
nostr.KindSimpleGroupRemovePermission, | ||
nostr.KindSimpleGroupDeleteEvent, | ||
nostr.KindSimpleGroupEditGroupStatus, | ||
} | ||
|
||
var MetadataEventKinds = KindRange{ | ||
nostr.KindSimpleGroupMetadata, | ||
nostr.KindSimpleGroupAdmins, | ||
nostr.KindSimpleGroupMembers, | ||
} | ||
|
||
func (kr KindRange) Includes(kind int) bool { | ||
_, ok := slices.BinarySearch(kr, kind) | ||
return ok | ||
} |