forked from coracle/zooid
Clean up groups stuff
This commit is contained in:
+75
-48
@@ -1,8 +1,6 @@
|
||||
package zooid
|
||||
|
||||
import (
|
||||
"iter"
|
||||
|
||||
"fiatjaf.com/nostr"
|
||||
)
|
||||
|
||||
@@ -16,17 +14,74 @@ func GetGroupIDFromEvent(event nostr.Event) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func MakeGroupMetadataFilter(h string) nostr.Filter {
|
||||
return nostr.Filter{
|
||||
type GroupStore struct {
|
||||
Config *Config
|
||||
Events *EventStore
|
||||
}
|
||||
|
||||
func (g *GroupStore) GetMetadata(h string) nostr.Event {
|
||||
filter := nostr.Filter{
|
||||
Kinds: []nostr.Kind{nostr.KindSimpleGroupMetadata},
|
||||
Tags: nostr.TagMap{
|
||||
"d": []string{h},
|
||||
},
|
||||
}
|
||||
|
||||
for event := range g.Events.QueryEvents(filter, 1) {
|
||||
return event
|
||||
}
|
||||
|
||||
return nostr.Event{}
|
||||
}
|
||||
|
||||
func MakeGroupEventFilters(h string) []nostr.Filter {
|
||||
return []nostr.Filter{
|
||||
func (g *GroupStore) AddMember(h string, pubkey nostr.PubKey) error {
|
||||
event := nostr.Event{
|
||||
Kind: nostr.KindSimpleGroupPutUser,
|
||||
CreatedAt: nostr.Now(),
|
||||
Tags: nostr.Tags{
|
||||
nostr.Tag{"p", pubkey.Hex()},
|
||||
nostr.Tag{"h", h},
|
||||
},
|
||||
}
|
||||
|
||||
return g.Events.SignAndSaveEvent(event, true)
|
||||
}
|
||||
|
||||
func (g *GroupStore) RemoveMember(h string, pubkey nostr.PubKey) error {
|
||||
event := nostr.Event{
|
||||
Kind: nostr.KindSimpleGroupRemoveUser,
|
||||
CreatedAt: nostr.Now(),
|
||||
Tags: nostr.Tags{
|
||||
nostr.Tag{"p", pubkey.Hex()},
|
||||
nostr.Tag{"h", h},
|
||||
},
|
||||
}
|
||||
|
||||
return g.Events.SignAndSaveEvent(event, true)
|
||||
}
|
||||
|
||||
func (g *GroupStore) SetMetadataFromEvent(event nostr.Event) error {
|
||||
tags := nostr.Tags{}
|
||||
|
||||
for _, tag := range event.Tags {
|
||||
if len(tag) >= 2 && tag[0] == "h" {
|
||||
tags = append(tags, nostr.Tag{"d", tag[1]})
|
||||
} else {
|
||||
tags = append(tags, tag)
|
||||
}
|
||||
}
|
||||
|
||||
metadataEvent := nostr.Event{
|
||||
Kind: nostr.KindSimpleGroupMetadata,
|
||||
CreatedAt: event.CreatedAt,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
return g.Events.SignAndSaveEvent(metadataEvent, true)
|
||||
}
|
||||
|
||||
func (g *GroupStore) DeleteGroup(h string) {
|
||||
filters := []nostr.Filter{
|
||||
{
|
||||
Tags: nostr.TagMap{
|
||||
"d": []string{h},
|
||||
@@ -38,20 +93,24 @@ func MakeGroupEventFilters(h string) []nostr.Filter {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, filter := range filters {
|
||||
for event := range g.Events.QueryEvents(filter, 0) {
|
||||
g.Events.DeleteEvent(event.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MakeGroupMembershipCheckFilter(h string, pubkey nostr.PubKey) nostr.Filter {
|
||||
return nostr.Filter{
|
||||
func (g *GroupStore) IsMember(h string, pubkey nostr.PubKey) bool {
|
||||
filter := nostr.Filter{
|
||||
Kinds: []nostr.Kind{nostr.KindSimpleGroupPutUser, nostr.KindSimpleGroupRemoveUser},
|
||||
Tags: nostr.TagMap{
|
||||
"p": []string{pubkey.Hex()},
|
||||
"h": []string{h},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func CheckGroupMembership(events iter.Seq[nostr.Event]) bool {
|
||||
for event := range events {
|
||||
for event := range g.Events.QueryEvents(filter, 1) {
|
||||
if event.Kind == nostr.KindSimpleGroupPutUser {
|
||||
return true
|
||||
}
|
||||
@@ -64,42 +123,10 @@ func CheckGroupMembership(events iter.Seq[nostr.Event]) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func MakePutUserEvent(h string, pubkey nostr.PubKey) nostr.Event {
|
||||
return nostr.Event{
|
||||
Kind: nostr.KindSimpleGroupPutUser,
|
||||
CreatedAt: nostr.Now(),
|
||||
Tags: nostr.Tags{
|
||||
nostr.Tag{"p", pubkey.Hex()},
|
||||
nostr.Tag{"h", h},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func MakeRemoveUserEvent(h string, pubkey nostr.PubKey) nostr.Event {
|
||||
return nostr.Event{
|
||||
Kind: nostr.KindSimpleGroupRemoveUser,
|
||||
CreatedAt: nostr.Now(),
|
||||
Tags: nostr.Tags{
|
||||
nostr.Tag{"p", pubkey.Hex()},
|
||||
nostr.Tag{"h", h},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func MakeMetadataEvent(event nostr.Event) nostr.Event {
|
||||
tags := nostr.Tags{}
|
||||
|
||||
for _, tag := range event.Tags {
|
||||
if len(tag) >= 2 && tag[0] == "h" {
|
||||
tags = append(tags, nostr.Tag{"d", tag[1]})
|
||||
} else {
|
||||
tags = append(tags, tag)
|
||||
}
|
||||
}
|
||||
|
||||
return nostr.Event{
|
||||
Kind: nostr.KindSimpleGroupMetadata,
|
||||
CreatedAt: event.CreatedAt,
|
||||
Tags: tags,
|
||||
func (g *GroupStore) HasAccess(h string, pubkey nostr.PubKey) bool {
|
||||
if !HasTag(g.GetMetadata(h).Tags, "closed") {
|
||||
return true
|
||||
}
|
||||
|
||||
return g.IsMember(h, pubkey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user