Fix various group related bugs
This commit is contained in:
+21
-6
@@ -3,12 +3,21 @@ package zooid
|
||||
import (
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/nip29"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// Utils
|
||||
|
||||
func GetGroupIDFromEvent(event nostr.Event) string {
|
||||
tag := event.Tags.Find("h")
|
||||
var tagName string
|
||||
|
||||
if slices.Contains(nip29.MetadataEventKinds, event.Kind) {
|
||||
tagName = "d"
|
||||
} else {
|
||||
tagName = "h"
|
||||
}
|
||||
|
||||
tag := event.Tags.Find(tagName)
|
||||
|
||||
if tag != nil {
|
||||
return tag[1]
|
||||
@@ -27,7 +36,7 @@ type GroupStore struct {
|
||||
|
||||
// Metadata
|
||||
|
||||
func (g *GroupStore) GetMetadata(h string) nostr.Event {
|
||||
func (g *GroupStore) GetMetadata(h string) (nostr.Event, bool) {
|
||||
filter := nostr.Filter{
|
||||
Kinds: []nostr.Kind{nostr.KindSimpleGroupMetadata},
|
||||
Tags: nostr.TagMap{
|
||||
@@ -36,13 +45,13 @@ func (g *GroupStore) GetMetadata(h string) nostr.Event {
|
||||
}
|
||||
|
||||
for event := range g.Events.QueryEvents(filter, 1) {
|
||||
return event
|
||||
return event, true
|
||||
}
|
||||
|
||||
return nostr.Event{}
|
||||
return nostr.Event{}, false
|
||||
}
|
||||
|
||||
func (g *GroupStore) SetMetadataFromEvent(event nostr.Event) error {
|
||||
func (g *GroupStore) UpdateMetadata(event nostr.Event) error {
|
||||
tags := nostr.Tags{}
|
||||
|
||||
for _, tag := range event.Tags {
|
||||
@@ -214,7 +223,13 @@ func (g *GroupStore) UpdateMembersList(h string) error {
|
||||
// Other stuff
|
||||
|
||||
func (g *GroupStore) HasAccess(h string, pubkey nostr.PubKey) bool {
|
||||
if !HasTag(g.GetMetadata(h).Tags, "closed") {
|
||||
meta, found := g.GetMetadata(h)
|
||||
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
|
||||
if !HasTag(meta.Tags, "closed") {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
+20
-10
@@ -397,13 +397,13 @@ func (instance *Instance) OnEvent(ctx context.Context, event nostr.Event) (rejec
|
||||
return true, "invalid: h tag is required"
|
||||
}
|
||||
|
||||
meta := instance.Groups.GetMetadata(h)
|
||||
_, found := instance.Groups.GetMetadata(h)
|
||||
|
||||
if event.Kind == nostr.KindSimpleGroupCreateGroup {
|
||||
if !IsEmptyEvent(meta) {
|
||||
if found {
|
||||
return true, "invalid: that group already exists"
|
||||
}
|
||||
} else if IsEmptyEvent(meta) {
|
||||
} else if !found {
|
||||
return true, "invalid: no such group exists"
|
||||
}
|
||||
|
||||
@@ -415,9 +415,9 @@ func (instance *Instance) OnEvent(ctx context.Context, event nostr.Event) (rejec
|
||||
return true, "duplicate: not currently a member"
|
||||
}
|
||||
} else if h != "" {
|
||||
meta := instance.Groups.GetMetadata(h)
|
||||
_, found := instance.Groups.GetMetadata(h)
|
||||
|
||||
if IsEmptyEvent(meta) {
|
||||
if !found {
|
||||
return true, "invalid: no such group exists"
|
||||
}
|
||||
|
||||
@@ -434,12 +434,22 @@ func (instance *Instance) OnEvent(ctx context.Context, event nostr.Event) (rejec
|
||||
}
|
||||
|
||||
func (instance *Instance) OnEventSaved(ctx context.Context, event nostr.Event) {
|
||||
var groupMeta nostr.Event
|
||||
var groupFound bool
|
||||
|
||||
h := GetGroupIDFromEvent(event)
|
||||
|
||||
if event.Kind == nostr.KindSimpleGroupJoinRequest && instance.Config.Groups.AutoJoin {
|
||||
meta := instance.Groups.GetMetadata(h)
|
||||
if h != "" {
|
||||
groupMeta, groupFound = instance.Groups.GetMetadata(h)
|
||||
|
||||
if !HasTag(meta.Tags, "closed") {
|
||||
if !groupFound && event.Kind != nostr.KindSimpleGroupCreateGroup {
|
||||
log.Printf("Attempted to process event for nonexistent group %s", h)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if event.Kind == nostr.KindSimpleGroupJoinRequest && instance.Config.Groups.AutoJoin {
|
||||
if !HasTag(groupMeta.Tags, "closed") {
|
||||
instance.Groups.AddMember(h, event.PubKey)
|
||||
instance.Groups.UpdateMembersList(h)
|
||||
}
|
||||
@@ -459,12 +469,12 @@ func (instance *Instance) OnEventSaved(ctx context.Context, event nostr.Event) {
|
||||
}
|
||||
|
||||
if event.Kind == nostr.KindSimpleGroupCreateGroup {
|
||||
instance.Groups.SetMetadataFromEvent(event)
|
||||
instance.Groups.UpdateMetadata(event)
|
||||
instance.Groups.UpdateAdminsList(h)
|
||||
}
|
||||
|
||||
if event.Kind == nostr.KindSimpleGroupEditMetadata {
|
||||
instance.Groups.SetMetadataFromEvent(event)
|
||||
instance.Groups.UpdateMetadata(event)
|
||||
instance.Groups.UpdateAdminsList(h)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user