From 106ca136fd0b05faafcd0fe26314746bae0f6b5b Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 27 Aug 2024 12:05:10 -0300 Subject: [PATCH] nip34: repository state and some ToEvent() helpers. --- nip34/repository.go | 45 +++++++++++++++++++++++++++++++ nip34/state.go | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 nip34/state.go diff --git a/nip34/repository.go b/nip34/repository.go index 9ff4b48..2046366 100644 --- a/nip34/repository.go +++ b/nip34/repository.go @@ -52,6 +52,51 @@ func ParseRepository(event nostr.Event) Repository { return repo } +func (r Repository) ToEvent() nostr.Event { + tags := make(nostr.Tags, 0, 10) + + tags = append(tags, nostr.Tag{"d", r.ID}) + + if r.Name != "" { + tags = append(tags, nostr.Tag{"name", r.Name}) + } + if r.Description != "" { + tags = append(tags, nostr.Tag{"description", r.Description}) + } + if r.EarliestUniqueCommitID != "" { + tags = append(tags, nostr.Tag{"r", r.EarliestUniqueCommitID, "euc"}) + } + if len(r.Maintainers) > 0 { + tag := make(nostr.Tag, 1, 1+len(r.Maintainers)) + tag[0] = "maintainers" + tag = append(tag, r.Maintainers...) + tags = append(tags, tag) + } + if len(r.Web) > 0 { + tag := make(nostr.Tag, 1, 1+len(r.Web)) + tag[0] = "web" + tag = append(tag, r.Web...) + tags = append(tags, tag) + } + if len(r.Clone) > 0 { + tag := make(nostr.Tag, 1, 1+len(r.Clone)) + tag[0] = "clone" + tag = append(tag, r.Clone...) + tags = append(tags, tag) + } + if len(r.Relays) > 0 { + tag := make(nostr.Tag, 1, 1+len(r.Relays)) + tag[0] = "relays" + tag = append(tag, r.Relays...) + tags = append(tags, tag) + } + + return nostr.Event{ + Kind: 30617, + Tags: tags, + } +} + func (repo Repository) GetPatchesSync(ctx context.Context, s nostr.RelayStore) []Patch { res, _ := s.QuerySync(ctx, nostr.Filter{ Kinds: []int{nostr.KindPatch}, diff --git a/nip34/state.go b/nip34/state.go new file mode 100644 index 0000000..6e96892 --- /dev/null +++ b/nip34/state.go @@ -0,0 +1,64 @@ +package nip34 + +import ( + "strings" + + "github.com/nbd-wtf/go-nostr" +) + +type RepositoryState struct { + nostr.Event + + ID string + HEAD string + Tags map[string]string + Branches map[string]string +} + +func ParseRepositoryState(event nostr.Event) RepositoryState { + st := RepositoryState{ + Event: event, + Tags: make(map[string]string), + Branches: make(map[string]string), + } + + for _, tag := range event.Tags { + if len(tag) < 2 { + continue + } + switch tag[0] { + case "d": + st.ID = tag[1] + case "HEAD": + if strings.HasPrefix(tag[1], "ref: refs/heads/") { + st.HEAD = tag[1][16:] + } + default: + if strings.HasPrefix(tag[0], "refs/heads/") { + st.Branches[tag[0][11:]] = tag[1] + } else if strings.HasPrefix(tag[0], "refs/tags/") { + st.Branches[tag[0][10:]] = tag[1] + } + } + } + + return st +} + +func (rs RepositoryState) ToEvent() nostr.Event { + tags := make(nostr.Tags, 0, 2+len(rs.Branches)+len(rs.Tags)) + + tags = append(tags, nostr.Tag{"d", rs.ID}) + tags = append(tags, nostr.Tag{"ref: refs/heads/" + rs.HEAD}) + for branchName, commitId := range rs.Branches { + tags = append(tags, nostr.Tag{"refs/heads/" + branchName, commitId}) + } + for tagName, commitId := range rs.Tags { + tags = append(tags, nostr.Tag{"refs/tags/" + tagName, commitId}) + } + + return nostr.Event{ + Kind: 30618, + Tags: tags, + } +}