| 1 | package storage |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 8 | ) |
| 9 | |
| 10 | // HasPrivateRepositories checks the stored announcements, including legacy |
| 11 | // events. Multiword tags such as private are not in the NIP-01 tag index. |
| 12 | func (s *Store) HasPrivateRepositories(ctx context.Context) (bool, error) { |
| 13 | return s.hasPrivateRepository(ctx, "" , "" ) |
| 14 | } |
| 15 | |
| 16 | // IsPrivateRepository resolves a state event's privacy from its announcement. |
| 17 | func (s *Store) IsPrivateRepository(ctx context.Context, pubkey, identifier string) (bool, error) { |
| 18 | return s.hasPrivateRepository(ctx, pubkey, identifier) |
| 19 | } |
| 20 | |
| 21 | func (s *Store) hasPrivateRepository(ctx context.Context, pubkey, identifier string) (bool, error) { |
| 22 | query := `SELECT EXISTS(SELECT 1 FROM events e WHERE e.kind=?` |
| 23 | args := []any{event.KIND_REPO} |
| 24 | if pubkey != "" { |
| 25 | query += ` AND e.pubkey=? AND e.d=?` |
| 26 | args = append(args, pubkey, identifier) |
| 27 | } |
| 28 | query += ` AND EXISTS(SELECT 1 FROM json_each(e.raw, '$.tags') tag |
| 29 | WHERE json_extract(tag.value, '$[0]' )='private' |
| 30 | AND lower(json_extract(tag.value, '$[1]' ))='true' ))` |
| 31 | var found bool |
| 32 | if err := s.DB().QueryRowContext(ctx, query, args...).Scan(&found); err != nil { |
| 33 | return false, fmt.Errorf("check stored repository privacy: %w" , err) |
| 34 | } |
| 35 | return found, nil |
| 36 | } |