nip29: add .MergeInRolesEvent()

This commit is contained in:
fiatjaf
2026-01-18 22:07:08 -03:00
parent 459a10294f
commit 31af06f4c7

View File

@@ -333,3 +333,38 @@ func (group *Group) MergeInMembersEvent(evt *nostr.Event) error {
return nil
}
func (group *Group) MergeInRolesEvent(evt *nostr.Event) error {
if evt.Kind != nostr.KindSimpleGroupRoles {
return fmt.Errorf("expected kind %d, got %d", nostr.KindSimpleGroupRoles, evt.Kind)
}
if evt.CreatedAt < group.LastRolesUpdate {
return fmt.Errorf("event is older than our last update (%d vs %d)", evt.CreatedAt, group.LastRolesUpdate)
}
group.LastRolesUpdate = evt.CreatedAt
for _, tag := range evt.Tags {
if len(tag) < 2 {
continue
}
if tag[0] != "role" {
continue
}
roleName := tag[1]
roleDescription := ""
if len(tag) >= 3 {
roleDescription = tag[2]
}
if idx := slices.IndexFunc(group.Roles, func(role *Role) bool { return role.Name == roleName }); idx >= 0 {
// update existing role description
group.Roles[idx].Description = roleDescription
} else {
// add new role
group.Roles = append(group.Roles, &Role{Name: roleName, Description: roleDescription})
}
}
return nil
}